Stop 12 · Transactions & ConcurrencyTopics · 9/9

MVCC (Multi-Version Concurrency Control)

Part of the SQL Roadmap

In short

How can one person read a table while another updates it, without either waiting? The trick is that the database keeps more than one version of a row at a time.

MVCC lets readers and writers work at the same time by keeping several versions of a row. Each transaction sees a consistent snapshot, so reads do not block writes. PostgreSQL and MySQL InnoDB both use it.

The jaadu hands out crisp folded newspapers to people sipping tea at a roadside chai stall, while behind it a small printing press rolls out a fresh stack.

Think of it like this

Morning and evening editions

Readers at the chai stall are reading the morning paper while the press is already printing the evening edition. Nobody stops reading for the press, and the press doesn't wait for readers. Once the evening edition is out, new readers pick that up, and the old copies go to the raddiwala later. MVCC works the same way with row versions.

Watch it happen

Versions of account 501

v1: ₹8,000
0

1/5One committed version of the row. Every transaction sees it.

The big ideas

What you need to know

Writes make new versions

An UPDATE writes a new row version and marks the old one as expired, instead of changing it in place.

Reads use a snapshot

Each transaction or statement sees the versions that were committed when its snapshot was taken.

Readers don't block writers

A plain SELECT never waits for an UPDATE, and an UPDATE never waits for a SELECT. Two writers on one row still queue.

Old versions need cleanup

PostgreSQL removes dead versions with VACUUM, usually done automatically by autovacuum.

See it in code

Walk through the code

sql
1-- Hidden system columns: the transaction that created / expired this version2SELECT xmin, xmax, account_id, balance3FROM accounts4WHERE account_id = 501;5 6-- Session A7BEGIN ISOLATION LEVEL REPEATABLE READ;8SELECT balance FROM accounts WHERE account_id = 501;   -- 80009 10-- Session B (not blocked by A)11UPDATE accounts SET balance = 6500 WHERE account_id = 501;12 13-- Session A, still inside its snapshot14SELECT balance FROM accounts WHERE account_id = 501;   -- still 800015COMMIT;16 17-- Dead versions pile up after many updates; VACUUM reclaims them18VACUUM (VERBOSE) accounts;

1/5xmin is the id of the transaction that created this version. Run it before and after an update to see it change.

PostgreSQL keeps old versions inside the table and cleans them with VACUUM. MySQL InnoDB keeps them in an undo log and purges them in the background, so there are no xmin or xmax columns there.

Save yourself the pain

Common mistakes

Oops

Leaving a transaction open for hours in a SQL client.

Do this instead

Old versions can't be cleaned while it's open, so tables bloat. Commit or close idle sessions.

Oops

Thinking MVCC means two writers never block each other.

Do this instead

Readers and writers don't block. Two transactions updating the same row still wait for each other.

Your mission

Watch versions change

~20 min

Quick check

Under MVCC, what happens when a long report is reading a table and another session updates a row?

Go deeper

  1. 1Start a transaction, read a row, change it in another session, and read again to see the snapshot.
  2. 2Learn why PostgreSQL needs VACUUM to clean up old row versions.
  3. 3Explain in two sentences why MVCC helps busy applications.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge