Previous coding was passing the wrong table's tuple descriptor, which
accidentally fails to fail because no existing test case exercises a
foreign key in which the referenced attributes are further to the right
of the referencing attributes.
Add a test so that further breakage is visible.
This got broken in
16828d5c0273.
Discussion: https://postgr.es/m/
20180403204723.fqte755nukgm42uf@alvherre.pgsql
index 3bb708f86389316f7362c0a2faee15dab0d83648..d0fe65cea9b8f91cce50649cfc960b17e0255555 100644 (file)
@@ -514,7 +514,7 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel,
bool result;
/* Only called for non-null rows */
- Assert(ri_NullCheck(RelationGetDescr(fk_rel), old_row, riinfo, true) == RI_KEYS_NONE_NULL);
+ Assert(ri_NullCheck(RelationGetDescr(pk_rel), old_row, riinfo, true) == RI_KEYS_NONE_NULL);
if (SPI_connect() != SPI_OK_CONNECT)
elog(ERROR, "SPI_connect failed");
index fef072eddfa5e3870197a091cc415a2e9d573791..e1077824663e1969924856bdce9f6917aa25cf74 100644 (file)
(10 rows)
delete from t1 where a = 1;
+-- Test a primary key with attributes located in later attnum positions
+-- compared to the fk attributes.
+create table pktable2 (a int, b int, c int, d int, e int, primary key (d, e));
+create table fktable2 (d int, e int, foreign key (d, e) references pktable2);
+insert into pktable2 values (1, 2, 3, 4, 5);
+insert into fktable2 values (4, 5);
+delete from pktable2;
+ERROR: update or delete on table "pktable2" violates foreign key constraint "fktable2_d_fkey" on table "fktable2"
+DETAIL: Key (d, e)=(4, 5) is still referenced from table "fktable2".
+update pktable2 set d = 5;
+ERROR: update or delete on table "pktable2" violates foreign key constraint "fktable2_d_fkey" on table "fktable2"
+DETAIL: Key (d, e)=(4, 5) is still referenced from table "fktable2".
+drop table pktable2, fktable2;
--
-- Test deferred FK check on a tuple deleted by a rolled-back subtransaction
--
index 5f19dad03cd7c3122268345ef1480988a27e33f0..63d643c9cc7c5dc52f32496953d0803924305149 100644 (file)
@@ -1020,6 +1020,16 @@ create rule r1 as on delete to t1 do delete from t2 where t2.b = old.a;
explain (costs off) delete from t1 where a = 1;
delete from t1 where a = 1;
+-- Test a primary key with attributes located in later attnum positions
+-- compared to the fk attributes.
+create table pktable2 (a int, b int, c int, d int, e int, primary key (d, e));
+create table fktable2 (d int, e int, foreign key (d, e) references pktable2);
+insert into pktable2 values (1, 2, 3, 4, 5);
+insert into fktable2 values (4, 5);
+delete from pktable2;
+update pktable2 set d = 5;
+drop table pktable2, fktable2;
+
--
-- Test deferred FK check on a tuple deleted by a rolled-back subtransaction
--