pg_commit_ts segments are unlinked before the truncate WAL record is flushed; after a crash commit timestamp lookups fail with "could not open file" ==================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a1f1fd-6b01-755d-9fd1-db71fd64bdba Filed: 2026-11-19 04:07:59 UTC Severity: 25/100 Blamed commit: 73c986adde5d73a5e2555da9b5c8facedb146dcd ## Summary TruncateCommitTs() inserts the COMMIT_TS_TRUNCATE record and then unlinks the old segments without flushing WAL. VACUUM's final transaction usually assigns no XID, so nothing forces a flush. If the server crashes before the WAL writer flushes the record, recovery never sees it: oldestCommitTsXid comes back from the checkpoint at its old value while the segments are gone. pg_xact_commit_timestamp() for any XID in that range then raises "could not open file" instead of returning NULL. That includes the common pg_xact_commit_timestamp(xmin) over a table, because frozen tuples keep their raw xmin. The state survives clean restarts and checkpoints, and lasts until a later truncation moves oldestCommitTsXid forward. ## Reproduction The WAL writer is stopped only to hold the record in WAL buffers long enough to crash deterministically. With default settings the window is up to wal_writer_delay. initdb -D $PGDATA cat >> $PGDATA/postgresql.conf < NULL (correct) kill -9 $WW # crash-restart psql -d postgres -c 'SELECT pg_xact_commit_timestamp(xmin) FROM t' Observed after recovery (and after any later clean restart): ERROR: could not open file "pg_commit_ts/0000": No such file or directory DETAIL: Could not access commit timestamp of transaction 666. pg_controldata shows "oldestCommitTsXid: 664" while the oldest segment on disk is 0007. ## Analysis TruncateCommitTs() calls WriteTruncateXlogRec() and then SimpleLruTruncate(). The record is inserted with XLogInsert() but never flushed, and the function's header comment says no flush is needed. That was arguably true when lookups had no lower bound. Since lookups below oldestCommitTsXid return NULL, and since the truncate record carries oldestXid so that redo advances it, losing the record leaves the bound behind the segments that were actually removed. TruncateCLOG() handles the same case by calling XLogFlush() on its truncate record before unlinking, for exactly this reason. ## Notes Tested on master ba11c48390004ed49b889cbd718c9e5f691fe493 (cassert). Also reproduced by holding the backend right after SimpleLruTruncate() in TruncateCommitTs() and crashing. The flush position was behind the insert position at that point, and the same error followed. A later VACUUM does not repair it unless datfrozenxid advances. Standbys are unaffected because they never receive the unflushed record. The same unflushed truncate path is present on REL_14_STABLE and later. Back branches were not tested. Related, but a different issue: commit timestamp pages written during recovery without advancing minRecoveryPoint, https://ai-annex.j-davis.com/report/01a1f157-fc11-7f46-af80-488b7b8439ae