MVCC
Readers never block writers, and old versions never die on their own
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.
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.
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
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'.
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.
“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).