MVCC Snapshots in SQL Databases
An MVCC snapshot is a transaction’s view of which committed row versions existed at a particular point in time. MVCC—multiversion concurrency control—lets a database keep several versions of a row so readers can use an older consistent version while a writer creates a newer one.
The snapshot is not a copied database. It is metadata and visibility rules that answer a question for each row version: may this transaction see this version?
The problem MVCC solves
Without MVCC, a database commonly coordinates readers and writers with locks. A reader might take a shared lock while inspecting a row; a writer would need an exclusive lock to change it. That can make a writer wait for every reader, and readers wait behind writes. Holding locks for the whole duration of a transaction can produce long queues and deadlocks.
There is also a correctness problem. Suppose a transaction reads half of a report while another transaction changes rows that the report has not read yet. The report could combine values from two different states of the database. Or it could read a value that another transaction has written but not committed, then depend on a change that later disappears.
MVCC addresses these problems by separating the version a reader can see from the version a writer is currently creating. A reader generally does not need to wait for an unrelated writer, and a writer generally does not need to wait for an unrelated reader.
A row becomes a chain of versions
When an MVCC database updates a row, it usually does not overwrite the only physical copy immediately. It creates a new row version and records enough information to relate it to the old one. A deletion similarly creates a version that means “this row is no longer present.”
A version carries database-internal information such as:
- which transaction created it;
- which transaction replaced or deleted it, if any; and
- links or pointers to older versions.
The exact representation differs by database. PostgreSQL stores transaction identifiers and version metadata with table rows. InnoDB, the storage engine used by MySQL, keeps related undo information. The important idea is the same: an update leaves a history that visibility checks can inspect.
Consider an account whose balance changes from 100 to 80. A transaction that started before the change may still find the version containing 100. A transaction that starts after the update commits may find 80. Neither needs to pretend that one physical row simultaneously has two values; each transaction follows the visibility rules for its own snapshot.
How a transaction chooses visible versions
At a snapshot boundary, the database records a description of transaction progress. In a typical implementation, that description can be thought of as:
- transactions that had already committed;
- transactions that were still active; and
- a cutoff beyond which transactions had not started yet.
The names and exact fields vary by database, but the decision usually follows this logic for each candidate version:
- A version created by a transaction that committed before the snapshot is visible.
- A version created by a transaction still active when the snapshot was taken is not visible, even if that transaction commits later.
- A version created after the snapshot is not visible.
- A transaction normally sees its own writes, so its newly inserted or updated versions are visible to itself.
- If a visible version was later replaced or deleted by a transaction that the snapshot can see, the older version is hidden and the newer state is considered instead.
The database checks these rules while locating rows, a process often called visibility checking. If the newest physical version is too new for the reader, the engine follows the version history until it finds an older version that the snapshot permits.
This is why a snapshot is a view, not a frozen copy. Two transactions can inspect the same stored row and select different versions from its history.
A transaction snapshot answers “which committed state am I allowed to observe?” It does not necessarily answer “which state is true right now?”
When the snapshot is taken
The timing depends on the isolation level and database. An isolation level is a database-defined set of rules describing what changes a transaction may observe when other transactions run concurrently.
At a commonly used READ COMMITTED level, each SQL statement may get a fresh snapshot. Two SELECT statements in one transaction can therefore see different committed results if another transaction commits between them.
At a snapshot-style REPEATABLE READ level, the transaction normally keeps one snapshot for its whole lifetime. Repeating a query sees the same committed versions, although the transaction may still encounter conflicts when it tries to write.
For example, the first statement might establish a snapshot like this:
BEGIN;
SELECT balance FROM accounts WHERE id = 42;
-- This statement's snapshot chooses the visible version.
-- Other transactions may commit changes here.
SELECT balance FROM accounts WHERE id = 42;
-- Under repeatable-read semantics, this sees the same snapshot.
The precise behavior of READ COMMITTED, REPEATABLE READ, and SERIALIZABLE differs between PostgreSQL, MySQL, SQL Server, and other systems. The snapshot-selection idea is shared, but details such as statement timing, locking reads, and conflict handling are not interchangeable.
Why readers and writers can overlap
A normal snapshot read can use an old committed version while a concurrent transaction writes a new version. The reader does not have to wait for that write, and the writer does not have to overwrite data that the reader is currently using.
This does not mean all operations are lock-free. Two transactions updating the same row still need coordination so one update does not silently destroy the other. Foreign-key checks, unique indexes, explicit SELECT ... FOR UPDATE reads, and schema changes can also wait for locks. MVCC primarily removes the need for ordinary readers and writers to block each other over already-committed row versions.
Snapshots also do not automatically prevent every logical race. Write skew is a race where two transactions read the same condition, update different rows, and together violate a rule. For example, two doctors might each see that another doctor is on call, then each remove themselves, leaving nobody on call. A serializable isolation level may detect this dependency and abort one transaction; weaker levels may allow it.
Cleanup and the clues you may have seen
Old versions cannot be discarded while an active snapshot might still need them. A long-running transaction can therefore prevent cleanup, causing table or undo storage to grow and making visibility checks more expensive. PostgreSQL’s VACUUM, InnoDB history-list growth, and warnings about long-running transactions are often symptoms of this relationship.
When an incident report mentions a transaction snapshot, “tuple visibility,” or a serialization failure, it is describing these same rules: the database is deciding which row history belongs in a transaction’s view, and whether concurrent work can safely coexist with that view.