From: Amit Langote Date: Fri, 10 Apr 2026 08:44:06 +0000 (+0900) Subject: Add test case for same-type reordered FK columns X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=009ea1b08d7b8843435bd0f1137fa3df09aac79f;p=thirdparty%2Fpostgresql.git Add test case for same-type reordered FK columns The test added in 980c1a85d819 covered reordered FK columns with different types, which triggered an "operator not a member of opfamily" error in the fast-path prior to that commit. Add a test for the same-type case, which is also fixed by that commit but where the wrong scan key ordering instead produced a spurious FK violation without any internal error. Reported-by: Fredrik Widlert Discussion: https://postgr.es/m/CADfhSr8hYc-4Cz7vfXH_oV-Jq81pyK9W4phLrOGspovsg2W7Kw@mail.gmail.com --- diff --git a/src/test/regress/expected/foreign_key.out b/src/test/regress/expected/foreign_key.out index 9fa2e22329a..8b3b268de0f 100644 --- a/src/test/regress/expected/foreign_key.out +++ b/src/test/regress/expected/foreign_key.out @@ -3672,6 +3672,16 @@ INSERT INTO fp_fk_order VALUES (3, 99, 'none', 9); -- should fail ERROR: insert or update on table "fp_fk_order" violates foreign key constraint "fp_fk_order_a_c_b_fkey" DETAIL: Key (a, c, b)=(9, 99, none) is not present in table "fp_pk_order". DROP TABLE fp_fk_order, fp_pk_order; +-- Same-type columns in different order: +CREATE TABLE fp_pk_same (c1 int, c2 int, PRIMARY KEY (c1, c2)); +INSERT INTO fp_pk_same VALUES (1, 2); +CREATE TABLE fp_fk_same (c1 int, c2 int, + FOREIGN KEY (c2, c1) REFERENCES fp_pk_same (c2, c1)); +INSERT INTO fp_fk_same VALUES (1, 2); -- should succeed +INSERT INTO fp_fk_same VALUES (9, 9); -- should fail +ERROR: insert or update on table "fp_fk_same" violates foreign key constraint "fp_fk_same_c2_c1_fkey" +DETAIL: Key (c2, c1)=(9, 9) is not present in table "fp_pk_same". +DROP TABLE fp_fk_same, fp_pk_same; -- Deferred constraint: batch flushed at COMMIT, not at statement end CREATE TABLE fp_pk_commit (a int PRIMARY KEY); CREATE TABLE fp_fk_commit (a int REFERENCES fp_pk_commit diff --git a/src/test/regress/sql/foreign_key.sql b/src/test/regress/sql/foreign_key.sql index 9afee64d1e0..7eb86b188f0 100644 --- a/src/test/regress/sql/foreign_key.sql +++ b/src/test/regress/sql/foreign_key.sql @@ -2643,6 +2643,15 @@ INSERT INTO fp_fk_order VALUES (2, 20, 'two', 2); -- should succeed INSERT INTO fp_fk_order VALUES (3, 99, 'none', 9); -- should fail DROP TABLE fp_fk_order, fp_pk_order; +-- Same-type columns in different order: +CREATE TABLE fp_pk_same (c1 int, c2 int, PRIMARY KEY (c1, c2)); +INSERT INTO fp_pk_same VALUES (1, 2); +CREATE TABLE fp_fk_same (c1 int, c2 int, + FOREIGN KEY (c2, c1) REFERENCES fp_pk_same (c2, c1)); +INSERT INTO fp_fk_same VALUES (1, 2); -- should succeed +INSERT INTO fp_fk_same VALUES (9, 9); -- should fail +DROP TABLE fp_fk_same, fp_pk_same; + -- Deferred constraint: batch flushed at COMMIT, not at statement end CREATE TABLE fp_pk_commit (a int PRIMARY KEY); CREATE TABLE fp_fk_commit (a int REFERENCES fp_pk_commit