Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

Autovacuum on your largest table (3 billion rows) runs for 18 hours, is cancelled by a nightly DDL job, and restarts from scratch. Bloat and wraparound age keep growing. What would you change?

Autovacuum gives way to anyone waiting for a conflicting lock, so the nightly DDL job cancels it after 18 hours of work and it starts again from scratch, never finishing. We would stop running conflicting DDL during vacuum, make vacuum much faster for this table with higher cost limits, lower cost delay and more maintenance memory, run a supervised manual VACUUM with parallel index cleanup in a quiet period, partition the table in the long run, and alert on age(relfrozenxid) and dead tuples well before they become an emergency.

Why this question is asked

Huge tables expose vacuum problems that small tables never show. Eighteen hours is longer than the gap between nightly jobs, and a cancelled vacuum wastes all its work. Meanwhile dead rows and transaction ID age keep climbing until PostgreSQL forces an aggressive vacuum at the worst moment. Interviewers ask this scenario to see whether we understand vacuum's locking, what makes it slow, and how to design a large table so vacuum stays manageable. In this answer, we will see why the job cancels vacuum, make vacuum faster, run it safely by hand, and fix the design with partitioning and monitoring.

1Why the Nightly DDL Cancels Autovacuum

Vacuum takes a SHARE UPDATE EXCLUSIVE lock on the table. It does not block reads or writes, but it conflicts with itself and with stronger locks such as those taken by most ALTER TABLE forms, CREATE INDEX (without CONCURRENTLY), ANALYZE run by hand, and TRUNCATE.

Autovacuum is designed to be polite. When another session waits for a lock that conflicts with autovacuum's, after deadlock_timeout (1 second by default) the server cancels the autovacuum worker so the other session can proceed. The log shows 'canceling autovacuum task'. There is one exception: an anti-wraparound autovacuum, started because the table's transaction ID age passed autovacuum_freeze_max_age, does not give way, and the DDL waits behind it instead, which can block everything queued behind the DDL.

Step throughThe vacuum that never finishes
Autovacuumbig_tableNightly DDL job
08:00 start vacuum, SHARE UPDATE EXCLUSIVE
scan heap, clean indexes for 18 hours
02:00 ALTER TABLE, waits for a stronger lock
after deadlock_timeout: cancel autovacuum
DDL runs
later: start again from the first page

6 messages in order. Play them, or step through them one at a time.

Unless the vacuum is an anti-wraparound one, it gives way and loses its work.

So every night at, say, 02:00, the DDL job, perhaps an ALTER TABLE that adjusts a column default or a non-concurrent index build, cancels 18 hours of vacuum work. Vacuum restarts from the beginning and is cancelled again the next night.

2First Fix: Stop the Conflict

The quickest win is removing the conflict. We look at what the nightly job does and whether it needs a strong lock at all: CREATE INDEX CONCURRENTLY takes a SHARE UPDATE EXCLUSIVE lock too, so it waits for vacuum instead of cancelling it, and many schema changes can be done once instead of nightly. If the job must run, we schedule it for when no vacuum is in progress on that table, checking pg_stat_progress_vacuum first, or we move it to a weekly window after vacuum completes.

3Second Fix: Make Vacuum Faster

An 18-hour vacuum is mostly a throttling and memory problem. By default autovacuum sleeps every time it has done a certain amount of I/O work, controlled by the cost limit and cost delay, so it barely affects other queries. On a 3-billion-row table this pacing is far too gentle. We raise the cost limit and lower the delay for this table only.

Memory matters too. Vacuum collects the ids of dead tuples in memory, sized by maintenance_work_mem or autovacuum_work_mem. When that memory fills, it must stop, scan every index of the table to remove those entries, and then continue scanning the table. With many indexes, each extra index pass costs hours. Before PostgreSQL 17, the memory was also capped at 1 GB. PostgreSQL 17 stores dead tuple ids in a much more compact structure and removed that cap, so a single pass can cover far more dead rows; check your version.

SQL
ALTER TABLE big_table SET (
autovacuum_vacuum_cost_limit = 4000, -- default effectively 200
autovacuum_vacuum_cost_delay = 1 -- milliseconds; default 2
);
ALTER SYSTEM SET autovacuum_work_mem = '2GB'; -- per worker; size against RAM
SELECT pg_reload_conf();

We also make sure the storage can take the extra I/O; on cloud disks we may need more provisioned throughput during the catch-up.

4Third Fix: Run a Supervised Manual Vacuum

To catch up, we run vacuum ourselves in a quiet period, with the DDL job paused. A manual VACUUM is not cancelled by lock waiters the way autovacuum is; other DDL simply waits. It also does not use the autovacuum cost limits unless we set vacuum_cost_delay, so it runs at full speed by default. The PARALLEL option lets several workers clean indexes at the same time, which helps a table with many large indexes. We watch its progress in pg_stat_progress_vacuum.

