PostgreSQL autovacuum tuning: n_dead_tup, thresholds, per-table
PostgreSQL autovacuum waits for dead tuples above 50 + 20% of the table. Compare n_dead_tup to that, lower it per table, and find long transactions.
When autovacuum runs late on a big table, the cause is usually the default threshold 50 + 0.2 × rows. A 100,000,000-row table needs more than 20,000,050 dead tuples before it starts. Put n_dead_tup from pg_stat_user_tables next to the table's threshold first, then lower the threshold per table for the large ones only. If dead tuples survive a vacuum, look for a long-open transaction, not a setting.
[1] Check the current values. The test server below was started with autovacuum_naptime at 5 seconds and log_autovacuum_min_duration at 0 to see results quickly. PostgreSQL 17 defaults are 1 minute and 10 minutes.
$ psql -U postgres -c "SELECT name, setting, unit FROM pg_settings WHERE name ~ '^autovacuum' OR name IN ('vacuum_cost_limit','vacuum_cost_delay','log_autovacuum_min_duration') ORDER BY name" name | setting | unit ---------------------------------------+-----------+------ autovacuum | on | autovacuum_analyze_scale_factor | 0.1 | autovacuum_analyze_threshold | 50 | autovacuum_freeze_max_age | 200000000 | autovacuum_max_workers | 3 | autovacuum_multixact_freeze_max_age | 400000000 | autovacuum_naptime | 5 | s autovacuum_vacuum_cost_delay | 2 | ms autovacuum_vacuum_cost_limit | -1 | autovacuum_vacuum_insert_scale_factor | 0.2 | autovacuum_vacuum_insert_threshold | 1000 | autovacuum_vacuum_scale_factor | 0.2 | autovacuum_vacuum_threshold | 50 | autovacuum_work_mem | -1 | kB log_autovacuum_min_duration | 0 | ms vacuum_cost_delay | 0 | ms vacuum_cost_limit | 200 | (17 rows)The docs give three formulas:
| Job | Starts when |
|---|---|
| vacuum (updates/deletes) | dead tuples > autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor × reltuples |
| vacuum (inserts) | inserts since last vacuum > …_insert_threshold (1000) + …_insert_scale_factor (0.2) × reltuples |
| analyze | rows changed since last analyze > autovacuum_analyze_threshold + autovacuum_analyze_scale_factor × reltuples |
[2] Put each table's dead tuples next to its threshold. Here a 10,000-row table has had 1,000 rows updated:
$ psql -U postgres -c "SELECT c.relname, c.reltuples::bigint, s.n_dead_tup, round(current_setting('autovacuum_vacuum_threshold')::int + current_setting('autovacuum_vacuum_scale_factor')::float8 * c.reltuples) AS vacuum_at FROM pg_class c JOIN pg_stat_user_tables s ON s.relid = c.oid WHERE c.relname = 't'" relname | reltuples | n_dead_tup | vacuum_at ---------+-----------+------------+----------- t | 10000 | 1000 | 2050(1 row)1,000 dead tuples is under the 2,050 threshold, so nothing ran after more than two naptime cycles:
$ psql -U postgres -c "SELECT relname, n_live_tup, n_dead_tup, last_autovacuum, autovacuum_count FROM pg_stat_user_tables WHERE relname = 't'" relname | n_live_tup | n_dead_tup | last_autovacuum | autovacuum_count ---------+------------+------------+-----------------+------------------ t | 10000 | 1000 | | 0(1 row)After 2,000 more updates (3,000 dead) it ran on the next cycle. The query uses global values only; for tables with per-table settings, recompute from reloptions.
$ psql -U postgres -c "SELECT relname, n_live_tup, n_dead_tup, last_autovacuum, autovacuum_count FROM pg_stat_user_tables WHERE relname = 't'" relname | n_live_tup | n_dead_tup | last_autovacuum | autovacuum_count ---------+------------+------------+------------------------------+------------------ t | 10000 | 0 | 2026-10-27 10:55:38.63015+00 | 1(1 row)[3] Read the log to see what it did. Only runs longer than log_autovacuum_min_duration are logged, so lower it while investigating and restore it afterwards (applied on reload).
LOG: automatic vacuum of table "postgres.public.t": index scans: 1 pages: 0 removed, 58 remain, 58 scanned (100.00% of total) tuples: 2904 removed, 10000 remain, 0 are dead but not yet removableremoved is below 3,000 most likely because page pruning (HOT) cleaned some during the updates. The line to watch is dead but not yet removable; if it is not 0, go to step 5.
[4] Lower the threshold per table, on large tables only. Lowering the global autovacuum_vacuum_scale_factor makes small tables run often too, and they share the same 3 workers.
$ psql -U postgres -c 'ALTER TABLE t SET (autovacuum_vacuum_scale_factor = 0.01, autovacuum_vacuum_threshold = 100)'ALTER TABLE$ psql -U postgres -Atc "SELECT reloptions FROM pg_class WHERE relname = 't'"{autovacuum_vacuum_scale_factor=0.01,autovacuum_vacuum_threshold=100}The threshold becomes 100 + 0.01 × 10,000 = 200, and 300 updated rows triggered another run (autovacuum_count 1 → 2):
$ psql -U postgres -c "SELECT relname, n_live_tup, n_dead_tup, last_autovacuum, autovacuum_count FROM pg_stat_user_tables WHERE relname = 't'" relname | n_live_tup | n_dead_tup | last_autovacuum | autovacuum_count ---------+------------+------------+-------------------------------+------------------ t | 10232 | 0 | 2026-10-27 10:55:53.630569+00 | 2(1 row)n_live_tup 10232 is an estimate and can drift from the real 10,000. Pick values by watching the n_dead_tup trend: how often must it run so dead tuples stop piling up. Roll back with RESET:
$ psql -U postgres -c 'ALTER TABLE t RESET (autovacuum_vacuum_scale_factor, autovacuum_vacuum_threshold)'ALTER TABLE$ psql -U postgres -Atc "SELECT coalesce(reloptions::text, '(none)') FROM pg_class WHERE relname = 't'"(none)[5] If dead tuples remain after a vacuum, find the long-open transaction. Here another session held a REPEATABLE READ transaction idle while 1,000 rows were updated, then VACUUM (VERBOSE) ran:
$ psql -U postgres -c 'VACUUM (VERBOSE) t2'INFO: vacuuming "postgres.public.t2"INFO: finished vacuuming "postgres.public.t2": index scans: 0pages: 0 removed, 49 remain, 10 scanned (20.41% of total)tuples: 0 removed, 10853 remain, 1000 are dead but not yet removable…VACUUMNo setting will remove those 1,000 rows. Find the session holding the snapshot by backend_xmin:
$ psql -U postgres -c "SELECT pid, state, age(backend_xmin) AS xmin_age, now() - xact_start AS xact_age FROM pg_stat_activity WHERE backend_xmin IS NOT NULL AND pid <> pg_backend_pid()" pid | state | xmin_age | xact_age -----+---------------------+----------+---------------- 249 | idle in transaction | 1 | 00:00:03.32946(1 row)$ psql -U postgres -Atc 'SELECT pg_terminate_backend(249)'t$ psql -U postgres -c 'VACUUM (VERBOSE) t2'INFO: vacuuming "postgres.public.t2"INFO: finished vacuuming "postgres.public.t2": index scans: 1pages: 0 removed, 49 remain, 10 scanned (20.41% of total)tuples: 1000 removed, 9028 remain, 0 are dead but not yet removable…VACUUMTo keep it from coming back, set idle_in_transaction_session_timeout (tested with rollback in step 5 of Fix PostgreSQL "sorry, too many clients already").
[6] If it runs but too slowly, look at the cost limit. Autovacuum sleeps 2ms every time it spends 200 cost units (vacuum_cost_limit), and running workers share that limit. To speed up one large table, set it per table:
$ psql -U postgres -c 'ALTER TABLE t SET (autovacuum_vacuum_cost_limit = 1000)'Per the docs, workers on tables with per-table cost settings are left out of the sharing calculation. Raising the global autovacuum_vacuum_cost_limit raises disk I/O by the same amount, so raise it in steps while watching disk wait. A manual VACUUM does not sleep by default (vacuum_cost_delay 0). autovacuum_max_workers needs a restart, and more workers still share the same total cost limit.
Plain VACUUM marks space for reuse but does not return it to the OS (except empty pages at the end of the table), so a table file that does not shrink is normal. VACUUM FULL rewrites the table under an exclusive lock; do not run it during service hours.
Official references: Routine Vacuuming — threshold formulas, cost balancing, space reuse · Automatic Vacuuming settings · Cost-based Vacuum Delay · log_autovacuum_min_duration · pg_stat_user_tables · Storage parameters