Scrivio Interview library
InternalsFoundations7 min read · updated 12 November 2026

How does PostgreSQL implement MVCC at the row level? Explain xmin, xmax, snapshots, and why an UPDATE leaves a dead tuple behind.

Try it yourself

Isolation levels: what a second read sees

Step through two transactions and see exactly when read committed and repeatable read give different answers.

Read this example as text

T1 at read committed

  1. T1 begins. Transaction T1 starts with isolation level READ COMMITTED. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under read committed each statement looks afresh at what is committed when the statement starts.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 reads 150. The same query, inside one transaction, returned 100 and then 150. This is a non-repeatable read, and read committed allows it.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100, 100, then 150: a non-repeatable read.

T1 at repeatable read

  1. T1 begins. Transaction T1 starts with isolation level REPEATABLE READ. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under repeatable read this first read fixes T1's snapshot: T1 will keep seeing the data as it was committed at this moment.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 still reads 100. Its snapshot was fixed at its first read, so T2's commit is invisible to it. The read is repeatable.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100 three times: its reads were repeatable.

What this example leaves out
  • Modelled on PostgreSQL and MySQL InnoDB, where a plain SELECT reads a snapshot and never waits for a writer.
  • SQL Server's default read committed uses locks instead: there, T1's second read would wait for T2 to finish.
  • Only reads are compared. What happens when T1 also writes the row (a lost update, or an error in PostgreSQL) is not modelled.

PostgreSQL keeps several versions of each row. Every row version, called a tuple, carries xmin, the id of the transaction that created it, and xmax, the id of the transaction that deleted or locked it; an UPDATE never changes a tuple in place but writes a new version and sets xmax on the old one. A snapshot lists which transactions had finished when it was taken, and a tuple is visible if its creator committed before the snapshot and its deleter did not, so the old version stays behind as a dead tuple until VACUUM removes it.

Why this question is asked

MVCC is the reason PostgreSQL readers do not block writers, and it is also the reason for table bloat, vacuum tuning and wraparound warnings. Every one of those operational problems makes sense once we know what is stored in a row header. Interviewers ask about xmin and xmax because the answer shows whether we understand the mechanism or only the slogan. In this answer, we will look at the hidden columns of a row, follow an UPDATE, see how a snapshot decides visibility, and then cover dead tuples, VACUUM, HOT updates and freezing.

1A Quick Recap of MVCC

Multi-version concurrency control, MVCC for short, means the database keeps more than one version of a row at a time. A transaction that started earlier can keep reading the version that was current for it, while a newer transaction writes a newer version. Nobody has to wait for anybody to read.

In PostgreSQL, each transaction that writes gets a transaction id, a 32-bit number called an xid that increases over time. Each stored row version is called a tuple. A table, also called a heap, is a set of 8 KB pages full of tuples.

2The Hidden Columns on Every Tuple

Each tuple has a header with system columns we can actually select. The two that matter most are xmin, the xid of the transaction that inserted this version, and xmax, the xid of the transaction that deleted it, replaced it with an update, or locked it (0 when nobody has). ctid is the tuple's physical address: page number and slot.

SQL
CREATE TABLE account (id int PRIMARY KEY, balance int);
INSERT INTO account VALUES (1, 100);
SELECT ctid, xmin, xmax, * FROM account;
-- (0,1) | 750 | 0 | 1 | 100
BEGIN;
UPDATE account SET balance = 50 WHERE id = 1;
SELECT txid_current(); -- 751
SELECT ctid, xmin, xmax, * FROM account;
-- (0,2) | 751 | 0 | 1 | 50 the new version, at a new address
COMMIT;
Page 0 after the update commits
ctidxminxmaxbalanceState
(0,1)750751100old version, dead once no snapshot needs it
(0,2)751050current version

Notice that after the UPDATE the row lives at a new ctid, (0,2). The old tuple at (0,1) is still on the page, with balance 100, and its xmax is now 751. An UPDATE in PostgreSQL is really an insert of a new version plus a mark on the old one. A DELETE only sets xmax; it removes nothing yet.

3How a Snapshot Decides What We See

A snapshot is a small record taken when a statement or transaction starts. It holds xmin, the oldest transaction still running at that moment; xmax, the first xid not yet assigned; and the xip list, the ids of transactions in progress between them. Everything below the snapshot's xmin is finished; everything at or above its xmax had not even started.

A tuple is visible to a snapshot if its inserting transaction committed and was already finished when the snapshot was taken, and its deleting transaction either does not exist, aborted, or had not finished when the snapshot was taken. Whether a transaction committed or aborted is looked up in the commit log (pg_xact), and the answer is cached on the tuple as hint bits so later readers skip the lookup.

So in our example, a REPEATABLE READ transaction whose snapshot was taken while 751 was still in the xip list keeps seeing (0,1) with balance 100: its deleter, 751, had not finished for that snapshot. A new statement sees (0,2) with 50.

