Quick revision
The short answer
The table is full of dead row versions. Every claim and status change in PostgreSQL writes a new row version and leaves the old one dead, and if VACUUM falls behind, or cannot remove them because a long-running transaction or a forgotten replication slot holds back the xmin horizon, the claim query has to step over millions of dead index entries to find a few hundred live ones. The fix is to clear whatever holds the horizon, tune autovacuum for this table, use a partial index on pending jobs, and remove finished jobs quickly, ideally by dropping time partitions.
What to remember
In this answer, we saw that each status change and delete leaves a dead tuple, and that when VACUUM falls behind, the claim query has to step over millions of dead index entries. Long-running transactions, abandoned replication slots and prepared transactions hold back the xmin horizon and stop VACUUM entirely, which pg_stat_user_tables and pg_stat_activity reveal. The fix is to clear the horizon, tune autovacuum per table, use a partial index on pending jobs, and remove finished jobs promptly, best by dropping time partitions.
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.
The table is full of dead row versions. Every claim and status change in PostgreSQL writes a new row version and leaves the old one dead, and if VACUUM falls behind, or cannot remove them because a long-running transaction or a forgotten replication slot holds back the xmin horizon, the claim query has to step over millions of dead index entries to find a few hundred live ones. The fix is to clear whatever holds the horizon, tune autovacuum for this table, use a partial index on pending jobs, and remove finished jobs quickly, ideally by dropping time partitions.
Why this question is asked
Queue tables are the worst case for PostgreSQL's storage model: a small live set with a huge churn of updates and deletes. The failure is slow and silent, which makes it a good interview question: the symptom is latency, but the cause is MVCC housekeeping. In this answer, we will recap why updates leave dead rows, see how they slow the claim, find what stops VACUUM from cleaning them, check the statistics that prove it, and then fix it with autovacuum tuning, a partial index, retention and partitioning.
1A Quick Recap of Dead Tuples
PostgreSQL never updates a row in place. An UPDATE writes a new version of the row, called a tuple, and marks the old one as deleted by the updating transaction. A DELETE just marks the tuple. The old versions must stay until no running transaction could still need them, because a transaction with an older snapshot might read them. Once nobody can, they are dead, and VACUUM, usually run automatically by autovacuum, marks their space reusable and removes index entries pointing at them.
A queue table with a million jobs a day sees each job inserted, claimed (PENDING to RUNNING), finished (RUNNING to DONE) and maybe deleted. That is three or four dead tuples per job, several million a day, plus an index entry per version in each index whose columns change, because a status change cannot use the cheaper heap-only update path when status is part of an index or a partial index predicate.
2How Dead Tuples Slow the Claim
The claim reads an index ordered by run_at and stops after ten live pending rows. But the index also holds entries for every dead version that has not been vacuumed yet, and the scan must visit them, check them against the table, and skip them. When only 300 rows are live but 20 million dead entries sit in front of them, a 2-millisecond lookup becomes a 2-second one. The table and index files also grow, so fewer pages fit in memory and more reads go to disk.
3What Holds Back the xmin Horizon
If autovacuum is running but the dead count keeps growing, the usual reason is that VACUUM is not allowed to remove the tuples. VACUUM can only remove versions older than the oldest snapshot anyone might still use, called the xmin horizon. A single transaction that has been open for three weeks, perhaps a forgotten psql session or an application connection that began a transaction and went idle, keeps every tuple deleted since then alive in the whole database.
Other things hold the horizon too: a replication slot whose consumer is gone, a standby with hot_standby_feedback on running a long query, and prepared transactions left behind by a two-phase commit.
- Check dead tuplespg_stat_user_tables: n_dead_tup, last_autovacuum
- Is something holding the xmin horizon?Yesend old transactions, drop dead slotsNoautovacuum is just too slow
- Tune autovacuum for the tablelower scale factor, higher cost limit
- Shrink the claim's searchpartial index on pending jobs
- Remove finished workdelete in batches or drop old partitions
5 steps in order. Play them, or step through them one at a time.
4Fix the Horizon and Tune Autovacuum
First, end what pins the horizon: terminate the ancient idle transaction with pg_terminate_backend, drop the abandoned slot, and set idle_in_transaction_session_timeout, for example 5 minutes, so it cannot happen again. Then VACUUM can actually clean.
Next, make autovacuum visit this table often. By default it triggers when about 20 percent of a table has changed (autovacuum_vacuum_scale_factor = 0.2), which on a churning table is far too late, and it throttles itself to protect I/O. Per-table settings fix both.
VACUUM makes space reusable but rarely shrinks the files. If the table and its indexes have grown to many gigabytes, REINDEX CONCURRENTLY rebuilds the indexes online, and a tool such as pg_repack can rewrite the table without a long exclusive lock.
5Partial Index, Retention and Partitioning
A partial index on run_at WHERE status = 'PENDING' helps in two ways: the claim scans only pending entries, and the index stays tiny, so even its dead entries are few. Finished jobs should not live in the hot table for weeks either. Either delete DONE jobs in small batches every few minutes, or move them into a history table.
The cleanest approach at this volume is to partition the jobs table by time, for example one partition per day by created_at. Old partitions, holding only finished jobs, are removed with DROP TABLE or DETACH PARTITION, which is instant and leaves no dead tuples at all, instead of deleting millions of rows and making VACUUM clean up after them.
6Follow-up questions
When would you move from a database queue to a message broker?
When the queue's needs outgrow what a table does well: many independent consumers each needing every message (fan-out), sustained rates of thousands of messages per second, strict per-key ordering with parallelism, or retention and replay of a stream. A broker like Kafka or RabbitMQ is built for those. A database queue stays attractive while the main benefit is enqueueing in the same transaction as the business change, and the rate is moderate.
Why does autovacuum not keep up with the default settings?
The defaults are tuned for ordinary tables, where waiting until 20 percent of rows have changed is reasonable. On a queue table that is mostly churn, the live set is tiny but the dead set grows by millions a day, so a threshold based on table size triggers late, and the cost-based throttle slows each run. Per-table settings with a fixed threshold fit this pattern.
Does SKIP LOCKED itself cause bloat?
No. SKIP LOCKED only changes how locked rows are treated during the claim. The bloat comes from the updates and deletes that every queue performs, combined with VACUUM being unable or too slow to clean up.
7Conclusion
In this answer, we saw that each status change and delete leaves a dead tuple, and that when VACUUM falls behind, the claim query has to step over millions of dead index entries. Long-running transactions, abandoned replication slots and prepared transactions hold back the xmin horizon and stop VACUUM entirely, which pg_stat_user_tables and pg_stat_activity reveal. The fix is to clear the horizon, tune autovacuum per table, use a partial index on pending jobs, and remove finished jobs promptly, best by dropping time partitions.
Open with
This is bloat. In PostgreSQL every claim and status change writes a new row version and leaves a dead one behind, so a million jobs a day produce several million dead tuples, and because status is in the index, each change also adds index entries.
Then, if they want more
If VACUUM doesn't clean them, the claim's index scan has to step over millions of dead entries to find the few hundred live pending jobs. I'd check pg_stat_user_tables for n_dead_tup and last_autovacuum.
If dead tuples keep growing even though autovacuum runs, something is holding back the xmin horizon, typically a transaction open for weeks, an idle-in-transaction connection, a replication slot whose consumer is gone, or an old prepared transaction. I'd look at pg_stat_activity ordered by xact_start and at pg_replication_slots, end the culprit, and set idle_in_transaction_session_timeout.
Then I'd tune autovacuum for this table, a zero scale factor with a fixed threshold of a few thousand and a higher cost limit, reindex concurrently to recover the bloat, and make sure there's a partial index on run_at for pending jobs only.
Longer term I'd stop deleting rows from the hot table and partition by day, dropping old partitions instead.
About this answer
- Last updated
- 12 November 2026
- 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 queue table processes a million jobs a day. After a few weeks, claiming a...": https://scrivio-interview-library.netlify.app/databases/database-locking/8fa10ec085/