Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

Monitoring shows age(datfrozenxid) for your main database at 1.6 billion and rising, and the warning 'database must be vacuumed within 400000000 transactions' is near. What is your plan during the next hours?

We treat it as an incident with a clear order: first remove whatever stops vacuum from freezing, meaning long transactions, idle in transaction sessions, orphaned prepared transactions and replication slots holding xmin or catalog_xmin. Then find the tables with the oldest relfrozenxid and run VACUUM (FREEZE, VERBOSE) on them at full speed where nothing can cancel it, while slowing transaction id consumption. Afterwards we add alerts at a safe threshold, tune autovacuum for the big tables and hold a post-incident review.

Why this question is asked

At 1.6 billion, the database is not down yet, but it is on a clock. Every transaction brings it closer to the point where PostgreSQL refuses new transaction ids and all writes stop. The pressure tempts people to start vacuuming everything at once or to restart the server, neither of which helps much. Interviewers ask this scenario to see whether we can work calmly in the right order. In this answer, we will recap what the number means, find and remove the blockers, freeze the oldest tables, slow consumption meanwhile, and plan the follow-up.

1What 1.6 Billion Means

age(datfrozenxid) is the number of transactions since the oldest unfrozen transaction id that any table in the database may still contain. PostgreSQL can distinguish about 2.1 billion ids in the past, so at 1.6 billion we have roughly 500 million transactions left before the hard stop. With default settings, PostgreSQL itself logs the 'must be vacuumed within N transactions' warning only once about 40 million ids remain, and refuses new transaction ids at about 3 million left (check your version). So at 1.6 billion we are still ahead of the server's own warnings, but far past the 200 million where autovacuum should have kept us, and we plan with a working margin of about 400 million.

How long that gives us depends on the rate. We measure it rather than guess, by sampling the current xid twice.

SQL
SELECT pg_current_xact_id(); -- 1904221873
-- ten minutes later
SELECT pg_current_xact_id(); -- 1907821873
-- 3.6 million per 10 minutes = about 21.6 million per hour
-- 400 million / 21.6 million = about 18 hours of headroom

Eighteen hours is enough if we act in the right order, and far too little if we spend it vacuuming tables while something still blocks freezing.

2Find What Blocks Freezing

Vacuum can only freeze rows older than the xmin horizon, the oldest transaction any snapshot or slot still needs. If autovacuum has been running and age still grows, something is almost certainly holding that horizon. So we check every kind of holder before starting any manual vacuum: open transactions, sessions idle in transaction, prepared transactions from two-phase commit, and replication slots. A logical slot holds catalog_xmin, which blocks freezing of system catalogs; a physical slot used with hot_standby_feedback can hold xmin for user tables.

SQL
SELECT pid, state, now() - xact_start AS age, backend_xmin
FROM pg_stat_activity WHERE backend_xmin IS NOT NULL
ORDER BY age(backend_xmin) DESC LIMIT 5;
SELECT gid, prepared, owner, database FROM pg_prepared_xacts; -- orphans: ROLLBACK PREPARED 'gid'
SELECT slot_name, slot_type, active, xmin, catalog_xmin, age(xmin), age(catalog_xmin)
FROM pg_replication_slots; -- inactive and old: drop after confirming with the owner
Step throughIncident order
  1. Measure xid consumption ratehours of headroom
  2. Remove blockerssessions, prepared transactions, slots
  3. List tables by age(relfrozenxid)oldest first
  4. VACUUM (FREEZE, VERBOSE) the oldest tablesfull speed, nothing that cancels it
  5. Is age(datfrozenxid) falling?
    YesContinue down the listNoLook for a blocker missed in step 2
  6. Reduce consumption meanwhilebatch writes, pause jobs

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

We end old sessions with pg_terminate_backend(), roll back orphaned prepared transactions with ROLLBACK PREPARED, and drop abandoned slots after checking with their owner, knowing the consumer will need a fresh snapshot afterwards.

3Find the Oldest Tables

The database's age is the age of its oldest table, so a few tables usually account for the whole problem. We list tables by age, including TOAST tables, which hold large column values and have their own relfrozenxid.

SQL
SELECT c.oid::regclass AS table_name, age(c.relfrozenxid) AS xid_age,
pg_size_pretty(pg_total_relation_size(c.oid)) AS size
FROM pg_class c
WHERE c.relkind IN ('r', 'm', 't')
ORDER BY age(c.relfrozenxid) DESC LIMIT 10;
-- events | 1600412883 | 2.1 TB
-- pg_toast.pg_toast_16422 | 1600412001 | 300 GB
-- orders | 988120355 | 410 GB

Notice that only the events table and its TOAST table are near the limit; orders is old but has more room. We freeze in that order.

4Freeze the Oldest Tables at Full Speed

