the xmin horizon
one forgotten transaction and nothing gets cleaned. dead, but not yet removable.
- address
- everything.mvcc.xmin_horizon
- part
- part 14, MVCC and vacuum
- chapter
- chapter 9 of 12
- reading time
- 8 min
VACUUM can only remove a dead version once no snapshot anywhere could still see it. the oldest snapshot xmin (or xid) any session still holds is the xmin horizon, and everything deleted after it stays put: dead, but not yet removable. one transaction left open in an idle session holds back cleanup of every table in the database.
the picture
you know how snapshots let an old transaction keep reading old versions? those versions have to physically stay on the page for as long as the transaction might look. postgres can't know which rows it will read, so it keeps all of them: every version deleted after that snapshot was taken.
think of a museum that keeps every exhibit any visitor in the building might still come back to see. one visitor who wandered in at 9 and never left means nothing replaced since 9 can go to storage, in any gallery, however many visitors have come and gone since. the oldest visitor still inside is the horizon.
the capture is a real postgres 18.6 server. T1 opens a repeatable read transaction and reads (that's all it does). T2 deletes half the table. then VACUUM, twice.
T1 opens a repeatable read transaction and reads. that fixes its snapshot, and the snapshot's xmin is now the oldest thing anybody needs.
T1 took its snapshot before T2 had even started: 759:759, nothing in progress. T2 then got xid 759 and deleted eight rows. here's what VACUUM said, both times:
- INFO: finished vacuuming "postgres.public.t": index scans: 0
- tuples: 0 removed, 16 remain, 8 are dead but not yet removable
- removable cutoff: 759, which was 1 XIDs old when operation ended
- INFO: finished vacuuming "postgres.public.t": index scans: 1
- tuples: 8 removed, 8 remain, 0 are dead but not yet removable
- removable cutoff: 760, which was 0 XIDs old when operation ended
"removable cutoff: 759": versions deleted by xids below 759 can go. T2's delete is 759, so nothing could. after T1 ends, the cutoff moves to 760 and all eight go.
who holds the horizon
/* T1 */ BEGIN ISOLATION LEVEL REPEATABLE READ;
/* T1 */ SELECT count(*) FROM t;
/* T1 */ -- ... an hour of nothingpg_stat_activity shows it. backend_xid is the session's xid if it has one, backend_xmin the xmin of its snapshot:
| sessiontext | backend_xidxid | backend_xminxid | statetext |
|---|---|---|---|
| T1 | null | 759 | idle in transaction |
| T2 | null | null | idle |
your own session's, in the browser (one session, so no one to hold anything back):
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT count(*) FROM accounts;
SELECT backend_xid, backend_xmin, state FROM pg_stat_activity WHERE backend_xmin IS NOT NULL;
COMMIT;
SELECT backend_xid, backend_xmin, state FROM pg_stat_activity WHERE backend_xmin IS NOT NULL;| countint8 |
|---|
| 3 |
| backend_xidxid | backend_xminxid | statetext |
|---|---|---|
| null | 753 | active |
| backend_xidxid | backend_xminxid | statetext |
|---|---|---|
| null | 753 | active |
inside the transaction, backend_xmin is 753 and there's no xid. after COMMIT it still says 753, and state is still active: that's the snapshot of the query reading pg_stat_activity, which is itself running. a session can't watch itself sit idle. on a server you look at the other sessions' rows, and the query to run when vacuum seems stuck sorts them by the age of those two:
SELECT pid, state, backend_xid, backend_xmin,
age(backend_xmin) AS xmin_age, now() - xact_start AS open_for, left(query, 60) AS query
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL OR backend_xid IS NOT NULL
ORDER BY greatest(age(backend_xmin), age(backend_xid)) DESC;sessions aren't the only thing that holds the horizon:
- a long query, even at read committed. a single statement that runs for an hour holds its snapshot for that hour.
- a prepared transaction (
PREPARE TRANSACTION, two-phase commit) that nobody committed:pg_prepared_xacts. it survives restarts. - a replication slot with
xminorcatalog_xminset:pg_replication_slots. a logical slot whose consumer went away holds back catalog cleanup forever. - a standby with
hot_standby_feedback = onreports its oldest snapshot to the primary, so a long query on a read replica holds back vacuum on the primary.
ending the forgotten ones
the most common culprit is an application that opened a transaction and never finished it: a connection checked out of a pool, an exception path that forgot to roll back, a person in psql who went to lunch. postgres can end those for you. idle_in_transaction_session_timeout (off by default, 0) ends any session that sits idle inside a transaction for longer than that:
SHOW idle_in_transaction_session_timeout;
SET idle_in_transaction_session_timeout = '5min';
SHOW idle_in_transaction_session_timeout;| idle_in_transaction_session_timeouttext |
|---|
| 0 |
| idle_in_transaction_session_timeouttext |
|---|
| 5min |
the same scenario as above on a real server, with T1's session given a 500 ms limit:
T1 opens a repeatable read transaction, reads, and then the client goes quiet. its session has a 500 ms limit on sitting idle inside a transaction.
- FATAL: terminating connection due to idle-in-transaction timeout
- INFO: finished vacuuming "postgres.public.t": index scans: 1
- tuples: 8 removed, 8 remain, 0 are dead but not yet removable
- removable cutoff: 760, which was 0 XIDs old when operation ended
set it for the whole server or per role, at something comfortably longer than any legitimate pause in your application. transaction_timeout pg 17+ caps the whole transaction instead, busy or idle, and statement_timeout caps single statements.
the horizon matters for freezing too: vacuum can't freeze tuples newer than it, so a transaction left open for days also stops relfrozenxid from advancing. and it's one reason postgres can't just reuse undo space the way undo-log databases do: there, a long reader runs out of old versions instead.
keylines
- VACUUM removes a version only if no snapshot anywhere could still see it.
- the horizon is the oldest snapshot xmin or xid held by anyone: sessions, prepared transactions, slots, standbys.
- one idle transaction holds back cleanup in every table of the database, even if it never wrote.
- VACUUM VERBOSE says it: "dead but not yet removable", and the removable cutoff.
idle_in_transaction_session_timeoutends the forgotten ones.