A SERIALIZABLE transaction runs without SSI tracking (or is tracked as READ ONLY) and allows write skew if its first snapshot is taken after SET TRANSACTION READ ONLY inside a savepoint that is then rolled back ================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a2198c-7a16-7f1e-8a26-94dfd690f362 Filed: 2026-11-26 20:29:26 UTC Severity: 30/100 Blamed commit: dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21 ## Summary A SERIALIZABLE transaction that is READ WRITE at the top level can run without SSI conflict tracking for its whole life, and then commit writes, if its first snapshot is taken while a savepoint has temporarily switched it to READ ONLY. SET TRANSACTION READ ONLY is allowed inside a subtransaction, and ROLLBACK TO SAVEPOINT restores transaction_read_only to off. But the SERIALIZABLEXACT is set up when the first snapshot is taken, and at that point GetSerializableTransactionSnapshotInt() reads XactReadOnly. If no other writable serializable transaction exists, the transaction "opts out" of SSI entirely (MySerializableXact stays invalid); otherwise it is registered with SXACT_FLAG_READ_ONLY and handled with the read-only optimizations. After the rollback the transaction is read-write again and can write, but SSI still treats it as read-only or as not serializable at all. The result is a committed write skew under SERIALIZABLE. ## Reproduction CREATE TABLE ws (k int PRIMARY KEY, v int); INSERT INTO ws VALUES (1, 0), (2, 0); Rule both sessions try to keep: sum(v) <= 1. -- session 1 BEGIN ISOLATION LEVEL SERIALIZABLE; SAVEPOINT s; SET TRANSACTION READ ONLY; SELECT 1; -- first snapshot, taken read-only ROLLBACK TO SAVEPOINT s; -- read-write again SELECT sum(v) FROM ws; -- 0 -- session 2 BEGIN ISOLATION LEVEL SERIALIZABLE; SELECT sum(v) FROM ws; -- 0 -- session 1 UPDATE ws SET v = 1 WHERE k = 1; COMMIT; -- succeeds -- session 2 UPDATE ws SET v = 1 WHERE k = 2; COMMIT; -- succeeds SELECT sum(v) FROM ws; -- 2 Expected: one of the two transactions fails with SQLSTATE 40001, as it does without the SAVEPOINT / SET TRANSACTION READ ONLY / ROLLBACK TO lines in session 1 (session 2 then gets "could not serialize access due to read/write dependencies among transactions"). No serial order of the two transactions produces sum(v) = 2. Actual: both commit and sum(v) = 2. In the order above, session 1 holds no SIREAD locks at all (pg_locks shows none after its SELECT). Starting session 2 first, so that session 1 sees a concurrent writable transaction, gives the same result: session 1 is then tracked as a READ ONLY transaction although it writes. The first snapshot must be taken while READ ONLY is in effect; if ROLLBACK TO SAVEPOINT runs before any snapshot-taking statement, SSI works normally. ## Analysis check_transaction_read_only() (commands/variable.c) always allows a change from read-write to read-only, including inside a subtransaction, and restricts only the change back to read-write. Subtransaction abort restores the GUC without going through the check hook, so a subtransaction can make the transaction read-only only temporarily. GetSerializableTransactionSnapshotInt() (storage/lmgr/predicate.c) treats XactReadOnly at first-snapshot time as a property of the whole transaction: - if XactReadOnly && PredXact->WritableSxactCount == 0, it releases the new SERIALIZABLEXACT and returns, so no predicate locks are taken and CheckForSerializableConflictIn() is a no-op for later writes; - otherwise it sets SXACT_FLAG_READ_ONLY, does not count the transaction in WritableSxactCount, and the transaction can be marked RO_SAFE and stop tracking. Neither case expects the transaction to write later. For comparison, SET TRANSACTION ISOLATION LEVEL and SET TRANSACTION [NOT] DEFERRABLE are both rejected inside a subtransaction. Possible fixes: reject SET TRANSACTION READ ONLY in a subtransaction before the first snapshot, or make the SSI read-only decision from the top-level setting rather than the current XactReadOnly. Blame: the SSI commit dafaa3efb75c, which made the serializable snapshot setup depend on XactReadOnly. Read-only subtransactions of a read-write parent were already allowed at that time. ## Notes Tested on master 496311a7e37efa7b54e276443ff3a1e92fa4b546 with a freshly initialized cluster and default settings. Branches: REL_14 through master (reproduced on master; the blamed code dates from 2011 and the same read-only handling in GetSerializableTransactionSnapshotInt() and check_transaction_read_only() is present on all supported branches by code inspection).