In PostgreSQL, how do you measure how close each database and table is to XID wraparound, and which freeze settings act at which age?
answer
- one column per table, one per database
- oldest unfrozen XID
- age() counts transactions, not time
- don't forget the TOAST table
- 50, 150, 200 million
basics
~10 sQuery age(datfrozenxid) from pg_database and age(relfrozenxid) from pg_class, taking the larger of each table and its TOAST table. Compare with vacuum_freeze_table_age (150M), autovacuum_freeze_max_age (200M) and the roughly 2.1-billion hard limit.
solid answer
~40 s`pg_class.relfrozenxid` is the oldest unfrozen XID that can remain in a table after its last relfrozenxid-advancing VACUUM, and `pg_database.datfrozenxid` is the minimum of those across a database. `age(xid)` returns how many transactions ago that XID was, so check `age(datfrozenxid)` per database, then `age(relfrozenxid)` per table, taking the greater of the table and its TOAST table. The settings act at successive ages: `vacuum_freeze_min_age` (50 million) decides which rows a VACUUM freezes; past `vacuum_freeze_table_age` (150 million) a VACUUM turns aggressive; past `autovacuum_freeze_max_age` (200 million, restart to change) autovacuum forces a run; past `vacuum_failsafe_age` (1.6 billion) vacuum drops throttling. The server warns at 40 million XIDs left and stops assigning them at 3 million left.
code
sql · 17 lines-- Per database: oldest possibly unfrozen XID and MultiXact, as ages
SELECT datname,
age(datfrozenxid) AS xid_age,
mxid_age(datminmxid) AS mxid_age
FROM pg_database
ORDER BY xid_age DESC;
-- Per table in the current database, counting its TOAST table
SELECT c.oid::regclass AS table_name,
greatest(age(c.relfrozenxid), age(t.relfrozenxid)) AS xid_age,
round(100.0 * greatest(age(c.relfrozenxid), age(t.relfrozenxid))
/ current_setting('autovacuum_freeze_max_age')::int, 1) AS pct_of_forced
FROM pg_class c
LEFT JOIN pg_class t ON c.reltoastrelid = t.oid
WHERE c.relkind IN ('r', 'm')
ORDER BY xid_age DESC
LIMIT 10;go deeper
Know that relfrozenxid and datfrozenxid hold the oldest unfrozen XID, that age() turns them into a transaction count, and that 200 million is when autovacuum forces freezing.
Explain each threshold in order, 50, 150, 200 million, the 1.6-billion failsafe and the 40-million and 3-million limits, and why the TOAST table and every database must be checked.
Read the trend: a healthy saw-tooth versus an age climbing past the forced threshold, and connect a stuck relfrozenxid to horizon holders, cancelled runs or vacuums that skip all-visible pages.
Design alerting that combines age with write-transaction rate per cluster, so time-to-limit rather than raw age drives paging, and decide which teams own fixing a stuck horizon.
## The two catalog columns PostgreSQL tracks freezing progress in two catalog columns, both of type `xid`: - **`pg_class.relfrozenxid`**: for each table, materialized view and TOAST table, the oldest unfrozen XID that can remain in it, as recorded at the end of the most recent VACUUM that managed to advance it (usually the last aggressive one). Every XID older than this in that relation is known to be frozen. - **`pg_database.datfrozenxid`**: for each database, the minimum of the `relfrozenxid` values of its relations, a lower bound on any unfrozen XID in that database. Because the XID counter is shared by the whole cluster, the oldest `datfrozenxid` of **any** database, including ones nobody connects to, is what the server's wraparound limits are computed from. MultiXact IDs have parallel columns, `relminmxid` and `datminmxid`, read with `mxid_age()`. ## Measuring with age() `age(xid)` returns the number of transactions between the given XID and the current XID counter. Applied to the columns above, it says how far behind freezing is, measured in transactions rather than time. Two details matter: 1. **Include the TOAST table.** A TOAST relation has its own `pg_class` row and its own `relfrozenxid`, and autovacuum processes it separately. Taking `greatest()` of a table and its TOAST table is the manual's own query, and `vacuumdb --min-xid-age` uses the same rule. 2. **Look at every database.** `age()` on `relfrozenxid` only covers the database you are connected to, so start from `pg_database`, then run the table query inside whichever database is oldest. ## The ladder of thresholds Each setting acts at a different age. Defaults are PostgreSQL 18's: | Age reaches | Setting or limit | Default | Effect | |---|---|---|---| | per row | `vacuum_freeze_min_age` | 50 million | rows older than this are frozen when VACUUM processes their page | | per table | `vacuum_freeze_table_age` | 150 million | VACUUM runs aggressively, scanning every page not all-frozen; effective value capped at 0.95 x `autovacuum_freeze_max_age` | | per table | `autovacuum_freeze_max_age` | 200 million | autovacuum forces an anti-wraparound vacuum, even when disabled; changeable only at server start | | per table | `vacuum_failsafe_age` | 1.6 billion | VACUUM drops cost delay and skips index vacuuming to finish sooner | | cluster | warning limit | 40 million XIDs left | `WARNING: database "..." must be vacuumed within N transactions` | | cluster | stop limit | 3 million XIDs left | new XIDs refused; write transactions fail | The last two are counted back from the wraparound point, about 2^31 transactions past the oldest `datfrozenxid`, so the warning appears when `age(datfrozenxid)` is roughly **2.1 billion**, not when it is 40 million. A per-table `autovacuum_freeze_max_age` storage parameter can only **lower** the threshold for one table; autovacuum ignores a per-table value larger than the server-wide setting. ## Reading the numbers - An age that **saw-tooths** below about 200 million is healthy: autovacuum forces a run near the threshold, and a completed aggressive run resets the table to a little above `vacuum_freeze_min_age`. - An age that **keeps climbing past** `autovacuum_freeze_max_age` means forced vacuums start but do not finish or cannot advance `relfrozenxid`: an old transaction, prepared transaction or replication slot is holding back the horizon, or the runs keep being cancelled. - `VACUUM (VERBOSE)` output, and autovacuum log lines once `log_autovacuum_min_duration` (default 10 minutes) is met, report `new relfrozenxid: ..., which is N XIDs ahead of previous value`, which confirms whether a run moved it. - A normal VACUUM that only cleaned dead rows may leave `relfrozenxid` unchanged, because it skipped all-visible pages that still hold unfrozen XIDs. ## What to alert on Alert on the trend rather than on a single number: - **Page** when the oldest database's age stays well above `autovacuum_freeze_max_age`, because that means forced vacuums are not keeping up. - **Treat the server's own 40-million-left warning as an incident already in progress**, never as the first alert anyone sees. - **Plot the write-transaction rate beside the age**: the same age is weeks from trouble on a quiet server and hours from it on a busy ingest, so time-to-limit is the number that matters. - **Track `mxid_age(datminmxid)` as well**; MultiXacts have their own limits, and a dashboard built only on XIDs misses them.
- Why might age(relfrozenxid) stay high on a PostgreSQL table right after a normal VACUUM finished?`relfrozenxid` only advances when VACUUM scans every page that might hold unfrozen XIDs. A normal vacuum skips all-visible pages through the visibility map, so it can remove dead rows without moving `relfrozenxid`. An aggressive vacuum, `VACUUM FREEZE`, or a run that happens to visit every not-all-frozen page advances it, and an old transaction or slot still caps how far it can move.
- Why does the PostgreSQL table query take greatest() of a table and its TOAST table?A TOAST table has its own `pg_class` row and `relfrozenxid`, and autovacuum vacuums it as a separate relation. Checking only the main table can miss an old TOAST `relfrozenxid` that is holding `datfrozenxid` back. `vacuumdb --min-xid-age` defines a relation's age the same way, as the greater of the two.
- age(relfrozenxid) counts how many rows in the table are still unfrozen.
- datfrozenxid is the newest XID that VACUUM has frozen in the database.
- autovacuum_freeze_max_age can be changed with a configuration reload.
- A table at 200 million age means writes are about to be refused.
- A per-table autovacuum_freeze_max_age above the global value delays forced vacuums.
- Checking the connected database is enough, since XIDs are per database.