A table that holds sensor readings grows, and the queries against it slow down. Disk fills faster than the row count explains. You check, and autovacuum is running, so what is wrong?
On large, busy tables the default settings do not run autovacuum often enough. The defaults suit small tables, and your biggest tables are the ones that suffer.
What vacuum is for
PostgreSQL uses multiversion concurrency control. When you update or delete a row, the old version stays on disk until no running transaction can still see it. Those leftover versions are dead tuples.
Vacuum finds dead tuples and marks their space reusable. It also updates the visibility map, which helps index-only scans, and it freezes old transaction IDs so the database does not hit wraparound.
Plain VACUUM does not normally shrink the file. It makes space reusable inside the file. If a table carries far more dead space than it needs, the file stays large until the table is rewritten.
When autovacuum decides to run
Autovacuum triggers on a table when dead tuples exceed a threshold:
threshold = autovacuum_vacuum_threshold
+ autovacuum_vacuum_scale_factor * number_of_rows
The defaults are a threshold of 50 and a scale factor of 0.2. That means a table needs roughly 20 percent of its rows dead before autovacuum starts.
For a table of 10,000 rows, that is about 2,000 dead rows. No problem. For a table of 500 million rows, it is about 100 million dead rows before the first vacuum begins. By then the table has bloated, the vacuum takes a long time, and the next one starts late as well.
Recent versions also trigger on inserts, which helps append-only tables get their visibility map and freezing work done. Check the documentation for your version to see which settings apply.
Look before you tune
Start by finding which tables have the problem. The statistics view shows dead tuples and the last vacuum times:
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
from pg_stat_user_tables
order by n_dead_tup desc
limit 20;
These counts are estimates, so treat them as a guide and not an audit. A large n_dead_tup with an old last_autovacuum means autovacuum is not keeping up on that table. A table that is vacuumed constantly and still shows a lot of dead rows points somewhere else, covered below.
Set per-table scale factors
You do not need to change the global setting for every table. Change it for the large, hot ones with a storage parameter:
alter table raw.readings set (
autovacuum_vacuum_scale_factor = 0.01,
autovacuum_vacuum_threshold = 10000
);
A scale factor of 0.01 triggers a vacuum at 1 percent dead rows plus the threshold. Pick values from the table’s churn rather than copying these. A table that is only ever appended to needs different treatment from one that is updated heavily.
Analyze has matching settings, autovacuum_analyze_scale_factor and its threshold. Stale statistics produce bad query plans, so large tables often need both lowered.
When vacuum cannot remove dead rows
If autovacuum runs and the dead tuples stay, something is holding them. Vacuum can only remove a row version that no transaction can still see, so one old transaction blocks cleanup everywhere.
Three culprits come up repeatedly:
- A long-running transaction, including one stuck as
idle in transaction. An application that opens a transaction and forgets it will hold back cleanup for every table. - A replication slot that is not being consumed. A stalled consumer pins old data and, with it, disk space.
- A prepared transaction left behind by a failed two-phase commit.
Find them with pg_stat_activity, pg_replication_slots and pg_prepared_xacts. Look at the transaction start time and the slot’s activity, and compare them to what you expect.
Setting idle_in_transaction_session_timeout ends sessions that sit idle inside a transaction too long. That protects you from application bugs, but pick a value that your applications can live with.
Throttling and speed
Autovacuum limits its own I/O using a cost-based delay so it does not starve your queries. In current versions the default delay is short, but the cost limit is modest. On fast storage, autovacuum workers can spend most of their time sleeping.
Two settings control how much work it does: autovacuum_vacuum_cost_limit and autovacuum_vacuum_cost_delay. Raise the limit gradually and watch your I/O and latency. The number of workers is autovacuum_max_workers, and the cost limit is shared among active workers, so adding workers without raising the limit does not make the total faster.
Change one setting at a time. Autovacuum problems are slow to appear and slow to resolve, and stacking changes makes it hard to know what helped.
If the table is already bloated
Once a table has grown, regular vacuum will stop it growing further but will not hand the space back. To shrink the file you have to rewrite the table.
VACUUM FULL does this and takes an exclusive lock on the table for the duration, which blocks reads and writes. On a table that feeds a live process, that can be unacceptable. Extensions and external tools exist for rewriting a table with a much shorter lock. Evaluate them on a copy first and read their requirements, because they need free disk space for a second copy of the table.
Often the better fix is partitioning. If your readings table is partitioned by time, old partitions can be dropped or archived without any vacuum at all, and the active partition stays small enough for autovacuum to keep up.
What to do this week
Run the statistics query and pick the three tables with the most dead tuples. Check whether any long transactions or stale slots exist. Lower the scale factor on the worst table and watch last_autovacuum over the next few days.
Log the autovacuum activity too, using log_autovacuum_min_duration, so you can see how long each run takes. A vacuum that finishes in seconds is healthy. One that runs for hours, or never completes, tells you where to look next.

