How does an MVCC database's vacuum or purge worker find dead row versions older than the cleanup horizon, and what does it do with them?
answer
- same horizon test, different search
- counters pick the table, a map skips pages
- index entries go before slots
- purge walks undo in commit order
- reused in place; only the tail shrinks
basics
~20 sIn engines that keep versions in the table, vacuum scans pages, skipping known-clean ones, removes index entries for dead versions, then marks their space reusable. In undo-based engines, purge walks the undo history oldest-first, removing delete-marked records and freeing undo.
solid answer
~50 sBoth apply the horizon test first. **Vacuum (versions in the table's pages).** Work is found per table: the engine counts dead versions and starts a pass once a threshold is crossed. The worker scans the table, skipping pages that a per-page map, where the engine keeps one, marks as holding only versions visible to everyone, and collects the row ids of dead versions. It removes those ids from **every index** before freeing the table slots, so no index entry can point at a slot reused by another row. Freed space goes into a free-space map for reuse; only empty pages at the end of the file are returned. **Purge (versions in undo).** Committed transactions' undo is already a list of what changed, kept in commit order, so the worker walks it oldest-first up to the horizon, removes delete-marked rows and stale index entries, then frees the undo it has passed.
code
pseudocode · 15 lines-- one vacuum-style pass over a table
horizon := oldest snapshot in use
dead_ids := []
for page in table.pages:
if all_visible_map[page]: continue -- nothing to reclaim here
for v in page.versions:
if superseded_before(v, horizon): dead_ids.add(v.row_id)
if dead_ids.is_full():
for idx in table.indexes: idx.remove(dead_ids) -- index entries first
table.free_slots(dead_ids) -- then the slots
dead_ids.clear()
for idx in table.indexes: idx.remove(dead_ids)
table.free_slots(dead_ids)
free_space_map.update(table)
truncate_empty_tail(table)go deeper
Know that a background worker removes versions older than the horizon, that it marks space reusable rather than shrinking files, and that the two engine families clean in different places.
Explain how each family finds its work, per-table counters and page skipping versus walking undo in commit order, and why index entries are removed before table slots are freed.
Use the mechanics to diagnose: index count and cleanup memory set the cost of a pass, purge history length shows lag, and a pass that frees nothing points at the horizon.
Shape schema and settings around the cleanup cost model: fewer indexes on churned tables, per-table thresholds for large ones, and cleanup capacity sized for the worst churn period.
## The rule both workers apply Under **multi-version concurrency control (MVCC)**, an update or delete leaves the superseded row version behind for transactions with older snapshots. The engine tracks one bound, the **cleanup horizon**: the oldest snapshot still in use. A version superseded by a transaction that committed before the horizon can never be seen again, so it is garbage. The background worker that reclaims garbage reads the horizon when it starts and applies that test. What differs between engines is **where the garbage sits**, and therefore how the worker finds it. ## Vacuum-style: versions live in the table's pages In engines that store every version in the table itself, as heap-organised tables do, dead versions are scattered through the table's pages and its indexes. ### Finding the work - **Choosing a table.** The engine keeps per-table counters of inserted, updated and deleted rows. When the estimated number of dead versions crosses a threshold, typically a fixed number plus a fraction of the table's size, a worker is assigned to that table. - **Choosing pages.** Rather than read everything, some such engines keep a **per-page map** recording which pages hold only versions visible to every transaction. Those pages contain nothing to reclaim and are skipped, so a mostly static table is cheap to clean. - **Opportunistic pruning.** Some engines also tidy a single page when an ordinary query visits it and finds dead versions there, which removes part of the work before the worker arrives. ### Removing it: indexes first, then the table For each page it visits, the worker tests every version against the horizon and collects the **row identifiers** of the dead ones. It cannot simply free those slots, because index entries still point at them; if a new row reused a slot first, an old index entry could lead a query to the wrong row. So the order is: 1. collect dead row identifiers into a memory buffer; 2. scan **each index** on the table and delete the entries pointing at those identifiers; 3. return to the table pages and mark the slots free. If the buffer fills before the table is done, the worker runs step 2 for what it has and then continues, which means another full pass over every index. That is why a large table with many indexes and a small cleanup memory budget is slow to clean. ### What the pass gives back Freed space is recorded in a **free-space map**, so future inserts and updates reuse it. The file shrinks only if pages at its very end are completely empty, so that tail can be truncated. The worker also refreshes the table's dead-version counters and, in engines with finite transaction identifiers, may **freeze** old versions so their stamps can safely be reused later. ## Purge-style: versions live in undo In undo-based engines, the table holds the current version and superseded images live in **undo logs**; each committed transaction's undo joins a history list in commit order. The worker therefore does not search the tables: the history is already a list of exactly what changed. - The **purge worker** reads that history oldest-first and stops at the horizon, because records newer than that may still be needed to rebuild an old snapshot's view. - For a **delete**, which only marked the row, purge now removes the row from the clustered index and its entries from the secondary indexes. - For an **update that changed an indexed column**, the old secondary-index entry was marked rather than removed; purge removes it. - Once a stretch of undo is fully processed and older than the horizon, that undo space is **freed for reuse**, and where the engine supports undo truncation, the undo files can shrink. The backlog shows up as the length of the not-yet-purged history, which is a metric worth alerting on. ## Side by side | | Vacuum-style | Purge-style | |---|---|---| | Where garbage sits | Table pages and indexes | Undo logs, plus delete-marked rows and index entries | | How work is found | Per-table counters, then a page scan | Walking the history in commit order | | Cost scales with | Table and index size | Volume of change | | Space returned | Reused inside the table; only an empty tail is truncated | Undo reused; undo files shrink only if truncated | ## Why this matters in practice A worker that **runs but frees nothing** is obeying the horizon, not failing. A worker that frees things but **falls behind** has a capacity problem: thresholds too coarse for a huge table, too few workers, a throttled I/O budget, or many indexes multiplying every pass. Knowing which step does the work tells you which setting to change.
- Why must the worker remove index entries before it frees the table slots they point at?Because a freed slot can be reused by a new row. If an index entry for the old version still pointed at that slot, a lookup through the index would land on an unrelated row. Removing every index's entries first guarantees no pointer outlives its target, which is also why each extra index adds a full index scan to every cleanup pass.
- Why can a mostly static table with one hot region be cheap to vacuum?Because pages holding only versions visible to everyone are recorded in a per-page map, where the engine keeps one, and the worker skips them. Only pages changed since they were last marked clean are read, so the table-side cost tracks the hot region rather than the whole table. Index cleanup still scans each index, so many indexes erode the saving.
- Why does a purge-style worker not need to scan tables to find its work?Because every change already wrote an undo record, and committed transactions' undo is kept in commit order. The worker reads that history from the oldest end up to the horizon, so it visits exactly the rows that changed. Its cost tracks the volume of change, while a vacuum-style worker's cost tracks the size of the table and its indexes.
Vacuum is a cleaner walking every aisle of a warehouse, skipping aisles already ticked clean; purge is a clerk working through the returns ledger line by line, because the ledger already says what moved.
- Thinking the worker decides by the wall-clock age of a version
- Believing vacuum frees table slots before removing their index entries
- Assuming routine cleanup shrinks the table file
- Saying purge must scan every table to find dead versions
- Treating a pass that frees nothing as a broken worker
context
- Q-08899In a database that uses multi-version concurrency control (MVCC), deleting a million rows usually frees no disk space immediately. Why does the engine keep the old row versions, and what eventually removes them?level: juniorimportance: must knowfreq 60%
- Q-08997When a database takes a read snapshot for a transaction, what does that snapshot actually consist of, and why must it record the set of transactions that were still in flight at that instant rather than just a single cutoff number?level: middleimportance: must knowfreq 58%