From 2713aa23f33254d0fbc649f173b6ee59c02b38c3 Mon Sep 17 00:00:00 2001
From: Tanzeel Khan <tzlkhan@amazon.com>
Date: Tue, 11 Aug 2026 12:07:05 +0000
Subject: [PATCH v1] Fix non-deterministic ORDER BY in foreign_key regression
 test

Replace ORDER BY oid::regclass::text with ORDER BY conrelid::regclass::text,
conname.  The constraint OID does not reference pg_class, so casting it to
regclass is invalid and produces unstable ordering.
---
 src/test/regress/expected/foreign_key.out | 24 +++++++++++------------
 src/test/regress/sql/foreign_key.sql      | 16 +++++++--------
 2 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/src/test/regress/expected/foreign_key.out b/src/test/regress/expected/foreign_key.out
index 120d3319451..545481cec21 100644
--- a/src/test/regress/expected/foreign_key.out
+++ b/src/test/regress/expected/foreign_key.out
@@ -1936,7 +1936,7 @@ ALTER TABLE fk_partitioned_fk_1 ADD FOREIGN KEY (a, b) REFERENCES fk_notpartitio
 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_1 FOR VALUES FROM (0,0) TO (1000,1000);
 -- Child constraint will remain valid.
 SELECT conname, convalidated, conrelid::regclass FROM pg_constraint
-WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY oid::regclass::text;
+WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY conrelid::regclass::text, conname;
            conname            | convalidated |      conrelid       
 ------------------------------+--------------+---------------------
  fk_partitioned_fk_a_b_fkey   | f            | fk_partitioned_fk
@@ -1947,7 +1947,7 @@ WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY oid::regclass:
 ALTER TABLE fk_partitioned_fk VALIDATE CONSTRAINT fk_partitioned_fk_a_b_fkey;
 -- All constraints are now valid.
 SELECT conname, convalidated, conrelid::regclass FROM pg_constraint
-WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY oid::regclass::text;
+WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY conrelid::regclass::text, conname;
            conname            | convalidated |      conrelid       
 ------------------------------+--------------+---------------------
  fk_partitioned_fk_a_b_fkey   | t            | fk_partitioned_fk
@@ -1967,7 +1967,7 @@ TRUNCATE fk_partitioned_fk_2;
 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2 FOR VALUES FROM (1000,1000) TO (2000,2000);
 -- The child constraint will also be valid.
 SELECT conname, convalidated FROM pg_constraint
-WHERE conrelid = 'fk_partitioned_fk_2'::regclass ORDER BY oid::regclass::text;
+WHERE conrelid = 'fk_partitioned_fk_2'::regclass ORDER BY conrelid::regclass::text, conname;
            conname            | convalidated 
 ------------------------------+--------------
  fk_partitioned_fk_2_a_b_fkey | t
@@ -1984,7 +1984,7 @@ ALTER TABLE fk_partitioned_fk_3 ATTACH PARTITION fk_partitioned_fk_3_1 FOR VALUE
 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_3 FOR VALUES FROM (2000,2000) TO (3000,3000);
 -- All constraints are now valid.
 SELECT conname, convalidated, conrelid::regclass FROM pg_constraint
-WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY oid::regclass::text;
+WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY conrelid::regclass::text, conname;
             conname             | convalidated |       conrelid        
 --------------------------------+--------------+-----------------------
  fk_partitioned_fk_a_b_fkey     | t            | fk_partitioned_fk
@@ -2009,30 +2009,30 @@ ALTER TABLE fk_notpartitioned_fk ADD CONSTRAINT fk_notpartitioned_fk_a_b_fkey2
 	FOREIGN KEY (a, b) REFERENCES fk_partitioned_pk NOT ENFORCED;
 -- All constraints will be invalid, and _fkey2 constraints will not be enforced.
 SELECT conname, conenforced, convalidated FROM pg_constraint
