Sub topic 11
Write Skew: The Anomaly Snapshots Allow
Prerequisites: you should know basic SELECT and UPDATE statements, transactions, and the difference between COMMIT and ROLLBACK. You should also understand that two database sessions can run at the same time.
What write skew means
Write skew occurs when two transactions read the same rule, update different rows, and both commit. Each transaction makes a locally valid change. Together, the changes break the rule.
This can happen under snapshot isolation. Snapshot isolation gives each transaction a consistent view of committed data. It usually detects conflicts when two transactions update the same row. It may not detect a conflict when they update different rows, even if their decisions depend on the same set of rows.
Consider an on-call rule:
- At least one doctor must remain on duty.
- Alice and Bob are both on duty.
- Each doctor may go off duty if another doctor is on duty.
The rule is true when both transactions begin. Each transaction sees the other doctor, so each decides it is safe to leave.
A write-skew schedule
The following schedule uses PostgreSQL-style syntax. Session A and Session B must run concurrently.
CREATE TABLE on_call (
doctor text PRIMARY KEY,
on_duty boolean NOT NULL
);
INSERT INTO on_call (doctor, on_duty)
VALUES ('alice', true), ('bob', true);
-- Session A
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT count(*) AS doctors_on_duty
FROM on_call
WHERE on_duty = true;
-- Returns 2
UPDATE on_call
SET on_duty = false
WHERE doctor = 'alice';
-- Session B, started before Session A commits
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT count(*) AS doctors_on_duty
FROM on_call
WHERE on_duty = true;
-- Also returns 2
UPDATE on_call
SET on_duty = false
WHERE doctor = 'bob';
-- Both sessions can commit because they changed different rows.
-- Session A:
COMMIT;
-- Session B:
COMMIT;
SELECT count(*) AS doctors_on_duty
FROM on_call
WHERE on_duty = true;
-- Returns 0The two transactions did not overwrite the same row. Therefore, ordinary row-level conflict detection did not stop either one. The problem was a conflict between decisions, not a direct conflict between writes.
Why SERIALIZABLE prevents it
Serializable isolation requires the result to be equivalent to running transactions one at a time. A database can enforce this with locks, dependency tracking, or both.
In PostgreSQL, retry the transaction at SERIALIZABLE isolation:
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) AS doctors_on_duty
FROM on_call
WHERE on_duty = true;
UPDATE on_call
SET on_duty = false
WHERE doctor = 'alice';
COMMIT;If another transaction performs the conflicting change, PostgreSQL aborts one transaction with SQLSTATE 40001, called a serialization failure. The application must roll back and retry the complete transaction. Retrying only the failed statement is unsafe because the transaction's earlier decisions may no longer be valid.
Explicit locking as an alternative
You can also lock the rows that support the decision:
BEGIN;
SELECT doctor
FROM on_call
WHERE on_duty = true
FOR UPDATE;
UPDATE on_call
SET on_duty = false
WHERE doctor = 'alice';
COMMIT;The lock makes another transaction wait before checking the same set of rows. This approach can be easier to reason about, but it can reduce concurrency and create deadlocks if transactions lock rows in different orders.
| Technique | Protection | Main cost |
|---|---|---|
| Serializable isolation | Detects unsafe transaction dependencies | Retry handling |
| Explicit row locks | Prevents concurrent changes to selected rows | Waiting and possible deadlocks |
| Atomic single-row update | Protects one-row arithmetic or state change | Cannot express every multi-row rule |
Exercise
Two transactions each see two doctors on duty. One disables Alice and the other disables Bob. Both update different rows and commit. What anomaly is this, and what are two ways to prevent it?
Show answer
This is write skew. Use SERIALIZABLE isolation and retry serialization failures, or explicitly lock the rows needed to validate the rule before updating.