Quick revision
The short answer
VACUUM may only remove a dead tuple once no snapshot still in use could see it, and the oldest such snapshot sets a limit called the xmin horizon. The session that has been idle in transaction for six days holds that horizon back, so every row version updated since then is kept, the table bloats from 5 GB to 80 GB, and autovacuum works hard but may remove almost nothing. We terminate the session, let vacuum catch up, reclaim the space with pg_repack, and add timeouts and alerts so it cannot happen again.
What to remember
In this answer, we saw that VACUUM can only remove dead tuples older than the xmin horizon, the oldest snapshot in use, and that a six-day idle-in-transaction session pins that horizon so every newer version stays and the table bloats. Long queries, abandoned replication slots, prepared transactions and hot_standby_feedback can do the same. We terminate the holder, let vacuum catch up, reclaim space with pg_repack rather than VACUUM FULL, add idle_in_transaction_session_timeout, statement_timeout and transaction_timeout with alerts on age(backend_xmin), and fix the code that left the transaction open.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
VACUUM may only remove a dead tuple once no snapshot still in use could see it, and the oldest such snapshot sets a limit called the xmin horizon. The session that has been idle in transaction for six days holds that horizon back, so every row version updated since then is kept, the table bloats from 5 GB to 80 GB, and autovacuum works hard but may remove almost nothing. We terminate the session, let vacuum catch up, reclaim the space with pg_repack, and add timeouts and alerts so it cannot happen again.
Why this question is asked
This is one of the most common PostgreSQL production incidents, and it is confusing the first time we see it: autovacuum is clearly running, yet the table keeps growing. The cause is usually not in the database at all but in application code that opened a transaction and never finished it. Interviewers ask it to see whether we can connect MVCC internals to a real symptom and then fix both the database and the code. In this answer, we will recap dead tuples and the xmin horizon, explain the link to the idle session, list the other things that hold the horizon, and then go through the immediate fix, prevention and the application cause.
1A Quick Recap of Dead Tuples
In PostgreSQL an UPDATE writes a new version of the row, called a tuple, and marks the old one as deleted by setting its xmax. The old version becomes a dead tuple, but it cannot be thrown away at once, because some transaction that started earlier may still need to read it. VACUUM is the process that removes dead tuples, and autovacuum runs it automatically when enough rows have changed.
2The Link: The xmin Horizon
Before removing anything, VACUUM computes the oldest snapshot still in use across the whole cluster, the xmin horizon. Any dead tuple deleted by a transaction newer than that horizon might still be visible to that old snapshot, so VACUUM must keep it. A session that is idle in transaction still holds the snapshot or transaction id it took six days ago. So from VACUUM's point of view, every version replaced in the last six days could still be needed.
On a table with heavy updates, that is millions of versions. They pile up, the table and its indexes grow, queries read many more pages to find the live rows, and autovacuum keeps being triggered by the dead tuple count, scans the whole table, and reports that it found dead tuples it could not remove. That is the 'runs constantly but removes few tuples' symptom. The VACUUM VERBOSE output says this directly: it reports how many dead row versions cannot be removed yet and the oldest xmin it had to respect.
6 messages in order. Play them, or step through them one at a time.
Vacuum is working; it is simply not allowed to remove anything newer than the oldest snapshot.
3Other Things That Hold the Horizon
An idle-in-transaction session is the classic culprit, but it is not the only one, and after killing it we should check the others. A long-running query, such as a reporting query that has run for hours, holds its snapshot just the same. An abandoned replication slot keeps xmin or catalog_xmin in pg_replication_slots. A prepared transaction from two-phase commit that nobody finished appears in pg_prepared_xacts and survives even a restart. And a replica with hot_standby_feedback = on reports the snapshots of its own long queries to the primary, which then holds back vacuum on the primary.
The first query answers the follow-up question directly: the session at the top, with the largest age(backend_xmin), is the one holding the horizon.
4The Immediate Fix
We end the session with SELECT pg_terminate_backend(48211);. Its transaction rolls back, the horizon moves forward, and the next VACUUM can finally remove the dead tuples. We can run VACUUM (VERBOSE) on the table by hand to speed that up instead of waiting.
But there is a catch: a plain VACUUM makes the space reusable inside the table; it does not shrink the 80 GB file. To give disk back we need to rewrite the table. VACUUM FULL does that but holds an ACCESS EXCLUSIVE lock for the whole rewrite, which blocks even reads, so it is only acceptable in a maintenance window. pg_repack, an extension, rebuilds the table and its indexes online and takes the strong lock only briefly at the end, so it is the usual choice in production. Both need free disk space roughly the size of the live data.
5Preventing It From Happening Again
PostgreSQL gives us timeouts that cap each of these situations, and we can set them per role so the application gets strict limits while a DBA role does not.
Then we alert on the age of the horizon, for example when the largest age(backend_xmin) passes a few million transactions or the oldest xact_start passes an hour, and on replication slots that are inactive with an old xmin. An alert like that would have caught this incident on day one instead of day six.
6Finding the Application Cause
The database only shows the symptom. The application_name and client address in pg_stat_activity point us at the service, and the query column shows the last statement it ran before going idle. Two causes are typical in Java services. One is code that borrows a connection with autocommit off, for example plain JDBC or a batch job, and on some path never calls commit or rollback, while the pool keeps the connection open. The other is a @Transactional method that does its first query and then waits on something slow, such as a remote HTTP call, a message broker or user input, while the transaction stays open. The fix is to keep remote calls outside the transaction, commit or roll back on every path with try-with-resources or Spring's transaction management, and turn on the pool's leak detection (HikariCP's leakDetectionThreshold) to log where a connection was held too long.
7Follow-up questions
Which query shows the oldest backend_xmin?
SELECT pid, state, xact_start, age(backend_xmin) FROM pg_stat_activity WHERE backend_xmin IS NOT NULL ORDER BY age(backend_xmin) DESC LIMIT 1. Including backend_xid too catches sessions that wrote and then went idle, and checking pg_replication_slots and pg_prepared_xacts covers the horizon holders that are not sessions.
Does the long transaction also block other things?
Yes. It may hold row locks it took, block DDL that needs a strong lock on the table, and, if it holds the horizon long enough, prevent freezing, which pushes the cluster towards transaction id wraparound.
Why not just make autovacuum more aggressive?
Because the problem is not vacuum frequency. Any vacuum, however often it runs, must respect the horizon. More aggressive settings only burn more IO scanning the same unremovable tuples.
8Conclusion
In this answer, we saw that VACUUM can only remove dead tuples older than the xmin horizon, the oldest snapshot in use, and that a six-day idle-in-transaction session pins that horizon so every newer version stays and the table bloats. Long queries, abandoned replication slots, prepared transactions and hot_standby_feedback can do the same. We terminate the holder, let vacuum catch up, reclaim space with pg_repack rather than VACUUM FULL, add idle_in_transaction_session_timeout, statement_timeout and transaction_timeout with alerts on age(backend_xmin), and fix the code that left the transaction open.
Open with
The link is the xmin horizon. VACUUM can only remove a dead tuple if no snapshot that's still in use could see it, so it looks for the oldest snapshot in the cluster.
Then, if they want more
The session that's been idle in transaction for six days still holds its snapshot, so every row version replaced since then has to be kept. With heavy updates that's millions of dead tuples, so the table goes from 5 to 80 gigabytes, scans get slower, and autovacuum keeps running because of the dead tuple count but can't remove them.
I'd confirm it with pg_stat_activity ordered by age of backend_xmin, and also check replication slots, prepared transactions and hot_standby_feedback on replicas, which hold the horizon too. The immediate fix is pg_terminate_backend on that pid, then a manual vacuum. To get disk back I'd use pg_repack, because VACUUM FULL takes an exclusive lock for the whole rewrite.
To prevent it I'd set idle_in_transaction_session_timeout and statement_timeout on the application role, transaction_timeout on PostgreSQL 17, and alert on age of backend_xmin. Then I'd find the code: usually a connection with autocommit off that's never committed, or a transactional method waiting on a remote call.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A PostgreSQL table with heavy updates grows from 5 GB to 80 GB in a week, queries slow...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/fb8a870f8c/