Lesson 2 of 9 · 26m
Vacuum Internals: MVCC, Bloat, and Autovacuum Tuning
Why PostgreSQL never updates rows in place, how dead tuples pile up, what visibility maps and freezing do, and how to tune autovacuum before it becomes the incident.
A table that accepts updates is never finished writing those rows. PostgreSQL implements MVCC by creating a new row version and leaving the old one behind until no transaction can still see it. That is why readers do not block writers — and why a small hot table can become a 200 GB archaeology dig if vacuum falls behind.
The model to remember: dead tuples are garbage, vacuum is garbage collection, and long-running transactions pin garbage in place.
Updates are insert plus delete
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 42;
-- the old row version remains visible to older snapshots
COMMIT;Every tuple carries transaction visibility metadata (xmin and xmax). The old version cannot be removed merely because you committed; it can be removed only when no active snapshot can still see it.
SELECT pid, state, age(backend_xmin) AS xmin_age, now() - xact_start AS xact_age, query
FROM pg_stat_activity WHERE backend_xmin IS NOT NULL ORDER BY age(backend_xmin) DESC;A very old backend_xmin means vacuum may scan constantly and still be unable to reclaim newer dead tuples.
Autovacuum is a pressure system
Autovacuum workers wake up based on table activity:
autovacuum = on
autovacuum_naptime = 15s
autovacuum_vacuum_scale_factor = 0.1
autovacuum_vacuum_threshold = 500
autovacuum_vacuum_cost_delay = 2msA 100-million-row table at scale factor 0.1 may wait for 10 million dead tuples before vacuum starts. Tune hot large tables directly instead of changing the whole cluster's personality:
ALTER TABLE orders SET (autovacuum_vacuum_scale_factor = 0.02, autovacuum_vacuum_cost_delay = 2);Read the health signals
SELECT relname, n_dead_tup, n_live_tup, last_autovacuum, last_autoanalyze
FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 10;n_dead_tup is an estimate, but it is the right first dashboard. High dead tuples plus no recent vacuum means the table did not trigger, vacuum cannot finish, throttling is too aggressive, or an old snapshot is pinning rows.
Freezing cannot be postponed forever
Transaction IDs are 32-bit and compared modulo wraparound. Very old tuples must be frozen so future transactions interpret them correctly. autovacuum_freeze_max_age triggers aggressive anti-wraparound vacuum even when a table looks calm. Watch age(datfrozenxid) in pg_database and age(relfrozenxid) in pg_class.
Field notes
- Ordinary
VACUUMmakes space reusable inside PostgreSQL; it usually does not return space to the operating system. VACUUM FULLrewrites the table with an exclusive lock. It is a maintenance-window tool.- Indexes bloat independently of their tables.
- Bulk loads often deserve a manual
ANALYZEeven when autovacuum is healthy. - The correct response to runaway bloat is often killing the transaction pinning
xmin, not merely vacuuming harder.
Was this lesson helpful?