Report
SSI does not detect write-skew after a conflicting committed transaction is summarized into pg_serial
- Severity
- 80
- 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
When the predicate-lock transaction table fills, the oldest committed SERIALIZABLE transaction is summarized: its SIREAD locks are moved to a dummy "old committed" transaction and its rw-conflicts become summary flags. After that happens, a still-running overlapping transaction can finish the other half of a write-skew and commit, so both withdrawals succeed. The same interleaving is cancelled with SQLSTATE 40001 if the first writer has not yet been summarized.
Reproduction
Start with a table of two rows, each with v = 50:
CREATE TABLE acc (id int PRIMARY KEY, v int);
INSERT INTO acc VALUES (1, 50), (2, 50);
CREATE TABLE filler (id int PRIMARY KEY, v int);
INSERT INTO filler VALUES (1, 0);
Session A:
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT * FROM acc;
Session B:
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT * FROM acc;
UPDATE acc SET v = v - 40 WHERE id = 1;
COMMIT;
Then run and commit enough other short SERIALIZABLE transactions to fill the serializable transaction table, which is sized at (MaxBackends + max_prepared_transactions) * 10. Each one is:
BEGIN ISOLATION LEVEL SERIALIZABLE;
UPDATE filler SET v = v + 1 WHERE id = 1;
COMMIT;
With max_connections = 100 and max_prepared_transactions = 10, 2200 of these were enough. Finally, in session A:
UPDATE acc SET v = v - 40 WHERE id = 2;
COMMIT;
Expected: session A fails with SQLSTATE 40001, and SUM(v) remains 60.
Actual: session A commits, and SUM(v) is 20 (both rows are 10). Omitting the filler transactions yields the expected cancellation and SUM(v) = 60.
Analysis
CheckTargetForConflictsIn() in predicate.c treats a committed transaction's SIREAD lock as a conflict only when GetTransactionSnapshot()->xmin precedes sxact->finishedBefore. Locks transferred onto OldCommittedSxact keep the original commitSeqNo on the lock itself, but OldCommittedSxact->finishedBefore is InvalidTransactionId. That value does not precede a normal xmin, so a later write never calls FlagRWConflict() and never sets SXACT_FLAG_SUMMARY_CONFLICT_IN. The dangerous structure is therefore not recognized, even though the transferred SIREAD lock is still present and FlagRWConflict() already special-cases OldCommittedSxact.
The dummy transaction, the finishedBefore overlap test, and lock transfer on summarization were introduced together in the original SSI commit.
Notes
Tested at master 81b2179b492e3af891544813b7e22a42386b34d0.