Sub topic 7
Why VACUUM Sometimes Cannot Remove Dead Tuples
Assume you know basic SQL, PostgreSQL transactions, isolation levels, and indexes. You should also know how to connect with psql or another PostgreSQL client.
VACUUM must protect active snapshots
PostgreSQL uses multiversion concurrency control (MVCC). An UPDATE creates a new row version and leaves the old version behind. A DELETE marks the old version as deleted. These old versions are called dead tuples.
VACUUM can remove a dead tuple only when no running transaction could still see it. Each transaction has a snapshot, which is its view of committed and uncommitted rows. If a transaction started before a row was deleted, PostgreSQL must preserve the old row version until that transaction finishes.
A long-running transaction therefore acts as a cleanup blocker. The table can continue accumulating dead tuples even while autovacuum runs successfully.
Find old transaction snapshots
Start by inspecting sessions with an old transaction horizon:
SELECT pid,
usename,
datname,
state,
xact_start,
age(backend_xmin) AS xmin_age,
left(query, 80) AS query
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL
ORDER BY age(backend_xmin) DESC
LIMIT 10;backend_xmin is the oldest transaction identifier that the session's snapshot might still need. A large xmin_age indicates that cleanup may be delayed. Pay special attention to sessions with state = 'idle in transaction'. They began a transaction but are currently waiting for the client to send another command.
The query text may be harmless. The important facts are xact_start, backend_xmin, and whether the application has left the transaction open.
Other cleanup blockers
Long-running sessions are only one source of old transaction horizons.
| Blocker | What it can retain | How to inspect it |
|---|---|---|
| Long transaction | Old row versions | `pg_stat_activity` |
| Prepared transaction | Transaction IDs and row versions | `pg_prepared_xacts` |
| Replication slot | Row or catalog cleanup horizons, plus WAL | `pg_replication_slots` |
A prepared transaction remains open across client disconnects. Inspect it with:
SELECT transaction,
gid,
prepared,
owner,
database
FROM pg_prepared_xacts
ORDER BY prepared;Replication slots need separate inspection:
SELECT slot_name,
slot_type,
active,
age(xmin) AS xmin_age,
age(catalog_xmin) AS catalog_xmin_age,
restart_lsn
FROM pg_replication_slots
ORDER BY slot_name;A logical slot with an old catalog_xmin can prevent cleanup of system catalog rows. A slot with an old xmin can retain ordinary table row versions. An inactive slot can still cause this problem. restart_lsn mainly indicates WAL retention, which is a separate disk-space risk.
> callout warning > Do not terminate sessions, roll back prepared transactions, or drop replication slots just to make bloat disappear. First identify the owner, confirm the transaction is abandoned, and understand the effect on the application or replication consumer.
A practical response
First, confirm that the dead-tuple count is growing and identify the oldest blocker. Then resolve the cause:
- Commit or roll back an abandoned transaction.
- Fix application code that holds transactions across user input or network calls.
- Complete or remove an abandoned prepared transaction according to the application's recovery plan.
- Repair or remove an unused replication slot after confirming that its consumer is no longer needed.
After the blocker is gone, run VACUUM or let autovacuum catch up. Do not expect VACUUM to override a valid old snapshot. It can report and scan the table, but preserving transaction visibility is part of PostgreSQL's correctness rules.
Exercise
An orders table has many dead tuples, and autovacuum runs every few minutes. pg_stat_activity shows an idle-in-transaction session whose xact_start is two days old. What should you investigate first?
Show answer
Investigate the session and the application that opened it. Its old snapshot may still need the dead row versions, so resolve the transaction safely before expecting VACUUM to remove them.