Report
Replay of a CLOG truncation unlinks pg_xact segments without advancing minRecoveryPoint, so recovery restarted with an earlier target (e.g. after a standby crash) cannot read the commit status of tuples that were not yet frozen at that target
- Severity
- 50
- Blamed commit
- 48188e1621bb6711e7d092bee48523b18cd80177
On 21 branches
REL8_2_STABLE, REL8_3_STABLE, REL8_4_STABLE, REL9_0_STABLE, REL9_1_STABLE, REL9_2_STABLE, REL9_3_STABLE, REL9_4_STABLE, 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, TruncateCLOG() WAL-logs and flushes the
CLOG_TRUNCATE record before unlinking any pg_xact segment. On a
standby, clog_redo() replays that record by calling
SimpleLruTruncate() directly, without advancing minRecoveryPoint
first. The segment removal is irreversible, but minRecoveryPoint can
still point before the VACUUM that froze the tuples and advanced
datfrozenxid.
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 freeze
records are not replayed, and the heap still contains unfrozen tuples
whose xmin lies in the removed segment. Reading them fails:
ERROR: could not open file "pg_xact/0000":
No such file or directory
DETAIL: Could not access commit status of transaction 667.
This is the same class as the already reported replay paths for COMMIT/ROLLBACK PREPARED, commit timestamps, DROP DATABASE/TABLESPACE and relation map updates (see Notes), but in clog_redo().
Reproduction
The XID consumption needed to make a whole pg_xact segment truncatable is shortened with pg_resetwal; consuming about one million XIDs normally has the same effect. Two clusters on ports P and S.
initdb -D pri -A trust
cat >> pri/postgresql.conf <<C
port=P
wal_level=replica
autovacuum=off
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)"
# do not read u, so its tuples carry no hint bits
pg_ctl -D pri -m fast stop
pg_resetwal -x 2097152 pri
dd if=/dev/zero of=pri/pg_xact/0002 bs=8192 count=32
pg_ctl -D pri -l pri.log -w start
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_xact/0000
# wait until the standby has replayed everything;
# sby/pg_xact/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 count(*) from u"
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 reads 1000 rows.
Actual: recovery reports "consistent recovery state reached" before the target, stops at the target and promotes; template0 still has its old datfrozenxid, and
ERROR: could not open file "pg_xact/0000":
No such file or directory
DETAIL: Could not access commit status of transaction 667.
With replay paused before the VACUUM (control), the same restart reads all 1000 rows. The default bgwriter settings were used; the window stays open as long as no buffer write or restartpoint pushes minRecoveryPoint past the truncation record.
Analysis
src/backend/access/transam/clog.c, clog_redo(), CLOG_TRUNCATE: AdvanceOldestClogXid() and SimpleLruTruncate() run without a prior XLogFlush(lsn). During recovery XLogFlush() advances minRecoveryPoint (UpdateMinRecoveryPoint()); smgr_redo() does exactly this before truncating a relation, with a comment explaining that once the file is truncated there is no going back. The primary side, WriteTruncateXlogRec(), already flushes before unlinking.
The freeze records that made the truncation safe only dirty shared buffers on the standby, so they are not durable, and minRecoveryPoint does not cover them. A restart with an earlier target therefore sees the pre-freeze heap together with the post-truncation pg_xact.
Blame: 48188e1621bb6711e7d092bee48523b18cd80177 introduced the CLOG_TRUNCATE record and its redo action, which truncates pg_xact during replay.
Notes
- Tested on master a20d50a00edc40ff11b08b1e3e3be6e4f9da51f6. The same clog_redo() code is present on REL_14_STABLE through REL_19_STABLE; back branches were not tested.
- No recovery_min_apply_delay is needed; VACUUM's freeze, inplace and truncation records carry no commit record.
- Related reports for the same pattern in other redo routines: 01a1f157-7641 (two-phase state file), 01a1f157-fc11 (commit_ts), 01a1f2dd-a41c (DROP DATABASE/TABLESPACE), 01a1f2de-1cf8 (relation map). Near matches that are primary-side: 01a1f1fd-e9f4 (stale oldestXid from a checkpoint during vac_truncate_clog()), 01a1f1fd-6b01 (commit_ts unlink before flush).
- Multixact truncation replay has the same problem and is reported separately.