Write skew is a concurrency anomaly in which two concurrent transactions read the same data, each decides on a write based on what it read, and each then updates a different row, so that the combined result violates a constraint that neither transaction violated alone. In database transactions, write skew is the characteristic weakness of snapshot isolation: it is permitted there, yet it cannot occur under true serializable isolation.
The anomaly is subtle because no row is written twice and no individual change is invalid. The damage exists only in the relationship between rows, which is exactly what application invariants such as “at least one doctor stays on call” express.
How write skew arises under snapshot isolation
Write skew arises because snapshot isolation checks write-write conflicts only. Each transaction reads from a consistent snapshot taken at its start, and the database aborts a transaction only when two transactions modify the same row. When they modify disjoint rows, both commit, even if each one based its decision on data the other has just made obsolete.
The standard illustration involves two doctors on call. The rule requires that at least one doctor remains on call. The following sequence shows how both doctors can leave at once.
| Step | Transaction A, first doctor | Transaction B, second doctor |
|---|---|---|
| 1 | Counts on-call doctors: 2 | Counts on-call doctors: 2 |
| 2 | Concludes it is safe to leave | Concludes it is safe to leave |
| 3 | Sets own status to off call | Sets own status to off call |
| 4 | Commits successfully | Commits successfully |
| Result | Zero doctors on call | Invariant violated |
The pattern always has the same shape: read a condition, decide, write somewhere else. Formally, it forms a dependency cycle of two read-write antidependencies, each transaction reading a version that the other overwrites, which is why serializability theory classifies it as a non-serializable history.
Write skew among isolation anomalies
Write skew belongs to the family of isolation anomalies, but it differs from its better known relatives because it involves two different objects. The ANSI SQL standard defined isolation levels only through dirty reads, non-repeatable reads and phantoms, and the 1995 critique by Berenson and colleagues showed that this list missed write skew entirely.
| Anomaly | What goes wrong | Objects involved |
|---|---|---|
| Dirty read | Reading uncommitted data | One row |
| Non-repeatable read | A reread row has changed | One row |
| Lost update | Two writers overwrite one value | One row |
| Phantom read | A repeated query returns new rows | A predicate range |
| Write skew | Disjoint writes break a shared invariant | Two or more rows |
A lost update can be seen as the degenerate case of write skew in which both transactions happen to write the same row; snapshot isolation prevents it through first-committer-wins. A related variant is phantom-induced write skew, in which the conflicting write inserts a new row that would have changed the other transaction’s search result, such as two bookings for the same meeting room and time slot.
Which isolation levels allow write skew
Whether write skew can happen depends on the actual implementation of an isolation level, not only on its name. Several databases label snapshot isolation as repeatable read or even as serializable, so the guarantees must be checked product by product.
| Isolation level | Write skew possible | Note |
|---|---|---|
| Read committed | Yes | Also allows several other anomalies |
| Snapshot isolation | Yes | Classic setting for write skew |
| PostgreSQL repeatable read | Yes | Implemented as snapshot isolation |
| Oracle serializable | Yes | Historically snapshot isolation in practice |
| PostgreSQL serializable | No | Serializable snapshot isolation aborts cycles |
| Two-phase locking serializable | No | Predicate or range locks block the pattern |
Serializable snapshot isolation (SSI), used by PostgreSQL since version 9.1, tracks read-write dependencies at run time and aborts one transaction when it detects a dangerous structure of two consecutive antidependencies. Systems based on strict two-phase locking prevent write skew differently, by holding shared locks on everything read until commit.
Preventing write skew in applications
The most robust defense is to run the affected transactions under true serializable isolation and to retry on serialization failure. Where that is not possible, the application must turn the hidden read dependency into an explicit conflict the database can see.
| Technique | How it works | Trade-off |
|---|---|---|
| Serializable isolation | Database detects or blocks the cycle | Requires retry logic on aborts |
| SELECT … FOR UPDATE | Locks the rows the decision depends on | Fails when the checked rows do not yet exist |
| Materialized conflict | Adds a lock row, such as a time slot record | Pollutes the data model |
| Database constraint | Unique, exclusion or check constraint enforces the rule | Only some invariants are expressible |
| Single atomic statement | Combines read and write in one UPDATE | Works only for simple conditions |
Explicit locking with FOR UPDATE protects only rows that already exist. When the invariant concerns the absence of rows, as in double booking, there is nothing to lock, and the solution is either an exclusion constraint or a materialized lock row.
Detecting and testing for write skew
Write skew rarely appears in ordinary testing because it needs precise interleaving of concurrent transactions. It surfaces in production under load, often as data that breaks a business rule without any error in the logs. Code review should therefore look for the check-then-act pattern across multiple rows.
- Audit invariants that span rows: minimum staffing, account balance limits, uniqueness without a constraint.
- Reproduce with two sessions: open two transactions manually and interleave their statements step by step.
- Use concurrency testing tools: frameworks such as Jepsen and Elle analyze histories for anomalies.
- Monitor serialization failures: under SSI, a rising abort rate indicates real contention that retry logic must absorb.
In plain words
Imagine two roommates who share a rule: someone must stay home with the dog. Each one looks around, sees the other at home, and decides to go out. Both leave at the same moment. Neither did anything wrong alone, yet the dog ends up alone.
Write skew is the database version of this situation. Two operations each check the same facts, then change different things. Together, they break a rule that both of them believed they were respecting.
A real-world example
A conference center runs its room booking system on PostgreSQL at the default read committed level. The booking code first checks whether any reservation overlaps the requested time, and if none exists, it inserts a new one. During a busy registration morning, two staff members book Room 4 for 10:00 to 11:00 within the same few milliseconds. Both checks find no overlap, both inserts succeed, and two groups arrive for the same room.
The developers fix the problem by adding an exclusion constraint on the room identifier and the time range, so that the database itself rejects overlapping reservations. They also move the booking transaction to serializable isolation with an automatic retry of up to three attempts. Double bookings stop entirely, and the retry rate stays below one percent of booking requests.