Report
Replay of a commit timestamp truncation unlinks pg_commit_ts segments without advancing minRecoveryPoint, so recovery restarted with an earlier target (e.g. after a standby crash) fails commit timestamp lookups with "could not open file"
- Severity
- 38
- 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
On the primary, TruncateCommitTs() WAL-logs the COMMIT_TS_TRUNCATE
record and then unlinks pg_commit_ts segments. On a standby,
commit_ts_redo() replays that record by calling
AdvanceOldestCommitTsXid() and SimpleLruTruncate() directly,
without advancing minRecoveryPoint first. The segment removal is
irreversible, but minRecoveryPoint can still point before the VACUUM
that advanced datfrozenxid and caused the truncation.
If the standby then crashes (or is stopped with -m immediate) and is
restarted with a recovery target between minRecoveryPoint and the
truncation, recovery accepts the target as consistent. The checkpoint
it starts from still has the old oldestCommitTsXid, so lookups of XIDs
in the removed segment are not short-circuited and fail:
ERROR: could not open file "pg_commit_ts/0000":
No such file or directory
DETAIL: Could not access commit timestamp of transaction 665.
The expected answer is the commit timestamp (the segment still existed at the target LSN), or at worst NULL.
This is the same class as the reported replay paths for CLOG and multixact truncation and others (see Notes), but in commit_ts_redo(). It is a different path from the primary-side report about unlinking pg_commit_ts before the truncate record is flushed, and from the report about replayed commit timestamps being written without advancing minRecoveryPoint.
Reproduction
Two clusters on ports P and S. About 30000 XIDs are consumed with subtransactions so that the first pg_commit_ts segment (26208 XIDs) becomes truncatable.
initdb -D pri -A trust
cat >> pri/postgresql.conf <<C
port=P
wal_level=replica
autovacuum=off
track_commit_timestamp=on
C
pg_ctl -D pri -l pri.log -w start
psql -p P -c "create table u(a int)"
psql -p P -c "insert into u select generate_series(1,1000)"
psql -p P -c "create table burn(a int)"
psql -p P -c "do \$\$ begin for i in 1..30000 loop
begin insert into burn values (i);
exception when others then null; end;
end loop; end \$\$"
psql -p P -c "checkpoint"
pg_basebackup -p P -D sby -R -X stream -c fast
cat >> sby/postgresql.conf <<C
port=S
hot_standby=on
checkpoint_timeout=1h
max_wal_size=4GB
C
pg_ctl -D sby -l sby.log -w start
psql -p P -c "update pg_database set datallowconn=true
where datname='template0'"
TGT=$(psql -p P -Atc "select pg_current_wal_insert_lsn()")
vacuumdb -p P --all --freeze # truncates pg_commit_ts/0000
psql -p P -c "select pg_switch_wal()"
# wait until the standby has replayed everything;
# sby/pg_commit_ts/0000 is now gone, pg_controldata sby still
# shows "Minimum recovery ending location" before TGT
pg_ctl -D sby -m immediate stop
pg_ctl -D pri -m fast stop
cat >> sby/postgresql.conf <<C
recovery_target_lsn='$TGT'
recovery_target_inclusive=off
recovery_target_action=promote
C
pg_ctl -D sby -l sby2.log -w start
psql -p S -c "select pg_xact_commit_timestamp(xmin) from u limit 1"
Expected: recovery either refuses the target (as it does after a fast stop: "requested recovery stop point is before consistent recovery point") or the promoted server returns the commit timestamp of the insert, as the primary did before the VACUUM.
Actual: recovery reports "consistent recovery state reached" before the target, stops at the target and promotes; pg_control_checkpoint() shows oldest_commit_ts_xid 664, and
ERROR: could not open file "pg_commit_ts/0000":
No such file or directory
DETAIL: Could not access commit timestamp of transaction 665.
With replay paused before the VACUUM (control), the same restart returns the timestamp. The window stays open with default bgwriter settings as long as no buffer write or restartpoint pushes minRecoveryPoint past the truncation record.
Analysis
src/backend/access/transam/commit_ts.c, commit_ts_redo(), COMMIT_TS_TRUNCATE: AdvanceOldestCommitTsXid() and SimpleLruTruncate() run without a prior XLogFlush(lsn). During recovery XLogFlush() advances minRecoveryPoint (UpdateMinRecoveryPoint()); smgr_redo() does exactly this before truncating a relation, because once the file is gone there is no going back.
The records that made the truncation possible (the inplace datfrozenxid updates) only dirty shared buffers on the standby, and the advanced oldestCommitTsXid lives only in shared memory until the next restartpoint. A restart with an earlier target therefore uses the old oldestCommitTsXid together with the post-truncation pg_commit_ts directory.
Blame: 73c986adde5d73a5e2555da9b5c8facedb146dcd introduced commit timestamps together with the COMMIT_TS_TRUNCATE record and its redo action, which truncates pg_commit_ts during replay.
Notes
- Tested on master d94964f14f39936d69c6b8d3fc5020f0fdae1617; back branches were not tested, but commit_ts_redo() has the same code on every supported branch.
- No recovery_min_apply_delay is needed; VACUUM's inplace and truncation records carry no commit record.
- Same pattern in other redo routines, already reported: 01a1f62b-2913-73fe-ac2a-ddb962e79038 (CLOG truncation), 01a1f62b-a47f-7cc6-96dd-5ed4b38b7b16 (multixact truncation), 01a1f157-7641-715d-82db-bcf87b553485 (two-phase state file), 01a1f2dd-a41c-7cac-bf0c-945d10f79b7f (DROP DATABASE/TABLESPACE), 01a1f2de-1cf8-7c9f-92ce-ba4e44fa207f (relation map).
- Near matches, different paths: 01a1f1fd-6b01-755d-9fd1-db71fd64bdba (primary side, pg_commit_ts unlinked before the truncate record is flushed; that report notes standbys are unaffected by it), 01a1f157-fc11-7f46-af80-488b7b8439ae (replayed commit timestamps written without advancing minRecoveryPoint, showing a timestamp for an aborted transaction).