Every SQL database lets you choose how isolated concurrent transactions are from each other. The four standard levels trade safety for concurrency. This cheat sheet lists what each level prevents, what it still allows, how PostgreSQL and MySQL actually behave, and when to use which.
The anomalies in one line each
- Dirty read: you see another transaction's uncommitted change, which may be rolled back.
- Non-repeatable read: you read the same row twice in one transaction and get different values.
- Phantom read: you run the same filtered query twice and new rows appear or disappear.
- Lost update: two transactions read the same value and write back, and one write erases the other.
- Write skew: two transactions check the same rule, each changes a different row, and together they break the rule.
The four levels
Read Uncommitted. The weakest level in the standard. Dirty reads are allowed. In practice it is rarely useful, and PostgreSQL does not even implement it: asking for it gives you Read Committed.
Read Committed. You only ever see committed data, but each statement can see a newer committed state than the previous one. The default in PostgreSQL, SQL Server and Oracle. Good enough for many simple request handlers if you write updates carefully.
Repeatable Read. Rows you have read do not change under you for the whole transaction. The default in MySQL InnoDB. In PostgreSQL it is snapshot isolation: the whole transaction sees one snapshot, phantoms are prevented too, and a concurrent update to a row you also update causes a serialization error instead of a lost update.
Serializable. The result is equivalent to running the transactions one at a time. Every anomaly above is prevented. The price is more blocking (lock-based engines) or more aborted transactions that must be retried (PostgreSQL's Serializable Snapshot Isolation).
What the standard requires
| Level | Dirty read | Non-repeatable read | Phantom read |
|---|---|---|---|
| Read Uncommitted | allowed | allowed | allowed |
| Read Committed | prevented | allowed | allowed |
| Repeatable Read | prevented | prevented | allowed |
| Serializable | prevented | prevented | prevented |
The standard sets a minimum. Real databases often prevent more.
What you actually get (PostgreSQL and MySQL InnoDB)
| Level | Lost update | Write skew | Notes |
|---|---|---|---|
| Read Committed | possible | possible | new snapshot per statement |
| Repeatable Read (PostgreSQL) | prevented, by error | possible | one snapshot per transaction |
| Repeatable Read (InnoDB) | possible with plain reads | possible | locking reads see latest data |
| Serializable | prevented | prevented | retries or more locking |
At InnoDB's Repeatable Read, a plain SELECT reads the snapshot, but UPDATE and SELECT ... FOR UPDATE read the latest committed row and take locks, including gap locks against phantoms. That mix surprises people: a read-then-write in application code can still lose an update unless the read is a locking read.
Setting the level
-- PostgreSQL: one transaction
BEGIN ISOLATION LEVEL SERIALIZABLE;
...
COMMIT;
-- MySQL: the next transaction in this session
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
START TRANSACTION;
...
COMMIT;
Most frameworks and ORMs keep the database default unless you configure the level on the connection or the transaction.
Tools that work at any level
You do not always need a stricter level:
-
Atomic updates:
UPDATE stock SET qty = qty - 1 WHERE id = 7 AND qty > 0cannot lose an update. -
Row locks:
SELECT ... FOR UPDATEbefore a read-modify-write. -
Constraints: a
UNIQUEindex or aCHECKconstraint stops invalid states at any level. -
Optimistic locking: a
versioncolumn checked in theUPDATE'sWHERE.
When to pick which
- Read Uncommitted: practically never. If you want approximate numbers fast, use a replica or a summary table instead.
-
Read Committed: the default for most web request handlers. Combine it with atomic updates,
FOR UPDATEor constraints for the few places where races matter. - Repeatable Read: reports, exports and multi-query reads that must see one consistent picture. In PostgreSQL, also a cheap way to turn lost updates into retryable errors.
- Serializable: complex check-then-write rules (bookings, limits, on-call schedules) where reasoning about every race by hand is risky. Always pair it with a retry loop, and keep those transactions short.
Originally published at abdeldjalilchougui.biz.
Top comments (0)