Isolation protects rows, not invariants
Isolation levels promise consistency of the rows a reader looks at, so invariants spanning rows or missing rows need a mechanism that names the set.
Isolation protects rows, so an invariant spanning several rows, or a row that does not exist yet, lies outside its reach. Snapshot isolation gives each reader one consistent set of committed versions, yet two writers can each read a snapshot, touch different rows, and still break a rule. Write skew is the everyday case. Stopping it means locking the whole set the rule covers, or detecting the conflict at commit and retrying.
Snapshot isolation keeps every version a row has held, each tagged with the transaction that wrote it. When a transaction starts, it records which transactions are still in progress, then reads each row's newest version written by a transaction that committed before that point. Readers never wait for writers, and a long scan sees one instant of the database. The guarantee covers reads only. A transaction decides from its snapshot and then writes, and the engine checks those writes for collisions with other writes to the same row. Two transactions can read the same rows and write disjoint ones, so no write-write collision occurs and both commit. Each snapshot was consistent on its own, yet the combined outcome breaks the rule.
The abstraction hides that a lock protects a set only if someone chooses the set. Consider an on-call table with a rule that at least one doctor stays active. Two transactions each count two active doctors, mark themselves inactive, and commit. Snapshot isolation accepts both because each updated a different row, and the database ends with no one on call. Locking the row you wrote does nothing, since the two writes are disjoint. Locking every active row fixes that schedule, yet it fails once the rule becomes at most two active and two transactions insert new doctors, because no row exists to lock. The alternative has a cost too. Serializable snapshot isolation aborts one transaction, and the whole transaction, with all its work, must retry. Each row version also takes storage until vacuum reclaims it, so a busy writer leaves garbage behind.
List every invariant in the system before choosing an isolation level. For each, write the set of rows it ranges over. Then identify the three ways it can break: a write to one of those rows, a write to another row that changes the total, or an insert that enlarges the set. If the set is one row, read committed or stronger protects it with that row's lock. If the set is several existing rows, lock the whole set with predicate locks, or use serializable snapshot isolation and retry on abort. If the set may contain rows that do not exist yet, create placeholder rows so each claimable slot has a lock, or use serializable snapshot isolation. If none of these fit, change the schema so the invariant becomes one row or a constraint the engine enforces itself. Budget for retries before you choose serializable, because each abort discards the transaction's work.
- MVCC versions per row
- one per concurrent writer
- Version retention
- until vacuum
- SSI abort cost
- whole transaction
- Transaction id width
- about 4 billion
Each concurrent writer to a row leaves its own version, so a hot row accumulates versions in proportion to its writers. Readers pick among those versions by snapshot.
Old versions stay in storage until vacuum reclaims the ones no open snapshot can read. Until then, they consume space and slow scans of the table.
An abort discards every read and write in the transaction, not only the conflicting statement. The application must rerun the entire transaction.
A 32-bit transaction id wraps after about 4 billion transactions. A busy system must reclaim old ids before the counter reuses them.
The first approach seemed obvious. PostgreSQL's serializable mode relied on two-phase locking, which blocks transactions and sometimes deadlocks them. The natural improvement was to lock each row a query reads, so conflicting transactions would wait instead of corrupting data.
The wall is that the invariant spans rows. Two transactions can each read what the other writes while touching different rows, so no row lock ever conflicts. Locking every row a query reads does not reach the rule the two transactions jointly break. Each transaction looks clean to the lock manager, and the bad outcome appears only in the combined result.
What PostgreSQL shipped followed Cahill, Röhm and Fekete: serializable snapshot isolation. The engine records which rows each transaction read, detects a dangerous structure of dependencies at commit, and aborts one transaction. The lesson is that isolation protects rows while the invariant lives between them. The only general mechanism is detection plus retry, and the retry is the price of never blocking.