skip to content

On a PostgreSQL telemetry database, an autovacuum marked '(to prevent wraparound)' is blocking an ALTER TABLE; how does it differ from normal autovacuum, and what changes once vacuum_failsafe_age is crossed?

level: seniorimportance: should knowfreq 42%

answer

  1. triggered by age, not dead rows
  2. does not yield to lock requests
  3. scans every page not all-frozen
  4. 1.6 billion: drop throttling, skip indexes

basics

~20 s

An anti-wraparound autovacuum is forced by relfrozenxid age, runs even with autovacuum off, normally scans every not-all-frozen page, and is not interrupted by conflicting locks. Past vacuum_failsafe_age (1.6 billion) VACUUM stops throttling and skips index vacuuming.

solid answer

~40 s

When a table's `relfrozenxid` age passes `autovacuum_freeze_max_age` (200 million), autovacuum starts a vacuum whose `pg_stat_activity` query ends in `(to prevent wraparound)`. It runs even with autovacuum disabled, is normally aggressive because the table is past `vacuum_freeze_table_age`, and, unlike a normal autovacuum, is **not interrupted** when another session requests a lock conflicting with its `SHARE UPDATE EXCLUSIVE`. So the `ACCESS EXCLUSIVE` request from `ALTER TABLE` waits. Let it finish and retry with a short `lock_timeout`, or cancel it and run a manual `VACUUM` straight away; cancelling alone only brings it back. If the age passes `vacuum_failsafe_age` (1.6 billion), any VACUUM switches to its failsafe: no cost delay, index vacuuming bypassed and the buffer ring disabled, logging `bypassing nonessential maintenance ... as a failsafe`.

code

sql · 7 lines
sql
-- Running anti-wraparound autovacuums and how long they have been at it
SELECT pid,
       now() - xact_start AS running_for,
       query
FROM pg_stat_activity
WHERE backend_type = 'autovacuum worker'
  AND query LIKE '%(to prevent wraparound)';

go deeper

for a junior

Recognise the '(to prevent wraparound)' suffix in pg_stat_activity and know it means autovacuum was forced by transaction-ID age, not by dead rows.

for a middle

Explain the 200-million trigger, why the run is normally aggressive, and why it does not yield to a conflicting lock request the way a normal autovacuum does.

for a senior

Choose between waiting, retrying DDL with lock_timeout, or replacing the worker with an unthrottled manual VACUUM, and explain exactly what the 1.6-billion failsafe drops and the bloat it leaves.

for a principal

Treat a failsafe event as a monitoring failure: decide how far ahead forced vacuums must finish on the largest tables and how migrations are scheduled around them.

