Add test case for same-type reordered FK columns

The test added in 980c1a85d8 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 <fredrik.widlert@digpro.se>
Discussion: https://postgr.es/m/CADfhSr8hYc-4Cz7vfXH_oV-Jq81pyK9W4phLrOGspovsg2W7Kw@mail.gmail.com
This commit is contained in:
Amit Langote 2026-04-10 17:44:06 +09:00
parent d6e96bacd3
commit 009ea1b08d
2 changed files with 19 additions and 0 deletions

View file

@ -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

View file

@ -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