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 levelSnapshotDirty ReadNon-repeatable ReadPhantomOn write conflict
Read UncommittedBehaves like Read CommittedPreventedOccursOccursWaits, then re-evaluates against the latest row
Read Committed (default)Per statementPreventedOccursOccursWaits, then re-evaluates against the latest row
Repeatable ReadOnce, at transaction startPreventedPreventedPreventedError (40001) if the other transaction commits
SerializableOnce at start + SSIPreventedPreventedPreventedError (40001) on a serialization violation
ItemValue
Default settingdefault_transaction_isolation
Serialization failure errorSQLSTATE 40001, could not serialize access ...
SSI memorymax_pred_locks_per_transaction and others (number of predicate locks)
Read-only optimizationSERIALIZABLE 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

PostgreSQLInnoDBOracleSQL Server
DefaultRead CommittedRepeatable ReadRead CommittedRead Committed (locking), RCSI on Azure SQL
Phantom preventionSnapshot (RR and above)Gap locks (locking reads), snapshot (plain reads)Serializable (snapshot)Serializable (key-range locks), Snapshot
SerializableSSI (handled as failure)Every read takes a shared lockSnapshot isolationKey-range locks

In the articles

NOTE Behavior can vary by version.

Related pages