SERIALIZABLE: deleting a referenced row ignores a referencing row deleted by a concurrent committed transaction, allowing a non-serializable result =================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a22203-16e8-752f-a840-1b9bec628236 Filed: 2026-11-28 11:55:57 UTC Severity: 45/100 Blamed commit: dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21 ## Summary Under SERIALIZABLE, the NO ACTION / RESTRICT check that runs when a referenced row is deleted (or its key is updated) looks for referencing rows with a fresh snapshot rather than the transaction snapshot. A referencing row that the transaction can still see, but that a concurrent transaction deleted and committed after the snapshot was taken, is therefore ignored and the delete succeeds. If the same transaction has read that referencing row, the two transactions commit with a result that no serial order produces, and SSI raises no serialization failure. Referential integrity itself is preserved (no orphan rows are left); the broken guarantee is serializability of what the transactions observed. ## Reproduction CREATE TABLE p (k int PRIMARY KEY); CREATE TABLE c (id int PRIMARY KEY, pk int REFERENCES p); INSERT INTO p VALUES (0); INSERT INTO c VALUES (100, 0); -- session 1 BEGIN ISOLATION LEVEL SERIALIZABLE; SELECT count(*) FROM c WHERE pk = 0; -- 1 -- session 2 BEGIN ISOLATION LEVEL SERIALIZABLE; DELETE FROM c WHERE id = 100; -- DELETE 1 COMMIT; -- session 1 DELETE FROM p WHERE k = 0; -- DELETE 1 COMMIT; -- succeeds Expected: one of the two transactions fails with SQLSTATE 40001 (serialization_failure). Neither serial order is possible: - session 1 first: its DELETE FROM p would fail with a foreign key violation, since the referencing row still exists; - session 2 first: session 1's SELECT would have returned 0. Actual: both commit. Session 1 saw a row referencing k = 0 and then deleted k = 0 without a foreign key error. The same happens with ON DELETE RESTRICT, with a key-changing UPDATE of the referenced row, with a DEFERRABLE INITIALLY DEFERRED constraint, when session 2 moves the referencing row to another parent instead of deleting it, and with ON DELETE CASCADE (session 1's cascade then deletes nothing although session 2 reported DELETE 1 for the same row; again neither serial order matches). ## Analysis ri_restrict() (used by RI_FKey_noaction_del/upd and RI_FKey_restrict_del/upd) and the cascade/set-null/set-default actions call ri_PerformCheck() with detectNewRows = true. In ri_PerformCheck(), for IsolationUsesXactSnapshot() this selects test_snapshot = GetLatestSnapshot(); crosscheck_snapshot = GetTransactionSnapshot(); The crosscheck snapshot only makes the executor raise a serialization failure for rows visible to the fresh snapshot but not to the transaction snapshot (newly inserted referencing rows). The opposite case, a referencing row visible to the transaction snapshot but already deleted or re-pointed under the fresh snapshot, is simply not returned by the scan, so the check passes. In SSI terms, the check effectively reads the concurrent transaction's write (a dependency session 2 -> session 1), which SSI does not track because the read happens through a snapshot in which session 2's change is already visible: the deleted tuple is not visible to the fresh snapshot, and for such a tuple HeapCheckForSerializableConflictOut() considers only its inserter, so nothing is recorded. Together with the ordinary rw-antidependency session 1 -> session 2 (session 1 read the row that session 2 deleted) this forms a cycle with only one rw edge, which the dangerous-structure test cannot see. The fresh-snapshot trick predates true serializability; it was adequate when SERIALIZABLE meant snapshot isolation. The guarantee that every set of concurrent SERIALIZABLE transactions behaves like some serial order was introduced by commit dafaa3efb75c ("Implement genuine serializable isolation level"), and the RI parent-side check was not adjusted. README-SSI notes that foreign key enforcement uses special tricks and needs no predicate locks, which holds for integrity but not for the transactions' observed results. A possible fix is to raise a serialization failure, as ExecLockRows() does for TM_Deleted/TM_Updated under a transaction snapshot, when the check under SERIALIZABLE finds a referencing row that is visible to the transaction snapshot but not to the fresh snapshot. ## Notes Reproduced on master at e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8. The referencing-side check (RI_FKey_check, including its fast path) is not involved. REPEATABLE READ shows the same behavior, which is acceptable there since it does not promise serializability. Branches: REL_14 through master (ri_PerformCheck() uses the same GetLatestSnapshot()/crosscheck pair on every supported branch). Related: 01a21a4d-38a8-7f8f-bdf6-550c5f606011 (a different code path: the referencing-side check against a partitioned referenced table, which also uses a fresh snapshot).