SERIALIZABLE misses a dependency cycle when a key's absence is learned via a successful INSERT (or ON CONFLICT with no conflict) in a rolled-back savepoint =========================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a21486-c278-789a-9597-c34548a2dde3 Filed: 2026-11-25 21:05:05 UTC Severity: 30/100 Blamed commit: dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21 ## Summary At SERIALIZABLE, a successful INSERT into a table with a unique index tells the transaction that the key was absent, but that read is never recorded by SSI. Normally the inserted index entry itself protects the read: a concurrent inserter of the same key blocks or fails. If the INSERT ran inside a savepoint that is then rolled back, the entry is gone and nothing is left to record the read. Another transaction can insert the same key, the two transactions can form a dependency cycle, and both commit. The same happens with INSERT ... ON CONFLICT DO NOTHING (and DO UPDATE / DO SELECT) when no conflicting row is found. Replacing the INSERT with a SELECT of the same key in the same rolled back savepoint correctly raises a serialization failure. ## Reproduction CREATE TABLE t (k int PRIMARY KEY, v int NOT NULL); INSERT INTO t VALUES (3, 8); -- s1 BEGIN ISOLATION LEVEL SERIALIZABLE; SAVEPOINT a; INSERT INTO t VALUES (1, 2) RETURNING k; -- 1 row: k=1 absent ROLLBACK TO a; -- s2 BEGIN ISOLATION LEVEL SERIALIZABLE; INSERT INTO t VALUES (1, 4); SELECT v FROM t WHERE k = 3; -- 8 -- s1 UPDATE t SET v = v + 6 WHERE k = 3; COMMIT; -- succeeds -- s2 COMMIT; -- succeeds SELECT * FROM t ORDER BY k; -- (1,4), (3,14) Expected: one of the transactions fails with a serialization failure. s1 observed that k=1 did not exist, so it must precede s2, which inserted k=1. s2 read v=8 for k=3, so it must precede s1, which changed it to 14. No serial order explains both results. Actual: both commit. The result is the same with "INSERT INTO t VALUES (1, 2) ON CONFLICT (k) DO NOTHING RETURNING k" as the probe. With "SELECT k FROM t WHERE k = 1" as the probe, s2's COMMIT fails with "could not serialize access due to read/write dependencies among transactions", as expected. Isolation spec: setup { CREATE TABLE t (k int PRIMARY KEY, v int NOT NULL); INSERT INTO t VALUES (3, 8); } teardown { DROP TABLE t; } session s1 setup { BEGIN ISOLATION LEVEL SERIALIZABLE; } step s1sp { SAVEPOINT a; } step s1ins { INSERT INTO t VALUES (1, 2) RETURNING k; } step s1ioc { INSERT INTO t VALUES (1, 2) ON CONFLICT (k) DO NOTHING RETURNING k; } step s1sel { SELECT k FROM t WHERE k = 1; } step s1rb { ROLLBACK TO a; } step s1upd { UPDATE t SET v = v + 6 WHERE k = 3; } step s1c { COMMIT; } session s2 setup { BEGIN ISOLATION LEVEL SERIALIZABLE; } step s2ins { INSERT INTO t VALUES (1, 4); } step s2read { SELECT v FROM t WHERE k = 3; } step s2c { COMMIT; } session s3 step s3chk { SELECT * FROM t ORDER BY k; } permutation s1sp s1ins s1rb s2ins s2read s1upd s1c s2c s3chk permutation s1sp s1ioc s1rb s2ins s2read s1upd s1c s2c s3chk permutation s1sp s1sel s1rb s2ins s2read s1upd s1c s2c s3chk The first two permutations commit both transactions; the third fails s2c with a serialization failure. ## Analysis The uniqueness check in _bt_doinsert()/_bt_check_unique() and the ON CONFLICT arbiter probe in check_exclusion_or_unique_constraint() run with a dirty snapshot, which predicate locking ignores, and _bt_doinsert() only calls CheckForSerializableConflictIn() for the target leaf page; it takes no SIREAD lock on it. README-SSI lists "an index scan which is just finding the right position for an index insertion need not acquire a predicate lock" as an allowed optimization. That relies on the inserted entry lasting until commit. A subtransaction abort removes the entry but keeps the transaction's knowledge that the key was absent, which README-SSI says must survive a subtransaction rollback ("predicate locks must survive a subtransaction rollback"). The heap-tuple equivalent is already handled: the optimization that drops a SIREAD lock when the tuple is later written is skipped inside a subtransaction for this reason (see the IsSubTransaction() check in predicate.c). The ON CONFLICT fix that rereads the conflicting row with the query snapshot covers only the case where a conflicting row is found. When no row is found, there is still nothing that records the read. A possible fix is to take a SIREAD lock on the leaf page the key would be on (or record the read some other way) when a serializable transaction inserts into a unique index inside a subtransaction, or when an ON CONFLICT probe finds no conflict. The behaviour dates from the introduction of SSI. ## Notes Tested on master 4255812efdbcf7f1acf6be2211e1b5b11c4d4240 with default settings. Found with a randomized checker that compares the committed transactions with every serial order; it showed up on plain and range-partitioned tables, with plain INSERT, ON CONFLICT DO NOTHING, DO UPDATE and DO SELECT as the probe. The same schedule also commits both transactions when the key is enforced by an exclusion constraint (EXCLUDE USING hash (k WITH =)) instead of a primary key, with either a plain INSERT or ON CONFLICT DO NOTHING. To be affected, an application has to rely on the success of an INSERT that it makes inside a savepoint and later rolls back. ## Related - A December 2016 pgsql-hackers discussion of SSI and work discarded by subtransaction rollback (https://www.mail-archive.com/pgsql-hackers@postgresql.org/msg299910.html) touched on this area; no fix for this case followed. - Commit fcff8a575198 notes that reads done for constraint checks do not take part in SSI. Branches: all supported branches (REL_14_STABLE through master; code from dafaa3ef, 2011). ON CONFLICT DO SELECT exists only on REL_19_STABLE and master.