This is a good database concurrency control question about write skew anomaly in snapshot isolation. Let me explain this carefully.
Write Skew Anomaly
Write skew is a type of concurrency anomaly that can occur under snapshot isolation but not under full serializability. It's particularly tricky because it involves transactions that don't actually conflict on the same data items in the traditional sense (no dirty reads, lost updates, etc.), but the results violate application-level constraints.
The Doctors On-Call Example
Let me explain the classic two doctors example:
Constraint: At least one doctor must always be on-call.
Initial State:
- Doctor A: on_call = true
- Doctor B: on_call = true
Scenario:
Both doctors decide they want to go off-call, but each assumes the other will remain on-call.
Transaction T1 (Doctor A's transaction):
- Read both doctors' on_call status
- A: on_call = true
- B: on_call = true
- Check: at least one is on-call ✓ (B is on-call)
- Set A's on_call = false
- Commit
Transaction T2 (Doctor B's transaction):
- Read both doctors' on_call status
- A: on_call = true (sees the original snapshot)
- B: on_call = true
- Check: at least one is on-call ✓ (A is on-call)
- Set B's on_call = false
- Commit
Final State:
- Doctor A: on_call = false
- Doctor B: on_call = false
Constraint violated! No doctor is on-call anymore.
Why This Happens Under Snapshot Isolation
Snapshot isolation works by:
-
Each transaction sees a consistent snapshot of the database from when it started
-
Transactions can only commit if they don't have write conflicts with concurrent transactions
-
Write conflicts are detected only on the same data items being written
In the doctor example, T1 and T2 read the same rows but write to different ones—T1 modifies only A while T2 modifies only B. Since there's no write-write conflict, both transactions commit successfully under snapshot isolation, even though the application constraint is violated.
Why This Doesn't Happen Under Serializability
Under full serializability (like 2PL), one transaction would acquire locks on the data it reads, preventing the other from proceeding until the first completes, ensuring the constraint is maintained.
Key Characteristics of Write Skew
Write skew occurs when transactions write to different data items, each satisfying the constraint locally but violating it globally. Snapshot isolation cannot detect this because it only checks for write-write conflicts, not application-level constraint violations.
Other Examples of Write Skew
Similar patterns emerge in booking systems where multiple users reserve the same slot after each checking availability, or in materialized view maintenance where concurrent transactions update summary tables based on reads from base tables.
Solutions
To prevent write skew, explicit locking with SELECT ... FOR UPDATE can lock the rows being read, or databases offering true serializability like PostgreSQL can enforce it. Alternatively, applications can implement retry logic with conflict detection, artificially create write-write conflicts through a dedicated conflict table, or validate constraints before committing.
Write Skew Anomaly in Snapshot Isolation
Definition
Write skew is a concurrency anomaly where snapshot isolation (SI) allows two transactions to each satisfy an application constraint at their snapshot point, but their combined writes violate that constraint—even though the transactions write to different data items.
It's "invisible" to SI's conflict detection because SI only tracks write-write conflicts, not read-write dependencies on constraints.
The Two Doctors On-Call Example
Constraint
At least one doctor must always be on-call
Initial State
Doctor A: on_call = true
Doctor B: on_call = true
Concurrent Execution Under Snapshot Isolation
Transaction T1 (Doctor A leaves):
BEGIN TRANSACTION;
SELECT on_call FROM doctors WHERE id IN (A, B);
-- Snapshot sees: A=true, B=true ✓ Constraint satisfied
UPDATE doctors SET on_call=false WHERE id=A;
COMMIT; -- ✓ Commits (no write-write conflict)
Transaction T2 (Doctor B leaves):
BEGIN TRANSACTION;
SELECT on_call FROM doctors WHERE id IN (A, B);
-- Snapshot sees: A=true, B=true ✓ Constraint satisfied
-- (doesn't see T1's update yet)
UPDATE doctors SET on_call=false WHERE id=B;
COMMIT; -- ✓ Commits (no write-write conflict)
Final State (Violated!)
Doctor A: on_call = false
Doctor B: on_call = false
❌ CONSTRAINT VIOLATED: No doctor on-call
Timeline Visualization
T1: READ(A,B)----CHECK✓----WRITE(A)--COMMIT
T2: READ(A,B)----CHECK✓----WRITE(B)--COMMIT
↑
Both see original snapshot
(constraint satisfied in isolation)
Why SI Allows This
| Aspect | Behavior |
|---|
| Write conflict detection | T1 writes A, T2 writes B → No overlap → No conflict |
| Read isolation | Each transaction's read sees consistent snapshot → Each locally valid |
| Constraint checking | Application-level constraint, not DB-level → SI unaware |
Why Serializability Prevents This
Under 2-phase locking or other serializable isolation:
T1: READ-LOCK(A,B)----CHECK✓----WRITE(A)--RELEASE--COMMIT
T2: WAITS.....................LOCK(A,B)
READ(A,B)--CHECK✗--ROLLBACK
The second transaction waits for the first to release locks, then sees the updated state and detects the constraint violation before committing.
Key Insight: Not a Read/Write Conflict Problem
Write skew is distinct from other anomalies:
- Dirty Read, Lost Update: Detected by write-write conflicts ✓ SI prevents
- Write Skew: No write-write conflict, but constraint violated ✗ SI allows
T1 writes X ──────┐
← Different items (no conflict in SI)
T2 writes Y ──────┘
But X + Y violates constraint!
Practical Solutions
1. Explicit Locking (Most Common)
SELECT * FROM doctors WHERE id IN (A, B) FOR UPDATE;
-- Forces serialization without full SI overhead
2. Materialize the Constraint
-- Create artificial write-write conflict via phantom table
INSERT INTO constraint_check VALUES ('only_one_oncall');
-- Both transactions update this row → write conflict detected
3. Serializable Isolation
PostgreSQL's SERIALIZABLE mode detects SI anomalies:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- Detects read-write conflicts and rolls back one transaction
4. Application-Level Retry
Catch constraint violation → Retry from scratch
Real-World Examples
| Scenario | Constraint |
|---|
| Booking system | "Only N seats available" → Both users book last seat |
| Meeting rooms | "Room free 2-3pm" → Both reserve same slot |
| Materialized views | Summary (A+B=total) consistency → Both update A and B separately |
| Email uniqueness | "Email must be unique" during concurrent migrations |
Write skew is particularly dangerous because it silently violates constraints without triggering obvious concurrency errors.