skip to content

In PostgreSQL, VACUUM VERBOSE reports many tuples as dead but not yet removable; what can hold cleanup back, and how do you find it?

level: middleimportance: should knowfreq 60%

answer

  1. the removable cutoff line
  2. oldest xmin anyone still needs
  3. sessions, prepared xacts, slots
  4. standby feedback counts too

basics

~10 s

VACUUM removes only tuples older than the oldest xmin still needed: a long or idle-in-transaction session (pg_stat_activity backend_xmin, backend_xid), a forgotten prepared transaction (pg_prepared_xacts), or a replication slot or standby feedback (pg_replication_slots xmin).

solid answer

~40 s

`VACUUM VERBOSE`, and autovacuum's log entry, print a `removable cutoff` XID and how old it was; tuples deleted after that cutoff are counted as `dead but not yet removable`. The cutoff is the oldest xmin something still needs, and the usual holders are: a long query or a session idle in transaction that still holds a snapshot or an XID, seen as a large `age(backend_xmin)` or `age(backend_xid)` in `pg_stat_activity`; a prepared transaction in `pg_prepared_xacts`, which survives disconnects and restarts; and replication — a slot whose `xmin` or `catalog_xmin` is old in `pg_replication_slots`, or a standby sending `hot_standby_feedback`. Fix the holder: end or terminate the session, `COMMIT PREPARED` or `ROLLBACK PREPARED`, drop a dead slot. Then set `idle_in_transaction_session_timeout` or `transaction_timeout` so it cannot recur.

code

sql · 13 lines
sql
-- Forgotten two-phase transactions
SELECT gid, prepared, owner, database, age(transaction) AS xid_age
FROM pg_prepared_xacts
ORDER BY age(transaction) DESC;

-- Slots pinning the horizon
SELECT slot_name, slot_type, active,
       age(xmin) AS xmin_age, age(catalog_xmin) AS catalog_xmin_age
FROM pg_replication_slots
ORDER BY greatest(age(xmin), age(catalog_xmin)) DESC NULLS LAST;

-- Stop idle-in-transaction sessions from pinning it again
ALTER DATABASE social SET idle_in_transaction_session_timeout = '5min';

go deeper

for a junior

Recall that VACUUM cannot remove rows an open transaction might still see, so a session left idle in a transaction makes tables grow.

for a middle

Name the holders and their views: backend_xmin and backend_xid in pg_stat_activity, pg_prepared_xacts, slot xmin and catalog_xmin, standby feedback, and read the removable cutoff line.

for a senior

Show the diagnosis order from the VERBOSE line to the holder, the per-database scope of the horizon, and the timeouts and alerts that stop a repeat.

for a principal

Argue the policy: which workloads may hold long snapshots, whether reporting belongs on a standby with feedback or elsewhere, and who owns two-phase commit cleanup.

