skip to content

A PostgreSQL sessions table in a social app keeps growing even though autovacuum runs on it; how do cost throttling and per-table settings explain and fix it?

level: seniorimportance: should knowfreq 50%

answer

  1. credits per page, then a nap
  2. 200 credits, 2 ms sleep
  3. budget split across workers
  4. ALTER TABLE ... SET storage parameters
  5. cancelled by conflicting locks

basics

~20 s

Autovacuum workers share one throttled cost budget (200 credits, then a 2 ms nap), so a constantly updated table can outpace them. Give the table its own lower trigger and higher cost limit with ALTER TABLE ... SET, after ruling out xmin blockers.

solid answer

~40 s

First rule out a blocker: if `VACUUM VERBOSE` or the autovacuum log shows many tuples `dead but not yet removable`, tuning will not help. If tuples are removed but too late or too slowly, look at the trigger and the throttle. By default the table waits until 20% of its rows are dead, and each worker charges `vacuum_cost_page_hit` 1, `vacuum_cost_page_miss` 2 and `vacuum_cost_page_dirty` 20 credits, sleeping `autovacuum_vacuum_cost_delay` (2 ms) whenever it reaches the limit of 200 inherited from `vacuum_cost_limit`. That limit is divided among running workers, so adding workers adds no throughput. Fix it per table — `ALTER TABLE user_sessions SET (autovacuum_vacuum_scale_factor = 0.01, autovacuum_vacuum_cost_limit = 1000)` — and confirm runs finish with `log_autovacuum_min_duration` output and the absence of `canceling autovacuum task` errors.

code

ini · 5 lines
ini
# postgresql.conf, applied with SELECT pg_reload_conf();
autovacuum_max_workers = 6           # PostgreSQL 18: up to autovacuum_worker_slots without restart
autovacuum_vacuum_cost_limit = 800   # shared by all running workers
log_autovacuum_min_duration = 1s     # default is 10min
track_cost_delay_timing = on         # fills delay_time; off by default

go deeper

for a junior

Recall that autovacuum is deliberately slowed down so it does not swamp the disks, and that busy tables can be given their own autovacuum settings.

for a middle

Explain the credit model with its defaults, the 2 ms delay, why manual VACUUM is unthrottled, and how ALTER TABLE ... SET overrides thresholds per table.

for a senior

Show the triage order, blocked versus cancelled versus behind, then tune the hot table's trigger and budget and prove the fix with autovacuum logging and delay timing.

for a principal

Weigh I/O headroom against bloat: decide which tables earn a private vacuum budget, and how the storage and WAL capacity plan accounts for faster vacuums.

