Skip to content
WebsiteHosting.com

concept Data & storage

Write skew and snapshot isolation anomalies

Two doctors can each see the other on call, update different rows, and leave the roster empty. Write skew is about the rule their decisions share, not a collision between their writes.

Latest recorded source check · P0137

What it means

Write skew occurs when two transactions each make a valid decision from a snapshot, but their changes together break a rule. They need not write to the same row. What connects them is that each decision depends on data the other transaction changes.

Consider a roster with one row per doctor. The rule, or invariant, is that at least one doctor must remain on call. Before taking a doctor off call, the application checks whether anyone else is on call. With two doctors on duty, either one can leave. Both cannot.

Try it

Simulation. Start with Execution set to concurrent, Conflict handling to snapshot, and Required scenario evidence to provided. Both doctors request to go off call. Each transaction sees two doctors on duty and changes only its own doctor's row. The result is 2 committed off-call changes and 0 doctors on call.

Write skew and snapshot isolation anomalies

Demonstrate an invariant violation despite no direct same-row write conflict

Simulation
Modeled outcome: Violation

Both snapshot readers see two on-call staff and update distinct rows; invariant breaks.

Write skew and snapshot isolation anomalies: synthetic scenario inputs
Replaces all inputs with the documented starting example and the indicated change. Reset restores the starting example.
Two snapshots and a final invariant

Stipulated execution

  1. Scheduleconcurrent
  2. Isolation premisesnapshot
  3. Committed changes2
  4. On call after changes0

Invariant

  1. At least one remainsViolated
Final two-person roster (counts only)2 people
On call: 0 peopleOff call: 2 people

The fixture has two initially on-call people. Concurrent snapshot transactions can each observe the other still on call; the serial/serializable contrast represents a schedule with only one committed change, not a full database implementation.

Evaluated inputs

Required scenario evidence
provided
Execution
concurrent
Conflict handling
snapshot

Two doctors initially on call, invariant at least one, application check; abstract serializable conflict handling, no real DB mutation.

About this model

The controls set only a local, bounded scenario. The computed branch is not a verdict about a live system. Inputs listed as not evaluated did not participate in this branch after validation.

Reset restores the original inputs. It does not change the open or closed state of help or FAQ answers.

Keep execution concurrent and change only Conflict handling to serializable. Now 1 change commits and 1 doctor remains on call. The checks can still overlap; the difference is that the simulation rejects one conflicting transaction rather than letting both changes commit. It does not specify which doctor stays.

Use Reset, then change only Execution to serial. Again, 1 change commits and 1 doctor remains, but this time there is no need to reject a conflicting transaction. The second check takes place after the first change commits, sees just one doctor left, and declines the update. Serial order alone determines this result; the selected conflict handling is not evaluated.

Setting Required scenario evidence to missing gives an unknown outcome, not a count of zero: neither execution nor conflict handling is evaluated. Reset restores all three starting choices and the original two-change example. The simulation does not read or change a database.

Why the two commits cannot fit a serial order

Call the doctors Avery and Blair. Follow the evidence for each decision in the concurrent snapshot example:

RequestEvidence that permits leavingRow changed
Avery goes off callBlair is on call in Avery's snapshotAvery's row: on to off
Blair goes off callAvery is on call in Blair's snapshotBlair's row: on to off

Read across either row of the table and the decision makes sense. Read down the last column and there is no same-row write conflict. The problem appears when you compare the middle and last columns: Avery changes the very fact Blair relied on, and Blair changes the fact Avery relied on. Neither transaction had to miscount anyone for the roster to end up empty.

Could those two changes both commit if the requests took turns? If Avery went first, Blair would find nobody else on duty. If Blair went first, Avery would find nobody else on duty. Neither order explains two departures. That is the distinction between a stable snapshot and a serializable outcome: seeing a consistent roster during each check does not establish that the committed decisions fit any serial order.

The final roster strip shows counts, not the identity of the doctor who remains. In the serializable premise the model does not select a victim or retry schedule. That is why the preserved invariant is the result to compare, rather than a prediction about which database transaction will abort.

In practice

When reviewing a rule that spans rows, write down the question the application asks before a change. For the roster it is “Will someone else remain on call?” For each transaction, identify the row that supplied its yes answer, the row it changed, and whether the change committed. A list of successful writes alone leaves out the evidence on which those writes depended.

In PostgreSQL 18, Repeatable Read provides a stable snapshot but can permit serialization anomalies. Serializable adds checks and may reject a transaction. The simulation stipulates a serialization failure in the conflicting case; it is not a test of a particular installation. For example, PostgreSQL 18.7 fixes a missing predicate-lock case involving INSERT ON CONFLICT under Serializable. That fix concerns a different operation from the two roster updates. Check the deployed minor version and the fixes relevant to the actual workload before relying on the documented behavior.

Judge the rule against the committed roster. “Did these transactions overwrite the same row?” answers a narrower question than “After both decisions, is anyone still on call?”

Frequently asked questions

Editorial questions based on the listed sources and explicitly labeled local examples.

Why no direct write conflict?

Each transaction updates a different row in the example, so there is no direct same-row write conflict. Their decisions still depend on overlapping reads and can violate the shared invariant.

Would a single locked row help?

A common lock can make transactions take turns, but it does not automatically refresh a snapshot that was already established. The design must enforce the invariant under the chosen isolation level. PostgreSQL Serializable transactions also require handling serialization failures.

Does repeatable read guarantee serializability?

No. PostgreSQL Repeatable Read gives a stable snapshot but can permit serialization anomalies. Serializable adds checks and may reject a conflicting transaction.

What should a serialization retry do?

If an operation is authorized and safe to retry, repeat the complete transaction, including the reads and decisions that justified the writes, under a bounded retry policy. Repeating only the final update can preserve a decision made from the old snapshot.

Sources & model boundaries

Primary references used for this explanation. The interactive example is an educational model, not a live check.

  1. PostgreSQL transaction isolation

    Section 13.2 (transaction isolation). Repeatable Read does not guarantee serial equivalence. Serializable checks for anomalies and callers must handle serialization failures.

    Checked .

  2. PostgreSQL explicit locking

    Section 13.3 (explicit locking). Explicit locking can serialize access to a protected resource. It does not by itself refresh an established transaction snapshot or prove the application's invariant.

    Checked .

  3. PostgreSQL 18.7 release notes

    E.1.2 Changes: Serializable INSERT ON CONFLICT predicate locking. 18.7 corrects missing predicate locking in Serializable INSERT ON CONFLICT.

    Checked .

Report an issue. Include the article URL and ID, not passwords or access keys.