MVCC: how readers stop blocking writers
Lesson 3.04 handed you the isolation dial and told you which anomalies each setting permits. It did not say how the database delivers any of it. There are two families of answer, and which one your engine belongs to changes what your slow queries cost everyone else.
The old answer is locking: a reader takes a shared lock, a writer takes an exclusive one, and they wait for each other. It is correct, and it serialises your traffic behind whoever is slowest. The modern answer, in Postgres, Oracle, InnoDB and nearly everything else you will meet, is multi-version concurrency control — worth understanding as a mechanism, because its costs surface as operational problems that look like nothing to do with transactions.
Nothing is overwritten
An update does not modify a row. It writes a new version of that row stamped with the id of the transaction that created it, and marks the old version as deleted by that same transaction. Both versions are physically present. A delete writes no new version and only stamps the deletion.
A reader, on starting, takes a snapshot: the transaction id watermark at that instant, plus the set of transactions still in flight. Every row it meets is then a visibility question with a deterministic answer — was this version created by a transaction that had committed as of my snapshot, and not deleted by one? Read the newest version that passes.
The consequence is the entire reason for the design: readers never block writers and writers never block readers. A ninety-second report and a checkout transaction touching the same table do not queue behind each other, because they are looking at different versions of the same rows.
Four costs that follow directly
- Old versions must be kept while any snapshot might still need them. This is the one that matters. A transaction that started an hour ago pins every version that existed when it began — across the whole database, not only the tables it reads.
- Reclaiming them is a background process you depend on. Vacuum, purge, whatever your engine calls it, is not tidying. When it falls behind, tables occupy several times the size of their live data, sequential scans read the dead versions too, and the buffer pool caches garbage. Performance degrades everywhere at once with no slow query to blame.
- An update is physically an insert. Every index on the table must learn about the new version, so a table with eight indexes can pay eight index writes to change one unindexed column. Lesson 3.02 priced indexes on the write path; this is the mechanism that makes that price real.
- Transaction ids are finite. On engines using a fixed-width counter, old rows must be frozen before it wraps, and an engine that cannot keep up will eventually refuse writes rather than risk returning wrong answers. It is a rare failure and a spectacular one, and it is always preceded by weeks of vacuum falling behind.
What snapshots still do not give you
Snapshot isolation is strong, and it is not serialisable. The gap has a name — write skew — and one canonical shape: two transactions read an overlapping set of rows, each checks an invariant that currently holds, each writes a different row based on that check, and both commit. Two doctors each cancel their on-call shift after confirming the other is still rostered. No row was written twice, none of 3.04's anomalies occurred, and the invariant is broken.
The fixes are ordinary: take an explicit row lock on what you read, materialise the conflict as a real row the database can serialise on, or ask for genuine serialisable isolation and handle the retries it hands back. What does not work is reasoning about it harder — snapshot isolation permits this by construction, and no amount of care in application code changes that.
The idle transaction nobody can see. Someone opens a session and walks away, a reporting tool leaks a connection mid-transaction, a batch job holds one open while it waits on an external call. It burns no CPU, runs no query, and shows up on every dashboard as an idle connection — while its snapshot forbids vacuum from reclaiming a single version created since it began, anywhere in the database. Bloat grows across tables it has never touched, and by the time anyone notices, the remedy is hours of maintenance on a system already struggling. Alert on transaction age, not connection count: any transaction open for more than a few minutes is either a bug or a batch job that should be committing more often, and it is the highest-value alert an MVCC database has.
Going deeper
- Serializable Snapshot Isolation in PostgreSQL Ports & Grittner, VLDB 2012
How the gap between snapshot isolation and serialisability is closed by detecting dangerous read-write patterns at runtime, and what that detection costs.