## The scenario A social app keeps one row per logged-in device in `user_sessions` and updates `last_seen_at` on every request. Autovacuum shows up in `pg_stat_activity` as `autovacuum: VACUUM public.user_sessions` many times a day, yet the table keeps growing. There are three different causes, and they need different fixes. ## Step 1: blocked, behind, or cancelled? 1. **Blocked.** The autovacuum log or `VACUUM VERBOSE` shows a large `dead but not yet removable` count and an old `removable cutoff`. Something (a session, prepared transaction or slot) is pinning the horizon; fix that first, because no setting below helps. 2. **Cancelled.** The log shows `canceling autovacuum task` for this table. A session asked for a lock that conflicts with autovacuum's `SHARE UPDATE EXCLUSIVE` (an `ALTER TABLE`, `LOCK TABLE`, a manual `ANALYZE`, `CREATE INDEX CONCURRENTLY`), and PostgreSQL cancelled the autovacuum so the request could proceed. Only anti-wraparound autovacuums are exempt. If a job does this every few minutes, the vacuum restarts from scratch and may never finish. 3. **Behind.** Runs complete and remove tuples, but they start too late and take too long. That is the trigger and the throttle. ## How cost-based throttling works Every page a vacuum touches costs credits. When a worker's running total reaches the cost limit, it sleeps for the cost delay and starts counting again. | Setting | Default | Meaning | |---|---|---| | `vacuum_cost_page_hit` | 1 | page found in shared buffers | | `vacuum_cost_page_miss` | 2 | page read from outside shared buffers | | `vacuum_cost_page_dirty` | 20 | page vacuum modified | | `vacuum_cost_limit` | 200 | credits before a sleep | | `autovacuum_vacuum_cost_limit` | -1 | use `vacuum_cost_limit` | | `autovacuum_vacuum_cost_delay` | 2ms | autovacuum's sleep | | `vacuum_cost_delay` | 0 | manual `VACUUM`'s sleep: off | A worker that reads and dirties every page spends 22 credits per page, so it handles about nine pages, roughly 72 kB, per 200-credit round before sleeping 2 ms. Ignoring the work itself, that caps it at about 36 MB/s of dirtied pages. A hot, wide table with several indexes can outrun that. Manual `VACUUM` is unthrottled by default, which is why it often finishes far faster than autovacuum on the same table. ## Why more workers is not the answer - The cost limit is **balanced**: it is divided among running autovacuum workers, so three or six workers together spend the same budget. - Only one worker vacuums a given table at a time, so more workers never speed up the one hot table. - Workers on tables whose own `autovacuum_vacuum_cost_limit` or `autovacuum_vacuum_cost_delay` storage parameters are set are **left out of the balancing** and keep their own budget. - More workers do help when many tables are waiting at once. In PostgreSQL 18 `autovacuum_max_workers` (default 3) changes with a reload, up to `autovacuum_worker_slots` (default 16), which needs a restart; in 17 the worker count itself needed a restart. ## Tuning the one hot table ```sql ALTER TABLE user_sessions SET ( autovacuum_vacuum_scale_factor = 0.0, autovacuum_vacuum_threshold = 20000, autovacuum_vacuum_cost_limit = 1000, autovacuum_analyze_scale_factor = 0.02 ); ``` - **Scale factor 0 with a fixed threshold** vacuums after a set number of dead tuples, whatever the table's size, so each run has a small, predictable backlog. - **A table-level cost limit** gives this table five times the default budget and takes it out of balancing, without unthrottling every other table. - **`toast.`-prefixed versions** of the same parameters tune the table's TOAST relation if large values are updated too. - The `ALTER TABLE` takes a `SHARE UPDATE EXCLUSIVE` lock, so it waits for or cancels a running autovacuum on that table. - Reducing how many dead index entries each update creates (HOT updates, fillfactor) is a storage-layout topic of its own. ## Confirming it worked 1. Lower `log_autovacuum_min_duration` (default `10min`), cluster-wide or as a storage parameter on this table, to log every run's removed tuples, index scans, I/O rates and timing. 2. Turn on `track_cost_delay_timing` (PostgreSQL 18, default off) to see time spent sleeping, both in the log and in `pg_stat_progress_vacuum.delay_time`. 3. Watch the table's dead-tuple counters and size over a few days; a flat size under steady load means vacuum keeps up. ## What not to do - Do not disable autovacuum on the table and schedule a nightly `VACUUM FULL`: the table bloats all day and is locked during the rewrite. - Do not set `autovacuum_vacuum_cost_delay = 0` cluster-wide on shared storage without measuring the I/O and WAL it releases. - Do not raise worker counts expecting a faster vacuum of one table.

  • What does 'canceling autovacuum task' in the PostgreSQL log tell you about this table?
    A session requested a lock that conflicts with autovacuum's `SHARE UPDATE EXCLUSIVE` lock, and PostgreSQL cancelled the autovacuum so the request could go ahead. The work done so far is lost. If a scheduled `ALTER TABLE`, `LOCK TABLE` or manual `ANALYZE` does this repeatedly, the table is never fully vacuumed. Anti-wraparound autovacuums are not cancelled this way.
  • Why set autovacuum_vacuum_scale_factor to 0 with a fixed threshold on a hot table?
    It makes the trigger independent of table size. With a steady update rate, vacuum then runs after the same number of dead tuples each time, so runs stay short and predictable instead of growing as the table grows.
  • Would you set autovacuum_vacuum_cost_delay to 0 on that table?
    Only with measured I/O headroom. It removes throttling for that table's worker, which can saturate disks and WAL during each run. Raising the table's `autovacuum_vacuum_cost_limit` first is the gentler step; `delay_time` with `track_cost_delay_timing` on shows whether sleeping is really the bottleneck.

  • Adding autovacuum workers makes one busy table get vacuumed faster.
  • Disable autovacuum on the hot table and run VACUUM FULL every night instead.
  • Manual VACUUM is throttled by the same 2 ms delay as autovacuum.
  • Lowering the scale factor fixes growth even when tuples are reported not yet removable.
  • Per-table cost settings still share the global budget with the other workers.

Assumes PostgreSQL 18: autovacuum_max_workers changes with a reload up to autovacuum_worker_slots, and track_cost_delay_timing with delay_time is new; in 17 changing autovacuum_max_workers needs a restart.