MVCC and VACUUM: Synthesis and Self-Test
Synthesis
MVCC → VACUUM: Lock-free reads are paid for by background maintenance
The entire point of MVCC is that readers never block writers and vice versa. But that guarantee has a hidden cost: old tuple versions accumulate indefinitely on disk until VACUUM reclaims them. VACUUM is not optional cleanup — it is the structural counterpart that makes MVCC practical. Disable or neglect autovacuum on a write-heavy table and you have not gained read freedom; you have deferred a growing bill that eventually manifests as table bloat, slow scans, and wasted I/O budget. Every environment with MVCC needs a companion maintenance system — Postgres chose VACUUM.
Long Transactions → Dead tuples and MVCC, Transaction ID Wraparound: One open transaction poisons the entire cluster
A long-running analytics query or forgotten BEGIN simultaneously causes three failures: (1) VACUUM cannot reclaim any dead tuple with xmax newer than the open transaction's xmin — dead tuples accumulate cluster-wide (Dead tuples and MVCC: bloat). (2) Pages fill with unreclaimable dead tuples; sequential scans and index lookups degrade (Dead tuples and MVCC: performance). (3) VACUUM cannot advance relfrozenxid on any table covered by the snapshot — XID age creeps toward the wraparound threshold (Transaction ID Wraparound: risk). A single idle-in-transaction session is therefore a multi-vector attack on cluster health. Set idle_in_transaction_session_timeout = '10min' and alert on pg_stat_activity queries older than 30 minutes.
VACUUM and Autovacuum + Transaction ID Wraparound: VACUUM, Freeze, and write throughput compete for the same I/O budget
Autovacuum is deliberately throttled to avoid starving application queries. But on a high-write workload, dead tuples accumulate faster than autovacuum can reclaim them. The freeze horizon ages because autovacuum cannot complete full freeze passes. relfrozenxid falls further behind, XID age grows, and the cluster edges toward a wraparound shutdown — not from a burst, but from sustained underperformance of background maintenance.
High write throughput
→ dead tuples faster than autovacuum cost-limit allows
→ freeze passes incomplete
→ relfrozenxid advances slowly
→ age(datfrozenxid) climbs
→ XID wraparound risk (Transaction ID Wraparound)Teams running Postgres at scale routinely tune autovacuum aggressiveness per table, monitor XID age in dashboards (not just "is autovacuum on?"), and run manual VACUUM FREEZE during maintenance windows on the highest-age tables.
Autovacuum → Planner: The same bottleneck starves both cleanup and statistics
Autovacuum does two jobs on the same throttled budget: reclaiming dead tuples and running ANALYZE to refresh pg_statistic. A table falling behind on VACUUM (see VACUUM and Autovacuum) is very often also falling behind on ANALYZE — the planner's row-count estimates go stale at exactly the same time the table is bloating. A planner that thinks a table has 50 rows when it actually has 500K will pick catastrophically wrong join strategies (see Reading the planner) against the exact tables already under maintenance pressure. This is also why a partial index — see the Index Types guide — can silently stop being chosen: it's only selected when the planner's estimate of predicate selectivity is accurate, and stale statistics break that precondition.
Self-Test
Question 1
You run UPDATE users SET last_login = now() WHERE id = 42. How many tuples now exist on disk for this row, and what are the xmin and xmax values on each? Why doesn't Postgres just overwrite the original tuple?
Show answer
Two tuples exist. The original tuple has xmin = <original inserting XID> and xmax = <XID of the UPDATE transaction> — it is dead. The new tuple has xmin = <XID of the UPDATE transaction> and xmax = 0 — it is live.
Postgres cannot overwrite in-place because concurrent transactions may still need to read the old version (MVCC visibility rule). A transaction that started before the UPDATE and holds a snapshot at the pre-UPDATE XID must see balance = old value, not last_login = now(). Overwriting would destroy that snapshot — reads would then require locks, eliminating the entire benefit of MVCC.
Question 2
A table has 5 million live rows with default autovacuum settings. (a) At what dead-tuple count does autovacuum trigger? (b) What GUC controls the percentage threshold? (c) You change it to 1% per-table — what is the new trigger count, and what SQL accomplishes this?
Show answer
(a) 50 + 0.2 × 5,000,000 = 1,000,050 dead tuples.
(b) autovacuum_vacuum_scale_factor (default 0.2 = 20%).
(c) New trigger: 50 + 0.01 × 5,000,000 = 50,050 dead tuples. SQL:
ALTER TABLE users SET (
autovacuum_vacuum_scale_factor = 0.01,
autovacuum_vacuum_threshold = 1000
);
This triggers autovacuum 20× more aggressively, critical for a high-churn table like users with frequent last_login updates.
Question 3
Your monitoring shows age(datfrozenxid) at 1.8 billion for one database. (a) What happens at ~2.1 billion? (b) A colleague notices the culprit table has n_dead_tup = 0 and says "autovacuum has been running fine." Why are they wrong? (c) What is the emergency mitigation?
Show answer
(a) At ~2.1 billion (~2^31), old tuple xmin values wrap around and become mathematically "in the future" from the current XID's perspective. Every row in those pages becomes invisible — the database enters a protective read-only shutdown and refuses all new transactions.
(b) The colleague is wrong because n_dead_tup = 0 only means autovacuum has reclaimed dead tuples. It says nothing about freeze passes. XID age is determined by age(relfrozenxid), not dead-tuple count. A read-heavy table or a cold reference table with zero writes never gets dead tuples — but its XID age still advances as other transactions increment the cluster XID counter.
(c) Emergency: shut down all connections, start Postgres in single-user mode, run VACUUM FREEZE on the highest-age tables, restart. Prevention: alert when age(datfrozenxid) > 1,500,000,000 and run VACUUM FREEZE during a maintenance window.
Question 4
EXPLAIN ANALYZE shows rows=10 actual rows=80,000 on a Seq Scan. Name three root causes and the corresponding fix for each. Then explain why this misestimate specifically can cause an index to be ignored even when one exists.
Show answer
Three root causes and fixes:
- Stale statistics —
pg_statisticnot rebuilt after large data changes. Fix:ANALYZE table;or lowerautovacuum_analyze_scale_factor. - Low statistics target on a skewed column — the histogram has insufficient resolution to represent the distribution. Fix:
ALTER TABLE t ALTER COLUMN c SET STATISTICS 500; ANALYZE t; - Correlated columns — planner multiplies selectivities of independent predicates, underestimating result count when columns co-vary. Fix:
CREATE STATISTICS s ON col_a, col_b FROM t; ANALYZE t;
Why this ignores the index: If the planner believes only 10 rows qualify, an Index Scan looks cheap (10 random lookups). If 80,000 rows actually qualify, those are 80,000 random heap fetches vs. one sequential table scan. But the planner's cost model, based on the underestimate, calculates the index as cheaper and chooses it — causing catastrophically worse performance than a Seq Scan would have been.
Related pages
- Index Types → — B-tree, GIN, GiST, BRIN, and partial indexes — the mechanisms the planner in the Reading the planner part is choosing between.