Learn SQL
-- performance.isolation_anomalies_levels

Isolation Levels & Anomalies in SQL

Which anomaly each level forbids — and the retry price

--concept

READ COMMITTED (PG default): no dirty reads; statement snapshots. REPEATABLE READ: transaction snapshot — no non-repeatables (PG snapshot isolation). SERIALIZABLE: serial-equivalence, aborts on conflicts — apps retry. Anomalies: dirty (uncommitted visible), non-repeatable (re-read changed), phantom (range recount gains rows). Higher costs concurrency + retries.

--how it works
1

Dirty never on PG

No READ UNCOMMITTED effect — uncommitted invisible always.

2

Non-repeatable vs phantom

Re-read changed row vs range recount gains rows — different anomalies.

3

Levels ladder up

COMMITTED → REPEATABLE (snapshot) → SERIALIZABLE (serial + retry).

4

Lowest that forbids

Default COMMITTED; snapshot reports; serializable money with retries.

--visualization
REPEATABLE READSnapshot-stable recounts (no phantoms in snapshot)
Stage 1 / 1
Lowest level that forbids your anomaly. Long snapshots stall VACUUM.
--example
query.sql
1-- PostgreSQL:
2BEGIN ISOLATION LEVEL REPEATABLE READ;
Result
levelguarantee
READ COMMITTEDno dirty; default
REPEATABLE READsnapshot-stable
SERIALIZABLEserial + retry

Report re-reads the same range twice — REPEATABLE freezes the view; READ COMMITTED could show new rows (phantom). SERIALIZABLE would additionally abort conflicting writers.

--variants

Phantom in range recount

Second count sees new commits below serializable.

query.sql
1-- txn A counts twice; txn B inserts + commits between
2-- READ COMMITTED: 41 then 42 (phantom)
Result
readrows
first count41
second count42
1Statement snapshots refresh per statement.
2New committer visible to second count.
3REPEATABLE would freeze at 41.

Serializable + retry loop

Abort is expected, not exceptional.

query.sql
1-- on 40001 / deadlock: ROLLBACK whole txn, refetch, retry with backoff
Result
signalaction
abort rateretry with backoff
1Conflicts abort instead of allowing skew.
2Idempotent txn bodies retry safely.
3Higher isolation = more aborts, fewer anomalies.
--key facts
Anomaly griddirty/non-repeatable/phantom per level
Retry contractserializable/deadlock aborts retried whole-txn
DefaultREAD COMMITTED unless proven otherwise
--use cases
Report snapshots vs live counters
Money movements with serial guarantees
--under the hood
PG REPEATABLE is snapshot isolation — true serial needs SERIALIZABLE (SSI predicate tracking).
Long snapshots stall VACUUM — keep snapshot txns short.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.