Sub topic 16
Serializable Snapshot Isolation
This chapter assumes basic SQL, transactions, BEGIN, COMMIT, and ROLLBACK. You should also understand that PostgreSQL's MVCC lets each statement read a snapshot while other transactions continue writing.
Serializable isolation is PostgreSQL's strongest transaction isolation level. It makes committed transactions behave as if they had run one at a time, even though PostgreSQL may run them concurrently. PostgreSQL achieves this with Serializable Snapshot Isolation (SSI). SSI keeps snapshot-based reads, then detects dangerous read/write dependencies between transactions.
Why Repeatable Read is not enough
Repeatable Read gives a transaction one stable snapshot. It prevents many anomalies, but concurrent transactions can still produce a result that could not happen in any serial order.
For example, two doctors might each check that another doctor is on call, then both mark themselves off call. Each transaction read a valid snapshot, but the final state has no doctor on call.
Serializable isolation detects this kind of dependency and aborts one transaction instead of allowing the unsafe result.
How SSI detects conflicts
A read does not normally block a concurrent write. Under Serializable isolation, PostgreSQL records the read using a predicate lock, also called an SIREAD lock. This lock represents data that was read, including a range where no row currently exists.
An rw-dependency occurs when one transaction reads data and another concurrent transaction changes that data or a matching range. A single dependency is usually safe. SSI aborts a transaction when dependencies form a dangerous structure that could violate serial execution.
Predicate locks are for conflict detection. They do not prevent another transaction from changing the data, and they are not the same as ordinary row locks.
Using Serializable transactions
Set the isolation level when starting the transaction:
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*)
FROM appointments
WHERE doctor_id = 42
AND starts_at < timestamp '2026-12-01 00:00:00';
INSERT INTO appointments (doctor_id, starts_at)
VALUES (42, timestamp '2026-11-28 09:00:00');
COMMIT;If PostgreSQL detects an unsafe dependency, COMMIT or a statement can fail with a serialization error:
ERROR: could not serialize access due to read/write dependencies among transactions
SQLSTATE: 40001The application must roll back and retry the entire transaction. Retrying only the failed statement is not sufficient because the transaction's snapshot and earlier decisions are part of the conflict.
Choosing Serializable
Use Serializable when correctness depends on multi-row or predicate-based decisions and preventing anomalies is more important than avoiding retries. Use Repeatable Read when a stable snapshot is enough and the application can accept its weaker guarantees.
Keep transactions short. Long transactions hold snapshots and conflict information longer, increasing memory use, delaying cleanup, and making serialization failures more likely.
Read-only transactions can sometimes use SERIALIZABLE, READ ONLY, DEFERRABLE. PostgreSQL may wait for a safe snapshot, reducing the chance that the read-only transaction must be retried.
Exercise
A serializable transaction receives SQLSTATE 40001 during COMMIT. What should the application do?
Show answer
Roll back the whole transaction, wait according to its retry policy, and run the complete transaction again from the beginning.