skip to content

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
snapshot 759:759:
T2
·
1
BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT count(*) FROM t→ 16
t, page by pageoldest xmin anyone needs: 759
no VACUUM in this step.

T1 opens a repeatable read transaction and reads. that fixes its snapshot, and the snapshot's xmin is now the oldest thing anybody needs.

live tuplesdead, not yet removabledead, removablethe horizon
step 1/6
real postgres 18.6 output · a native server · traces/long-txn
step through. after T2's delete, the deleted rows are dead, but not yet removable (dashed): T1's snapshot predates the delete. the first VACUUM leaves every one of them. once T1 commits they turn removable, and the second VACUUM frees them. the horizon is the oldest snapshot xmin or xid anything still holds.

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:

while T1 is open
  1. INFO: finished vacuuming "postgres.public.t": index scans: 0
  2. tuples: 0 removed, 16 remain, 8 are dead but not yet removable
  3. removable cutoff: 759, which was 1 XIDs old when operation ended
after T1 commits
  1. INFO: finished vacuuming "postgres.public.t": index scans: 1
  2. tuples: 8 removed, 8 remain, 0 are dead but not yet removable
  3. removable cutoff: 760, which was 0 XIDs old when operation ended
real output, a postgres 18.6 server · long-txn
VACUUM VERBOSE, from the server log, while T1 was open and after it committed. the not yet removable count, and the removable cutoff: the horizon VACUUM computed.

"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

predict the output
T1 opened a repeatable read transaction, ran one SELECT, and has been idle for an hour. it never wrote anything, so it has no xid. does it hold back VACUUM?
sql
/* T1 */ BEGIN ISOLATION LEVEL REPEATABLE READ;
/* T1 */ SELECT count(*) FROM t;
/* T1 */ -- ... an hour of nothing

pg_stat_activity shows it. backend_xid is the session's xid if it has one, backend_xmin the xmin of its snapshot:

sessiontextbackend_xidxidbackend_xminxidstatetext
T1null759idle in transaction
T2nullnullidle
(2 rows)
real output, a postgres 18.6 server · long-txn
pg_stat_activity during the capture. T1 has no xid but holds backend_xmin 759, and it's idle in transaction: the classic shape.

your own session's, in the browser (one session, so no one to hold anything back):

sqldb=lab · session h
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;
output, from postgres 18.6
#1 · BEGIN
#2 · SELECT 1
countint8
3
(1 row)
#3 · SELECT 1
backend_xidxidbackend_xminxidstatetext
null753active
(1 row)
#4 · COMMIT
#5 · SELECT 1
backend_xidxidbackend_xminxidstatetext
null753active
(1 row)

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:

sql
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 xmin or catalog_xmin set: pg_replication_slots. a logical slot whose consumer went away holds back catalog cleanup forever.
  • a standby with hot_standby_feedback = on reports 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:

sqldb=lab · session h
continues session h (1 earlier block run first)
SHOW idle_in_transaction_session_timeout;
SET idle_in_transaction_session_timeout = '5min';
SHOW idle_in_transaction_session_timeout;
output, from postgres 18.6
#1 · SHOW
idle_in_transaction_session_timeouttext
0
(1 row)
#2 · SET
#3 · SHOW
idle_in_transaction_session_timeouttext
5min
(1 row)

the same scenario as above on a real server, with T1's session given a 500 ms limit:

T1
·
T2
·
1
BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT count(*) FROM t→ 16

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.

step 1/5
real output, a postgres 18.6 server · idle-timeout
server log
  1. FATAL: terminating connection due to idle-in-transaction timeout
  2. INFO: finished vacuuming "postgres.public.t": index scans: 1
  3. tuples: 8 removed, 8 remain, 0 are dead but not yet removable
  4. removable cutoff: 760, which was 0 XIDs old when operation ended
step through. T1 reads and goes quiet. a second later the server has ended its session (the log line), its snapshot is gone, and VACUUM removes all eight rows. T1's application finds out on its next statement, when the connection is closed.

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_timeout ends the forgotten ones.