skip to content

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.

RURCRRSER
dirty read
non-repeatable read
phantom
lost update
read skew
write skew
the standard allows itpostgres: it happenedpostgres: prevented (40001 = by an error)the run below
lost updateatread committedthe standard: allowed · postgres: it happened
T1
snapshot 762:762:
T2
·
1
BEGIN ISOLATION LEVEL READ COMMITTED

read committed. T1 will deposit 10.00.

step 1/9
real output, a postgres 18.6 server · anomaly-lost-update
click any cell, then step through its run underneath. the top of a cell is the standard, as the postgres manual's table describes it. the bottom is what postgres did: happens, no, or 40001 when it stopped the anomaly by aborting a transaction.

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 · read uncommitted
T1
·
T2
·
1
BEGIN
2
UPDATE accounts SET balance = balance - 100.00 WHERE id = 1→ UPDATE 1
3
BEGIN ISOLATION LEVEL READ UNCOMMITTED
4
SHOW transaction_isolation→ read uncommitted
5
SELECT balance FROM accounts WHERE id = 1→ 100.00
6
ROLLBACK
7
COMMIT

dirty read: prevented. T2 read 100.00, the committed balance. T1's uncommitted 0.00 never reached it.

real output, a postgres 18.6 server · anomaly-dirty-read
T2 asks for read uncommitted and gets it, by name. its read still returns the committed 100.00, not T1's 0.00, which T1 then rolls back.

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 · read committed
T1
·
T2
·
1
BEGIN ISOLATION LEVEL READ COMMITTED
2
SELECT balance FROM accounts WHERE id = 1→ 100.00
3
UPDATE accounts SET balance = 150.00 WHERE id = 1→ UPDATE 1
4
SELECT balance FROM accounts WHERE id = 1→ 150.00
5
COMMIT

non-repeatable read: happened. the same read gave 100.00, then 150.00.

real output, a postgres 18.6 server · anomaly-non-repeatable-read
at read committed, the second read returns T1's 150.00. the snapshot under each session's name moves forward with every statement.

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 · read committed
T1
·
T2
·
1
BEGIN ISOLATION LEVEL READ COMMITTED
2
SELECT count(*) FROM accounts WHERE balance >= 100.00→ 2
3
INSERT INTO accounts VALUES (3, 'carol', 100.00)→ INSERT 0 1
4
SELECT count(*) FROM accounts WHERE balance >= 100.00→ 3
5
COMMIT

phantom: happened. the same count gave 2, then 3: carol's new row showed up.

real output, a postgres 18.6 server · anomaly-phantom
T2 counts the accounts with at least 100.00, twice. in between, T1 opens carol's account. at read committed the count goes from 2 to 3.
predict the output
same schedule at repeatable read. what does T2's second count return?
sql
/* 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 · read committed
T1
·
T2
·
1
BEGIN ISOLATION LEVEL READ COMMITTED
2
SELECT balance FROM accounts WHERE id = 1→ 100.00
3
BEGIN ISOLATION LEVEL READ COMMITTED
4
SELECT balance FROM accounts WHERE id = 1→ 100.00
5
UPDATE accounts SET balance = 110.00 WHERE id = 1→ UPDATE 1
6
UPDATE accounts SET balance = 120.00 WHERE id = 1waits for T1: ShareLock on transaction 762
7
COMMIT
woke:UPDATE accounts SET balance = 120.00 WHERE id = 1→ UPDATE 1
8
COMMIT
9
SELECT balance FROM accounts WHERE id = 1→ 120.00

lost update: happened. alice has 120.00, not 130.00. T1's deposit is gone, and nobody got an error.

real output, a postgres 18.6 server · anomaly-lost-update
two deposits, 10.00 and 20.00, each read, added up in the app, and written back at read committed. T2 waits for T1's row lock, then writes its 120.00 anyway. the balance ends at 120.00.

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 · read committed
T1
·
T2
·
1
BEGIN ISOLATION LEVEL READ COMMITTED
2
SELECT balance FROM accounts WHERE id = 1→ 100.00
3
BEGIN; UPDATE accounts SET balance = balance - 50.00 WHERE id = 1; UPDATE accounts SET balance = balance + 50.00 WHERE id = 2→ UPDATE 1
4
COMMIT
5
SELECT balance FROM accounts WHERE id = 2→ 150.00
6
COMMIT

read skew: happened. T2 saw alice 100.00 and bob 150.00: 250.00 in total, a state the bank was never in.

real output, a postgres 18.6 server · anomaly-read-skew
T2 reads alice (100.00), T1's transfer commits, T2 reads bob (150.00). T2 now believes there's 250.00, 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 · repeatable read
T1
·
T2
·
1
BEGIN ISOLATION LEVEL REPEATABLE READ
2
SELECT count(*) FROM doctors WHERE on_call→ 2
3
BEGIN ISOLATION LEVEL REPEATABLE READ
4
SELECT count(*) FROM doctors WHERE on_call→ 2
5
UPDATE doctors SET on_call = false WHERE name = 'asha'→ UPDATE 1
6
UPDATE doctors SET on_call = false WHERE name = 'ben'→ UPDATE 1
7
COMMIT
8
COMMIT
9
SELECT count(*) FROM doctors WHERE on_call→ 0

write skew: happened. 0 doctors on call. both checks passed, and together they broke the rule.

real output, a postgres 18.6 server · anomaly-write-skew
two doctors on call, the rule is at least one stays on. each checks (2 on call), each takes only themselves off. at repeatable read both commit, and 0 doctors are on call.

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.