skip to content

Your PostgreSQL IoT telemetry database logged 'must be vacuumed within' warnings and now rejects writes to avoid wraparound; how do you restore service?

level: seniorimportance: must knowfreq 58%

answer

  1. reads still work, VACUUM still runs
  2. stay in multi-user mode
  3. three things pin the oldest XID
  4. superuser, plain VACUUM, oldest first
  5. not FULL, not FREEZE

basics

~20 s

Stay in normal multi-user mode. Clear what pins the oldest XID, old prepared transactions, long-running sessions and stale replication slots, then run a plain VACUUM as a superuser in the named database, oldest tables first.

solid answer

~40 s

`ERROR: database is not accepting commands that assign new transaction IDs to avoid wraparound data loss` (SQLSTATE `54000`) means fewer than 3 million XIDs are left. Reads still work and plain `VACUUM` still runs, and the current manual says not to stop the server or use single-user mode. First commit or roll back old entries in `pg_prepared_xacts`; end sessions whose `age(backend_xid)` or `age(backend_xmin)` in `pg_stat_activity` is huge with `pg_terminate_backend`; drop stale slots whose `age(xmin)` or `age(catalog_xmin)` is huge. Then, as a superuser in the database the message names, run `VACUUM`, oldest `relfrozenxid` first. Avoid `VACUUM FULL`, which needs an XID, and `VACUUM FREEZE`, which does extra work. Writes resume on their own once `datfrozenxid` advances; then find out why autovacuum fell behind.

code

console · 7 lines
console
WARNING:  database "iot" must be vacuumed within 39985967 transactions
HINT:  To avoid transaction ID assignment failures, execute a database-wide VACUUM in that database.
You might also need to commit or roll back old prepared transactions, or drop stale replication slots.
...
ERROR:  database is not accepting commands that assign new transaction IDs to avoid wraparound data loss in database "iot"
HINT:  Execute a database-wide VACUUM in that database.
You might also need to commit or roll back old prepared transactions, or drop stale replication slots.

go deeper

for a junior

Recognise the 'must be vacuumed within' warning and the stop error as XID wraparound protection, and know the fix is a plain VACUUM, not a restart.

for a middle

Explain the 40-million and 3-million limits, what still works in the stopped state, and why VACUUM FULL fails there while plain VACUUM runs.

for a senior

Run the recovery in order: clear prepared transactions, long transactions and stale slots, vacuum as a superuser oldest-first, avoid single-user mode, then trace why forced vacuums failed for so long.

for a principal

Turn the incident into prevention: XID-burn budgets for ingest paths, ownership of two-phase-commit clients and slots, and alerting on time-to-limit across every cluster.

