PostgreSQL isolation levels and SSI
All PostgreSQL isolation levels run on top of MVCC snapshots, and the level determines when the snapshot is taken. Read Committed takes one per statement. Repeatable Read and Serializable take one once, at the first statement of the transaction. Serializable adds SSI (Serializable Snapshot Isolation), which looks for combinations that have no serial execution order and fails one side.
Key specs
| Isolation level | Snapshot | Dirty Read | Non-repeatable Read | Phantom | On write conflict |
|---|---|---|---|---|---|
| Read Uncommitted | Behaves like Read Committed | Prevented | Occurs | Occurs | Waits, then re-evaluates against the latest row |
| Read Committed (default) | Per statement | Prevented | Occurs | Occurs | Waits, then re-evaluates against the latest row |
| Repeatable Read | Once, at transaction start | Prevented | Prevented | Prevented | Error (40001) if the other transaction commits |
| Serializable | Once at start + SSI | Prevented | Prevented | Prevented | Error (40001) on a serialization violation |
| Item | Value |
|---|---|
| Default setting | default_transaction_isolation |
| Serialization failure error | SQLSTATE 40001, could not serialize access ... |
| SSI memory | max_pred_locks_per_transaction and others (number of predicate locks) |
| Read-only optimization | SERIALIZABLE READ ONLY DEFERRABLE: waits for a safe snapshot and then runs without failing |
How it works
Updates in Read Committed. If another transaction is already modifying the row you want to modify, you wait. When the other transaction commits, PostgreSQL re-checks the WHERE condition against that latest version and modifies the row if it still matches. So a single statement such as UPDATE account SET balance = balance - 10 WHERE id = 1 does not produce a lost update, but two statements that read, calculate, and then write can.
Repeatable Read. The standard allows phantoms at this level, but in PostgreSQL the whole transaction sees one snapshot, so newly inserted rows are not visible either. In practice this is snapshot isolation. If you try to modify a row that another transaction modified and committed after you read it, you do not wait. The transaction ends with a serialization failure.
Serializable (SSI). Snapshot isolation alone cannot prevent write skew. This is the case where two transactions modify different rows but each decides based on the row the other modified. SSI records the ranges that were read as predicate locks (SIReadLock), and when read-write dependencies form a cycle, it ends one transaction with 40001. It does not make transactions wait the way locks do, so when an application receives this error, it must re-run the entire transaction.
Settings and checks
SHOW transaction_isolation;
SHOW default_transaction_isolation;
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT sum(balance) FROM account; -- uses the snapshot from this point to the end
COMMIT;
BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; -- long report query
-- Predicate locks taken by SSI
SELECT locktype, relation::regclass, page, tuple FROM pg_locks WHERE mode = 'SIReadLock';
Comparison with other DBMSs
| PostgreSQL | InnoDB | Oracle | SQL Server | |
|---|---|---|---|---|
| Default | Read Committed | Repeatable Read | Read Committed | Read Committed (locking), RCSI on Azure SQL |
| Phantom prevention | Snapshot (RR and above) | Gap locks (locking reads), snapshot (plain reads) | Serializable (snapshot) | Serializable (key-range locks), Snapshot |
| Serializable | SSI (handled as failure) | Every read takes a shared lock | Snapshot isolation | Key-range locks |
In the articles
- 7.1 Problems when reading and writing concurrently
- 7.8 Isolation levels: when the snapshot is taken
- 7.10 Differences between DBMSs and what you see in practice
NOTE Behavior can vary by version.