There is probably already an anti-wraparound autovacuum running on events. If it is progressing well, we can let it run; if it is crawling because of cost delays, we run a manual vacuum instead. A manual VACUUM does not use autovacuum's cost limits unless we set vacuum_cost_delay, and it is not cancelled by lock waiters. We give it plenty of memory, use PARALLEL for index cleanup, and make sure no job or DDL will interfere. If time is very short, INDEX_CLEANUP OFF skips index vacuuming so freezing finishes sooner, which is what the failsafe does automatically.

SQL
SET maintenance_work_mem = '4GB';
SET vacuum_cost_delay = 0;
VACUUM (FREEZE, VERBOSE, PARALLEL 4) events;
-- if headroom is under a few hours:
-- VACUUM (FREEZE, VERBOSE, INDEX_CLEANUP OFF) events;
SELECT phase, heap_blks_scanned, heap_blks_total
FROM pg_stat_progress_vacuum; -- watch progress

We run separate sessions for different large tables if the storage has spare I/O, and we keep checking age(datfrozenxid) to make sure it falls.

5Slow Down Consumption Meanwhile

Every write transaction uses an xid, so we buy time by using fewer. We pause non-essential write jobs such as backfills, and ask teams to batch tiny transactions: a job that commits one row at a time uses one xid per row, while committing 1,000 rows per transaction uses one per thousand. Subtransactions also consume xids, so code that wraps every row in a savepoint, which some ORMs and PL/pgSQL exception blocks do, burns ids quickly.

6Afterwards: Make Sure It Does Not Return

Once the age is back near normal, we fix the conditions that allowed it. We alert at a safe threshold, for example age(datfrozenxid) above 500 million, which is far from the hard limit but well past where autovacuum should keep it. We tune autovacuum for the largest tables, with higher cost limits and more memory, and consider partitioning the biggest ones so each partition can be frozen separately. Finally we hold a blameless post-incident review covering why the blocker existed, why nobody was alerted earlier, and what standard prevents it elsewhere.

7Follow-up questions

Why do subtransactions and per-row commits speed up consumption?

A transaction gets an xid when it first writes, so committing every row separately uses one xid per row: a job loading 10 million rows uses 10 million xids instead of 10,000 when batched by 1,000. Each subtransaction, created by a SAVEPOINT or by a PL/pgSQL block with an EXCEPTION clause, gets its own xid when it writes too. A loop that writes one row per savepoint therefore uses one id per row even inside one big transaction, and heavy subtransaction use also has other performance costs.

Should we restart PostgreSQL or enter single-user mode now?

No. A restart does not freeze anything and kills the vacuums in progress. Single-user mode was traditionally the last resort after the server stopped issuing xids; current documentation recommends a normal VACUUM by a superuser in that situation, check your version's documentation. At 1.6 billion the server still accepts connections, so we work online.

What if the vacuum itself is too slow for the headroom?

Skip index cleanup with INDEX_CLEANUP OFF, run vacuums on different tables in parallel sessions, add I/O capacity if the cloud allows, and cut consumption harder. Freezing the heap is what moves relfrozenxid; index cleanup can wait.

8Conclusion

In this answer, we treated age(datfrozenxid) at 1.6 billion as an incident with an ordered plan. We measured the consumption rate to know our headroom, removed blockers first, long and idle transactions, orphaned prepared transactions and replication slots holding xmin or catalog_xmin, then listed tables by age(relfrozenxid) and ran VACUUM (FREEZE, VERBOSE) at full speed on the oldest, while batching tiny transactions and pausing jobs to slow consumption. Afterwards we added alerts at about 500 million, tuned autovacuum for large tables and held a post-incident review.

What to say in the interviewAbout 80 seconds aloud

Open with

First I would measure how fast we consume transaction ids, by sampling pg_current_xact_id a few minutes apart, so I know how many hours of headroom we really have.

Then, if they want more

Then, before vacuuming anything, I would find what stops freezing, because if autovacuum has been running and the age still rises, something is holding the horizon. That means long-running and idle in transaction sessions in pg_stat_activity, orphaned prepared transactions in pg_prepared_xacts, and replication slots with an old xmin or catalog_xmin.

I would terminate those sessions, roll back orphaned prepared transactions and drop abandoned slots after checking with their owners. Next I would list tables by age of relfrozenxid, including TOAST tables, because usually one or two big tables are the whole problem.

On those I would run VACUUM FREEZE VERBOSE with no cost delay, lots of maintenance_work_mem and parallel index cleanup, making sure no DDL or job interferes, and if time is very short, with index cleanup off. Meanwhile I would reduce consumption: pause non-essential write jobs and batch per-row commits.

Once age(datfrozenxid) is falling and back to normal, I would add alerts at around 500 million, tune autovacuum for the large tables, consider partitioning, and run a post-incident review.

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 "Monitoring shows age(datfrozenxid) for your main database at 1.6 billion and rising, and...": https://scrivio-interview-library.netlify.app/databases/postgresql/f12f4e5031/