PostgreSQL autovacuum explained, and how to tell when it's falling behind
Why PostgreSQL needs VACUUM at all, how autovacuum decides when to run, and the per-table settings that keep big tables healthy.
In PostgreSQL, an UPDATE doesn't change a row in place. It writes a new version and marks the old one as dead. DELETE marks rows dead too. Those dead tuples stay in the table until VACUUM cleans them up so the space can be reused. If vacuum can't keep up, tables bloat, queries read more pages than they need to, and in the worst case you head towards transaction ID wraparound.
When does autovacuum run on a table?
Autovacuum vacuums a table when its dead tuples exceed:
autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor × number of rows
With the defaults (50 and 0.2), that's 50 rows plus 20% of the table. For a 1,000-row table that's 250 dead rows, which is fine. For a 50-million-row table it's 10 million dead rows before anything happens, and that's usually far too late.
Statistics for the planner (ANALYZE) follow the same pattern with autovacuum_analyze_scale_factor (default 0.1).
Is it keeping up?
SELECT relname, n_live_tup, n_dead_tup,
round(100.0 * n_dead_tup / nullif(n_live_tup + n_dead_tup, 0), 1) AS dead_pct,
last_autovacuum, last_autoanalyze, n_mod_since_analyze
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC LIMIT 20;
Warning signs:
- Large tables with more than 10–20% dead tuples.
last_autovacuumempty or days old on a busy table.n_mod_since_analyzemuch larger than 10% of the table, so the planner works from stale statistics.
Fix big tables with per-table settings
Rather than changing the global defaults, lower the thresholds on the tables that need it:
ALTER TABLE events SET (autovacuum_vacuum_scale_factor = 0.02, autovacuum_vacuum_threshold = 1000);
ALTER TABLE events SET (autovacuum_analyze_scale_factor = 0.02);
Now vacuum runs after about 2% of the table changes instead of 20%.
When autovacuum is too gentle
Autovacuum throttles itself so it doesn't hurt your queries. It does a fixed amount of work (autovacuum_vacuum_cost_limit, which by default falls back to vacuum_cost_limit = 200) and then sleeps (autovacuum_vacuum_cost_delay, 2 ms since PostgreSQL 12). On fast SSDs with many busy tables that's often too slow. Raising the limit to 1000–2000 lets workers do more per cycle:
ALTER SYSTEM SET autovacuum_vacuum_cost_limit = 1000;
SELECT pg_reload_conf();
If you have many large tables, autovacuum_max_workers (default 3) may also need raising, which takes a restart. Note that the cost limit is shared between workers.
Things that stop vacuum from working
Vacuum can only remove a dead row when no transaction could still need it. These hold cleanup back across the whole server:
- Long-running or idle-in-transaction sessions. Find them in
pg_stat_activityand consideridle_in_transaction_session_timeout. - Forgotten prepared transactions in
pg_prepared_xacts. - Replication slots whose consumer has gone away, visible in
pg_replication_slots. - Replicas with
hot_standby_feedback = onrunning long queries.
VACUUM doesn't shrink the file
Regular VACUUM makes space reusable inside the table but rarely gives it back to the operating system. To shrink a badly bloated table, VACUUM FULL rewrites it but locks it completely while it runs. The pg_repack extension does the same rewrite online.
SQLNirvana's VACUUM advisor shows each table's dead rows, last vacuum, freeze age and statistics age, and generates the per-table settings. Try it once on your database with the free PostgreSQL VACUUM & health check.