write skew and SERIALIZABLE
two engineers go off call at once. snapshot isolation allows it, SERIALIZABLE doesn't.
- address
- everything.transactions.write_skew
- part
- part 11, transactions and isolation
- chapter
- chapter 8 of 11
- reading time
- 8 min
write skew: two transactions read the same rows, each decides it's safe to act, and each writes a different row. no row is written twice, so nobody waits and nothing conflicts. both commit, and the rule both of them checked is broken. postgres's REPEATABLE READ (snapshot isolation) allows it. SERIALIZABLE catches it and fails one of them with 40001. so does locking the rows the rule reads, with FOR UPDATE.
the picture
you know the on-call rule: at least one person holds the pager. two engineers feel ill on the same evening, both open the rota, both see someone else on call, both hand back the pager. the textbook tells it with doctors, so here are two: asha and ben, both on call, and the rule is that at least one stays on.
each of them is right, on what they saw. that's what makes write skew nasty: there's no bad read and no bad write, only two good decisions that can't both be true. like two people each taking "the last" slice of cake because each counted two slices before reaching for one.
| name | on_call |
|---|---|
| asha | t |
| ben | t |
asha (T1) wants to go off call, at repeatable read. the rule: at least one doctor stays on call.
why snapshot isolation misses it
REPEATABLE READ gives each transaction one snapshot and protects one thing on writes: if a row you update was changed by someone who committed after your snapshot, you get 40001 ("could not serialize access due to concurrent update"). that's how it stops lost updates. here, T1 updates asha's row and T2 updates ben's. different rows. there's no concurrent update to complain about, and no row lock to wait for.
what went wrong is in the reads. T1 read ben's row (on call) and T2 then changed it. T2 read asha's row and T1 then changed it. each one's decision depended on something the other one overwrote. draw that as arrows and you get a cycle:
a cycle like that is the definition of not serializable. if T1 had really run first, T2's count would have been 1 and ben would have stayed. if T2 had run first, asha would have. the actual result (both off) matches neither order. (schedules and serializability is the theory, with precedence graphs.)
SERIALIZABLE: track the reads too
at SERIALIZABLE, postgres also remembers what each transaction read (with SIRead locks, which block nothing; they're just notes) and watches for exactly those read-then-overwritten arrows between transactions that overlap. when it finds the dangerous pattern, it fails one of them. here's what T2 got:
- 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 count(*) FROM doctors WHERE on_call;
/* T2 */ BEGIN ISOLATION LEVEL SERIALIZABLE; SELECT count(*) FROM doctors WHERE on_call;
/* T1 */ UPDATE doctors SET on_call = false WHERE name = 'asha';
/* T2 */ UPDATE doctors SET on_call = false WHERE name = 'ben';
/* T1 */ COMMIT;
/* T2 */ COMMIT;run on its own, the same transaction is perfectly fine. this is the browser's postgres, one session, nobody else around:
CREATE TABLE doctors (name text PRIMARY KEY, on_call boolean NOT NULL);
INSERT INTO doctors VALUES ('asha', true), ('ben', true);
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) FROM doctors WHERE on_call;
UPDATE doctors SET on_call = false WHERE name = 'asha';
COMMIT;
SELECT name, on_call FROM doctors ORDER BY name;| countint8 |
|---|
| 2 |
| nametext | on_callbool |
|---|---|
| asha | f |
| ben | t |
that's the deal SERIALIZABLE offers: if each transaction is correct when it runs alone, the mix is correct too, or one of them gets 40001. you don't have to find the anomalies yourself. you do have to retry. how the tracking works (SIRead locks, rw-antidependencies, the pivot named in that DETAIL) is in serializable snapshot isolation.
or: lock what the rule reads
the third run doesn't use SERIALIZABLE at all. the rule depends on the on-call rows, so read them FOR UPDATE: T1 locks both rows, and T2's identical read waits. when T1 commits, T2's read re-checks each row's newest version against WHERE on_call, asha drops out, and T2 gets one row back. the app sees one doctor and says no.
that works when the rows the rule depends on exist. it doesn't when the rule is about rows that don't exist yet: "no two bookings for this room overlap" reads zero rows, and you can't lock a row that isn't there. that's phantom-shaped write skew, and the fixes are SERIALIZABLE, an exclusion constraint, or a row you create on purpose for both sides to lock (one per room per day, say).
one level deeperwhy not just make everything serializable?
it costs something, and how much depends on the workload. every serializable transaction keeps its read notes until the transactions it overlapped with finish, and the 40001 rate climbs with contention. long read-only reports can avoid both: BEGIN ISOLATION LEVEL SERIALIZABLE, READ ONLY, DEFERRABLE waits at its first read until it can get a snapshot that's guaranteed safe, then runs with no tracking and can never fail with 40001. for most apps the answer is: SERIALIZABLE everywhere plus a retry loop is simpler to get right than proving each transaction safe at a lower level. measure the retry rate.
keylines
- write skew: two reads, two decisions, two different rows written. nothing waits, both commit.
- REPEATABLE READ is snapshot isolation. it allows write skew.
- SERIALIZABLE tracks reads, finds the cycle, and fails one transaction with 40001.
- the 40001 can arrive at COMMIT. the retry loop has to include it.
- without SERIALIZABLE: lock the rows the rule reads. if they don't exist yet, that's not enough.