← Lessons

quiz vs the machine

Gold1470

Databases

Vacuum and Analyze Tuning

Autovacuum reclaims dead space and refreshes statistics. Tuning its thresholds keeps it ahead of write heavy tables.

5 min read · core · beat Gold to climb

Two Jobs in One

Vacuum reclaims space from dead tuples and updates the visibility map. Analyze refreshes the statistics the planner uses to estimate row counts. Autovacuum runs both automatically based on how much a table has changed.

How It Decides to Run

Autovacuum triggers when dead or changed rows exceed a threshold, computed as a base value plus a scale factor times the table size. A default scale factor of twenty percent means a large table waits until a fifth of its rows are dead before vacuum runs, which can be far too late.

Tuning Levers

  • Lower the scale factor on large hot tables so vacuum runs more often on a smaller absolute change.
  • Raise the number of autovacuum workers and their cost limits so vacuum keeps pace under heavy writes.
  • Set a separate, more aggressive analyze threshold for tables whose data distribution shifts quickly.

The Failure Mode

If vacuum falls behind, dead tuples pile up, bloat grows, and statistics drift, which together degrade plans and IO at the same time. Watch the last autovacuum time and dead tuple counts.

Key idea

Autovacuum reclaims space and refreshes statistics on a threshold tied to table size, so lower scale factors on big hot tables to stop it falling behind.

Check yourself

Answer to earn rating on the learn ladder.

1. What two jobs does autovacuum perform?

2. Why lower the autovacuum scale factor on a large hot table?