Isolation Levels & Concurrency Anomalies
Concurrency anomalies
| Anomaly | What happens |
|---|---|
| Dirty read | reading another transaction's uncommitted changes |
| Non-repeatable read | reading the same row twice in a transaction gives different values (someone committed in between) |
| Phantom read | re-running a query returns new rows that another transaction inserted |
| Lost update | two transactions read the same value and both write; one update silently overwrites the other |
| Write skew | two transactions each check a rule, then both write, together breaking the rule (e.g. both doctors go off call) |
Isolation levels in PostgreSQL
| Level | Prevents | Notes |
|---|---|---|
| Read Uncommitted | — | behaves as Read Committed in PostgreSQL (no dirty reads ever) |
| Read Committed (default) | dirty reads | each statement sees data committed before it started |
| Repeatable Read | + non-repeatable & phantom reads | the whole transaction sees one snapshot; conflicting updates fail with a serialization error |
| Serializable | + write skew (all anomalies) | as if transactions ran one at a time; may abort transactions you must retry |
Set it per transaction:
BEGIN ISOLATION LEVEL REPEATABLE READ; or SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
The lost update problem (very common!)
-- app reads balance = 1000, computes 1000 - 300 in code, writes 700
-- meanwhile another request also read 1000 and writes 1000 - 500 = 500
-- final 500: the 300 withdrawal is lost!
Fixes
- Atomic update in SQL:
SET balance = balance - 300✅ (simplest) - Lock the row first:
SELECT … FOR UPDATE(next lesson) - Optimistic locking with a version column (lesson 12.5)
- Repeatable Read / Serializable + retry on error
Choosing
- Read Committed + atomic updates / row locks covers most apps
- Serializable for complex invariants (bookings, inventory rules) — and always implement retries for error code
40001
The code example shows two sessions as comments; you can reproduce them with two SQL editor tabs.
Example
-- Simulating two sessions. Open two SQL tabs (A and B) to try it live.
-- ❌ LOST UPDATE when the app computes the new value itself:
-- A: BEGIN; SELECT balance FROM accounts WHERE id = 1; -- 5000
-- B: BEGIN; SELECT balance FROM accounts WHERE id = 1; -- 5000
-- A: UPDATE accounts SET balance = 4700 WHERE id = 1; COMMIT; -- spent 300
-- B: UPDATE accounts SET balance = 4500 WHERE id = 1; COMMIT; -- spent 500
-- Final: 4500 → the 300 expense vanished!
-- ✅ Atomic update: the database reads and writes in one step
UPDATE accounts SET balance = balance - 300 WHERE id = 1;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
SELECT balance FROM accounts WHERE id = 1; -- 4200.00 ✅
-- Read Committed: each statement sees the latest committed data
BEGIN; -- default level
SHOW transaction_isolation; -- read committed
SELECT sum(amount) FROM expenses WHERE user_id = 2;
-- (if session B committed a new expense now, the next SELECT would include it)
SELECT sum(amount) FROM expenses WHERE user_id = 2;
COMMIT;
-- Repeatable Read: one snapshot for the whole transaction
BEGIN ISOLATION LEVEL REPEATABLE READ;
SHOW transaction_isolation; -- repeatable read
SELECT count(*) FROM expenses; -- 31
-- (session B inserts + commits an expense here)
SELECT count(*) FROM expenses; -- still 31 inside this transaction
COMMIT;
-- Serializable: use for strict rules, and RETRY on error 40001
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) FROM bills WHERE user_id = 1 AND NOT paid;
INSERT INTO bills (user_id, title, amount, due_date) VALUES (1, 'Gym', 2000, '2026-12-05');
COMMIT;
-- If a concurrent serializable transaction conflicts, one of them gets:
-- ERROR: could not serialize access due to read/write dependencies among transactions
-- SQLSTATE 40001 → your app should simply retry the whole transaction.