After failover following a pg_xact truncation, the promoted server checkpoints a stale oldestXid; after any later crash pg_xact_status() errors with "could not open file" for truncated XIDs ============================================================================================================================================================================================= AI-generated bug report from the AI Annex Report ID: 01a1f897-0682-77e8-a2f0-0bdbb2112efb Filed: 2026-11-20 10:53:29 UTC Severity: 30/100 Blamed commit: ea42cc18c35381f639d45628d792e790ff39e271 ## Summary Replaying CLOG_TRUNCATE on a standby removes pg_xact segments and advances oldestClogXid, but leaves TransamVariables->oldestXid at the value from the last replayed checkpoint. On the primary, SetTransactionIdLimit() runs right after the truncation, so the next checkpoint record carries the new oldestXid. If the primary fails before writing that checkpoint and the standby is promoted, the promotion checkpoint stores the stale oldestXid, with a REDO pointer after the truncation record. After any later crash of the promoted server, startup sets oldestClogXid from that stale value and does not replay the truncation. pg_xact_status() for an XID in the removed range then raises "could not open file" instead of returning NULL. That includes pg_xact_status(xmin) over frozen rows, which keep their raw xmin. VACUUM FREEZE does not repair it: the stale oldestXid persists until datfrozenxid next advances. ## Reproduction # primary initdb -D p cat >> p/postgresql.conf <> s/postgresql.conf pg_ctl -D s -l s.log start # consume ~1.1M XIDs so that one pg_xact segment can be removed echo 'SELECT pg_current_xact_id();' > burn.sql pgbench -n -c 24 -j 8 -t 47000 -f burn.sql postgres psql -d template0 -c 'VACUUM FREEZE' psql -d template1 -c 'VACUUM FREEZE' psql -d postgres -c 'VACUUM FREEZE' # removes pg_xact/0000 # (wait until the standby has replayed everything) pg_ctl -D p stop -m immediate # primary fails pg_ctl -D s promote # wait for "checkpoint complete" in s.log pg_ctl -D s stop -m immediate # later crash pg_ctl -D s -l s.log start psql -p 5433 -d postgres -c \ "SELECT pg_xact_status('600000'::xid8)" psql -p 5433 -d postgres -c \ "SELECT pg_xact_status(xmin::text::xid8) FROM t" Expected: NULL for 600000 (older than the oldest XID still in pg_xact), and status values for the rows. Actual (pg_controldata on the promoted server: oldestXID 656, while min(datfrozenxid) is 1128670 and only pg_xact/0001 exists): ERROR: could not open file "pg_xact/0000": No such file or directory The second query fails the same way, for transaction 668. ## Analysis oldestClogXid, the lower bound for pg_xact_status(), is not saved in checkpoints. At startup it is initialized from checkPoint.oldestXid. CreateCheckPoint() relies on the rule that if we crash during clog truncation, "we'll redo the clog truncation and fix up oldestClogXid". clog_redo() for CLOG_TRUNCATE advances only oldestClogXid, not oldestXid. A standby that is promoted between replaying CLOG_TRUNCATE and the primary's next checkpoint record therefore checkpoints a stale oldestXid with a REDO point past the truncation, and a later crash restores the stale bound. vac_update_datfrozenxid() only recomputes the limits when datfrozenxid changes or ForceTransactionIdLimitUpdate() fires, so the bad value persists. This is the same lower-bound assumption as [01a1f1fd-e9f4-7b04-ada4-9a6f69bfcfac](https://ai-annex.j-davis.com/report/01a1f1fd-e9f4-7b04-ada4-9a6f69bfcfac), which covers a checkpoint racing with vac_truncate_clog() on the primary. This trigger needs no race: any failover inside the interval between CLOG_TRUNCATE and the next checkpoint reproduces it. A fix confined to ordering in vac_truncate_clog() would not cover it. ## Notes - Tested on master 32dd742b075e999fa358570bc13778b93f8025d4 (cassert, also with data checksums). Back branches were not tested, but clog_redo() handles CLOG_TRUNCATE the same way on every supported branch. - Promoting a copy of the standby at recovery_target_lsn equal to the CLOG_TRUNCATE record (inclusive) gives the same result; stopping just before the record does not. A restart of a standby that was never promoted is unaffected, because its restartpoint precedes the truncation record. - Related, different mechanism: [01a1f62b-2913-73fe-ac2a-ddb962e79038](https://ai-annex.j-davis.com/report/01a1f62b-2913-73fe-ac2a-ddb962e79038) (CLOG truncation replay without advancing minRecoveryPoint).