# Which isolation level should you use, and what does each one let through?

By Vishal Kumar · Published 2026-05-04 · Updated 2026-11-13
Source: https://fungyaan.com/p/isolation-levels-postgres
Tags: databases, postgres, transactions, concurrency

**The verdict, which the matrix below will prove cell by cell:** in PostgreSQL, use Read Committed (the default) and guard your invariants with constraints, atomic updates (`SET v = v + 1`) and `SELECT … FOR UPDATE`. Switch individual transactions to Serializable when a rule spans several rows that can't all be locked, and wrap them in a retry loop. Stricter levels don't make bugs disappear; they turn them into serialization errors you must retry. Measured on PostgreSQL 17: Read Committed lets lost updates and write skew through silently, Repeatable Read still lets write skew through, and only Serializable stops both.

Picture the case: two users buy the last item, a balance is off by exactly one payment, or the on-call rota ends up empty, and every transaction involved committed without a single error. Isolation levels have a reputation for being dry, but I find them absorbing: they're the database's promise about what you're allowed to see while everyone else is busy changing things. The best way to understand that promise is to watch it being kept, or broken.

The usual witness statement is unreliable. Most explanations of isolation levels are a table copied from the SQL standard: three anomalies, four levels, ticks and crosses. That table describes the *minimum* each level must prevent, not what your database does. It also leaves out the two anomalies that cause most real bugs: lost updates and write skew.