SQL
SET maintenance_work_mem = '4GB';
VACUUM (VERBOSE, PARALLEL 4) big_table;
-- in another session
SELECT phase, heap_blks_scanned, heap_blks_total, index_vacuum_count
FROM pg_stat_progress_vacuum WHERE relid = 'big_table'::regclass;
-- vacuuming indexes | 51203344 | 98304000 | 1

Notice index_vacuum_count. If it climbs above 1 or 2, vacuum is running out of memory for dead tuple ids and doing extra index passes.

5Long Term: Partition and Monitor

The lasting fix is to stop having one 3-billion-row table. With partitioning, by month for example, each partition is a separate table that autovacuum handles separately. A vacuum of the current partition takes minutes, old partitions that no longer change are frozen once and then rarely need work, and data past its retention can be detached or dropped instead of deleted, which creates no dead rows at all.

Finally, we make sure this never becomes an emergency again. We alert on age(relfrozenxid) for the largest tables well before autovacuum_freeze_max_age, on n_dead_tup above a set ratio, and on autovacuum cancellations in the log.

SQL
SELECT relname, age(relfrozenxid) AS xid_age, n_dead_tup
FROM pg_class c JOIN pg_stat_user_tables s ON s.relid = c.oid
ORDER BY age(relfrozenxid) DESC LIMIT 5;
-- big_table | 412000000 | 690000000
Step throughGetting the big table back under control
  1. Pause or reschedule the conflicting DDLor switch to CONCURRENTLY forms
  2. Raise cost limit, lower delay, more vacuum memoryfor this table
  3. Manual VACUUM (VERBOSE, PARALLEL 4)watch pg_stat_progress_vacuum
  4. Partition the table by timesmall vacuums, drop old data
  5. Alert on xid age and dead tuplesbefore it is an emergency

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

6Follow-up questions

Which lock does autovacuum hold, and which statements conflict with it?

It holds SHARE UPDATE EXCLUSIVE on the table. That lock conflicts with itself, so two vacuums or a vacuum and an ANALYZE, CREATE INDEX CONCURRENTLY or some ALTER TABLE forms cannot run together, and with all stronger locks: SHARE (plain CREATE INDEX), SHARE ROW EXCLUSIVE, EXCLUSIVE and ACCESS EXCLUSIVE (most ALTER TABLE, DROP, TRUNCATE, VACUUM FULL). SELECT, INSERT, UPDATE and DELETE take weaker locks and do not conflict.

What happens if we do nothing?

Dead tuples keep growing, so the table and its indexes bloat and queries slow down. Eventually age(relfrozenxid) passes autovacuum_freeze_max_age, and an anti-wraparound vacuum starts that will not yield; the nightly DDL then queues behind it and blocks other queries behind itself. In the worst case PostgreSQL approaches the wraparound limit and refuses new transaction ids.

Does vacuum_failsafe help here?

Yes, as a last line of defence. When a table's age passes vacuum_failsafe_age, vacuum skips index cleanup and ignores cost delays to finish freezing as quickly as possible. It prevents wraparound but leaves bloat for later.

7Conclusion

In this answer, we saw that autovacuum yields to lock waiters, so a nightly DDL job cancels an 18-hour vacuum that then restarts from scratch, unless it is an anti-wraparound vacuum, which does not yield. We removed the conflict, made vacuum faster with a higher cost limit, lower cost delay and more memory to avoid extra index passes, ran a supervised manual VACUUM with parallel index cleanup while watching pg_stat_progress_vacuum, and planned partitioning plus alerts on age(relfrozenxid) and dead tuples.

What to say in the interviewAbout 80 seconds aloud

Open with

The problem is that autovacuum is designed to give way. It holds a SHARE UPDATE EXCLUSIVE lock, and when the nightly DDL job waits for a conflicting lock, the autovacuum worker is cancelled after deadlock_timeout and all 18 hours of work are lost.

Then, if they want more

The only exception is an anti-wraparound vacuum, which does not yield. So first I would remove the conflict: pause or reschedule the job, or use CONCURRENTLY forms that wait instead of cancelling.

Second, I would make vacuum faster for this table: raise autovacuum_vacuum_cost_limit and lower the cost delay, and raise autovacuum_work_mem or maintenance_work_mem so vacuum can hold more dead tuple ids and needs fewer passes over the indexes. On PostgreSQL 17 that memory is used much more compactly and the old 1 GB cap is gone.

Third, to catch up, I would run a manual VACUUM VERBOSE with PARALLEL for the index phase in a quiet period, and watch pg_stat_progress_vacuum, especially index_vacuum_count. Long term, I would partition the table so each partition is vacuumed separately and old data can be dropped instead of deleted.

And I would alert on age(relfrozenxid) and n_dead_tup well before they reach dangerous levels.

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
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 "Autovacuum on your largest table (3 billion rows) runs for 18 hours, is cancelled by a...": https://scrivio-interview-library.netlify.app/databases/postgresql/30f9f10fae/