Skip to content

Commit cd5005b

Browse files
committed
Pass correct TupDesc to ri_NullCheck() in Assert
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 16828d5. Discussion: https://postgr.es/m/20180403204723.fqte755nukgm42uf@alvherre.pgsql
1 parent dddfc4c commit cd5005b

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/backend/utils/adt/ri_triggers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel,
514514
bool result;
515515

516516
/* Only called for non-null rows */
517-
Assert(ri_NullCheck(RelationGetDescr(fk_rel), old_row, riinfo, true) == RI_KEYS_NONE_NULL);
517+
Assert(ri_NullCheck(RelationGetDescr(pk_rel), old_row, riinfo, true) == RI_KEYS_NONE_NULL);
518518

519519
if (SPI_connect() != SPI_OK_CONNECT)
520520
elog(ERROR, "SPI_connect failed");

src/test/regress/expected/foreign_key.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,19 @@ explain (costs off) delete from t1 where a = 1;
13811381
(10 rows)
13821382

13831383
delete from t1 where a = 1;
1384+
-- Test a primary key with attributes located in later attnum positions
1385+
-- compared to the fk attributes.
1386+
create table pktable2 (a int, b int, c int, d int, e int, primary key (d, e));
1387+
create table fktable2 (d int, e int, foreign key (d, e) references pktable2);
1388+
insert into pktable2 values (1, 2, 3, 4, 5);
1389+
insert into fktable2 values (4, 5);
1390+
delete from pktable2;
1391+
ERROR: update or delete on table "pktable2" violates foreign key constraint "fktable2_d_fkey" on table "fktable2"
1392+
DETAIL: Key (d, e)=(4, 5) is still referenced from table "fktable2".
1393+
update pktable2 set d = 5;
1394+
ERROR: update or delete on table "pktable2" violates foreign key constraint "fktable2_d_fkey" on table "fktable2"
1395+
DETAIL: Key (d, e)=(4, 5) is still referenced from table "fktable2".
1396+
drop table pktable2, fktable2;
13841397
--
13851398
-- Test deferred FK check on a tuple deleted by a rolled-back subtransaction
13861399
--

src/test/regress/sql/foreign_key.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,16 @@ create rule r1 as on delete to t1 do delete from t2 where t2.b = old.a;
10201020
explain (costs off) delete from t1 where a = 1;
10211021
delete from t1 where a = 1;
10221022

1023+
-- Test a primary key with attributes located in later attnum positions
1024+
-- compared to the fk attributes.
1025+
create table pktable2 (a int, b int, c int, d int, e int, primary key (d, e));
1026+
create table fktable2 (d int, e int, foreign key (d, e) references pktable2);
1027+
insert into pktable2 values (1, 2, 3, 4, 5);
1028+
insert into fktable2 values (4, 5);
1029+
delete from pktable2;
1030+
update pktable2 set d = 5;
1031+
drop table pktable2, fktable2;
1032+
10231033
--
10241034
-- Test deferred FK check on a tuple deleted by a rolled-back subtransaction
10251035
--

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy