Scrivio Interview library
DebuggingFoundations7 min read · updated 12 November 2026

A queue table jobs(id, status, payload) receives 2,000 inserts and 2,000 deletes per second. The table holds about 10,000 live rows, but its size grew to 40 GB and SELECT ... WHERE status = 'NEW' LIMIT 10 FOR UPDATE SKIP LOCKED became slow. What is happening and what would you check?

The table is full of dead rows: 2,000 deletes per second create dead tuples faster than vacuum removes them, so the table and its indexes have bloated to 40 GB and the SKIP LOCKED query walks through mostly dead entries before finding 10 live ones. We would check pg_stat_user_tables for dead tuples and autovacuum activity, look for long transactions, idle-in-transaction sessions and stale replication slots that stop vacuum from removing anything, tune autovacuum aggressively for this table, recover the space once with pg_repack, and consider time partitions that are dropped instead of deleted from.

Why this question is asked

Using a PostgreSQL table as a job queue is common and works well, with FOR UPDATE SKIP LOCKED letting many workers take jobs without blocking each other. But a queue is the worst case for MVCC: every row is inserted, updated or deleted within seconds, so the table produces dead rows at the same rate it processes jobs. Interviewers ask this debugging question because it tests whether we know how dead tuples, vacuum and the xmin horizon interact. In this answer, we will see why 10,000 live rows can take 40 GB, check what is happening step by step, find what holds vacuum back, tune it, and reclaim the space.

1A Quick Recap: Where the 40 GB Comes From

In PostgreSQL, a DELETE does not remove a row. It marks the row version as deleted by our transaction, and the row stays in the page until VACUUM removes it, once no running snapshot can still see it. Index entries pointing to the row also stay until vacuum cleans them.

Our queue inserts 2,000 jobs and deletes 2,000 jobs every second. That is 7.2 million dead tuples per hour. If vacuum cannot keep up, the table holds millions of dead rows next to its 10,000 live ones. Every dead row still occupies space, so a table that should be a few megabytes reaches 40 GB.

2Why the Query Became Slow

The workers run SELECT ... WHERE status = 'NEW' LIMIT 10 FOR UPDATE SKIP LOCKED. Using an index on status, PostgreSQL walks index entries for 'NEW' in order. For each entry, it must visit the table to check whether that row version is visible to it. If the first million entries point to dead rows, it visits a million dead rows before it finds ten live ones.

So the query does not slow down because there are more jobs; it slows down because it reads through a graveyard. And it gets worse over time, because the dead rows are always at the front of the index, where the oldest jobs were.

Java
EXPLAIN (ANALYZE, BUFFERS)
SELECT id FROM jobs WHERE status = 'NEW' LIMIT 10 FOR UPDATE SKIP LOCKED;
-- Limit (actual rows=10)
-- -> LockRows
-- -> Index Scan using jobs_status_idx on jobs
-- Buffers: shared hit=48210 read=91544 -- 140,000 pages read for 10 rows
-- Execution Time: 2380 ms

3Is Autovacuum Keeping Up?

pg_stat_user_tables shows the counts PostgreSQL keeps for each table. We compare dead and live tuples, and look at when autovacuum last ran and how often.

SQL
SELECT n_live_tup, n_dead_tup, last_autovacuum, autovacuum_count,
pg_size_pretty(pg_total_relation_size('jobs')) AS total_size
FROM pg_stat_user_tables WHERE relname = 'jobs';
-- 10214 | 38455102 | 2026-11-05 03:12 | 2 | 40 GB

Two very different stories can produce this output. Either autovacuum rarely runs or runs too slowly, a tuning problem, or it runs but cannot remove anything because something holds back the xmin horizon. We must check the second before tuning the first, because tuning will not help if vacuum is blocked.

4What Holds Back the Horizon?

The xmin horizon is the oldest transaction that any snapshot may still need. Vacuum may only remove row versions deleted before that point. Three things commonly hold it back: a long-running transaction, a session left idle in transaction (the application began a transaction and never committed), and a replication slot whose consumer has stopped. Prepared transactions left behind by two-phase commit do the same.

SQL
-- oldest open transactions
SELECT pid, state, now() - xact_start AS age, backend_xmin, left(query, 60)
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL ORDER BY xact_start LIMIT 5;
-- 31877 | idle in transaction | 05:47:12 | 88123401 | SELECT * FROM report_view ...
-- slots that keep old rows
SELECT slot_name, active, xmin, catalog_xmin FROM pg_replication_slots;
-- forgotten prepared transactions
SELECT gid, prepared FROM pg_prepared_xacts;
Step throughDiagnosing a bloated queue table
  1. Many dead tuples in pg_stat_user_tables?
    YesFind out why vacuum does not remove themNoLook elsewhere: plan, locks
  2. Old backend_xmin, idle in transaction, slot or prepared xact?
    YesRemove the blocker and fix its causeNoVacuum is too slow: tune it
  3. Tune autovacuum for jobsscale factor 0, threshold 1000, no cost delay
  4. Reclaim space oncepg_repack, or VACUUM FULL in a window
  5. Consider partitions dropped instead of rows deleted

