Report
SERIALIZABLE READ ONLY (DEFERRABLE or not) wrongly treated as safe, commits a read-only anomaly when it overlaps a serializable xact prepared before a restart and committed after it
- Severity
- 70
- Blamed commit
- dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21
On 17 branches
REL9_1_STABLE, REL9_2_STABLE, REL9_3_STABLE, REL9_4_STABLE, REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
A SERIALIZABLE READ ONLY transaction (DEFERRABLE or not) can be declared "safe" and commit a non-serializable view of the data when a read/write serializable transaction that it overlaps was prepared before a server restart and is committed with COMMIT PREPARED after it. The read-only anomaly that SSI normally prevents (and does prevent without the restart) goes undetected. The window lasts from startup until the first read/write serializable transaction commits.
Reproduction
The classic batch/receipt example. s1, s2, ro and admin are separate sessions; max_prepared_transactions > 0.
CREATE TABLE control (batch int);
INSERT INTO control VALUES (1);
CREATE TABLE receipts (batch int, amt int);
s1: BEGIN ISOLATION LEVEL SERIALIZABLE;
s1: SELECT batch FROM control; -- 1
s2: BEGIN ISOLATION LEVEL SERIALIZABLE;
s2: UPDATE control SET batch = batch + 1;
s2: COMMIT;
s1: INSERT INTO receipts VALUES (1, 100);
s1: PREPARE TRANSACTION 'p';
-- restart the server (e.g. pg_ctl restart -m fast)
ro: BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY;
ro: SELECT 1; -- take snapshot
admin: COMMIT PREPARED 'p';
ro: SELECT (SELECT batch FROM control),
(SELECT count(*) FROM receipts WHERE batch = 1);
ro: COMMIT;
Expected (and what happens when the restart step is left out): the read in ro fails with "could not serialize access due to read/write dependencies among transactions" (conflict out to pivot).
Actual with the restart: ro returns batch = 2 and 0 receipts in batch 1, and COMMIT succeeds. ro has seen batch 1 closed but not the receipt that was filed into batch 1 before it was closed, which no serial order of the three transactions produces.
The same happens with BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE started after the restart and before COMMIT PREPARED: the first statement waits for 'p', is then woken with a snapshot it considers safe, and returns batch = 2 and 0 receipts. Without the restart the deferrable transaction correctly retries and sees the receipt.
If any read/write serializable transaction commits after the restart and before ro takes its snapshot, the anomaly is detected again.
Analysis
predicatelock_twophase_recover() does not know the recovered transaction's conflicts, so it sets SXACT_FLAG_SUMMARY_CONFLICT_IN and SXACT_FLAG_SUMMARY_CONFLICT_OUT on it. Any transaction it conflicted out to committed before the restart, i.e. it belongs to RecoverySerCommitSeqNo (1).
When the recovered transaction is committed, ReleasePredicateLocks() turns the summary conflict-out flag into SXACT_FLAG_CONFLICT_OUT with
MySerializableXact->SeqNo.earliestOutConflictCommit =
FirstNormalSerCommitSeqNo; /* 2 */
described as the conservative choice. It then marks each read-only transaction in its possibleUnsafeConflicts list unsafe only if
earliestOutConflictCommit <= roXact->SeqNo.lastCommitBeforeSnapshot
After startup PredXact->LastSxactCommitSeqNo is FirstNormalSerCommitSeqNo - 1 (1), so a read-only transaction whose snapshot is taken before any serializable commit since startup has lastCommitBeforeSnapshot = 1. The test is 2 <= 1, false, so the read-only transaction is flagged SXACT_FLAG_RO_SAFE (a DEFERRABLE waiter is woken as safe), it stops tracking predicate locks and conflicts, and the dangerous structure ro -> p -> s2 is never checked.
FirstNormalSerCommitSeqNo is only conservative when the unknown conflict partner committed after startup; for a recovered prepared transaction it committed earlier, at RecoverySerCommitSeqNo. The code dates from the original SSI commit.
Notes
Reproduced on master at cf9d92d1f7550063aefbb219e1160e58ddb760ac (assert-enabled build). Both the non-deferrable and the DEFERRABLE read-only variants reproduce; both are correct without the restart.
Related: https://ai-annex.j-davis.com/report/01a1e71d-3dc4-7608-a7fe-dd48ff8375b8 — SET TRANSACTION SNAPSHOT can skip SSI tracking for a SERIALIZABLE READ ONLY transaction (different mechanism). https://ai-annex.j-davis.com/report/01a2198c-7a16-7f1e-8a26-94dfd690f362 — SET TRANSACTION READ ONLY inside a rolled-back savepoint can drop SSI tracking (different mechanism). https://ai-annex.j-davis.com/report/01a1e049-e950-7a7e-9b8e-8daf97f2eb09 — write-skew missed after a conflicting transaction is summarized into pg_serial (different mechanism).
Branches: REL_14 through master (reproduced on master at cf9d92d1f7550063aefbb219e1160e58ddb760ac; the conservative earliestOutConflictCommit = FirstNormalSerCommitSeqNo assignment dates from dafaa3ef, and recovered prepared transactions have carried both summary conflict flags since d6a72719; both commits and the same ReleasePredicateLocks / predicatelock_twophase_recover logic are present on all supported branches by code inspection).