skip to content

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:

sqldb=lab · session av
SHOW autovacuum;
SELECT backend_type, count(*) FROM pg_stat_activity GROUP BY backend_type ORDER BY backend_type;
output, from postgres 18.6
#1 · SHOW
autovacuumtext
on
(1 row)
#2 · SELECT 1
backend_typetextcountint8
standalone backend1
(1 row)

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_typetextcountint8
autovacuum launcher1
background writer1
checkpointer1
client backend2
io worker3
logical replication launcher1
walwriter1
(7 rows)
real output, a postgres 18.6 server · autovacuum
the processes of a real server, from pg_stat_activity. the autovacuum launcher is one of them; its workers come and go.

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:

text
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.

rows =
1 million
scale_factor =
dead rows before a vacuum
200,050
inserted rows before a vacuum
201,000
changed rows before an analyze
100,050
live rows, and dead ones waitingdead = 20% of the live rows

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.

model: the trigger formulas from the postgres 18 docs, defaults as a postgres 18.6 server reports them; the insert trigger assumes no page is frozen yet
slide the table size. the dead share of the table before a worker comes is the same 20% for any big table, until postgres 18's cap kicks in. try the smaller scale factors, the usual fix for big tables.
predict the output
postgres 18, default settings, a 1,000,000,000-row table. how many dead rows before autovacuum vacuums it?
text
50 + 0.2 × 1,000,000,000 = 200,000,050

watching 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:

when =
stats.sql
  1. 1DELETE FROM t WHERE id <= 300;
  2. 2SELECT pg_stat_force_next_flush();
  3. 3SELECT n_dead_tup, autovacuum_count FROM pg_stat_user_tables WHERE relname = 't';
the result for right after the DELETE
n_dead_tupint8autovacuum_countint8
3000
(1 row)

delete 300 rows: 50 past the threshold. the statistics system counts them as dead.

real output, a postgres 18.6 server · autovacuum
switch moments. the statistics count 300 dead rows; a few seconds later a worker has been and gone: 0 dead, and autovacuum_count is 1. nobody typed VACUUM.

and the worker's own report, from the server log (with log_autovacuum_min_duration = 0 on the table):

  1. LOG: automatic vacuum of table "postgres.public.t": index scans: 1
  2. pages: 0 removed, 5 remain, 5 scanned (100.00% of total), 0 eagerly scanned
  3. tuples: 300 removed, 700 remain, 0 are dead but not yet removable
  4. removable cutoff: 761, which was 0 XIDs old when operation ended
  5. frozen: 0 pages from table (0.00% of total) had 0 tuples frozen
  6. visibility map: 2 pages set all-visible, 1 pages set all-frozen (0 were all-visible)
  7. index scan needed: 2 pages from table (40.00% of total) had 300 dead item identifiers removed
  8. index "t_pkey": pages: 5 in total, 0 newly deleted, 0 currently deleted, 0 reusable
  9. avg read rate: 0.000 MB/s, avg write rate: 0.000 MB/s
  10. buffer usage: 75 hits, 0 reads, 0 dirtied
  11. WAL usage: 9 records, 0 full page images, 2828 bytes, 0 buffers full
real output, a postgres 18.6 server · autovacuum
the worker's log line. the same report VACUUM VERBOSE prints, as a LOG line. 300 removed, two pages newly all-visible and one all-frozen, and the index cleaned.

in the browser, the dead rows just stay. the counter is there; nobody reads it:

sqldb=lab · session av
continues session av (1 earlier block run first)
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';
output, from postgres 18.6
#1 · DELETE 100
#2 · SELECT 1
pg_stat_force_next_flushvoid
''
(1 row)
#3 · SELECT 1
n_dead_tupint8last_autovacuumtimestamptzautovacuum_countint8
100null0
(1 row)

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.

pages found in shared buffers200 pages
pages read from disk100 pages
pages dirtied10 pages
real output, a postgres 18.6 server · vacuum-settings
pages a worker can process between two 2 ms naps, by what it does to them. arithmetic on the defaults a real server reported, not a measurement. a manual VACUUM has vacuum_cost_delay = 0: it never naps.

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:
sqldb=lab · session av
continues session av (2 earlier blocks run first)
ALTER TABLE notes SET (autovacuum_vacuum_scale_factor = 0.01, autovacuum_vacuum_threshold = 1000);
SELECT relname, reloptions FROM pg_class WHERE relname = 'notes';
output, from postgres 18.6
#1 · ALTER TABLE
#2 · SELECT 1
relnamenamereloptionstext[]
notes
(1 row)
  • the workers are busy. autovacuum_max_workers is 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 to autovacuum_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.