skip to content

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.

run it at =
T1
·
T2
·
1
BEGIN ISOLATION LEVEL REPEATABLE READ
committed in the database
nameon_call
ashat
bent

asha (T1) wants to go off call, at repeatable read. the rule: at least one doctor stays on call.

step 1/9
real output, a postgres 18.6 server · ws-rr
switch the level and step through. under repeatable read both go off call and nobody is left. under serializable, T2's COMMIT fails with 40001. with FOR UPDATE at read committed, T2 waits, then sees only ben on call and backs off.

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:

T1 read ben's rowT2 changed itT2 read asha's rowT1 changed itT1 (asha)T2 (ben)
read, then changed by the other: a cycle
drawn by hand · the textbook picture of the capture above
each arrow is "read something the other one then changed". with a cycle, no one-at-a-time order fits: run T1 first and T2 would have seen 1 doctor; run T2 first and T1 would have.

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:

T2: COMMIT
  1. ERROR: could not serialize access due to read/write dependencies among transactions
  2. DETAIL: Reason code: Canceled on identification as a pivot, during commit attempt.
T2's error, SQLSTATE 40001, as the server sent it. note which statement got it.
predict the output
at SERIALIZABLE, which statement of the schedule fails?
sql
/* 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:

sqlempty db
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;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 2
#3 · BEGIN
#4 · SELECT 1
countint8
2
(1 row)
#5 · UPDATE 1
#6 · COMMIT
#7 · SELECT 2
nametexton_callbool
ashaf
bent
(2 rows)

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.