How does an MVCC database's background cleanup work off the backlog left by a six-hour report and an all-morning idle transaction, and what space comes back?
answer
- garbage at once, freed gradually
- backlog is churn times stall length
- workers queue; the throttle stretches it
- purge history drains oldest-first
- reused, not returned; indexes stay sparse
basics
~20 sEvery version superseded during the stall becomes garbage at once, but cleanup must still visit each table or walk the long undo history, costing hours of extra I/O and log. Space is reused in place, not returned.
solid answer
~50 sWhen both sessions end, the horizon jumps forward and everything superseded during the stall, on every table, becomes garbage at the same moment. Nothing is freed yet: cleanup still has to reach it. - **Vacuum-style engines** have many tables over their thresholds at once, so workers queue; each pass scans the bloated table and every index, and a throttled budget can stretch the catch-up over hours. - **Purge-style engines** work through hours of undo history, oldest first. Until purge reaches them, delete-marked rows and stale index entries stay in place, so scans keep stepping over records that should be gone. The catch-up writes pages and log, so expect checkpoint pressure, replica lag and archive growth. What comes back: space reusable inside tables, undo freed for reuse, smaller undo files only where truncation runs; indexes stay sparse. Raise the cleanup budget off-peak, let it drain, re-measure, then decide on rewrites.
code
pseudocode · 15 lines-- sizing the catch-up after a stall
churn_per_sec := 2000 -- row updates + deletes
stall_seconds := 6 * 3600
backlog := churn_per_sec * stall_seconds -- about 43.2M dead versions
reclaim_rate := versions reclaimed per second at the current cleanup budget
net_drain := reclaim_rate - churn_per_sec -- new garbage keeps arriving
if net_drain <= 0:
stop: cleanup can never catch up; raise its budget first
drain_hours := backlog / net_drain / 3600
if drain_hours > off_peak_window_hours:
raise the cleanup budget for the window
watch foreground latency and replica lag
when backlog is near zero:
re-measure table and index size against live data, then decide on rebuildsgo deeper
Know that ending a long transaction lets cleanup start reclaiming, that the work still takes time, and that the freed space is reused rather than returned to the disk.
Explain how each engine family works off the backlog, queued vacuum passes over bloated tables versus purge draining its history, and what stays behind until it does.
Size the backlog from churn and stall length, weigh the cleanup budget against foreground latency and replica lag, and sequence any rebuild after the drain and a fresh measurement.
Treat the drain as the visible bill for unbounded transactions: quantify it after each incident and use it to justify lifetime limits and cleanup capacity for the worst case.
## What the stall left behind While the report and the idle transaction were open, they held the **cleanup horizon**, the oldest point any running snapshot might still read, at the moment they began. Every row version superseded after that point had to be kept, on every table, because the engine cannot know which rows an open transaction will read next. Background cleanup kept running and kept finding nothing it was allowed to remove. The size of the backlog is roughly the **churn rate times the stall duration**. A system doing 2,000 row updates and deletes per second for six hours has about 43 million superseded versions waiting, plus, in engines that index every version, an entry per version in every affected index. ## The moment it ends When the last of the two sessions finishes, the horizon jumps forward to the next-oldest snapshot. All 43 million versions become garbage **at the same instant**, but none of them is freed by that event. Reclamation is background work, and it now has hours of history to process. How it does so depends on where the engine keeps old versions. ## Vacuum-style engines: versions in the table - **Many tables qualify at once.** Every busy table crossed its cleanup threshold during the stall and still qualifies, so they all compete for a small pool of workers. Tables queue, and a large table can hold a worker for a long time. - **Each pass is expensive.** The worker scans the table pages, collects dead row identifiers and removes their entries from every index before freeing slots. Tables that bloated during the stall are now larger, so each pass reads more than it would have if cleanup had kept pace. - **Throttling stretches it out.** Cleanup is deliberately rate-limited so it does not starve user queries. Against a large backlog, the default budget can turn the catch-up into hours. - **Freezing catches up too.** Engines with finite transaction identifiers also freeze old versions so their stamps can be reused, and a held horizon delays that work, so some of the first passes afterwards are heavier, table-wide ones. ## Purge-style engines: versions in undo - **One long queue.** Committed transactions' undo waits in commit order, and purge works through it oldest-first. The backlog is visible as the length of the not-yet-purged history, which should now start falling. - **Leftovers until purge arrives.** Deleted rows were only delete-marked, and updates to indexed columns left marked secondary-index entries. Until purge reaches them, scans and index lookups keep stepping over records that should be gone, so performance recovers gradually rather than at once. - **Undo frees in stretches.** Undo space is released once purge has processed a whole stretch of it, and the undo files themselves shrink only where the engine truncates undo storage. ## The cost of catching up Cleanup is real I/O and real logging. For the duration of the drain, expect: 1. extra read and write I/O competing with the foreground workload; 2. a burst of write-ahead or redo log, which raises checkpoint pressure, **replica lag** and the volume of archived log; 3. buffer-cache churn as cleanup reads cold, bloated pages. The trade is direct: a bigger cleanup budget drains faster and hurts foreground latency more; a smaller one is gentler and leaves the system degraded for longer. Raising the budget temporarily during an off-peak window is often the right call. ## What space actually comes back | Where | What the catch-up returns | |---|---| | Table pages | Dead space becomes **reusable** inside the table; the file shrinks only if its trailing pages are empty | | Indexes | Dead entries are removed, but half-empty pages are not merged, so indexes stay larger than needed | | Undo | Space is freed for reuse; undo files shrink only where truncation runs | | Disk overall | Mostly unchanged; tables stop growing and absorb future writes | So the honest expectation after a six-hour stall is that performance recovers as cleanup drains, growth stops, and disk usage stays near its peak. ## Operating the drain - **Watch the backlog fall**: dead-version estimates per table, or the not-yet-purged history length. - **Do not cancel cleanup** because it is busy; it is doing the work the stall deferred. - **Do not start a full rewrite during the drain.** It competes for the same I/O, and a blocking rewrite takes an exclusive lock. Re-measure once the backlog is gone, and only then decide whether an index rebuild or an online reorganisation is worth it. - **Record the stall's cost**: backlog size, drain time, peak replica lag. That is the evidence for bounding transaction lifetimes so it does not happen again.
- Why does the system often stay slow for a while after the long transactions end?Because ending them only makes the old versions eligible. Cleanup still has to visit every bloated table and index, or work through the purge history, and until it does, scans step over dead rows and stale index entries. The catch-up also competes for I/O and log bandwidth, so the foreground pays twice: once for the leftovers and once for the cleanup removing them.
- Would you run a full table rewrite as soon as the stall ends, to get the disk back quickly?No. During the drain a rewrite competes with cleanup for the same I/O, and a blocking rewrite holds an exclusive lock on a table the workload needs. Most of the freed space will be reused anyway. Let cleanup finish, re-measure size against live data, then rebuild bloated indexes or reorganise online only where the gap is large and will not refill.
- Why can replica lag spike during the catch-up even though the application's write rate is unchanged?Cleanup itself writes. Freeing slots, removing index entries, freezing versions and purging undo all modify pages and generate log, and replicas must replay that log. A catch-up compresses hours of deferred cleanup into a short window, so log volume rises well above normal and replay falls behind until the drain finishes.
It is a post office reopening after a six-hour closure: every queued parcel is suddenly deliverable, but the same few vans still have to carry them, and the sorting hall stays as large as the pile once was.
- Expecting dead versions to disappear the moment the long transaction ends
- Expecting disk usage to drop back once cleanup catches up
- Cancelling a busy cleanup worker to protect foreground latency
- Starting a blocking full rewrite while the backlog is still draining
- Ignoring the log volume cleanup generates and the replica lag it causes
context
- Q-08897How 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?level: middleimportance: must knowfreq 45%