autovacuum
thresholds, scale factors, cost limits, and the busy table it never seems to reach.
- address
- everything.mvcc.autovacuum
- part
- part 14, MVCC and vacuum
- chapter
- chapter 8 of 12
- reading time
- 9 min
autovacuum is a background launcher that wakes up every autovacuum_naptime (a minute by default), checks every table's statistics, and starts a worker for each table past its threshold: 50 dead rows plus 20% of the table, by default. workers run slowly on purpose, against a cost limit, so they don't swamp the disk. it doesn't exist in this page's browser engine.
the picture
you know how VACUUM is a command you have to run? nobody runs it by hand on a real server. something has to notice that a table has collected enough dead rows, and vacuum it without anyone asking, without hogging the disk, and without ever blocking the application.
think of a building's cleaning crew. a supervisor (the launcher) walks the floors every so often and checks each room's tally of mess (the statistics). rooms past their limit get a cleaner (a worker). cleaners work at a steady pace, stopping for a breather after every so many bins (the cost limit), so the building stays usable while they work. a big hall needs a big mess before anyone is sent, because the limit grows with the room.
first, honesty about this page. the engine running your queries is one process, and nothing runs in the background:
SHOW autovacuum;
SELECT backend_type, count(*) FROM pg_stat_activity GROUP BY backend_type ORDER BY backend_type;| autovacuumtext |
|---|
| on |
| backend_typetext | countint8 |
|---|---|
| standalone backend | 1 |
the setting says on, and there's nobody to do it: one standalone backend, that's all. a real postgres 18.6 server, captured:
| backend_typetext | countint8 |
|---|---|
| autovacuum launcher | 1 |
| background writer | 1 |
| checkpointer | 1 |
| client backend | 2 |
| io worker | 3 |
| logical replication launcher | 1 |
| walwriter | 1 |
everything below about autovacuum itself is captured on a native server, or read from one.
the trigger
each round, for each table, the launcher compares the statistics system's count of dead rows (n_dead_tup) with:
vacuum threshold = min(autovacuum_vacuum_max_threshold,
autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor × reltuples)the defaults are 50 and 0.2, so 50 dead rows plus a fifth of the table. the cap is new in postgres 18 pg 18+, at 100,000,000. there are two more triggers on the same pattern: inserted rows (so insert-only tables still get vacuumed, for the visibility map and freezing), and changed rows for ANALYZE.
50 + 0.2 × 1,000,000 = 200,050. a fifth of the table has to be dead before a worker comes. the bigger the table, the more rows that is.
50 + 0.2 × 1,000,000,000 = 200,000,050watching it work
this capture switches autovacuum on with a one-second nap. (server-wide thresholds are pushed out of reach so the catalogs are left alone and the run repeats exactly; the table gets the default threshold back as a storage parameter.) a 1,000-row table has a threshold of 250. delete 300 rows, wait:
- 1DELETE FROM t WHERE id <= 300;
- 2SELECT pg_stat_force_next_flush();
- 3SELECT n_dead_tup, autovacuum_count FROM pg_stat_user_tables WHERE relname = 't';
| n_dead_tupint8 | autovacuum_countint8 |
|---|---|
| 300 | 0 |
delete 300 rows: 50 past the threshold. the statistics system counts them as dead.
and the worker's own report, from the server log (with log_autovacuum_min_duration = 0 on the table):
- LOG: automatic vacuum of table "postgres.public.t": index scans: 1
- pages: 0 removed, 5 remain, 5 scanned (100.00% of total), 0 eagerly scanned
- tuples: 300 removed, 700 remain, 0 are dead but not yet removable
- removable cutoff: 761, which was 0 XIDs old when operation ended
- frozen: 0 pages from table (0.00% of total) had 0 tuples frozen
- visibility map: 2 pages set all-visible, 1 pages set all-frozen (0 were all-visible)
- index scan needed: 2 pages from table (40.00% of total) had 300 dead item identifiers removed
- index "t_pkey": pages: 5 in total, 0 newly deleted, 0 currently deleted, 0 reusable
- avg read rate: 0.000 MB/s, avg write rate: 0.000 MB/s
- buffer usage: 75 hits, 0 reads, 0 dirtied
- WAL usage: 9 records, 0 full page images, 2828 bytes, 0 buffers full
in the browser, the dead rows just stay. the counter is there; nobody reads it:
DELETE FROM notes WHERE id <= 100;
SELECT pg_stat_force_next_flush();
SELECT n_dead_tup, last_autovacuum, autovacuum_count
FROM pg_stat_user_tables WHERE relname = 'notes';| pg_stat_force_next_flushvoid |
|---|
| '' |
| n_dead_tupint8 | last_autovacuumtimestamptz | autovacuum_countint8 |
|---|---|---|
| 100 | null | 0 |
going slowly on purpose
a worker charges itself for every page it touches: 1 for a page already in shared buffers, 2 for one it has to read, 20 for one it dirties. when the total reaches the cost limit (200, since autovacuum_vacuum_cost_limit is -1, meaning "use vacuum_cost_limit") it sleeps for autovacuum_vacuum_cost_delay, 2 ms. and the limit is shared: with several workers running, each gets a proportional slice of it.
that's the right default for a background chore on a shared machine, and the wrong one for a table that's falling behind. a worker that dirties every page it visits manages 10 pages per nap.
the busy table it never reaches
the usual ways autovacuum loses:
- the table is big. 20% of a big table is a lot of dead rows, and a scan of a big table at a polite pace takes a long time. lower the scale factor for that table:
ALTER TABLE notes SET (autovacuum_vacuum_scale_factor = 0.01, autovacuum_vacuum_threshold = 1000);
SELECT relname, reloptions FROM pg_class WHERE relname = 'notes';| relnamename | reloptionstext[] |
|---|---|
| notes |
- the workers are busy.
autovacuum_max_workersis 3. three huge tables can occupy all of them for hours while everything else waits. postgres 18 lets you raise it without a restart, up toautovacuum_worker_slots(16) pg 18+. - it's too polite. raise
autovacuum_vacuum_cost_limit(say to 1000 or 2000 on decent storage), or lower the delay. - it can't remove anything. a worker can be running constantly and still report "dead but not yet removable" for every row, because a forgotten transaction is holding the horizon back. no setting fixes that.
pg_stat_progress_vacuum shows a running vacuum's phase and how far through the heap it is. n_dead_tup, last_autovacuum and autovacuum_count in pg_stat_user_tables tell you whether it's keeping up.
keylines
- the launcher wakes every naptime; a table past 50 + 20% dead rows gets a worker.
- postgres 18 caps that at 100 million dead rows. inserts and changes for ANALYZE have triggers of their own.
- workers throttle themselves: page costs against a shared limit, then a short nap.
- big tables need a smaller scale factor. a stuck horizon needs a fixed transaction, not a setting.
- the browser engine has no autovacuum: every VACUUM on this site is one you can see.