the anomaly catalogue
dirty reads, non-repeatable reads, phantoms, lost updates, read skew and write skew. each one in two sessions.
- address
- everything.transactions.anomalies
- part
- part 11, transactions and isolation
- chapter
- chapter 3 of 11
- reading time
- 12 min
an anomaly is a result two transactions produce together that neither could produce running alone. six are worth knowing: dirty reads, non-repeatable reads, phantoms, lost updates, read skew and write skew. postgres never allows the first one. which of the other five you get depends on the isolation level.
the picture
you know how a transaction feels like having the database to yourself? BEGIN, do your thing, COMMIT. at the default level, it isn't quite that. other people's commits land in the middle of your transaction, and some combinations of them leave the data in a state nobody intended.
think of a shared notebook (the database) and two people writing in it at once (two transactions). each anomaly is one way the other person's writing leaks into your work: you copy down a number they're about to cross out (dirty read), you look at the same line twice and it changed (non-repeatable read), a new line appears under a heading you already counted (phantom), you both overwrite the same line and one edit vanishes (lost update), you add up two lines from two different moments (read skew), or you each check the other's line, then each change your own (write skew).
every cell below is a real run. the top half is what the SQL standard allows at that level, the bottom half is what postgres 18.6 did when two sessions actually ran the schedule.
| top: the standardbottom: postgres | RUread uncommitted | RCread committed | RRrepeatable read | SERserializable |
|---|---|---|---|---|
| dirty read | ||||
| non-repeatable read | ||||
| phantom | ||||
| lost update | ||||
| read skew | ||||
| write skew |
read committed. T1 will deposit 10.00.
read it by columns. postgres's read uncommitted column is the same as its read committed one, cell for cell: the level exists by name only. repeatable read stops everything except write skew, and it stops lost updates with an error rather than quietly. serializable stops all six, write skew with an error too. the rest of the chapter is the six rows, one at a time.
dirty reads: never, at any level
a dirty read is reading another transaction's uncommitted write. the standard allows it at READ UNCOMMITTED. postgres doesn't, and can't: a snapshot never includes a transaction that's still running, so its new row versions are invisible to everyone else no matter what level they asked for (the visibility rules are the whole story).
dirty read: prevented. T2 read 100.00, the committed balance. T1's uncommitted 0.00 never reached it.
that SHOW is the funny part. postgres accepts READ UNCOMMITTED, reports it back, and runs the transaction as READ COMMITTED. the manual says so in one line, and the capture agrees.
non-repeatable reads
read a row, read it again in the same transaction, get a different answer, because someone committed in between. READ COMMITTED does this on purpose: it takes a new snapshot for every statement. REPEATABLE READ takes one snapshot at its first statement and keeps it.
non-repeatable read: happened. the same read gave 100.00, then 150.00.
is that bad? only if your code assumes the two reads agree. a page that reads a balance to decide, then reads it again to display, can decide on one number and show another.
phantoms
a phantom is the same thing for a whole set of rows: run a query with a WHERE, run it again, and rows that match have appeared or disappeared. nothing you read changed; the set did.
phantom: happened. the same count gave 2, then 3: carol's new row showed up.
/* T2 */ BEGIN ISOLATION LEVEL REPEATABLE READ;
/* T2 */ SELECT count(*) FROM accounts WHERE balance >= 100.00; -- 2
/* T1 */ INSERT INTO accounts VALUES (3, 'carol', 100.00);
/* T2 */ SELECT count(*) FROM accounts WHERE balance >= 100.00; -- ?lost updates
two transactions read the same value, each computes a new one in the app, each writes it back. the second write lands on top of the first, and the first one's change is gone.
lost update: happened. alice has 120.00, not 130.00. T1's deposit is gone, and nobody got an error.
notice T2 did wait. postgres never lets two transactions write the same row at the same time (that would be a dirty write, which no level allows): the second writer queues behind the first one's row lock. the trouble is what it does after waiting. at read committed it writes the value it computed from its stale read. at repeatable read it gets 40001 instead and has to start over. lost updates has four ways out, compared on the same capture.
read skew
read skew is two reads, each correct on its own, that together describe a moment that never happened. bank's invariant is that alice and bob hold 200.00 between them, always. T1 moves 50.00 from one to the other in a single transaction, so every committed state adds up. T2 reads one before the transfer and the other after.
read skew: happened. T2 saw alice 100.00 and bob 150.00: 250.00 in total, a state the bank was never in.
this is the one that bites reports and exports: anything that reads many rows across several statements. a balance sheet that doesn't balance, a dump with half a transfer in it. it's why pg_dump runs the whole dump inside one repeatable read transaction: one snapshot, one moment.
write skew
write skew is the subtle one. two transactions read the same rows, decide based on what they read, and then each writes a different row. no row is written twice, so no row lock ever makes anyone wait. both commit. the rule both of them checked is broken.
write skew: happened. 0 doctors on call. both checks passed, and together they broke the rule.
repeatable read can't see it coming: each transaction's snapshot really did show two doctors, and neither wrote anything the other wrote. only SERIALIZABLE notices, by tracking who read what, and it refuses the second COMMIT. write skew and SERIALIZABLE walks through it, and serializable snapshot isolation shows how postgres does the tracking.
one level deeperwhere the names come from
the SQL-92 standard defines its levels by three phenomena (dirty read, non-repeatable read, phantom) and says SERIALIZABLE must behave like some one-at-a-time order. in 1995 Berenson, Bernstein, Gray, Melton, O'Neil and O'Neil published "a critique of ANSI SQL isolation levels", which showed the phenomena were worded loosely enough to read two ways, added the ones the standard never named (dirty write, lost update, read skew, write skew), and described snapshot isolation, the thing postgres calls REPEATABLE READ. the postgres manual cites that paper for the full treatment.
so for the last three rows of the grid, the "the standard allows" half needs a footnote: the standard doesn't mention them at all. they're serialization anomalies, and the manual's table marks serialization anomalies possible at every level below SERIALIZABLE.
keylines
- an anomaly is a result two overlapping transactions produce that no one-at-a-time order would.
- postgres never shows uncommitted data. READ UNCOMMITTED is READ COMMITTED.
- READ COMMITTED allows the other five. REPEATABLE READ stops all but write skew.
- some anomalies are stopped with an error (40001), not silently. your app has to retry.
- write skew writes different rows, so no lock ever notices. only SERIALIZABLE does.