MVCC

Readers never block writers, and old versions never die on their own

L318 MINDATABASES
PREDICT

T1 is a REPEATABLE READ transaction: all its reads come from one snapshot of the database, taken when it starts. T1 reads balance = 100. T2 then updates balance to 150 and commits. T1 reads balance again. What does T1 see?

MVCC never overwrites a row. An UPDATE writes a new version stamped with its creator’s transaction id (xmin) and stamps the old version with its replacer (xmax). At REPEATABLE READ each transaction takes one snapshot (the sim takes it at BEGIN; PostgreSQL at the first statement) and reads the newest version whose creator had committed by then. READ COMMITTED takes a fresh snapshot for every statement, so there T1 would read 150. Readers never wait for writers.

The price comes in two places. A read-modify-write can compute from a stale snapshot, so a write must refuse to replace a version it cannot see: first-committer-wins. And old versions pile up until vacuum removes them, which it may only do once no live snapshot can see them.

▶ SIMULATION
TRANSACTIONS
none yet
—
ROW balance · 1 VERSION
100 xmin init#100 · xmax — · current
CHECKS
writes: naive chain · vacuum: naive
balance 100 · expected 100
conflicts 0 · vacuumed 0 · removed while visible 0
✓ no transaction sees uncommitted data✓ every committed update survives (balance = 100 + committed deltas)✓ no version visible to a live snapshot is removed
SCHEDULE
01[VACUUM]first-committer-wins writes, horizon vacuum
02[A]A: BEGIN (take snapshot)
03[B]B: BEGIN (take snapshot)
04[B]B: UPDATE balance = balance + 50
05[A]A: SELECT balance
06[B]B: ROLLBACK
07[C]C: BEGIN (take snapshot)
08[C]C: SELECT balance
09[A]A: SELECT balance
10[VACUUM]VACUUM: remove dead versions
11[A]A: COMMIT
12[C]C: COMMIT
13[VACUUM]report
Start of snapshot read + rollback scenario. 13 steps.
MVCC (PostgreSQL-style, REPEATABLE READ: one snapshot per transaction;
PostgreSQL takes it at the first statement, this sim at BEGIN)
===================================================

Every version: value, xmin (creator txid), xmax (replacer txid or null)

Snapshot S = { txid, xmax: first txid not yet started, active: [in progress] }
sees(t, S)  = t == S.txid  OR  (t < S.xmax AND t not in S.active AND committed(t))
visible(v, S) = sees(v.xmin, S) AND NOT (v.xmax AND sees(v.xmax, S))

READ: newest version visible to S. Never blocks, never sees uncommitted data.

WRITE (first-committer-wins):
  cur = newest version whose creator did not abort
  cur not visible to S?           -> serialization failure, abort
  cur.xmax set by a live/committed txn? -> serialization failure, abort
  else cur.xmax = me; append { value, xmin: me, xmax: null }
  (PostgreSQL waits on a live writer's row lock instead: it fails if that
   writer commits and proceeds if it aborts. This sim aborts at once.)

ROLLBACK: nothing is undone in place. The version stays, dead.

VACUUM: horizon = oldest txid any active snapshot may consider running
  remove v if creator aborted, or xmax committed AND xmax < horizon
  one long transaction holds the horizon back: dead versions pile up (bloat)

Watch the three invariants. NAIVE CHAIN: LOST UPDATE ends at 70 instead of 120. FIX: FIRST-COMMITTER-WINS runs the same interleaving, refuses B’s write and retries it. VACUUM IGNORES OLD SNAPSHOT deletes the version R is reading. FIX: VACUUM RESPECTS HORIZON keeps it and pays in bloat: four versions of one row until R commits.

BREAK IT NOT YET

Writes use the naive chain: an UPDATE stacks a new version on whatever the transaction read, with no conflict check. A adds 50, B subtracts 30, starting from 100. Both commit. Order the steps so the balance does not end at 120.

GOAL: both commit and a committed update is lost

FIX IT NOT YET

Implement visible(version, snapshot) and write(row, txn, value). A snapshot is { txid, xmax, active, status }: a txid is in the snapshot if it is txid itself, or it is below xmax, not in active, and status[t] === 'committed'. write must refuse (return 'conflict') when the newest non-aborted version is not visible to the writer or was already replaced by a transaction that did not abort (PostgreSQL would wait for a still-running replacer to finish; here you refuse at once); otherwise set its xmax, append { value, xmin: txn.id, xmax: null } and return 'ok'.

Mental model:

MVCC is a pile of versions plus a rule for which one each snapshot sees. Reads are free because nothing is overwritten. Writes are safe only if they refuse to replace a version they could not see. Space comes back only through vacuum, and one forgotten transaction holds back every version created since it began.

IN PRODUCTION

PostgreSQL (xmin/xmax tuples, VACUUM, “could not serialize access due to concurrent update” at REPEATABLE READ), MySQL InnoDB (undo-log versions, purge lag and history list length), Oracle (undo segments, ORA-01555 snapshot too old), CockroachDB and every snapshot-isolation store. Symptoms: table and index bloat, autovacuum that cannot keep up, and serialization failures your code has to retry.

IN INTERVIEWS

“How does PostgreSQL let readers and writers run concurrently?” and “Why is this table growing when row count is flat?” Explain versions and snapshot visibility, show the lost update under naive snapshot writes and how first-committer-wins aborts the second writer, then explain the vacuum horizon and how to find the long-running transaction holding it back (pg_stat_activity, idle in transaction).

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: B+ TREE →