So I went and gathered the evidence myself. The [database-tradeoffs-lab](https://github.com/the-vishal-kumar/database-tradeoffs-lab) repo runs ten scenarios at every isolation level against PostgreSQL 17. Each scenario drives two transactions on separate connections, in a fixed interleaving, and checks the data at the end. The tests run the whole matrix five times to confirm the result doesn't depend on timing, and CI re-runs it weekly. You can reproduce everything below with `make isolation`.

> **Case file**
>
> | | |
> |---|---|
> | **The case** | The last item sold twice, a balance off by one payment, an empty on-call rota, and no errors |
> | **Suspects** | The SQL standard's table: dirty reads, non-repeatable reads, phantoms |
> | **Culprit** | Lost updates and write skew, let through silently below Serializable |
> | **The fix** | Read Committed plus constraints, atomic SQL and `FOR UPDATE`; Serializable with a retry loop where rules span rows |
> | **Evidence** | database-tradeoffs-lab on PostgreSQL 17.11, `make isolation` |

## The evidence

Measured on PostgreSQL 17.11, default configuration:

| Scenario | Read Uncommitted | Read Committed | Repeatable Read | Serializable |
|---|---|---|---|---|
| Dirty read | prevented | prevented | prevented | prevented |
| Non-repeatable read | **anomaly** | **anomaly** | prevented | prevented |
| Phantom read | **anomaly** | **anomaly** | prevented | prevented |
| Lost update (read, then write) | **anomaly** | **anomaly** | aborted, retry | aborted, retry |
| …fixed with `UPDATE v = v + 1` | prevented | prevented | aborted, retry | aborted, retry |
| …fixed with `SELECT … FOR UPDATE` | prevented | prevented | aborted, retry | aborted, retry |
| Write skew | **anomaly** | **anomaly** | **anomaly** | aborted, retry |
| …fixed with `SELECT … FOR UPDATE` | prevented | prevented | aborted, retry | aborted, retry |
| Lost update, with a retry on 40001 | **anomaly** | **anomaly** | prevented | prevented |
| Write skew, with a retry on 40001 | **anomaly** | **anomaly** | **anomaly** | prevented |

*Anomaly* means both transactions committed and the data is wrong. *Prevented* means both committed and the data is right. *Aborted, retry* means Postgres refused one transaction with SQLSTATE `40001` (serialization failure): the data is right, but only if your application runs the transaction again.

Three things in that table surprise people. Let's question the rows one at a time, then come back to them.

## Ruled out early: dirty reads

T1 sets a balance to 0 and doesn't commit. T2 reads the balance. At every level, including Read Uncommitted, T2 read 100, the committed value.

That's documented behaviour: Postgres accepts `READ UNCOMMITTED` but runs it as Read Committed. Each statement sees only committed data. If you came from a database where Read Uncommitted is real, there's nothing to configure here; it doesn't exist. One suspect off the list.

## Clue: non-repeatable reads and phantoms

T1 reads a balance, another session changes it to 50 and commits, T1 reads again.

- **Read Committed:** T1 read 100, then 50. Each *statement* takes a fresh snapshot.
- **Repeatable Read:** T1 read 100, then 100. The *transaction* keeps the snapshot from its first statement.

Phantoms are the same idea for sets of rows. T1 counts a tenant's orders, another session inserts one, T1 counts again: 2, then 3 under Read Committed; 2, then 2 under Repeatable Read.

**Surprise #1:** the SQL standard allows phantoms at Repeatable Read. Postgres doesn't, because its Repeatable Read is snapshot isolation: the whole transaction reads from one snapshot, so new rows can't appear in it. The level names are standard, the behaviour isn't. MySQL's InnoDB, for example, defaults to Repeatable Read and implements it differently (I haven't measured MySQL in the lab, so I won't claim more than that). Never assume a level means the same thing across databases.

**When this matters:** a report or export that runs several queries and must see one consistent state (totals that add up, a page and its count). Run it at Repeatable Read. For a single statement, Read Committed is already consistent within that statement.

## The prime suspect: lost updates

This is the bug hiding in every read-modify-write. You've probably hit this one, perhaps without knowing it.

Both transactions read a counter (0), add 1 in application code, and write the result back. Two increments should give 2.

- **Read Committed:** v = 1. T2 overwrote T1's increment. Both transactions committed; nothing errored; an increment is gone. A crime with no fingerprints.
- **Repeatable Read / Serializable:** T2's `UPDATE` failed with `could not serialize access due to concurrent update`. v = 1, but now the application knows, and a retry fixes it.

This is the pattern behind "two users bought the last item", "the balance is off by one payment", and "a setting change disappeared". It's also what most ORMs generate by default: load an object, change a field, save.

There are three fixes, and the matrix compares them (the [lock or retry post](/p/lock-or-retry) measures them under load):

| Fix | Read Committed | Repeatable Read / Serializable | Cost |
|---|---|---|---|
| Do the arithmetic in SQL: `UPDATE … SET v = v + 1` | prevented | aborted, retry | None. Only works when the new value can be computed in SQL |
| Lock first: `SELECT … FOR UPDATE`, then update | prevented (T2 waits, then reads 1) | aborted, retry | T2 blocks while T1 holds the lock |
| Keep read-then-write, retry on `40001` | still an anomaly | prevented | Retries under contention |

**Surprise #2, and a false lead worth noticing:** at Repeatable Read and Serializable, the classic fixes don't stop the abort. With `FOR UPDATE`, T2 waits for T1's lock, and when it gets the row it finds a version newer than its snapshot, so Postgres aborts it rather than let it act on a row it can't see. At those levels the fix *is* the retry. Locking decides who waits; it doesn't make the retry unnecessary.

## The hidden culprit: write skew

The rule: at least one doctor must stay on call. Alice and Bob are both on call. Each, at the same time, checks "two doctors are on call, so I can go off call" and updates *their own* row.

- **Read Committed and Repeatable Read:** both committed. Nobody is on call.
- **Serializable:** T2 was aborted, and one doctor stays on call.

```mermaid
sequenceDiagram
  participant A as Alice (T1)
  participant DB as PostgreSQL
  participant B as Bob (T2)
  A->>DB: count doctors on call
  B->>DB: count doctors on call
  DB-->>A: 2
  DB-->>B: 2
  A->>DB: set Alice off call
  B->>DB: set Bob off call
  A->>DB: COMMIT
  B->>DB: COMMIT
  Note over A,B: Read Committed and Repeatable Read - both commit, nobody on call
  Note over A,B: Serializable - T2 aborted with 40001 and one doctor stays
```

This is the twist in the case: Repeatable Read can't catch this. The two transactions never write the same row, so there's no write-write conflict to detect. Each one's decision rested on a read the other invalidated. Postgres's Serializable level (Serializable Snapshot Isolation) tracks those read-write dependencies, and aborts a transaction when they form a cycle that no serial order could produce. The idea comes from research by Michael Cahill and colleagues, and I find it genuinely beautiful: instead of locking everything up front, watch for the one dangerous shape and step in only then. The error even names what it caught:

```
ERROR:  could not serialize access due to read/write dependencies among transactions
DETAIL: Reason code: Canceled on identification as a pivot, during commit attempt.
HINT:   The transaction might succeed if retried.
```

In this interleaving the abort came at `COMMIT`. In the retry scenario, where T1 commits before T2 writes, it came at T2's `UPDATE`. Your code has to treat `40001` from *any* statement, including `COMMIT`, as "retry".

Below Serializable, the fix is to make the conflict visible with a lock: `SELECT … FOR UPDATE` on the rows the decision depends on. At Read Committed, T2 waits for T1, then re-checks the rows, sees only Bob on call, and leaves him on. That works when the rows exist. It doesn't work when the rule is about rows that *don't* exist yet ("no two bookings overlap"), because you can't lock a row that isn't there. Then the options are a constraint (an exclusion constraint for overlaps, a unique index for "only one"), a lock on a parent row, or Serializable.

**Surprise #3:** write skew is the most dangerous row in the table and the standard's table doesn't mention it. Any rule that spans more than one row (quotas, balances across accounts, "at least one admin", scheduling) is exposed to it below Serializable.

## The reveal: the retry loop is not optional

The last two rows of the matrix are the same scenarios with a retry: if a transaction fails with `40001`, run the *whole* transaction again from its first read.

- Lost update with retry: prevented at Repeatable Read and Serializable (T2 committed on attempt 2; v = 2).
- Write skew with retry: prevented at Serializable (T2 re-counted, saw one doctor on call, and left him there).
- At Read Committed both are still anomalies. Nothing failed, so there was nothing to retry.

That's the core trade-off between the levels. **Weaker levels let bugs through silently; stronger levels turn them into errors that you must handle.** If you raise the isolation level without adding a retry loop, you've converted silent data corruption into user-facing 500s. Better, but not done; the case only closes when the loop is in place.

The loop in the lab is about twenty lines of Go:

```go
for attempt := 1; attempt <= 3; attempt++ {
	t, err := begin(ctx, c, iso)
	if err != nil {
		return attempt, err
	}
	for _, phase := range []func(*txn) error{w.read, w.write} {
		if err := phase(t); err != nil {
			t.rollback(ctx)
			return attempt, err
		}
	}
	if err := t.commit(ctx); err != nil {
		return attempt, err
	}
	if !t.aborted { // a 40001 from any statement marks the transaction aborted
		return attempt, nil
	}
}
```

Three rules for a production version: retry the *entire* transaction, including its reads, never just the failed statement; add jittered backoff; and keep side effects (emails, HTTP calls, queue publishes) out of the transaction or behind an [outbox](/p/consistency-without-2pc), because they run again on every attempt.

## The line-up, side by side

| | Read Committed | Repeatable Read | Serializable |
|---|---|---|---|
| **Guarantee** | Each statement sees committed data | The whole transaction sees one snapshot; concurrent updates to the same row abort | Result equals *some* serial order of the transactions |
| **Cost** | Lowest; no serialization failures from reads | Aborts on write-write conflicts | Aborts on read-write dependency cycles, including some false positives; extra tracking overhead |
| **Failure mode** | Silent lost updates and write skew | Silent write skew | Retries under contention |
| **I'd pick it for** | The default for OLTP, with atomic updates, `FOR UPDATE` and constraints guarding invariants | Reports and exports that run several queries and must agree | Transactions that enforce a rule across rows: money, quotas, inventory, scheduling |
| **I wouldn't** | Multi-query reports that must add up | Multi-row rules (it looks safe and isn't) | Long transactions, or anything without a retry loop |