## What the two messages mean PostgreSQL counts down to the **wraparound point**, about 2^31 transactions past the oldest `datfrozenxid` in the cluster, and acts at two distances from it (PostgreSQL 18 values): | XIDs left | Server behaviour | Message | |---|---|---| | 40 million | each new XID assignment warns the client and the server log | `WARNING: database "iot" must be vacuumed within N transactions` | | 3 million | new XIDs refused, SQLSTATE `54000` | `ERROR: database is not accepting commands that assign new transaction IDs to avoid wraparound data loss in database "iot"` | The database named is the one holding the oldest `datfrozenxid`, which may not be the one your application uses. In the stopped state: - transactions already in progress can continue; - **read-only transactions still start**, so dashboards and diagnostic queries keep working; - anything that needs a new XID fails, including `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE` and DDL; - a plain `VACUUM` does not need an XID, so it **still runs normally**. This is a write outage, not data loss: the rows are intact, and the server stopped precisely so they stay visible. ## Do not reach for single-user mode Older advice was to stop the postmaster and vacuum in single-user mode. The current manual says this is neither necessary nor desirable: it takes the whole server down and disables the wraparound safeguards that exist to prevent data loss. Its one remaining use is to `TRUNCATE` or `DROP` tables you no longer need instead of vacuuming them; the three-million margin exists to allow exactly that. ## Step 1: remove what pins the oldest XID VACUUM can only freeze rows older than the oldest XID something still needs. Clear the three usual holders first, or the vacuum will run and the age will not move: 1. **Old prepared transactions.** Rows in `pg_prepared_xacts` with a large `age(transaction)` must be finished with `COMMIT PREPARED` or `ROLLBACK PREPARED`. They survive restarts and belong to no session, so `pg_stat_activity` does not show them. 2. **Long-running transactions.** Sessions in `pg_stat_activity` with a large `age(backend_xid)` or `age(backend_xmin)`; commit, roll back, or end them with `pg_terminate_backend(pid)`. 3. **Stale replication slots.** Slots in `pg_replication_slots` with a large `age(xmin)` or `age(catalog_xmin)`. Drop slots for replicas or consumers that no longer exist; dropping one whose server still exists may force a rebuild of that replica. Slot retention itself is the replication-slots leaf's subject. ## Step 2: vacuum, smallest sufficient work first 1. Connect to the named database **as a superuser**. `datfrozenxid` is the minimum over all its relations, system catalogs included, and VACUUM skips relations the role may not maintain, so a lesser role can leave it stuck. 2. Run **plain `VACUUM`**. Database-wide is simplest; to finish sooner, vacuum the tables with the oldest `relfrozenxid` first, for example with `vacuumdb --min-xid-age`. 3. **Not `VACUUM FULL`**: it needs an XID, so it fails in this state. 4. **Not `VACUUM FREEZE`**: it does more work than restoring service requires; leave that to normal freezing later. When the oldest `datfrozenxid` moves far enough from the limit, XID assignment resumes automatically. No restart is needed. ## Step 3: find out why it happened The stop is the end of a long chain: forced autovacuums began at an age of 200 million, the failsafe at 1.6 billion, warnings at about 2.1 billion. Something kept every one of those from finishing. On an IoT ingest the usual contributors are: - **XID burn rate.** Each committed write transaction consumes one XID, so one autocommit `INSERT` per reading from thousands of devices spends XIDs as fast as readings arrive. Batching readings per transaction, with multi-row `INSERT` or `COPY`, cuts the rate by orders of magnitude. - **A horizon holder**: an abandoned prepared transaction from a two-phase-commit client, a session idle in transaction, a slot for a decommissioned consumer. - **Vacuums that never complete**: DDL or scripts cancelling them, or settings too slow for the table's size (tuning belongs to the autovacuum leaf). Then add an alert on `age(datfrozenxid)` so the next warning becomes a ticket rather than an outage.

  • When is PostgreSQL single-user mode still the right tool during XID-wraparound recovery?
    Only when you want to `TRUNCATE` or `DROP` tables you no longer need instead of vacuuming them, because those commands need an XID; the three-million-transaction margin exists for that. Single-user mode takes the whole server down and disables the wraparound safeguards, so the manual says to avoid it otherwise.
  • Why must the recovery VACUUM in PostgreSQL run as a superuser?
    `datfrozenxid` is the minimum `relfrozenxid` over every relation in the database, system catalogs included. VACUUM skips relations the role lacks the `MAINTAIN` privilege on, and even a database owner skips shared catalogs. A skipped catalog with an old `relfrozenxid` keeps `datfrozenxid`, and therefore the stop, in place.
  • How would you stop a PostgreSQL IoT ingest from burning XIDs this fast in the first place?
    Every committed write transaction consumes one XID, so per-row autocommit inserts spend one per reading. Batch readings into multi-row `INSERT` statements or `COPY` per transaction, which cuts consumption by orders of magnitude, and alert on `age(datfrozenxid)` so forced vacuums that are not finishing are noticed long before the 40-million warning.

  • Stop the server and VACUUM in single-user mode before anything else.
  • Run VACUUM FULL on the biggest tables to reset their transaction IDs.
  • Run VACUUM FREEZE everywhere so the problem never comes back.
  • Use pg_resetwal to move the transaction counter forward safely.
  • Once the stop limit hits, even SELECT queries stop working.
  • Any database owner can run the recovery VACUUM; superuser is unnecessary.