Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How does Serializable Snapshot Isolation (SSI) in PostgreSQL detect anomalies? Explain SIRead locks, rw-antidependencies and why some transactions abort even though no real anomaly happened.

Try it yourself

Isolation levels: what a second read sees

Step through two transactions and see exactly when read committed and repeatable read give different answers.

Read this example as text

T1 at read committed

  1. T1 begins. Transaction T1 starts with isolation level READ COMMITTED. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under read committed each statement looks afresh at what is committed when the statement starts.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 reads 150. The same query, inside one transaction, returned 100 and then 150. This is a non-repeatable read, and read committed allows it.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100, 100, then 150: a non-repeatable read.

T1 at repeatable read

  1. T1 begins. Transaction T1 starts with isolation level REPEATABLE READ. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under repeatable read this first read fixes T1's snapshot: T1 will keep seeing the data as it was committed at this moment.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 still reads 100. Its snapshot was fixed at its first read, so T2's commit is invisible to it. The read is repeatable.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100 three times: its reads were repeatable.

What this example leaves out
  • Modelled on PostgreSQL and MySQL InnoDB, where a plain SELECT reads a snapshot and never waits for a writer.
  • SQL Server's default read committed uses locks instead: there, T1's second read would wait for T2 to finish.
  • Only reads are compared. What happens when T1 also writes the row (a lost update, or an error in PostgreSQL) is not modelled.

PostgreSQL's SERIALIZABLE runs each transaction on a snapshot, like REPEATABLE READ, and also records what each transaction read with SIRead locks, which never block anyone. When a transaction writes data that a concurrent transaction read, PostgreSQL records a rw-antidependency, and when it sees two such edges in a row through one pivot transaction, a pattern that every serialization anomaly must contain, it aborts one transaction with SQLSTATE 40001. Because it reacts to the pattern rather than proving a full cycle, and because its read tracking can become coarse, some aborts happen even when no anomaly would have occurred.

Why this question is asked

SERIALIZABLE in PostgreSQL is often treated as a black box that sometimes throws errors. Knowing how it works tells us why those errors happen, how to make them rarer, and when they are false alarms. Interviewers ask about SIRead locks and rw-antidependencies at this level to see whether we can tune a serializable workload rather than just switch it off. In this answer, we will recap what serializable means and why snapshots are not enough, follow how SSI records reads and dependencies, see the dangerous structure it looks for, and then cover false positives and how read-only transactions avoid them.

1A Quick Recap: Serializable and the Gap in Snapshots

A schedule of concurrent transactions is serializable if its result equals running them one at a time in some order. Snapshot isolation, which PostgreSQL uses for REPEATABLE READ, gets close: it aborts two transactions that write the same row. But it ignores reads, so it allows write skew, where two transactions read the same rows and write different ones. Serializable Snapshot Isolation, SSI, added in PostgreSQL 9.1, keeps the snapshots and adds tracking of reads to close that gap, without making readers wait.

2Recording Reads With SIRead Locks

Every read a SERIALIZABLE transaction does leaves a marker called an SIRead lock, a predicate lock on what was read. An index scan locks the index pages or tuples it looked at; a sequential scan locks the whole table, since it read everything. The word lock is misleading: SIRead locks never block anyone. They are only notes saying this transaction read this data. They stay after the reader commits, as long as concurrent transactions are still running, because a later write may still need to be checked against them.

SQL
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT * FROM on_call WHERE shift_id = 7; -- uses index on shift_id
SELECT locktype, relation::regclass, page, tuple, mode
FROM pg_locks WHERE mode = 'SIReadLock';
-- page | on_call_shift_idx | 1 | | SIReadLock
-- tuple | on_call | 0 | 3 | SIReadLock
-- tuple | on_call | 0 | 4 | SIReadLock

3From Reads and Writes to rw-Antidependencies

When transaction T2 writes a row that a concurrent transaction T1 has an SIRead lock on, PostgreSQL records an edge T1 -rw-> T2, a read-write antidependency. It means T1 read a version of the data that T2 then changed, so in any equivalent serial order T1 must come before T2. It also works the other way round: if T1 reads data that T2 already changed but T1's snapshot cannot see, the same edge is recorded.

One such edge is harmless; it just fixes an order. A problem needs a cycle, where the order requirements contradict each other. The theory behind SSI shows that every anomaly under snapshot isolation has a dangerous structure: two consecutive rw-edges, T1 -rw-> T2 -rw-> T3, where T2 is the pivot with an edge in and an edge out (T1 and T3 can be the same transaction). PostgreSQL looks for this structure instead of searching for full cycles, which is much cheaper.

Step throughHow SSI decides to abort
  1. Transactions readeach read leaves an SIRead lock
  2. A concurrent transaction writes what was readrecord an rw-edge reader -> writer
  3. Does some transaction now have an rw-edge in and out?
    YesDangerous structure foundNoKeep running
  4. Abort one transactionSQLSTATE 40001, at a statement or at COMMIT

4 steps in order. Play them, or step through them one at a time.

4The Abort and the Doctors Example

In the on-call case, A reads shift 7 and B reads shift 7, then A updates doctor A's row and B updates doctor B's row. B's write touches data A read, giving A -rw-> B; A's write touches data B read, giving B -rw-> A. Each transaction is now a pivot, so PostgreSQL aborts one, usually when it tries to commit or at its next statement, with ERROR: could not serialize access due to read/write dependencies among transactions, SQLSTATE 40001. The application retries it, and on the retry the transaction sees one doctor on call and refuses.

PostgreSQL also avoids aborting where it can prove safety, for example when the transaction that would close the cycle committed after the others in a way that cannot form one, and it prefers to abort a transaction that has not committed yet so the retry has a chance to succeed.

5Why Some Aborts Are False Positives

SSI is deliberately conservative. A dangerous structure is necessary for an anomaly, but not sufficient, so some aborted transactions would in fact have produced a perfectly serializable result. That is the price of not searching for full cycles.

Coarse locks make it worse. SIRead locks take memory, so when a transaction holds too many on one page, PostgreSQL replaces them with one page lock, and with too many pages in a relation, with one relation lock. The limits are max_pred_locks_per_transaction, max_pred_locks_per_relation and max_pred_locks_per_page. Once a whole table is locked, any write to the table looks like a conflict with that reader, even for rows it never looked at. Sequential scans lock the whole table from the start. So the practical tuning is to give hot queries good indexes so they read and lock only a few tuples, keep transactions short, and raise the predicate lock limits if pg_locks shows many page or relation SIRead locks.

6Read-Only and Deferrable Transactions

Declaring a transaction READ ONLY helps PostgreSQL. A read-only transaction never writes, so it can never be the pivot; it can only be the first transaction of a dangerous structure, and only when the third transaction committed before the read-only one took its snapshot. Using that rule, PostgreSQL can often detect early that its snapshot is safe, meaning no anomaly can involve it, and then stop tracking its reads. SERIALIZABLE READ ONLY DEFERRABLE goes further: the transaction waits at its start until it can take a snapshot that is guaranteed safe, and then runs with no SIRead tracking and can never be aborted with a serialization failure.

SQL
BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; -- may wait briefly for a safe snapshot
SELECT region, sum(amount) FROM ledger GROUP BY region; -- no SIRead locks, cannot fail with 40001
COMMIT;

7Follow-up questions

When would you use SERIALIZABLE READ ONLY DEFERRABLE?

For long reports and exports that read a lot of data in a serializable system. Without DEFERRABLE, such a transaction holds SIRead locks on large parts of the database for a long time, which makes writers' transactions look dangerous and raises everyone's abort rate, and the report itself might be aborted after running for minutes. With DEFERRABLE it waits, usually a short time, for a safe snapshot, then reads with no tracking and no risk of a serialization failure. It is not worth it for short interactive reads, where the initial wait would hurt latency.

Do SIRead locks ever block a writer?

No. They are only used to detect dependencies. The only blocking in SSI is the ordinary row-level waiting between two writers of the same row.

Does mixing isolation levels affect SSI?

Yes. Only SERIALIZABLE transactions take SIRead locks and are checked, so a READ COMMITTED transaction writing the same data can still create an anomaly. All transactions on the data must run at SERIALIZABLE for the guarantee to hold.

8Conclusion

In this answer, we saw that PostgreSQL SSI runs on snapshots and records every read as a non-blocking SIRead lock, visible in pg_locks as SIReadLock. A write to data a concurrent transaction read creates a rw-antidependency, and two consecutive edges through a pivot form the dangerous structure that triggers an abort with SQLSTATE 40001. Because it reacts to that structure and because predicate locks can be coarsened to pages or relations, false positives happen; good indexes, short transactions, higher lock limits and READ ONLY or DEFERRABLE transactions reduce them.

What to say in the interviewAbout 80 seconds aloud

Open with

SSI starts from snapshot isolation, so every transaction reads from a snapshot, and it adds tracking of what each transaction read. Each read takes an SIRead lock, a predicate lock you can see in pg_locks with mode SIReadLock, but it never blocks anyone; it's just a record.

Then, if they want more

When a transaction writes something a concurrent transaction read, PostgreSQL records a read-write antidependency from the reader to the writer, which means the reader must come first in any serial order. A single edge is fine. The theory says every anomaly contains a dangerous structure: two consecutive rw-edges with a pivot transaction that has one edge in and one out.

When PostgreSQL sees that, it aborts one transaction with SQLSTATE 40001, and the app retries. It's conservative, because the structure is necessary but not sufficient for a real cycle, so some aborts are false positives.

They get more common when locks are coarsened from tuples to pages to the whole relation, controlled by settings like max_pred_locks_per_transaction, and when sequential scans lock the whole table, so good indexes and short transactions reduce them.

Declaring transactions READ ONLY helps PostgreSQL drop tracking, and READ ONLY DEFERRABLE waits for a safe snapshot so long reports can't be aborted at all.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does Serializable Snapshot Isolation (SSI) in PostgreSQL detect anomalies? Explain...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/32b0502919/