MVCC Snapshots and Tuple Visibility
An MVCC snapshot is a record of which transactions a database query is allowed to see. The database uses that record, together with transaction IDs stored on row versions, to decide whether each version is visible to the reader.
MVCC means multi-version concurrency control: instead of replacing a row in place, a database keeps enough information about old and new versions for concurrent readers and writers to proceed independently.
The problem MVCC solves
Without MVCC, a writer updating a row would need to protect that row with a lock until the update was complete. A reader arriving at the same time would either wait, read data while it was changing, or use a weaker mechanism that could expose inconsistent results.
Locks are still useful in databases, but making every reader wait for every writer would reduce concurrency. It would also make a long report compete directly with short updates.
MVCC gives a reader a consistent answer without requiring the writer to stop. The reader sees a version of each row that was valid at a particular point, while the writer creates a newer version for later readers.
A snapshot is not usually a copy of all the rows. It is metadata describing the transaction boundary against which row versions should be judged.
What a snapshot records
A transaction ID is a number assigned to a transaction so the database can order its work. A transaction may be running, committed, or aborted. The exact representation differs between database systems, but an MVCC snapshot commonly records:
- A lower boundary: transactions older than this are known to have finished.
- An upper boundary: transactions at or beyond this point were not yet part of the snapshot.
- The IDs of transactions that were still running when the snapshot was taken.
PostgreSQL exposes this idea through fields commonly described as xmin, xmax, and a list of active transaction IDs. Other databases store equivalent information differently, so the field names should not be treated as a universal MVCC interface.
The snapshot answers a question such as:
“Which transactions had committed before this query's view began, and which ones were still in progress?”
That distinction matters because a transaction that commits after the snapshot was taken must not suddenly make its changes appear halfway through the query.
What a tuple records
A tuple is a database's stored representation of a row. In an MVCC system, a tuple version normally carries transaction metadata including:
- The transaction ID that created the version.
- The transaction ID that deleted or replaced it, if one exists.
- Whether those transactions committed or aborted.
An UPDATE therefore commonly produces a new tuple version and marks the old version as replaced. A DELETE marks a version as no longer current rather than immediately erasing every trace of it.
The database can then compare the tuple's metadata with the reader's snapshot. Conceptually, it asks two questions:
- Was this version created by a transaction visible to the snapshot?
- Was it replaced by a transaction visible to the snapshot?
A version is visible when its creating transaction had committed before the snapshot's boundary, and its replacing transaction either does not exist, had not committed by that boundary, or was still running when the snapshot was taken. A version created by an aborted transaction is not visible.
The database applies special rules for the transaction performing the read—for example, a transaction generally needs to see its own completed writes—but the same boundary comparison is the core idea.
A concurrent update, step by step
Suppose the table contains this row:
id | balance
---+--------
7 | 100
Transaction A starts a report and takes a snapshot. Transaction B then runs:
UPDATE accounts
SET balance = 125
WHERE id = 7;
B creates a newer tuple version with balance = 125 and records B's transaction ID as the creator. The old version still represents balance = 100; it is associated with B as the transaction that replaced it.
If A's snapshot was taken before B committed, A continues to see the old version. From A's point of view, B was still in progress or had not yet entered the snapshot, so B's replacement is not visible. A gets a stable answer even though B is updating the same logical row.
A new query that takes its snapshot after B commits sees the new version and not the old one. No query needs to observe a half-written row.
This does not mean every query in every isolation mode keeps one snapshot forever. Under read committed, a common isolation level where each statement sees a committed view from when that statement began, two statements in one transaction can take different snapshots. Under repeatable read, the transaction keeps one snapshot for its reads, so later statements continue to use the same view. The exact names and guarantees vary slightly by database, but the snapshot lifetime is an important part of the behavior.
Why old versions do not remain forever
Old tuple versions need cleanup. A background process such as PostgreSQL's vacuum—maintenance that reclaims row versions no longer needed by any possible snapshot—can eventually remove them.
It cannot safely remove a version merely because the newest version exists. A long-running query may still have a snapshot that can see the old one. Once the database knows that no active snapshot can reach that version, cleanup can reclaim its space.
This explains a common operational symptom: a long-running transaction can prevent cleanup, causing a table to retain old row versions and grow larger than expected.
What the snapshot guarantees—and what it does not
An MVCC snapshot gives a reader a consistent visibility rule. It does not freeze the database, block all writers, or guarantee that a multi-step business decision is safe from every concurrent change. Higher isolation levels may add conflict detection, and applications may still need explicit locks or constraints for operations that must be mutually exclusive.
When an incident report mentions a snapshot, transaction IDs, an old row version, or vacuum being unable to clean up, it is describing this mechanism: the database preserves multiple versions, and each reader selects the versions allowed by its transaction-ID snapshot.