Skip to content
mentoor

Topic 2 of 5 · 5 min read

Isolation levels and the anomalies they allow

Goal: Name the anomaly a given interleaving produces and the lowest isolation level or lock that prevents it.

An isolation level decides which strange results two concurrent transactions may see, and you pick the lowest level that rules out the anomaly your code cannot tolerate. The default in PostgreSQL is read committed. Most application bugs come from assuming it is stronger.

Check yourself · 1 of 3

What is PostgreSQL's default isolation level?

Choose one answer

Why it matters

Single-user tests never interleave two transactions. In production they do, and the code that passed review can lose an update or break a rule only when two requests overlap by milliseconds.

The three PostgreSQL levels

  • Read committed (default). Each statement sees data committed before that statement started. Two reads in one transaction can differ, because someone may commit between them (a non-repeatable read).
  • Repeatable read. The whole transaction sees one snapshot from its first statement. If it tries to update a row another transaction changed after that snapshot, PostgreSQL aborts it with a serialization failure.
  • Serializable. The result is the same as if the transactions ran one after another. PostgreSQL tracks read and write dependencies and aborts one transaction when it finds a cycle.

PostgreSQL treats read uncommitted as read committed, so dirty reads never happen.

Two anomalies to know by name

  • Lost update. Two transactions read a balance of 100. Each adds 10 and writes 110. One addition vanished. At read committed this happens with read-then-write code. Repeatable read aborts the second writer instead.
  • Write skew. Two doctors are on call and a rule says at least one must stay. Each transaction checks "is another doctor on call?", sees yes, and removes itself. They update different rows, so no row conflict is raised, and nobody is on call. Repeatable read does not stop this. Serializable does, or a lock on the rows the check read.

How to choose

  1. Write down the anomaly you fear (lost update, write skew, a report that sees half a change).
  2. If one row is involved, prefer an atomic UPDATE or SELECT ... FOR UPDATE at read committed.
  3. If a rule spans several rows, use serializable or lock the rows you read.
  4. At repeatable read or serializable, retry on SQLSTATE 40001 (serialization failure) and 40P01 (deadlock). A failure is the database protecting you, not an outage.

Before and after

BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) FROM on_call WHERE shift = 'night';
UPDATE on_call SET active = false WHERE doctor_id = 7;
COMMIT;

Without a retry loop around this, one of two concurrent requests returns a 500 error. With a loop that reruns the whole transaction up to three times, both succeed or the second correctly sees one doctor left.

What goes wrong

  • Believing "I used a transaction, so it is safe".
  • Raising the level but not retrying, so users see random errors.
  • Retrying only the failed statement instead of the whole transaction.

Examples

Read two rows, write one

A generated booking handler checks that a room has no booking for 10:00, then inserts one. Two requests both see no booking and both insert. No row was updated twice, so repeatable read does not help. A unique constraint on (room, slot), or serializable with a retry loop, does.

The retry that was missing

After switching a payout job to serializable, the error rate showed 0.5% of runs failing with could not serialize access due to concurrent update. The fix was not to go back to read committed. It was to wrap the transaction in a retry that reruns it on 40001.

Key points

  • PostgreSQL defaults to read committed, where each statement sees a fresh snapshot.
  • A lost update happens when two transactions read the same value and both write back a result based on it.
  • Write skew changes different rows based on a shared read, so row-level conflict detection misses it.
  • Repeatable read aborts a conflicting writer; only serializable (or explicit locks) stops write skew.
  • At stricter levels, retry the whole transaction on SQLSTATE 40001 and 40P01.

Common mistakes

  • Assuming a transaction alone prevents lost updates at the default level.
  • Setting serializable without any retry logic.
  • Retrying a single statement rather than the whole transaction.
  • Using repeatable read to prevent write skew.

Check yourself · 2 of 3

Two transactions each read balance 100, add 10 and write 110. The final balance is 110, not 120. What is this anomaly called?

Choose one answer

Check yourself · 3 of 3

A service runs a transaction at serializable and now sometimes fails with SQLSTATE 40001. What should the code do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic