isolation levels: the standard vs postgres
READ UNCOMMITTED is READ COMMITTED, REPEATABLE READ is snapshot isolation. the table the standard doesn't show you.
- address
- everything.transactions.isolation_levels
- part
- part 11, transactions and isolation
- chapter
- chapter 4 of 11
- reading time
- 8 min
the SQL standard defines four isolation levels by which anomalies each one must rule out. postgres accepts all four names but runs three: READ UNCOMMITTED is READ COMMITTED, REPEATABLE READ is snapshot isolation (stronger than the standard asks: no phantoms), and SERIALIZABLE really is serializable, by aborting a transaction when it has to.
the picture
you know the table every database course shows, four levels down the side and three anomalies across the top? that's the standard's table. it says what each level is allowed to let through, which is a floor, not a description. a database may do better, and postgres does, in two places.
think of building codes (the standard) and an actual house (postgres). the code says how strong a wall must be at least. it doesn't tell you how strong the wall is.
| level | dirty read | non-repeatable read | phantom | serialization anomaly |
|---|---|---|---|---|
| read uncommitted | possible | possible | possible | possible |
| read committed | no | possible | possible | possible |
| repeatable read | no | no | possible | possible |
| serializable | no | no | no | no |
| level | dirty read | non-repeatable read | phantom | lost update | read skew | write skew |
|---|---|---|---|---|---|---|
| read uncommitted | no | happens | happens | happens | happens | happens |
| read committed | no | happens | happens | happens | happens | happens |
| repeatable read | no | no | no | 40001 | no | happens |
| serializable | no | no | no | 40001 | no | 40001 |
the standard doesn't mention lost updates, read skew or write skew at all. it defines the three lower levels by three phenomena, and SERIALIZABLE as "the same result as some one-at-a-time order". everything else is a serialization anomaly, which the standard only rules out at the top. so the postgres table has six columns where the standard's has four.
picking a level
per transaction, at BEGIN (or with SET TRANSACTION before the first query), or as the default for a session, a role or the whole server:
SHOW default_transaction_isolation;
BEGIN ISOLATION LEVEL REPEATABLE READ;
SHOW transaction_isolation;
COMMIT;
SET default_transaction_isolation = 'serializable';
BEGIN;
SHOW transaction_isolation;
COMMIT;| default_transaction_isolationtext |
|---|
| read committed |
| transaction_isolationtext |
|---|
| repeatable read |
| transaction_isolationtext |
|---|
| serializable |
the level is fixed once the transaction has run a query. changing it after that is an error:
BEGIN;
SELECT 1;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;| ?column?int4 |
|---|
| 1 |
transaction aborted: ROLLBACK to continue
READ UNCOMMITTED is READ COMMITTED
postgres takes the name, reports it back, and runs the transaction exactly like READ COMMITTED. it can't do anything else: a dirty read would mean showing a row version written by a transaction that's still running, and no snapshot ever includes one. here T2 asks for it while T1 has an uncommitted withdrawal:
read uncommitted. T1 opens a transaction.
REPEATABLE READ is snapshot isolation
one snapshot for the whole transaction, taken at its first statement (not at BEGIN), and every read comes from it. that gives you more than the standard's REPEATABLE READ: rows you've read can't change under you, and rows can't appear either, because a new row committed after your snapshot is simply not in it.
read committed. T2 opens a transaction.
/* T2 */ BEGIN ISOLATION LEVEL REPEATABLE READ;
/* T1 */ UPDATE accounts SET balance = 150.00 WHERE id = 1; -- commits
/* T2 */ SELECT balance FROM accounts WHERE id = 1; -- ?T2 begins a repeatable read transaction. no snapshot yet: BEGIN doesn't take one.
snapshot isolation has two consequences you have to plan for. a write that would overwrite a row changed after your snapshot fails with 40001 instead of losing an update, so every writer at this level needs a retry loop. and it checks only for write-write conflicts on the same row, so write skew, two transactions writing different rows based on what the other one changed, gets through.
SERIALIZABLE is SERIALIZABLE
snapshot isolation plus a record of what every transaction read. when the reads and writes of overlapping transactions form a pattern that no one-at-a-time order could produce, postgres fails one of them with 40001, sometimes at COMMIT. the guarantee is the standard's: if each transaction is correct alone, the mix is correct. the price is the retries, and a little bookkeeping. serializable snapshot isolation is how it's built.
so which one?
| level | each statement sees | stops | you must handle |
|---|---|---|---|
| READ COMMITTED (default) | everything committed before the statement began | dirty reads | lost updates, read and write skew, in your own code |
| REPEATABLE READ | everything committed before the transaction's first statement | all but write skew | 40001 on write conflicts |
| SERIALIZABLE | the same snapshot, plus read tracking | everything | 40001, more often, possibly at COMMIT |
short transactions that touch a few known rows: READ COMMITTED, with the arithmetic in the UPDATE. reports and exports that read a lot: REPEATABLE READ (one consistent moment, never fails a read-only transaction). anything that checks a rule across rows before writing: SERIALIZABLE everywhere, with a retry loop. and if you move between databases, check what the names mean there: mysql's InnoDB, for one, defaults to REPEATABLE READ, reads plain SELECTs from a snapshot but takes locks on index ranges for writes and locking reads, so the same name behaves differently (its manual).
keylines
- the standard says what each level must prevent. a database may prevent more.
- postgres: READ UNCOMMITTED = READ COMMITTED. dirty reads never happen.
- REPEATABLE READ is snapshot isolation: one snapshot from the first statement, no phantoms, no protection from write skew.
- REPEATABLE READ and SERIALIZABLE both answer conflicts with 40001. plan the retry.
- pick by what your transaction decides: few known rows, READ COMMITTED; rules across rows, SERIALIZABLE.