Yousef AldabbasSystems & backend engineering
● ENGINEERING NOTESAMMAN, JORDAN
← back to posts

Write Skew and SSI in PostgreSQL

2026-11-21 · databases

Two database transactions can each make a sensible decision and still break a rule when their changes happen together. This is called write skew.

PostgreSQL’s SERIALIZABLE isolation level is designed to catch this kind of problem. It uses Serializable Snapshot Isolation (SSI) to detect when concurrent transactions cannot be explained as if they ran one at a time.

The example: at least one doctor must stay on call

Alice and Bob are both on call. The rule says a doctor can leave only if at least one other doctor remains on call.

CREATE TABLE on_call (
  name text PRIMARY KEY,
  active boolean NOT NULL
);

INSERT INTO on_call VALUES
  ('Alice', true),
  ('Bob', true);

Now two transactions run at the same time. One wants to take Alice off call; the other wants to take Bob off call. Each checks how many doctors are active:

SELECT count(*) FROM on_call WHERE active;

Both transactions see 2. They then update different rows:

-- Transaction A
UPDATE on_call
SET active = false
WHERE name = 'Alice'
  AND (SELECT count(*) FROM on_call WHERE active) > 1;

-- Transaction B
UPDATE on_call
SET active = false
WHERE name = 'Bob'
  AND (SELECT count(*) FROM on_call WHERE active) > 1;

Under snapshot isolation (REPEATABLE READ in PostgreSQL), both updates can succeed. Each transaction saw two active doctors, and they changed different rows, so neither update directly conflicts with the other. After both commit, nobody is on call.

The check was correct for each transaction’s view. The problem is that both views were old by the time the decisions were combined.

The arrows show the problem

Transaction A counted Bob as active, but B turned Bob off. So A’s read depends on a version that B replaced. We draw that as A →rw B (“A read, B wrote”).

At the same time, B counted Alice as active, but A turned Alice off. That gives us B →rw A.

A ──rw──> B
A <──rw── B

Each arrow means: the reader must come before the writer in any equivalent one-at-a-time order.

Here that would mean A before B and B before A. Both cannot be true. There is no serial order that explains both transactions committing, so their combined result is unsafe.

What SSI does

One read-write arrow alone is fine. The warning sign is a pair of arrows through a transaction:

T1 ──rw──> T2 ──rw──> T3

This is called a dangerous structure. It does not always mean there is an actual problem, but it is the pattern PostgreSQL looks for because a snapshot-isolation anomaly must contain one.

When PostgreSQL sees a dangerous pattern that could make the result non-serializable, it cancels one transaction. That transaction gets SQLSTATE 40001, a serialization failure. Which transaction gets canceled can depend on timing, so your application should not rely on a specific one losing.

Retry the whole transaction

A failed transaction’s earlier reads are no longer a safe basis for making the decision. Start a new transaction, repeat the reads and checks, then decide what to write.

First attempt:  see 2 doctors → try to take Bob off call → 40001
Retry:         see 1 doctor  → keep Bob on call → commit

Retry the complete transaction, not just the failed statement. Use a bounded retry policy, because repeated attempts can also fail under heavy contention. Keep external side effects, such as sending an email, outside the retryable transaction body or make them safe to repeat.

One thing about predicate locks

SSI also needs to remember what a query looked for, including conditions such as WHERE active. PostgreSQL tracks these reads with predicate locks, shown as SIReadLock. Despite the name, they do not block writers; they help PostgreSQL detect conflicts.

In short

To try the example against PostgreSQL, download the reproduction script. It uses a temporary local database cluster.

References