Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is transaction ID wraparound in PostgreSQL, and what does freezing do to prevent it?

PostgreSQL transaction ids are 32-bit numbers compared in a circle, so only about 2 billion ids in the past and 2 billion in the future can be told apart. If an old row kept its creating xid forever, after enough new transactions it would seem to come from the future and become invisible, which is data loss. Freezing marks old rows as visible to everyone so their xid no longer matters; vacuum does it routinely, autovacuum forces it when a table gets too old, and near the limit PostgreSQL stops issuing new transaction ids to protect the data.

Why this question is asked

Transaction ID wraparound sounds like a theoretical edge case, but it has caused real multi-hour write outages on large, busy databases. It is also the reason vacuum is needed even on tables that never get updates. Interviewers ask about it to see whether we understand how PostgreSQL compares transaction ids and why freezing is part of normal maintenance. In this answer, we will look at how xids are compared, see what would go wrong without freezing, explain what freezing does and when vacuum does it, and see how PostgreSQL protects itself near the limit.

1A Quick Recap: Every Row Remembers Its Transaction

Each row version stores xmin, the id of the transaction that created it. When a query decides whether it can see a row, it asks whether xmin committed before the query's snapshot, which means comparing xmin with the current transaction ids.

Transaction ids, xids, are 32-bit integers. That gives about 4.2 billion values. A busy system running 10,000 write transactions per second uses them up in roughly 5 days. So the counter must eventually start again from the bottom, and PostgreSQL has to make comparisons work across that wrap.

2How Circular Comparison Works

PostgreSQL treats the xid space as a circle. For any transaction, the 2 billion ids before it on the circle count as the past, and the 2 billion after it count as the future. Comparison uses 32-bit modular arithmetic, so it keeps working when the counter wraps from about 4.29 billion back to 3 (ids 0 to 2 are reserved for special purposes).

This only works while every xid we still compare is within 2 billion of the current one. An xid further back than that falls off the past half of the circle and lands in the future half.

How the current transaction sees other xids
xid comparedDistance from currentTreated as
current minus 1,0001,000 behindpast: visible if committed
current minus 1.9 billion1.9 billion behindpast, but close to the edge
current minus 2.2 billionbeyond the half circlefuture: invisible
frozennot comparedalways in the past

3What Would Go Wrong Without Freezing

Imagine a customers row inserted by xid 100 and never updated. Years later the database has run 2.2 billion more transactions. Compared circularly, xid 100 is now more than 2 billion behind the current id, which means it falls into the future half. A query would conclude the row was created by a transaction that has not happened yet, and the row would vanish from every result.

Nothing would be deleted on disk, but to the application the data would be gone, which is why this is treated as data loss. Freezing exists to make sure no row ever gets that old without being marked.

4What Freezing Does

Freezing marks a row version as created so long ago that it is visible to every transaction, now and in the future. Modern PostgreSQL does this by setting a flag in the row header (older versions replaced xmin with the special FrozenTransactionId, 2). Once frozen, the row's xmin is never compared again, so wraparound cannot affect it.

Vacuum freezes rows as it goes. A row becomes eligible when its xmin is older than vacuum_freeze_min_age, 50 million transactions by default. Each table records the oldest unfrozen xid it may contain in pg_class.relfrozenxid, and each database records the oldest across its tables in pg_database.datfrozenxid. The age() function tells us how many transactions ago that was.

SQL
SELECT datname, age(datfrozenxid) FROM pg_database ORDER BY 2 DESC;
-- shop | 183402119
-- postgres | 42001771
SELECT relname, age(relfrozenxid) FROM pg_class
WHERE relkind = 'r' ORDER BY 2 DESC LIMIT 3;
-- events | 183402119
-- orders | 96011420
-- customers | 71200388

Notice that the database's age equals its oldest table's age. The events table decides how close the whole database is to trouble.

5Anti-Wraparound Vacuum and the Last Line of Defence

