FK check against a partitioned referenced table uses a fresh snapshot under REPEATABLE READ/SERIALIZABLE, letting a non-serializable schedule commit ==================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a21a4d-38a8-7f8f-bdf6-550c5f606011 Filed: 2026-11-26 23:59:58 UTC Severity: 50/100 Blamed commit: 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629 ## Summary When the referenced table of a foreign key is partitioned, the RI_FKey_check() lookup runs with a fresh (latest) snapshot even in REPEATABLE READ and SERIALIZABLE transactions. The referencing insert therefore succeeds against a referenced row that was committed after the transaction's snapshot and is invisible to it. Under SERIALIZABLE this lets a non-serializable schedule commit: the transaction reads "key 2 does not exist", then inserts a referencing row for key 2, and both it and the concurrent inserter commit with no serialization failure. With a plain (non-partitioned) referenced table the same schedule fails the FK check, as it should. The fresh snapshot is chosen by passing detectNewRows = true to ri_PerformCheck() for partitioned referenced tables. The comment in ri_PerformCheck() says the executor will then "error out if it finds any rows under the current snapshot that wouldn't be visible per the transaction snapshot", but the crosscheck snapshot is only honored by UPDATE and DELETE, not by the SELECT ... FOR KEY SHARE that the check runs, so no error is raised. ## Reproduction CREATE TABLE pk (id int PRIMARY KEY) PARTITION BY RANGE (id); CREATE TABLE pk1 PARTITION OF pk FOR VALUES FROM (0) TO (1000); CREATE TABLE fk (id int REFERENCES pk); INSERT INTO pk VALUES (1); -- session 1 BEGIN ISOLATION LEVEL SERIALIZABLE; SELECT count(*) FROM pk WHERE id = 2; -- 0 -- session 2 (autocommit) INSERT INTO pk VALUES (2); -- session 1 INSERT INTO fk VALUES (2); -- succeeds SELECT count(*) FROM pk WHERE id = 2; -- still 0 COMMIT; -- succeeds Expected: the INSERT INTO fk fails, as it does when pk is a plain table ("insert or update on table "fk" violates foreign key constraint "fk_id_fkey""). Either serial order is impossible for the committed result: if session 1 ran first, its FK insert would have failed; if session 2 ran first, session 1 would have seen key 2. Actual: session 1 commits a referencing row for a key that its own snapshot says does not exist. The same happens under REPEATABLE READ. A related symptom with the same cause: if session 2 instead deletes (or changes the key of) a referenced row that session 1's snapshot can still see, the FK insert in session 1 fails with SQLSTATE 23503 (foreign_key_violation) for a partitioned referenced table, whereas a plain referenced table gives 40001 (serialization_failure), which applications retry. ## Analysis RI_FKey_check() in src/backend/utils/adt/ri_triggers.c calls ri_PerformCheck() with detectNewRows = (pk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) with an XXX comment saying the snapshot must be fresh for partition lookup (find_inheritance_children() handling of partitions pending detach) to work. In ri_PerformCheck(), IsolationUsesXactSnapshot() && detectNewRows selects test_snapshot = GetLatestSnapshot() and crosscheck_snapshot = GetTransactionSnapshot(). The PK index scan then uses the latest snapshot, so it sees rows committed after the transaction snapshot. ExecLockRows() / table_tuple_lock() ignore es_crosscheck_snapshot, so the "error if a row is not visible to the transaction snapshot" behavior described in the comment never happens for this SELECT ... FOR KEY SHARE query. For SERIALIZABLE, the tuple is visible to the snapshot used for the read, so HeapCheckForSerializableConflictOut() records no conflict, and SSI only sees a single rw-antidependency (session 1's read of pk -> session 2's insert), which is not a dangerous structure. Plain referenced tables take the fast path (ri_FastPathCheck()), which uses the transaction snapshot, and before that the SPI path with detectNewRows = false; both give the expected FK violation. The behavior was introduced by commit 71f4c8c6f74b (ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY), which changed the detectNewRows argument from false to the partitioned-table test. The existing isolation test fk-snapshot.spec carries a permutation for the partitioned case whose expected output is marked as buggy. ## Notes Reproduced on master at ad33a140c76f53d66f37330edfa47c5d96e2d6ce. Related: the partitioned-PK snapshot behavior was discussed on pgsql-hackers in 2021 (linked threads). Fix 99392cdd78b7 was reverted by a90641eac24d in 2022, and fk-snapshot.spec marks the partitioned permutation as buggy. This report adds that a non-serializable schedule commits under SERIALIZABLE. Branches: REL_14 through master (reproduced on master; REL_14_STABLE through REL_19_STABLE pass detectNewRows for partitioned referenced tables in RI_FKey_check() by code inspection).