Video in progress
Which isolation levels does PostgreSQL have, and what does the default READ COMMITTED actually guarantee?
Dirty read / Non-repeatable read / Phantom / Serialization anomaly — Read Committed (default): no / possible / possible / possible; Repeatable Read: no / no / not in PG / possible; Serializable: no / no / no / no
📖 Answer
| Level | Dirty read | Non-repeatable read | Phantom | Serialization anomaly |
|---|---|---|---|---|
| Read Committed (default) | no | possible | possible | possible |
| Repeatable Read | no | no | not in PG | possible |
| Serializable | no | no | no | no |
Under READ COMMITTED the snapshot is taken at the start of each statement. So:
BEGIN;
SELECT balance FROM account WHERE id = 1; -- 100
-- meanwhile another transaction sets it to 50 and commits
SELECT balance FROM account WHERE id = 1; -- 50
COMMIT;
An important subtlety is how UPDATE behaves. If another transaction is modifying the row you want to update, you wait for its commit, then take the new version of the row and re-check the WHERE condition. That's why UPDATE account SET balance = balance - 10 works correctly even under READ COMMITTED.
The problem shows up in the "read, compute in Java, then write" pattern: the lost update. For that you need SELECT ... FOR UPDATE, a version column, or a higher level.
In Spring you set the level with @Transactional(isolation = Isolation.REPEATABLE_READ). But a higher level isn't free: serialization errors appear, and the application itself has to retry the transaction.
Sign up to mark studied questions and save your progress.