Report
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"
- Severity
- 25
- Blamed commit
- 73c986adde5d73a5e2555da9b5c8facedb146dcd
On 13 branches
REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
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 <<EOC
track_commit_timestamp = on
autovacuum = off
EOC
pg_ctl -D $PGDATA -l logfile start
psql -d postgres <<'SQL'
ALTER DATABASE template0 ALLOW_CONNECTIONS true;
CREATE TABLE t (a int);
INSERT INTO t VALUES (1);
DO $$BEGIN FOR i IN 1..200000 LOOP
PERFORM pg_current_xact_id(); COMMIT; END LOOP; END$$;
CHECKPOINT;
SQL
psql -d template0 -c 'VACUUM FREEZE'
psql -d template1 -c 'VACUUM FREEZE'
WW=$(psql -Atd postgres -c "SELECT pid FROM pg_stat_activity
WHERE backend_type = 'walwriter'")
kill -STOP $WW
psql -d postgres -c 'VACUUM FREEZE' # removes 0000-0006
psql -d postgres -c 'SELECT pg_xact_commit_timestamp(xmin) FROM t'
# -> 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