-WHERE conrelid = 'fk_notpartitioned_fk'::regclass ORDER BY oid::regclass::text;
+WHERE conrelid = 'fk_notpartitioned_fk'::regclass ORDER BY conrelid::regclass::text, conname;
              conname              | conenforced | convalidated 
 ----------------------------------+-------------+--------------
  fk_notpartitioned_fk_a_b_fkey    | t           | f
- fk_notpartitioned_fk_a_b_fkey_1  | t           | f
- fk_notpartitioned_fk_a_b_fkey_2  | t           | f
  fk_notpartitioned_fk_a_b_fkey2   | f           | f
  fk_notpartitioned_fk_a_b_fkey2_1 | f           | f
  fk_notpartitioned_fk_a_b_fkey2_2 | f           | f
+ fk_notpartitioned_fk_a_b_fkey_1  | t           | f
+ fk_notpartitioned_fk_a_b_fkey_2  | t           | f
 (6 rows)
 
 ALTER TABLE fk_notpartitioned_fk VALIDATE CONSTRAINT fk_notpartitioned_fk_a_b_fkey;
 ALTER TABLE fk_notpartitioned_fk ALTER CONSTRAINT fk_notpartitioned_fk_a_b_fkey2 ENFORCED;
 -- All constraints are now valid and enforced.
 SELECT conname, conenforced, convalidated FROM pg_constraint
-WHERE conrelid = 'fk_notpartitioned_fk'::regclass ORDER BY oid::regclass::text;
+WHERE conrelid = 'fk_notpartitioned_fk'::regclass ORDER BY conrelid::regclass::text, conname;
              conname              | conenforced | convalidated 
 ----------------------------------+-------------+--------------
  fk_notpartitioned_fk_a_b_fkey    | t           | t
- fk_notpartitioned_fk_a_b_fkey_1  | t           | t
- fk_notpartitioned_fk_a_b_fkey_2  | t           | t
  fk_notpartitioned_fk_a_b_fkey2   | t           | t
  fk_notpartitioned_fk_a_b_fkey2_1 | t           | t
  fk_notpartitioned_fk_a_b_fkey2_2 | t           | t
+ fk_notpartitioned_fk_a_b_fkey_1  | t           | t
+ fk_notpartitioned_fk_a_b_fkey_2  | t           | t
 (6 rows)
 
 -- test a self-referential FK
@@ -2042,7 +2042,7 @@ CREATE TABLE fk_partitioned_pk_3 PARTITION OF fk_partitioned_pk FOR VALUES FROM
 CREATE TABLE fk_partitioned_pk_3_1 PARTITION OF fk_partitioned_pk_3 FOR VALUES FROM (2000) TO (2100);
 SELECT conname, conenforced, convalidated FROM pg_constraint
 WHERE conrelid = 'fk_partitioned_pk'::regclass AND contype = 'f'
-ORDER BY oid::regclass::text;
+ORDER BY conrelid::regclass::text, conname;
   conname   | conenforced | convalidated 
 ------------+-------------+--------------
  selffk     | t           | f
@@ -2056,7 +2056,7 @@ ALTER TABLE fk_partitioned_pk_2 VALIDATE CONSTRAINT selffk;
 ALTER TABLE fk_partitioned_pk VALIDATE CONSTRAINT selffk;
 SELECT conname, conenforced, convalidated FROM pg_constraint
 WHERE conrelid = 'fk_partitioned_pk'::regclass AND contype = 'f'
-ORDER BY oid::regclass::text;
+ORDER BY conrelid::regclass::text, conname;
   conname   | conenforced | convalidated 
 ------------+-------------+--------------
  selffk     | t           | t
diff --git a/src/test/regress/sql/foreign_key.sql b/src/test/regress/sql/foreign_key.sql
index b9b88064ea5..94d7cc47e50 100644
--- a/src/test/regress/sql/foreign_key.sql
+++ b/src/test/regress/sql/foreign_key.sql
@@ -1449,14 +1449,14 @@ ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_1 FOR VALUES FR
 
 -- Child constraint will remain valid.
 SELECT conname, convalidated, conrelid::regclass FROM pg_constraint
-WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY oid::regclass::text;
+WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY conrelid::regclass::text, conname;
 
 -- Validate the constraint
 ALTER TABLE fk_partitioned_fk VALIDATE CONSTRAINT fk_partitioned_fk_a_b_fkey;
 
 -- All constraints are now valid.
 SELECT conname, convalidated, conrelid::regclass FROM pg_constraint
-WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY oid::regclass::text;
+WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY conrelid::regclass::text, conname;
 
 -- Attaching a child with a NOT VALID constraint.
 CREATE TABLE fk_partitioned_fk_2 (a int, b int);
@@ -1472,7 +1472,7 @@ ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2 FOR VALUES FR
 
 -- The child constraint will also be valid.
 SELECT conname, convalidated FROM pg_constraint
-WHERE conrelid = 'fk_partitioned_fk_2'::regclass ORDER BY oid::regclass::text;
+WHERE conrelid = 'fk_partitioned_fk_2'::regclass ORDER BY conrelid::regclass::text, conname;
 
 -- Test case where the child constraint is invalid, the grandchild constraint
 -- is valid, and the validation for the grandchild should be skipped when a
@@ -1486,7 +1486,7 @@ ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_3 FOR VALUES FR
 
 -- All constraints are now valid.
 SELECT conname, convalidated, conrelid::regclass FROM pg_constraint
-WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY oid::regclass::text;
+WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY conrelid::regclass::text, conname;
 
 DROP TABLE fk_partitioned_fk, fk_notpartitioned_pk;
 
@@ -1505,14 +1505,14 @@ ALTER TABLE fk_notpartitioned_fk ADD CONSTRAINT fk_notpartitioned_fk_a_b_fkey2
 
 -- All constraints will be invalid, and _fkey2 constraints will not be enforced.
 SELECT conname, conenforced, convalidated FROM pg_constraint
-WHERE conrelid = 'fk_notpartitioned_fk'::regclass ORDER BY oid::regclass::text;
+WHERE conrelid = 'fk_notpartitioned_fk'::regclass ORDER BY conrelid::regclass::text, conname;
 
 ALTER TABLE fk_notpartitioned_fk VALIDATE CONSTRAINT fk_notpartitioned_fk_a_b_fkey;
 ALTER TABLE fk_notpartitioned_fk ALTER CONSTRAINT fk_notpartitioned_fk_a_b_fkey2 ENFORCED;
 
 -- All constraints are now valid and enforced.
 SELECT conname, conenforced, convalidated FROM pg_constraint
-WHERE conrelid = 'fk_notpartitioned_fk'::regclass ORDER BY oid::regclass::text;
+WHERE conrelid = 'fk_notpartitioned_fk'::regclass ORDER BY conrelid::regclass::text, conname;
 
 -- test a self-referential FK
 ALTER TABLE fk_partitioned_pk ADD CONSTRAINT selffk FOREIGN KEY (a, b) REFERENCES fk_partitioned_pk NOT VALID;
@@ -1521,12 +1521,12 @@ CREATE TABLE fk_partitioned_pk_3 PARTITION OF fk_partitioned_pk FOR VALUES FROM
 CREATE TABLE fk_partitioned_pk_3_1 PARTITION OF fk_partitioned_pk_3 FOR VALUES FROM (2000) TO (2100);
 SELECT conname, conenforced, convalidated FROM pg_constraint
 WHERE conrelid = 'fk_partitioned_pk'::regclass AND contype = 'f'
-ORDER BY oid::regclass::text;
+ORDER BY conrelid::regclass::text, conname;
 ALTER TABLE fk_partitioned_pk_2 VALIDATE CONSTRAINT selffk;
 ALTER TABLE fk_partitioned_pk VALIDATE CONSTRAINT selffk;
 SELECT conname, conenforced, convalidated FROM pg_constraint
 WHERE conrelid = 'fk_partitioned_pk'::regclass AND contype = 'f'
-ORDER BY oid::regclass::text;
+ORDER BY conrelid::regclass::text, conname;
 
 DROP TABLE fk_notpartitioned_fk, fk_partitioned_pk;
 
-- 
2.47.3

