skip to content

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.

T1
·
T2
snapshot 761:761:
1
BEGIN ISOLATION LEVEL READ COMMITTED

read committed. T2 opens a transaction.

step 1/5
real output, a postgres 18.6 server · anomaly-non-repeatable-read
step through. the snapshot under T2's name (xmin:xmax:in-progress) is the one a statement in T2 would get at that moment. it moves forward as soon as T1 commits, so T2's second read sees 150.00.

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:

sqldb=bank
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;
output, from postgres 18.6
#1 · BEGIN
#2 · UPDATE 1
#3 · SELECT 2
ownertextbalancenumeric
alice50.00
bob100.00
(2 rows)
#4 · ROLLBACK
#5 · SELECT 2
ownertextbalancenumeric
alice100.00
bob100.00
(2 rows)

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.

predict the output
T2's UPDATE waits for alice's row, then T1 commits (alice is now 50.00). how many rows does T2's UPDATE change?
sql
/* 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 ?
T2 runs at =
T1
snapshot 759:759:
T2
·
1
BEGIN
committed in the database
idownerbalance
1alice100.00
2bob100.00

T1 starts taking 50.00 out of alice's account.

step 1/5
real output, a postgres 18.6 server · rc-recheck
switch the level and step. at read committed T2 wakes up, re-checks alice's new version, skips her and pays bob: UPDATE 1. at repeatable read the same wait ends in 40001. the table is what a new transaction would see after each step.

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.

T1
snapshot 759:759:
T2
·
1
BEGIN; UPDATE counters SET hits = hits + 1→ UPDATE 2
committed in the database
idhits
19
210

T1 adds one to every counter: 9 becomes 10, 10 becomes 11. not committed yet.

step 1/4
real output, a postgres 18.6 server · rc-delete
step through. T2's snapshot has 9 and 10, so only row 2 matches, and it's locked. after T1 commits, row 2 is 11 (no match), and row 1, now 10, was judged at 9. DELETE 0, with a counter at 10 both before and after.

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 + 1 and 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.