## What makes an autovacuum 'to prevent wraparound' Autovacuum normally starts a vacuum because enough rows died or were inserted; those triggers and their throttling settings belong to the autovacuum leaf. A second, independent trigger is **age**: when a table's `pg_class.relfrozenxid` is older than **`autovacuum_freeze_max_age`** (default 200 million), or its `relminmxid` is older than `autovacuum_multixact_freeze_max_age` (400 million), the launcher schedules an **anti-wraparound** vacuum. Its `pg_stat_activity.query` reads like `autovacuum: VACUUM telemetry.readings (to prevent wraparound)`. Three things set it apart: 1. **It runs even when autovacuum is off**, whether server-wide (`autovacuum = off`) or per table (`autovacuum_enabled = false`). 2. **It is normally aggressive.** Past 200 million the table is also past `vacuum_freeze_table_age`, so the vacuum must visit every page not already marked all-frozen in the visibility map, including the all-visible pages a normal run skips. On a large, mostly cold table that means reading most of it. 3. **It does not yield to lock requests.** Every autovacuum holds `SHARE UPDATE EXCLUSIVE`; a normal one is interrupted when another session requests a conflicting lock. An anti-wraparound autovacuum is **not automatically interrupted**. ## Why your ALTER TABLE hangs Most `ALTER TABLE` forms, such as `ADD COLUMN`, request `ACCESS EXCLUSIVE`, which conflicts with `SHARE UPDATE EXCLUSIVE`. Against a normal autovacuum the DDL would win. Against an anti-wraparound one, the DDL waits, and queries that arrive later line up behind the waiting request; that queueing, and `lock_timeout`, are the schema-changes leaf's subject. Options, in order of preference: - **Let it finish**, and retry the migration with a short `lock_timeout` so the DDL gives up instead of stalling traffic. - **Cancel it and immediately run a manual `VACUUM`** of that table at a quiet time. Manual VACUUM is unthrottled by default (`vacuum_cost_delay` is 0, while `autovacuum_vacuum_cost_delay` is 2 ms), so it usually finishes faster. - **Do not just cancel it and walk away.** Pages it already froze stay marked all-frozen, so not all work is lost, but `relfrozenxid` only advances when a run completes. The table stays over the threshold and the launcher picks it again soon. ## The failsafe: vacuum's last resort If a table's `relfrozenxid` age passes **`vacuum_failsafe_age`** (default **1.6 billion**), VACUUM switches to an emergency mode. The check happens when the vacuum starts and periodically while it scans, so the failsafe usually fires inside an anti-wraparound autovacuum that has been running a long time, but it applies to **any** VACUUM, manual ones included. When it triggers: - any **cost-based delay** stops being applied; - **index vacuuming** and other non-essential work are bypassed, so the run only has to finish the heap pass that freezes rows and advances `relfrozenxid`; - the **buffer access strategy** (the small ring buffer VACUUM normally reuses) is disabled, so VACUUM may use all of shared buffers. The server log shows a `WARNING` such as `bypassing nonessential maintenance of table "iot.telemetry.readings" as a failsafe after 0 index scans`, with the detail that the table's `relfrozenxid` or `relminmxid` is too far in the past. VACUUM silently raises a configured `vacuum_failsafe_age` to at least 105% of `autovacuum_freeze_max_age`; `vacuum_multixact_failsafe_age`, also 1.6 billion, is the MultiXact counterpart. ## The three modes side by side | Mode | Trigger (default) | Conflicting lock request | Cost-based delay | Index vacuuming | |---|---|---|---|---| | normal autovacuum | dead or inserted tuple thresholds | autovacuum is interrupted | applied | yes | | anti-wraparound autovacuum | age over 200 million | autovacuum keeps running | applied | yes | | failsafe (any VACUUM) | age over 1.6 billion | unchanged from its mode | dropped | bypassed | ## Costs and follow-up The failsafe trades bloat for safety: - dead index entries it skipped remain until a later normal VACUUM removes them, so plan one once the emergency is over; - an unthrottled vacuum using all of shared buffers competes directly with production reads and writes; - reaching it at all means the forced vacuums that began at 200 million failed to finish across a window of about 1.4 billion transactions. The real work afterwards is finding what stopped them: a horizon held back by an old transaction, prepared transaction or replication slot, repeated cancellations by DDL or scripts, or throttling too tight for the table's size. Fix that, or the next forced vacuum on the same table will stall the same way.

  • If you cancel a PostgreSQL anti-wraparound autovacuum with pg_cancel_backend, is its work lost?
    Not all of it. Pages it already froze are marked all-frozen in the visibility map, so the next run skips them. But `relfrozenxid` is only updated when a run completes, so the table stays over `autovacuum_freeze_max_age` and the launcher starts it again shortly. Cancelling buys minutes; replacing it with a manual `VACUUM` actually finishes the job.
  • Why does the PostgreSQL vacuum failsafe skip index vacuuming instead of simply running faster?
    Advancing `relfrozenxid` needs only the heap pass that freezes rows. Index vacuuming removes dead index entries and can take several passes over every index, which delays that goal. Under the failsafe, VACUUM bypasses index work, drops cost delay and leaves the buffer ring; the skipped cleanup becomes bloat that a later normal VACUUM handles.

  • Cancelling the anti-wraparound autovacuum resolves the blocking for good.
  • Setting autovacuum_enabled = false on the table prevents anti-wraparound vacuums.
  • The failsafe makes VACUUM take an exclusive lock so it can finish faster.
  • vacuum_failsafe_age applies only to autovacuum workers, never to a manual VACUUM.
  • Any autovacuum is cancelled when DDL requests a conflicting lock.