1.Dirty Reads, Non-Repeatable Reads, Phantom Reads and Serialization Anomalies
A dirty read sees another transaction's uncommitted change. A non-repeatable read sees the same row return different values on a second SELECT within one transaction, because another transaction committed a change to it in between. A phantom read sees a repeated query return a different set of rows (not just different values), because another transaction inserted or deleted matching rows in between. A serialization anomaly is the most subtle: the combined result of several committed, concurrent transactions could not have happened under ANY serial (one-at-a-time) ordering of them.
Key features
- Dirty read: seeing another transaction's uncommitted change — PostgreSQL never allows this, at any isolation level
- Non-repeatable read: the same row read twice in one transaction returns different values, because another transaction committed a change to it in between
- Phantom read: the same query re-run in one transaction returns a different SET of rows, because another transaction inserted/deleted matching rows in between
- Serialization anomaly: the combined outcome of concurrent, individually-fine transactions is inconsistent with every possible serial ordering of them
- PostgreSQL never permits dirty reads at any level; Repeatable Read additionally prevents non-repeatable reads AND phantom reads (stronger than the SQL standard requires); only Serializable prevents serialization anomalies too
The four phenomena
- Transaction A → accounts table: SELECT balance → 1000
- Transaction B → accounts table: UPDATE balance = 900; COMMIT
- Transaction A → accounts table: SELECT balance → 900 (different!)
Command
-- Read Committed (default): permits non-repeatable and phantom reads
BEGIN;
SELECT balance FROM accounts WHERE id = 1; -- 1000
-- (another transaction commits a change here)
SELECT balance FROM accounts WHERE id = 1; -- may now read 900
COMMIT;Dirty read (uncommitted data — never happens in PostgreSQL), non-repeatable read (a row's values change between two reads), phantom read (the matching row SET changes between two reads), serialization anomaly (the combined outcome is impossible under any serial ordering). Repeatable Read prevents the first three; only Serializable prevents all four.