Sub topic 8
Tuning Autovacuum for Busy Tables
Assume you know basic SQL, transactions, indexes, and how to use psql. You should also understand that PostgreSQL uses multiversion concurrency control (MVCC): an UPDATE creates a new row version and leaves the old version behind until it is safe to remove.
Autovacuum is PostgreSQL’s background maintenance process. It periodically runs VACUUM and ANALYZE without requiring a manual command. On a busy table, the default settings may react too slowly, especially when the table is large.
How autovacuum decides to run
For vacuuming, PostgreSQL uses this approximate rule:
dead rows > autovacuum_vacuum_threshold
+ autovacuum_vacuum_scale_factor × estimated table rowsThe default scale factor is commonly 0.2, or 20 percent. For a table with 10 million estimated rows, that means autovacuum may wait for roughly 2,000,050 dead rows before starting. That can allow substantial bloat.
Analyze uses a separate threshold and scale factor. Analyze refreshes planner statistics, while vacuum removes dead row versions and performs visibility maintenance.
flowchart TD
A[UPDATE or DELETE creates dead rows] --> B[Autovacuum checks table thresholds]
B --> C{Vacuum threshold reached?}
C -->|No| B
C -->|Yes| D[VACUUM table]
D --> E[Dead space becomes reusable]
B --> F{Analyze threshold reached?}
F -->|Yes| G[ANALYZE table]
G --> H[Planner statistics refreshed]Choose table-level settings
A frequently updated table usually benefits from a smaller vacuum scale factor. Keep an absolute threshold so very small tables do not trigger vacuum for every few changes.
ALTER TABLE public.orders SET (
autovacuum_vacuum_scale_factor = 0.02,
autovacuum_vacuum_threshold = 1000,
autovacuum_analyze_scale_factor = 0.01,
autovacuum_analyze_threshold = 1000
);For orders, vacuum starts after approximately 2 percent of the estimated rows plus 1,000 dead rows. These settings apply only to this table and override the matching server settings.
| Setting | Controls | Tuning question |
|---|---|---|
| `autovacuum_vacuum_scale_factor` | Dead-row proportion before vacuum | How quickly does this table accumulate dead rows? |
| `autovacuum_vacuum_threshold` | Fixed dead-row allowance | Should small tables avoid overly frequent runs? |
| `autovacuum_analyze_scale_factor` | Changed-row proportion before analyze | How quickly do planner statistics become stale? |
| `autovacuum_max_workers` | Concurrent autovacuum workers | Can the server handle more maintenance at once? |
A lower scale factor causes more frequent maintenance. That usually reduces bloat but consumes more I/O and CPU. Tune the busiest tables first instead of lowering every global setting.
Check whether tuning worked
Use pg_stat_user_tables to compare estimated dead rows with vacuum history:
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,
autovacuum_count,
last_autoanalyze
FROM pg_stat_user_tables
WHERE relname = 'orders';If n_dead_tup remains high while last_autovacuum is old, investigate before changing more settings. The table might be locked, the worker pool might be full, or an old transaction might prevent cleanup. Raising autovacuum_max_workers can increase concurrency, but each worker also adds resource pressure.
The cost settings, such as autovacuum_vacuum_cost_limit and autovacuum_vacuum_cost_delay, control how aggressively workers use I/O. Removing a delay can make cleanup faster, but it can also compete with application queries. Measure latency and I/O before changing them.
Exercise
Exercise
A 50-million-row table receives frequent updates, but autovacuum waits too long. Write an ALTER TABLE statement that starts vacuum after about 1 percent of rows plus 5,000 dead rows.
Show answer
ALTER TABLE public.orders SET (autovacuumvacuumscalefactor = 0.01, autovacuumvacuum_threshold = 5000);