Step throughIs this tuple visible to my snapshot?
  1. Did xmin commit before my snapshot?
    YesCheck xmaxNoNot visible
  2. Is xmax empty, aborted, or a lock only?
    YesVisibleNoCheck when xmax committed
  3. Did xmax commit before my snapshot?
    YesNot visible: deleted for meNoVisible: still the version I see

3 steps in order. Play them, or step through them one at a time.

4Why Readers and Writers Do Not Block Each Other

Because a reader picks the version its snapshot allows, it never needs a lock on the row, and a writer creating a new version never waits for readers. The only waiting happens between writers of the same row: a second UPDATE finds xmax set by a transaction still in progress, and waits for it to commit or abort before deciding what to do. At READ COMMITTED it then works on the newest version; at REPEATABLE READ it fails with a serialization error if the row changed.

5Dead Tuples, VACUUM and HOT Updates

Once no running snapshot can see the old version, it is dead: it still takes space on the page and still has index entries pointing at it. Nobody removes it during the UPDATE because some snapshot might still need it. Cleaning up is the job of VACUUM, usually run automatically by autovacuum. It removes dead tuples whose deleter committed before the oldest snapshot still in use, deletes their index entries, and records the free space so new tuples can reuse it. A plain VACUUM does not return space to the operating system; the table file keeps its size.

Every update also needs a new index entry in every index, which is expensive. A HOT update, short for heap-only tuple, avoids that: when no indexed column changes and the new version fits on the same page, PostgreSQL chains the new version from the old one inside the page and leaves the indexes alone. Leaving free space on pages with a lower fillfactor, such as 90, makes HOT updates more likely on frequently updated tables.

6Freezing and Transaction Id Wraparound

Because xids are only 32 bits, they wrap around after about 4 billion transactions, and PostgreSQL compares them in a circle: roughly 2 billion ids are in the past and 2 billion in the future. A tuple whose xmin became very old would suddenly look as if it came from the future and disappear. To prevent this, VACUUM freezes old tuples, marking them as visible to everyone regardless of their xmin. Autovacuum forces an anti-wraparound vacuum when a table's oldest unfrozen xid reaches autovacuum_freeze_max_age, 200 million by default, and if freezing falls far behind, PostgreSQL eventually refuses new transactions to protect the data.

7Follow-up questions

How does InnoDB's MVCC differ, using undo logs?

InnoDB updates the row in place in the clustered index and copies the previous values into the undo log. Each row carries the id of the last transaction that changed it and a roll pointer to its undo record. A reader whose read view should not see that change follows the roll pointer and rebuilds the older version from the undo chain. So the table itself stays compact and there is no table-level vacuum; instead a background purge thread discards undo records once no read view needs them. A long transaction in InnoDB grows the undo history list instead of bloating the table, and old version reads get slower as the chain gets longer.

Does a SELECT ever write to the table?

It can. The first reader after a commit may set hint bits on tuples, and a page with many dead tuples can be pruned during a read, so a SELECT can dirty pages even though it changes no data.

Why does SELECT ... FOR UPDATE set xmax?

PostgreSQL stores row locks in the tuple itself: the locker's xid goes into xmax, with flag bits saying it is a lock and not a delete. That is why row locks need no shared memory per row.

8Conclusion

In this answer, we saw that PostgreSQL stores every row version as a tuple with xmin and xmax, that an UPDATE writes a new tuple and sets xmax on the old one, and that a snapshot decides visibility from which transactions had committed when it was taken. Readers and writers do not block each other; only writers on the same row wait. Old versions linger as dead tuples until VACUUM removes them, HOT updates avoid new index entries, and freezing protects against 32-bit xid wraparound.

What to say in the interviewAbout 80 seconds aloud

Open with

PostgreSQL implements MVCC by keeping several versions of a row in the table itself. Every tuple has a header with xmin, the transaction id that inserted it, and xmax, the transaction that deleted, updated or locked it.

Then, if they want more

An UPDATE doesn't modify the tuple; it writes a new version, often at a new ctid, and sets xmax on the old one. A snapshot records which transactions were finished when it was taken: an xmin, an xmax and the list of transactions in progress. A tuple is visible if its inserter committed before my snapshot and its deleter didn't.

That's why readers never block writers and writers never block readers; only two writers on the same row wait for each other. The cost is that the old version stays on the page as a dead tuple, with its index entries, until VACUUM, normally autovacuum, removes it once no snapshot can see it.

HOT updates help when no indexed column changes and the page has room, because then no new index entries are written. And because transaction ids are 32 bits, VACUUM also has to freeze old tuples so they don't appear to be in the future after wraparound.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does PostgreSQL implement MVCC at the row level? Explain xmin, xmax, snapshots, and...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/89916c25ec/