5 steps in order. Play them, or step through them one at a time.

Here a session has been idle in transaction for almost six hours. While it lives, vacuum cannot remove any job deleted in those six hours, about 43 million rows. Ending it (pg_terminate_backend(31877)) and fixing the code that left it open is the first fix. Only then does tuning make sense.

5Tune Autovacuum for This Table

With the defaults, autovacuum waits until dead tuples exceed 50 + 0.2 x rows. That sounds harmless, but it is tied to the table's row count estimate, and for a queue the rule fits badly: we want vacuum to run continuously and quickly, triggered by a fixed number of dead rows. We also remove the cost delay, which deliberately slows vacuum down to protect other I/O, because this table is small once it is clean and must be cleaned fast.

SQL
ALTER TABLE jobs SET (
autovacuum_vacuum_scale_factor = 0,
autovacuum_vacuum_threshold = 1000, -- vacuum after about 1,000 dead rows
autovacuum_vacuum_cost_delay = 0 -- run at full speed
);

After this, autovacuum runs every few seconds on jobs, and each run is cheap because there are only thousands of dead rows to remove each time.

6Reclaim the Space and Rethink the Design

Tuning stops new bloat but does not give back the 40 GB, because plain vacuum keeps the space inside the file for reuse. To shrink the table once, we use pg_repack, which rebuilds the table and indexes online with only short locks, or VACUUM FULL in a maintenance window, which locks the table completely while it runs; on a table with 10,000 live rows, that may take only seconds once the dead rows are gone, but it must still read the whole 40 GB file.

For a long-term design, partitioning by time helps: jobs go into, say, hourly partitions, and once every job in a partition is done we drop or truncate the whole partition. Dropping a partition removes its rows instantly with no dead tuples at all, so vacuum has almost nothing to do.

7Follow-up questions

Why does a scale factor of 0.2 work badly for this table?

With 10,000 rows, 0.2 gives a threshold of about 2,050 dead rows, which sounds low, so the factor alone is not the whole story. The trouble is that the trigger is proportional to the row estimate, and a queue's row count swings: after a backlog of a million jobs, the threshold jumps to about 200,000. And once a run starts on a bloated file, the default cost delay throttles it, so at 2,000 deletes a second more dead rows arrive during the run than it removes. A fixed small threshold with no cost delay makes vacuum predictable: short, frequent runs, whatever the backlog.

How does pg_stat_activity show who holds the horizon?

The backend_xmin column shows the oldest transaction id each session's snapshot still needs, and backend_xid shows a session's own transaction id if it has written. Sorting by age(backend_xmin) or by xact_start shows the oldest. The state column reveals idle in transaction sessions, and query shows the last statement they ran, which usually points at the code responsible.

Would a different index help instead?

A partial index such as ON jobs (id) WHERE status = 'NEW' keeps the index small and focused, which helps, but dead entries still accumulate in it until vacuum removes them. It complements vacuum tuning; it does not replace it.

8Conclusion

In this answer, we saw that a queue table with 2,000 deletes per second produces dead tuples faster than default autovacuum removes them, so 10,000 live rows sat among tens of millions of dead ones, and SKIP LOCKED scans read through them. We checked pg_stat_user_tables, then looked for blockers of the xmin horizon in pg_stat_activity, replication slots and prepared transactions, tuned autovacuum for the table with a zero scale factor, a fixed threshold and no cost delay, reclaimed the space with pg_repack, and considered time partitions that are dropped instead of deleted.

What to say in the interviewAbout 80 seconds aloud

Open with

The table is bloated with dead tuples. Every delete leaves a dead row version until vacuum removes it, and at 2,000 deletes a second that is over 7 million an hour, so the 10,000 live rows sit among tens of millions of dead ones and the SKIP LOCKED query walks through dead index entries and heap rows before finding ten live jobs.

Then, if they want more

First I would check pg_stat_user_tables: n_dead_tup, last_autovacuum and autovacuum_count. Then, before tuning anything, I would check whether vacuum is even allowed to remove those rows: long-running transactions or idle in transaction sessions in pg_stat_activity with an old backend_xmin, replication slots with an old xmin, and orphaned prepared transactions.

Any of those holds back the xmin horizon, and no tuning helps until it is gone. Once vacuum can work, I would tune it for this table: autovacuum_vacuum_scale_factor 0, a fixed threshold around 1,000, and cost delay 0, so it runs often and fast.

To get the 40 GB back I would run pg_repack, or VACUUM FULL in a short window. And longer term I would consider partitioning the queue by time and dropping finished partitions instead of deleting rows.

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
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 queue table jobs(id, status, payload) receives 2,000 inserts and 2,000 deletes per...": https://scrivio-interview-library.netlify.app/databases/postgresql/5293dd2973/