diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri_triggers.c index 03fec04a71b..6f201ef0f31 100644 --- a/src/backend/utils/adt/ri_triggers.c +++ b/src/backend/utils/adt/ri_triggers.c @@ -3199,9 +3199,19 @@ ri_FastPathFlushArray(RI_FastPathEntry *fpentry, TupleTableSlot *fk_slot, if (!ri_LockPKTuple(pk_rel, pk_slot, snapshot, &concurrently_updated)) continue; - /* Extract the PK value from the matched and locked tuple */ + /* + * Extract the PK value from the matched and locked tuple. + * + * A foreign key may reference a nullable unique column, not just a + * NOT NULL primary key. If ri_LockPKTuple() chased an update chain + * to a version whose referenced key is now NULL, that version cannot + * equal any buffered (non-null) FK value, so skip it. This mirrors + * the SPI path, where the requalifying "pkatt = $n" yields NULL and + * the row is not returned. + */ found_val = slot_getattr(pk_slot, riinfo->pk_attnums[0], &found_null); - Assert(!found_null); + if (found_null) + continue; if (concurrently_updated) { @@ -3453,9 +3463,14 @@ recheck_matched_pk_tuple(Relation idxrel, ScanKeyData *skeys, int nkeys, { ScanKeyData *skey = &skeys[i]; - /* A PK column can never be set to NULL. */ - Assert(!isnull[i]); - if (!DatumGetBool(FunctionCall2Coll(&skey->sk_func, + /* + * A foreign key may reference a nullable unique column, so the + * version we chased the update chain to may have a NULL in a key + * column. A NULL never equals the value we searched for, so treat it + * as no match, as the SPI path's requalification would. + */ + if (isnull[i] || + !DatumGetBool(FunctionCall2Coll(&skey->sk_func, skey->sk_collation, values[i], skey->sk_argument))) diff --git a/src/test/isolation/expected/fk-fastpath-null-key.out b/src/test/isolation/expected/fk-fastpath-null-key.out new file mode 100644 index 00000000000..3efe1e781b2 --- /dev/null +++ b/src/test/isolation/expected/fk-fastpath-null-key.out @@ -0,0 +1,17 @@ +Parsed test spec with 2 sessions + +starting permutation: s1b s1upd_null s2ins s1c +step s1b: BEGIN; +step s1upd_null: UPDATE pktable SET u = NULL WHERE u = 5; +step s2ins: INSERT INTO fktable VALUES (5); +step s1c: COMMIT; +step s2ins: <... completed> +ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_a_fkey" + +starting permutation: s1b s1upd_null s2ins_arr s1c +step s1b: BEGIN; +step s1upd_null: UPDATE pktable SET u = NULL WHERE u = 5; +step s2ins_arr: INSERT INTO fktable VALUES (5), (6); +step s1c: COMMIT; +step s2ins_arr: <... completed> +ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_a_fkey" diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule index df8ce44ede6..a27480a86a2 100644 --- a/src/test/isolation/isolation_schedule +++ b/src/test/isolation/isolation_schedule @@ -38,6 +38,7 @@ test: fk-snapshot test: fk-snapshot-2 test: fk-snapshot-3 test: fk-concurrent-pk-upd +test: fk-fastpath-null-key test: subxid-overflow test: eval-plan-qual test: eval-plan-qual-trigger diff --git a/src/test/isolation/specs/fk-fastpath-null-key.spec b/src/test/isolation/specs/fk-fastpath-null-key.spec new file mode 100644 index 00000000000..51e11b101dc --- /dev/null +++ b/src/test/isolation/specs/fk-fastpath-null-key.spec @@ -0,0 +1,40 @@ +# A foreign key may reference a nullable UNIQUE column, not only a NOT NULL +# primary key. Test that the RI fast-path check copes when a concurrent +# transaction sets the referenced key to NULL. +# +# s2's INSERT probes the PK index, finds the (u=5) row, and blocks on s1's +# in-progress key-changing UPDATE. After s1 commits, the tuple lock follows +# the update chain (table_tuple_lock with TUPLE_LOCK_FLAG_FIND_LAST_VERSION) +# to the now-NULL version. The check must treat that as "referenced row not +# found" and raise an ordinary foreign-key violation -- not assume that a +# referenced key can never be NULL. +# +# Two permutations exercise the two fast-path flush routines: a single-row +# INSERT goes through ri_FastPathFlushLoop()/recheck_matched_pk_tuple(), while +# a multi-row single-column INSERT goes through ri_FastPathFlushArray(). + +setup +{ + CREATE TABLE pktable (u int UNIQUE, c int); + CREATE TABLE fktable (a int REFERENCES pktable (u)); + INSERT INTO pktable VALUES (5, 1), (6, 2); +} + +teardown +{ + DROP TABLE fktable, pktable; +} + +session s1 +step s1b { BEGIN; } +step s1upd_null { UPDATE pktable SET u = NULL WHERE u = 5; } +step s1c { COMMIT; } + +session s2 +# single-row batch -> per-row loop flush path +step s2ins { INSERT INTO fktable VALUES (5); } +# multi-row single-column batch -> SK_SEARCHARRAY flush path +step s2ins_arr { INSERT INTO fktable VALUES (5), (6); } + +permutation s1b s1upd_null s2ins s1c +permutation s1b s1upd_null s2ins_arr s1c