Home » Write Skew in PostgreSQL: Who Is Still on Call?

Write Skew in PostgreSQL: Who Is Still on Call?

Two people change separate on-call records, leaving the combined roster without an available person.

Alice and Ben are the only engineers on call. Each can leave if someone else stays. On PostgreSQL 18.6 we opened two database sessions, had both count the people on duty, and then let each change their own row. Under Repeatable Read, both commits succeeded and the roster ended empty. Under Serializable, Ben's commit returned SQLSTATE 40001. What his application did next determined whether the scheduling rule held.

Put the rule where both sessions must read it

The fixture starts with two rows in roster(person, on_call). Each session begins a transaction and runs the same query. If the result exceeds one, it sets its own on_call value to false. If only one person remains, it leaves the row alone. The rule applies to the whole roster, even though each update touches only one person.

SELECT count(*) FROM roster WHERE on_call;

We used two persistent connections and waited for each query result before allowing the next step. First we ran a sequential control. Alice finished her transaction before Ben began his. Then we reset the rows and repeated the test at each isolation level, holding both sessions after their reads until both had reported their counts. The downloadable transcript includes the exact statements, SQLSTATEs and final rows.

Run Alice and Ben's first counts First attempts Who remained?
Repeatable Read, sequential 2, then 1 Alice left. Ben kept his row. Both committed. Ben
Repeatable Read, overlapping 2 and 2 Both updated separate rows and committed. Nobody
Serializable, overlapping 2 and 2 Both updates succeeded initially. Alice committed. Ben's commit returned 40001. Ben

The control shows why the empty roster matters: either serial order must leave one person on call. With overlapping snapshots, each reader saw the other person's earlier assignment, and the writes targeted different rows. PostgreSQL's Serializable level tracks the dependencies that can make the resulting history inconsistent with a serial order. A different schedule may reject a different transaction or report the error earlier. PostgreSQL 18 transaction isolation.

A successful retry can decline the request

Ben's first update had appeared to succeed. His commit failed, so that whole attempt had no effect. Our driver started a fresh Serializable transaction and ran the count again. It got one. The application kept Ben on call and committed without issuing another update. The final query returned Ben. A useful application response at that point is “You are the last person on call.” Reporting that he left would give the operator the wrong roster.

If you replay only the earlier update, you reuse a decision based on a count of two. Put the read, the decision and the conditional write inside the retry boundary. Handle 40001 around the entire transaction, including commit. Apply a bounded retry policy for the service and handle exhaustion explicitly. Send the “left duty” notification only after a successful committed change, or record it transactionally for later delivery. PostgreSQL serialization failure handling.

Download the SQL, two-session driver and transcript (ZIP)

Before looking at the final roster in the transcript, cover the retry events and predict Ben's second count and whether an update should be issued. “One, skip the update” is the business result the test should assert alongside the commit status. The guide to conditional API updates addresses another concurrency question, detecting an outdated version of one resource. Checking Ben's row version alone would miss Alice's change.

This experiment uses two cooperative writers, one roster and a fixed interleaving. In a real system, include administrative and background write paths in the same invariant design. If the business rule can be expressed as a database constraint, evaluate that option as well. The recorded schedule establishes a correctness result for this fixture. It supplies no estimate of retry frequency under production load.

You may also like