## What 'dead but not yet removable' means PostgreSQL's `VACUUM` may remove a tuple version only when no transaction can still see it. To decide that, each vacuum computes a **removable cutoff**, the oldest transaction ID that some snapshot, transaction or replication consumer still needs. A tuple deleted by a transaction older than the cutoff is dead and removed. A tuple deleted after the cutoff is dead to most sessions but might still be visible to the holder, so vacuum keeps it and counts it as **dead but not yet removable**. `VACUUM (VERBOSE)` and autovacuum's log entry (written when a run exceeds `log_autovacuum_min_duration`, default `10min`) report both numbers: - `tuples: N removed, M remain, K are dead but not yet removable` - `removable cutoff: X, which was D XIDs old when operation ended` A large `K` with a large `D` means vacuum ran, did its work, and was not allowed to finish the job. No amount of autovacuum tuning fixes that: the next run will hit the same cutoff. ## The usual holders | Holder | Where you see it | How to clear it | Notes | |---|---|---|---| | Long query or session idle in transaction | `pg_stat_activity`: `age(backend_xmin)`, `age(backend_xid)`, `state`, `xact_start` | commit, roll back, or `pg_terminate_backend(pid)` | only while it holds a snapshot or an assigned XID | | Prepared transaction (two-phase commit) | `pg_prepared_xacts`: `gid`, `prepared`, `age(transaction)` | `COMMIT PREPARED 'gid'` or `ROLLBACK PREPARED 'gid'` | has no session; survives disconnects and restarts | | Physical slot of a standby using `hot_standby_feedback` | `pg_replication_slots`: `age(xmin)` | fix or drop the slot, or end the standby's long query | the slot keeps the xmin even while the standby is down | | Logical replication slot | `pg_replication_slots`: `age(catalog_xmin)` | let the consumer catch up, or drop an abandoned slot | holds back system catalog cleanup only | | Standby feedback without a slot | `pg_stat_replication`: `backend_xmin` | end the standby's long query or turn feedback off | protects only while the standby is connected | A session counts only while it holds something. Under the default `READ COMMITTED`, a session idle in transaction with no open cursor releases its snapshot between statements, but once it has written anything it holds an XID until it ends. Under `REPEATABLE READ` the snapshot lives as long as the transaction. ## Which tables are affected The horizon is not simply cluster-wide: 1. For **ordinary tables**, vacuum considers only sessions connected to the **same database**. A forgotten transaction in another database does not block them. 2. **Shared catalogs** consider sessions in every database. 3. **Replication slots** and **hot standby feedback** are not tied to one database, so they hold back cleanup in all of them. 4. A logical slot's `catalog_xmin` affects system catalogs, not user tables, which is why an abandoned logical slot shows up as catalog bloat (and as retained WAL, which is the slots topic). ## Finding the holder Ask each source in turn and compare the ages with the `D` in the log line; the one that matches is your holder. ```sql SELECT pid, datname, state, xact_start, greatest(age(backend_xmin), age(backend_xid)) AS xid_age FROM pg_stat_activity WHERE backend_xmin IS NOT NULL OR backend_xid IS NOT NULL ORDER BY xid_age DESC LIMIT 5; ``` Then check `pg_prepared_xacts` and `pg_replication_slots` the same way (see the code example). ## Preventing it - **`idle_in_transaction_session_timeout`** (default `0`, off) ends sessions that sit idle inside a transaction too long. - **`transaction_timeout`** (PostgreSQL 17 and later, default `0`) ends any transaction that runs too long, idle or not; it does not apply to prepared transactions. - **`max_prepared_transactions`** defaults to `0`, which disables two-phase commit. Enable it only where a transaction manager actually uses it, and alert on old rows in `pg_prepared_xacts`. - **Slots**: alert on slots that are inactive with an old `xmin` or `catalog_xmin`. Slot invalidation and WAL limits belong to the replication-slots topic. - **Standbys**: if analysts run long queries on a standby with `hot_standby_feedback = on`, the primary keeps their dead tuples; weigh that bloat against query cancellations there. ## Why this matters beyond bloat The same cutoff also limits freezing, so a holder left for days turns a bloat problem into a transaction-age problem. That escalation, and the recovery steps when PostgreSQL starts warning, belong to the XID wraparound topic.

  • Does a long transaction in one PostgreSQL database stop VACUUM in another database of the same cluster?
    Not for ordinary tables: the cutoff for a table counts only sessions in its own database. It does hold back shared catalogs. Replication slots and hot standby feedback are different: they are not tied to a database, so they hold back cleanup everywhere.
  • Why is a forgotten prepared transaction harder to deal with than an idle session?
    It has no backend to terminate and does not appear in `pg_stat_activity`; it survives client disconnects and server restarts, and `transaction_timeout` does not apply to it. You find it in `pg_prepared_xacts` and end it by gid with `COMMIT PREPARED` or `ROLLBACK PREPARED`, after checking with whichever transaction manager created it.
  • What does turning on hot_standby_feedback trade on a PostgreSQL standby?
    The standby reports its oldest snapshot upstream, so the primary keeps tuples its queries still need and replay stops cancelling them. The price is bloat on the primary for as long as those queries run; with a physical slot that xmin persists even while the standby is disconnected.

  • Raising autovacuum_max_workers will clear dead tuples a long transaction still pins.
  • Terminating the autovacuum worker frees the tuples it reported as not yet removable.
  • A prepared transaction goes away when the client that prepared it disconnects.
  • An abandoned logical replication slot holds back cleanup of every user table.
  • VACUUM FULL can remove the tuples that plain VACUUM reported as not yet removable.

Assumes PostgreSQL 18; transaction_timeout exists from 17, so on 16 only idle_in_transaction_session_timeout and statement_timeout are available.