> **Remember this**
> - Read Committed lets lost updates and write skew through silently.
> - Repeatable Read stops lost updates but still lets write skew through.
> - Stricter levels turn bugs into `40001` errors: retry the whole transaction.

## Case closed: what I'd pick for a multi-tenant SaaS

Read Committed as the default, because it's Postgres's default, it never throws serialization failures at ordinary reads, and most writes can be made safe directly:

1. **Constraints first.** Unique indexes, `CHECK`, foreign keys and exclusion constraints enforce rules no matter what isolation level a future engineer picks.
2. **Atomic SQL** for counters and balances (`SET v = v + $1`), not read-modify-write.
3. **`FOR UPDATE`** where a decision depends on rows that exist, locking in a consistent order to avoid deadlocks.
4. **Serializable, per transaction,** for the few flows that enforce rules spanning rows that can't be locked: `BEGIN ISOLATION LEVEL SERIALIZABLE` there, with the retry loop around it.

And whatever level you choose, test the anomaly, not the happy path. A test that runs two transactions in a fixed interleaving, like the ones in the lab, is the only way to know which row of the table your code is in.

What I like about this matrix is how honest it is: every anomaly that once hid in plain sight now has a row with its name on it. Every level is a carefully reasoned trade-off, and once you can see exactly what each one lets through, choosing between them stops being guesswork and starts being engineering.

## Scope and caveats

- PostgreSQL 17.11 in its default configuration. The test pins every cell, so if a future release changes behaviour, CI will say so.
- Two transactions and one interleaving per scenario. That's enough to show what each level permits; it's not a performance benchmark, and I haven't measured the overhead of Serializable here.
- MySQL, MongoDB and DynamoDB use different models and aren't measured in this post. Later posts in the series add them to the lab.

*Run it yourself:* `git clone https://github.com/the-vishal-kumar/database-tradeoffs-lab && cd database-tradeoffs-lab && make isolation`
