Sub topic 6
Autovacuum: the Background Cleaner
This chapter assumes basic SQL, PostgreSQL transactions, indexes, and use of psql. Autovacuum is PostgreSQL’s background maintenance system. It starts VACUUM and ANALYZE automatically when tables need them.
How autovacuum works
The autovacuum launcher checks table activity statistics. When a table passes a configured threshold, the launcher assigns it to an autovacuum worker.
A worker can:
- Mark dead tuple space as reusable.
- Update the visibility map.
- Freeze old transaction IDs when needed.
- Refresh planner statistics with
ANALYZE. - Prevent transaction ID wraparound.
Autovacuum does not run VACUUM FULL. It normally works inside the existing table file, so it does not return the table’s unused space to the operating system.
The trigger thresholds
For updates and deletes, the approximate vacuum trigger is:
autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor × estimated table rows
The default scale factor is often 0.2, or 20 percent. That may be reasonable for a small table, but it can be far too slow for a large table. A table with 100 million rows could accumulate about 20 million changed rows before reaching that percentage-based threshold.
Autovacuum also has insert-specific settings in supported PostgreSQL versions. These help it vacuum append-heavy tables, where updates and deletes are uncommon but visibility-map and freezing work still matter.
ANALYZE has separate threshold and scale-factor settings. A table can therefore receive ANALYZE without needing a vacuum, or need a vacuum without needing new planner statistics.
You can inspect recent activity with this query:
SELECT relname,
n_live_tup,
n_dead_tup,
last_autovacuum,
last_autoanalyze,
autovacuum_count,
autoanalyze_count
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 10;The row counts in this view are estimates. Use them for monitoring and triage, not as an exact measurement.
Important settings
Autovacuum uses both global settings and per-table storage parameters. A table setting overrides the corresponding global value.
| Setting | Purpose |
|---|---|
| `autovacuum_vacuum_scale_factor` | Fraction of estimated rows that must change |
| `autovacuum_vacuum_threshold` | Fixed number of changed rows required |
| `autovacuum_analyze_scale_factor` | Fraction of rows changed before `ANALYZE` |
| `autovacuum_max_workers` | Maximum number of autovacuum workers |
| `autovacuum_work_mem` | Memory available to each worker, when set |
For a busy large table, use a smaller scale factor and a fixed threshold:
ALTER TABLE public.orders SET (
autovacuum_vacuum_scale_factor = 0.02,
autovacuum_vacuum_threshold = 1000,
autovacuum_analyze_scale_factor = 0.01
);This does not start a vacuum immediately. It changes the conditions for future autovacuum decisions.
When autovacuum cannot keep up
Check whether workers are active and whether a session holds an old snapshot:
SELECT pid,
state,
xact_start,
age(backend_xmin) AS xmin_age,
left(query, 80) AS query
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL
ORDER BY age(backend_xmin) DESC
LIMIT 5;An old transaction can keep row versions visible to that transaction. Autovacuum must preserve those versions, so it cannot remove the dead tuples yet. Replication slots and prepared transactions can create similar cleanup delays.
Exercise
A table has 50 million estimated rows and uses a vacuum scale factor of 0.2 with a threshold of 50. About how many changed rows can accumulate before the normal vacuum trigger is reached?
Show answer
About 10,000,050 rows: 50 plus 20% of 50 million.