skip to content

In an MVCC database, which superseded row versions must the engine keep for transactions still running, and which can it treat as garbage?

level: juniorimportance: must knowfreq 55%

answer

  1. a snapshot sees one version per row
  2. created before, superseded after
  3. the oldest snapshot sets the horizon
  4. kept even if nobody reads it
  5. rolled-back versions need no protection

basics

~20 s

A snapshot needs the version of each row that was current when the snapshot was taken. Engines keep every version superseded since the oldest running snapshot began, and treat older superseded versions, and rolled-back ones, as garbage.

solid answer

~50 s

Under MVCC an update creates a new version of a row and a delete marks the current one dead, but the old version stays, because a **snapshot** taken earlier may still need it. A snapshot sees, for each row, the version that was committed and current when it was taken: once per transaction under REPEATABLE READ or SERIALIZABLE, once per statement under READ COMMITTED. Tracking every snapshot's needs separately would be expensive, so engines use one conservative bound, the **horizon**: the oldest snapshot still in use. A version superseded by a transaction that committed *before* the horizon is invisible to every current and future snapshot, so it is garbage. Anything superseded *after* it is kept, even an intermediate version no running snapshot happens to read. A rolled-back transaction's versions were never visible to anyone, so no snapshot protects them. When the oldest snapshot ends, the horizon moves forward.

code

pseudocode · 9 lines
pseudocode
-- may this superseded version be discarded?
horizon := oldest snapshot still in use in the instance

function is_garbage(version):
    if version.creator rolled back:
        return true                  -- never visible to anyone
    if version.superseder is none or not committed:
        return false                 -- still the current version
    return committed_before(version.superseder, horizon)

go deeper

for a junior

Be ready to state the rule: a snapshot needs the version that was current when it was taken, and the engine keeps everything superseded since the oldest running snapshot began.

for a middle

Explain the horizon as one conservative bound, why it keeps intermediate versions nobody reads, and how READ COMMITTED and REPEATABLE READ change how long a snapshot lives.

for a senior

Connect the rule to production: the horizon is instance-wide, so one long snapshot protects history for every table, which makes snapshot age the first thing to check when versions pile up.

for a principal

Treat the horizon as a shared resource the platform must bound, and decide how long any snapshot may live and which workloads may hold one.

## Why old versions exist at all **Multi-version concurrency control (MVCC)** lets readers and writers work at the same time without blocking each other. Instead of overwriting a row, an `UPDATE` produces a **new version** and leaves the previous one in place, in the table's pages or in a separate undo area depending on the engine. A `DELETE` marks the current version as dead from its commit onward. Each reader works from a **snapshot**: a record of which transactions had committed at the moment it was taken. For every row, the reader is shown the version that was the committed, current one as of its snapshot. When a snapshot is taken depends on the isolation level: - under **READ COMMITTED**, each statement takes a fresh snapshot, so a snapshot lives as long as one statement; - under **REPEATABLE READ** and **SERIALIZABLE**, one snapshot serves the whole transaction, so it lives until commit or rollback. ## Which version a snapshot needs A superseded version is needed by a snapshot exactly when it was that snapshot's visible version: the transaction that **created** it had committed before the snapshot was taken, and the transaction that **superseded** it had not. Each snapshot needs at most one version of each row. ## The horizon: one conservative bound Checking every version against every running snapshot would be expensive, so engines compute a single bound, usually called the **cleanup horizon**: the oldest snapshot, or oldest running transaction, still in use anywhere in the instance. The rule follows directly: 1. A version superseded by a transaction that **committed before the horizon** is invisible to every running snapshot, and every future snapshot will see something newer. It is **garbage**. 2. A version superseded by a transaction that **had not committed when the oldest snapshot was taken** might be some running snapshot's visible version, so it is **kept**. A version whose replacement is still uncommitted is still the current version for everyone else. 3. A version written by a transaction that **rolled back** was never visible to anyone, so no snapshot protects it. The bound is deliberately conservative. It is cheap to compute and can never discard something still needed, at the cost of sometimes keeping versions that nobody actually reads. ## A worked timeline | Time | Event | Versions of row 42 | |---|---|---| | 09:00 | Report R starts a REPEATABLE READ transaction | v1 is current | | 09:05 | An update commits | v2 current; v1 superseded | | 09:10 | Another update commits | v3 current; v2 superseded | | 09:15 | R is still open | v1, v2 and v3 all kept | | 09:20 | R commits | v1 and v2 become garbage | At 09:15, R needs **v1** and new snapshots need **v3**. No running snapshot reads **v2**, yet it is kept, because it was superseded after the horizon, which R holds at 09:00. When R ends, the horizon moves forward to the next-oldest snapshot, and both old versions fall behind it. ## Why the engine cannot discard at commit The committing transaction has no cheap way to know whether some running snapshot still needs the version it just replaced, and checking would put coordination work on every write. So engines defer the decision to the horizon test and let background cleanup reclaim versions later. Two consequences are worth knowing from the start: - the horizon belongs to the **whole instance**, not to one table, because a running transaction may read any table next; - a snapshot that stays open for a long time holds the horizon back, so superseded versions keep piling up behind it. ## What to remember A snapshot needs one version per row; the engine protects everything superseded since the oldest running snapshot was taken; and the horizon moves only when that oldest snapshot ends.

  • Why does the engine keep a version that no running snapshot actually reads?
    Because it does not track each snapshot's needs. It keeps one bound, the oldest snapshot in use, and protects everything superseded after it. Proving that an intermediate version is unread would mean checking it against every running snapshot, which costs more than the space the version occupies, so the conservative rule wins.
  • Under READ COMMITTED, how long does a single statement protect old versions?
    For as long as that statement runs. Each statement takes its own snapshot and releases it when it finishes, so a five-second query protects history for five seconds, while a two-hour export run as one statement protects it for two hours. Under REPEATABLE READ the protection lasts for the whole transaction instead.
  • What happens to the kept versions when the oldest snapshot finally ends?
    The horizon moves forward to the next-oldest snapshot still in use, and every version superseded before that new point becomes garbage at once. Nothing is freed at that instant, though: background cleanup still has to visit and reclaim those versions, so the backlog clears over the following passes rather than immediately.

It is a library that cannot ask readers which edition of a timetable they hold, so it keeps every edition withdrawn since its earliest still-seated reader arrived, and bins them only after that reader leaves.

  • Saying the old version is discarded as soon as the UPDATE commits
  • Thinking each table has its own horizon set by its own readers
  • Believing the engine keeps only versions some snapshot actually reads
  • Assuming a read-only transaction never protects old versions
  • Treating a rolled-back transaction's versions as protected history

context