READ COMMITTED, up close
a new snapshot per statement, and the UPDATE that re-checks a row after waiting for it.
- address
- everything.transactions.read_committed
- part
- part 11, transactions and isolation
- chapter
- chapter 5 of 11
- reading time
- 7 min
READ COMMITTED, postgres's default, gives every statement a fresh snapshot: each one sees everything committed before it started, and nothing after. writes add one twist: an UPDATE or DELETE that finds its row locked waits, then re-checks its WHERE on the row's newest version and acts on that version, or skips the row.
the picture
you know how two SELECTs in one transaction can disagree at the default level? that's not a bug in your transaction. it's the definition of this level: the transaction doesn't have a snapshot, each statement does.
think of a newspaper (a snapshot): every statement buys today's edition on its way in, reads only that, and throws it away when it's done. the next statement buys a newer edition. inside one statement the news never changes; between two statements it can.
read committed. T2 opens a transaction.
your own uncommitted writes are always in your snapshot, whatever the level. that part never surprises anyone, but it's in the definition, so here it is:
BEGIN;
UPDATE accounts SET balance = balance - 50.00 WHERE id = 1;
SELECT owner, balance FROM accounts WHERE id IN (1, 2) ORDER BY id;
ROLLBACK;
SELECT owner, balance FROM accounts WHERE id IN (1, 2) ORDER BY id;| ownertext | balancenumeric |
|---|---|
| alice | 50.00 |
| bob | 100.00 |
| ownertext | balancenumeric |
|---|---|
| alice | 100.00 |
| bob | 100.00 |
what a snapshot holds and how a version gets judged visible against it is snapshots and the visibility rules. this chapter is about what the default level does with them.
the UPDATE that re-checks
reads never wait. writes can't avoid it: two transactions can't both change one row. so what does an UPDATE do when the row it wants is locked by a transaction that hasn't finished? at READ COMMITTED it waits for that transaction to end. if it committed, the UPDATE takes the row's newest version (one its snapshot doesn't even include), runs its WHERE again on it, and if the row still matches, applies the change to that version. inside postgres this is called EvalPlanQual.
here's a case where the re-check changes the answer. T1 is halfway through taking 50.00 from alice. T2 pays a 10.00 bonus to every account with at least 100.00.
/* T1 */ BEGIN;
/* T1 */ UPDATE accounts SET balance = balance - 50.00 WHERE id = 1;
/* T2 */ UPDATE accounts SET balance = balance + 10.00
WHERE balance >= 100.00; -- waits for alice's row
/* T1 */ COMMIT;
/* T2 */ -- UPDATE ?| id | owner | balance |
|---|---|---|
| 1 | alice | 100.00 |
| 2 | bob | 100.00 |
T1 starts taking 50.00 out of alice's account.
that re-check is why most single-row updates are safe at the default level without anyone thinking about it. SET balance = balance - 50 WHERE id = 1 AND balance >= 50 computes from the newest version after any wait, so two withdrawals can't both spend the same money. it's also why balance = balance + 20 doesn't lose updates and balance = 120.00 does.
the DELETE that deleted nothing
the re-check only looks at rows the statement found in its own snapshot and had to wait for. everything else it judges by its snapshot. mix the two and you can get an answer no single moment would give. this is the example from the postgres manual, run for real: two counters at 9 and 10, T1 adds one to both, T2 deletes the counter at 10.
| id | hits |
|---|---|
| 1 | 9 |
| 2 | 10 |
T1 adds one to every counter: 9 becomes 10, 10 becomes 11. not committed yet.
no single moment in time has this result. before T1, row 2 was 10; after T1, row 1 is 10. T2 saw each row at a different time, because one of them it had to wait for and the other it didn't. the manual says it straight out: READ COMMITTED gives each command a consistent snapshot, except for rows it updates, and that makes it a bad fit for commands with complex search conditions.
why it's still the default
because for the common case it's exactly right and costs nothing. a transfer touches two known rows. it wants the newest balance, not the one from when the transaction started, and it doesn't want to fail because someone else touched the account a moment ago. READ COMMITTED gives it that: no 40001 to handle, waits only on real write conflicts, and readers never wait at all. it stops being enough when a transaction makes a decision from several reads (read skew, write skew), and then you go up a level: isolation levels.
keylines
- READ COMMITTED: a new snapshot for every statement, not every transaction.
- two reads in one transaction can disagree. that's the level working as designed.
- an UPDATE that waited for a row re-checks its WHERE on the newest version, and acts on that.
- the re-check is why
SET x = x + 1and conditional withdrawals are safe at the default level. - rows not waited for are judged by the old snapshot, so one statement can mix two moments.