serializable snapshot isolation
SIRead locks, rw-antidependencies, and the dangerous structure that gets you a 40001.
- address
- everything.locking.ssi
- part
- part 15, locks and concurrency control
- chapter
- chapter 10 of 10
- reading time
- 9 min
postgres's SERIALIZABLE is serializable snapshot isolation (SSI): snapshot isolation, plus bookkeeping that catches the cases where snapshot isolation goes wrong. every read leaves an SIRead lock (a note that blocks nobody), every write checks those notes, and a hit records an rw-antidependency. two of those in a row through one transaction, the dangerous structure, and somebody gets 40001.
the picture
you know write skew: two doctors each see two on call, each goes off, nobody's left. snapshot isolation lets it happen because each one wrote a row the other only read. no write-write conflict, nothing to block on.
the fix doesn't take locks that block. it takes notes. think of a shared document where readers leave pencil ticks next to the paragraphs they read. a writer who changes a ticked paragraph doesn't wait; they just jot down "I changed something X read". if the jottings ever form the wrong shape, someone is told to start over.
here are the doctors again, at SERIALIZABLE, on a real server, with the notes showing:
- no locks
T1 (asha) opens a serializable transaction.
SIRead locks
they live in pg_locks with mode SIReadLock, and they come in three sizes: a tuple (a row read through an index), a page (an index page that was searched, or a heap page once enough of its tuples are noted), and a relation (a seq scan read everything, so every row counts). you can watch them in the browser too; one session can't conflict with itself, but it still takes notes:
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT balance FROM accounts WHERE id = 1;
SELECT owner FROM accounts WHERE id = 2;
SELECT locktype, relation::regclass, page, tuple
FROM pg_locks WHERE mode = 'SIReadLock'
ORDER BY locktype, relation::regclass::text, page, tuple;
SELECT count(*) FROM accounts WHERE balance > 1000;
SELECT locktype, relation::regclass, page, tuple
FROM pg_locks WHERE mode = 'SIReadLock'
ORDER BY locktype, relation::regclass::text, page, tuple;
COMMIT;| balancenumeric |
|---|
| 100.00 |
| ownertext |
|---|
| bob |
| locktypetext | relationregclass | pageint4 | tupleint2 |
|---|---|---|---|
| page | accounts_pkey | 1 | null |
| tuple | accounts | 0 | 1 |
| tuple | accounts | 0 | 2 |
| countint8 |
|---|
| 982 |
| locktypetext | relationregclass | pageint4 | tupleint2 |
|---|---|---|---|
| page | accounts_pkey | 1 | null |
| relation | accounts | null | null |
two index lookups: a note on each of the two tuples, and one on the index page both searches went through. then one seq scan, and the two tuple notes on accounts are replaced by a single relation-wide one: it covers every row, so they're redundant. (the note on accounts_pkey stays. it's a different relation.) there's a budget too (max_pred_locks_per_transaction and friends): past it, postgres promotes many small notes into fewer big ones. bigger notes cost less memory and catch more false conflicts.
an SIRead lock never blocks anyone, and it isn't released at COMMIT. it has to last until every transaction that overlapped the reader has finished, because a later write by one of them can still create an edge to the reader.
rw-antidependencies and the dangerous structure
when a serializable transaction writes a row (or index entry) covered by another transaction's note, postgres records an rw-antidependency, reader → writer: the reader saw a version the writer replaced, so in any serial order the reader must come first. on its own, that's harmless: run the reader first, done.
the trouble is a transaction with an edge in and an edge out:
Cahill, Röhm and Fekete showed (in "Serializable Isolation for Snapshot Databases", SIGMOD 2008) that every non-serializable execution under snapshot isolation contains this pattern: two consecutive rw-antidependencies between concurrent transactions, with T_out committing first. so you don't have to search the whole dependency graph for cycles (expensive), only for the pattern (cheap). Ports and Grittner describe how postgres 9.1 implemented it, with read-only optimizations and the SIRead lock manager, in "Serializable Snapshot Isolation in PostgreSQL" (VLDB 2012).
in the doctors run, T1 and T2 are both T_in and T_out: T2 → T1 (T2 read asha, T1 changed her), T1 → T2 (T1 read ben, T2 changed him). T2 is a pivot, T1 committed first, so T2 gets it at its commit:
- ERROR: could not serialize access due to read/write dependencies among transactions
- DETAIL: Reason code: Canceled on identification as a pivot, during commit attempt.
/* T1 */ BEGIN ISOLATION LEVEL SERIALIZABLE; SELECT v FROM plain WHERE id = 1;
/* T2 */ BEGIN ISOLATION LEVEL SERIALIZABLE; SELECT v FROM plain WHERE id = 2;
/* T1 */ UPDATE plain SET v = v + 1 WHERE id = 1;
/* T2 */ UPDATE plain SET v = v + 1 WHERE id = 2;
/* T1 */ COMMIT;
/* T2 */ COMMIT; -- ?false positives
SSI is conservative. the pattern is necessary for an anomaly, not sufficient, and the notes are coarser than your rows. here are three rounds of "two transactions, two different rows":
- T1SIReadplain
round one: plain has no index. T1 reads row 1. with no index, that's a seq scan, and the SIRead lock covers the whole relation.
notice one more detail in the middle round: after T1 updates row 1, its own tuple note on row 1 is gone. a transaction that writes a row it read doesn't need the read note any more: its write lock covers it.
what you can do about false positives: index the columns your serializable transactions search by (smaller notes), keep transactions short (notes die sooner), declare read-only transactions READ ONLY, and retry 40001 everywhere, because some will happen anyway (retrying serialization failures).
READ ONLY DEFERRABLE
a long read-only report at SERIALIZABLE takes notes on everything it reads and can still be a pivot. the escape hatch: SERIALIZABLE, READ ONLY, DEFERRABLE. the first query waits until it can run on a snapshot that's provably safe, and from then on the transaction takes no notes and can never fail with 40001:
- T1SIReaddoctors
T1 is a serializable read-write transaction: it reads, then writes.
the wait can be long on a busy server, so it's for reports and backups (pg_dump --serializable-deferrable uses it), not for requests.
keylines
- SSI = snapshot isolation + notes on reads + a check on writes.
- SIRead locks block nobody, come in tuple, page and relation sizes, and outlive COMMIT.
- a write over someone's note is an rw-antidependency: the reader must go first.
- two in a row through one pivot, with the far end committed first: 40001.
- false positives happen. index what you search, keep transactions short, retry.