Normal vacuums can skip pages that the visibility map marks all-visible, so some old rows may stay unfrozen. When a table's age(relfrozenxid) passes autovacuum_freeze_max_age, 200 million by default, autovacuum starts an aggressive anti-wraparound vacuum on it that scans every page that is not already all-frozen. It does this even if autovacuum is disabled for that table, and it does not give way to lock waiters the way normal autovacuum does.

If freezing still cannot keep up, for example because a long-running transaction stops vacuum from freezing anything, PostgreSQL protects the data. As the remaining ids drop to around 40 million it logs warnings that the database must be vacuumed within N transactions. At about 3 million remaining it refuses to assign new transaction ids, so every write fails until an administrator runs VACUUM. Since PostgreSQL 14, vacuum_failsafe_age (1.6 billion by default) also makes vacuum drop its cost delay and skip index cleanup to freeze as fast as possible.

Step throughHow age is kept in check
  1. Rows older than vacuum_freeze_min_agefrozen by routine vacuums (50 million)
  2. age(relfrozenxid) above autovacuum_freeze_max_age?
    YesAggressive anti-wraparound vacuumNoRoutine vacuums continue
  3. Age above vacuum_failsafe_age?
    YesFailsafe: no cost delay, skip index cleanupNoContinue
  4. About 40 million ids leftwarnings in the log
  5. About 3 million ids leftnew transaction ids refused: writes stop

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

6Follow-up questions

How do you query the database with the oldest frozen id?

SELECT datname, age(datfrozenxid) FROM pg_database ORDER BY 2 DESC LIMIT 1; shows the database closest to the limit. Inside it, SELECT relname, age(relfrozenxid) FROM pg_class WHERE relkind IN ('r', 'm', 't') ORDER BY 2 DESC LIMIT 10; shows the tables, materialized views and TOAST tables holding it back. We alert on these well before 200 million is exceeded for long, for example at 500 million.

Does freezing cause write load?

Yes. Freezing modifies pages, so it dirties them and writes WAL, often with full page images. On large append-only tables, the first aggressive vacuum can rewrite most of the table. Since PostgreSQL 13, inserts also trigger autovacuum, which freezes such tables gradually and avoids one huge burst.

Are multixact ids affected too?

Yes. MultiXact ids, used when several transactions lock the same row, are also 32-bit and wrap around, so vacuum freezes them too, tracked by relminmxid and controlled by autovacuum_multixact_freeze_max_age.

7Conclusion

In this answer, we saw that transaction ids are 32-bit and compared in a circle, so only about 2 billion ids in each direction can be distinguished. Without freezing, old rows would eventually look like they come from the future and disappear. Freezing marks rows as visible to all so their xid is never compared again; vacuum does it for rows older than vacuum_freeze_min_age, autovacuum forces an aggressive anti-wraparound vacuum past autovacuum_freeze_max_age even when disabled for the table, and near the limit PostgreSQL refuses new transaction ids, a write outage that protects the data.

What to say in the interviewAbout 80 seconds aloud

Open with

Transaction ids in PostgreSQL are 32-bit, so there are only about 4 billion of them, and the counter wraps around. To make that work, ids are compared on a circle: roughly 2 billion ids behind the current one count as the past and 2 billion ahead count as the future.

Then, if they want more

Every row stores xmin, the id that created it. If an old row kept its raw xmin forever, after about 2 billion more transactions it would land in the future half, every query would think it was created by a transaction that has not happened yet, and the row would become invisible, which is effectively data loss. Freezing prevents that.

Vacuum marks rows older than vacuum_freeze_min_age as frozen, meaning visible to everyone, so their xmin is never compared again. Each table tracks the oldest unfrozen id in relfrozenxid, and when age(relfrozenxid) passes autovacuum_freeze_max_age, 200 million by default, autovacuum runs an aggressive anti-wraparound vacuum, even if autovacuum is disabled for that table, and it will not yield to lock waiters.

If that still cannot keep up, PostgreSQL warns as the remaining ids run low, and close to the limit it refuses to assign new transaction ids, so writes stop until the database is vacuumed.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 13, PostgreSQL 14
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 "What is transaction ID wraparound in PostgreSQL, and what does freezing do to prevent it?": https://scrivio-interview-library.netlify.app/databases/postgresql/7550052169/