Replayed commit timestamps reach pg_commit_ts on disk without advancing minRecoveryPoint, so recovery restarted with an earlier target shows a commit timestamp for a transaction that ends up aborted ====================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a1f157-fc11-7f46-af80-488b7b8439ae Filed: 2026-11-19 01:07:17 UTC Severity: 30/100 Blamed commit: 73c986adde5d73a5e2555da9b5c8facedb146dcd ## Summary xact_redo_commit() calls TransactionTreeSetCommitTsData() for every replayed commit. The commit_ts SLRU is created without group LSNs, so when a pg_commit_ts page is written during recovery (buffer eviction or a restartpoint), SlruPhysicalWritePage() does no XLogFlush() and minRecoveryPoint is not advanced past the commit records whose timestamps are on that page. This is the commit timestamp sibling of the pg_xact problem fixed in 8303f2e13c5c2fab539e11054073a58942cb0f30 ("Recovery: ensure CLOG updates wait for minRecoveryPoint"). That commit makes pg_xact writes advance minRecoveryPoint for hot_standby = off too; pg_commit_ts still has no such interlock, with either hot_standby setting. If the server stops without a shutdown restartpoint (crash, immediate stop) and recovery is restarted with a recovery target between minRecoveryPoint and a commit record whose timestamp already reached disk, the "requested recovery stop point is before consistent recovery point" check passes, recovery stops before the commit, the transaction is aborted, but pg_xact_commit_timestamp() still returns the commit timestamp from the discarded future. The same holds for a prepared transaction whose COMMIT PREPARED was not replayed: it shows as prepared (and after ROLLBACK PREPARED as aborted) with a commit timestamp. The stale entry is returned whenever a later xid committed before the recovery target, so that the xid is not above newestCommitTsXid. Consumers of commit timestamps (pg_xact_commit_timestamp(), pg_xact_commit_timestamp_origin(), logical replication conflict detection that compares local commit origins/timestamps) can see a commit time for a transaction that never committed on this timeline. ## Reproduction Primary and streaming standby, track_commit_timestamp = on on both. On the standby commit_timestamp_buffers = 16 so a pg_commit_ts page is evicted after about 13k xids, bgwriter_lru_maxpages = 0 and a long checkpoint_timeout so that nothing else advances minRecoveryPoint during the test (they only make the window easy to hit; any restartpoint that writes pg_commit_ts before buffers does the same). ```bash #!/bin/bash # needs PATH with the build under test; ports 5432x free set -uo pipefail W=${W:-/tmp/cts-minrec}; rm -rf $W; mkdir -p $W PRI=$W/pri SBY=$W/sby PP=54321 PS=54322 export PGHOST=/tmp PGDATABASE=postgres initdb -D $PRI -A trust >/dev/null cat >> $PRI/postgresql.conf </dev/null psql -p $PP -qXc "create table t1(a int); create table t2(a int)" pg_basebackup -p $PP -D $SBY -R -X stream -c fast cat >> $SBY/postgresql.conf </dev/null # X gets its xid first but commits after Y psql -p $PP -XAtc "begin; insert into t1 values (1); select pg_sleep(3); commit" >/dev/null & sleep 1 X=$(psql -p $PP -XAtc "select backend_xid from pg_stat_activity where backend_xid is not null and pid <> pg_backend_pid()") psql -p $PP -XAtc "insert into t2 values (2)" >/dev/null # Y TGT=$(psql -p $PP -XAtc "select pg_current_wal_insert_lsn()") wait # X commits echo "X=$X target=$TGT" psql -p $PP -XAtc \ "select 'primary ts(X)='||pg_xact_commit_timestamp('$X')" # burn ~30k xids so the standby evicts X's pg_commit_ts page echo "select pg_current_xact_id();" > $W/burn.sql pgbench -p $PP -n -c 2 -t 15000 -f $W/burn.sql >/dev/null 2>&1 END=$(psql -p $PP -XAtc "select pg_current_wal_lsn()") until [ "$(psql -p $PP -XAtc "select replay_lsn >= '$END' from pg_stat_replication")" = t ]; do sleep 0.5; done pg_controldata $SBY | grep "Minimum recovery ending" pg_ctl -D $SBY -m immediate stop >/dev/null pg_ctl -D $PRI -m fast stop >/dev/null cat >> $SBY/postgresql.conf </dev/null grep -E "consistent recovery|recovery stopping" $W/sby2.log sleep 2 psql -p $PS -XAtc "select 'status(X)='||pg_xact_status('$X'::xid8), 'ts(X)='||coalesce(pg_xact_commit_timestamp('$X')::text,'NULL'), 't1 rows='||(select count(*) from t1)" pg_ctl -D $SBY -m immediate stop >/dev/null ``` Output on master: ``` X=665 target=0/03001F70 primary ts(X)=2026-11-18 16:31:56.473928-08 Minimum recovery ending location: 0/02000138 LOG: consistent recovery state reached at 0/02000138 LOG: recovery stopping before WAL location (LSN) "0/03001F70" status(X)=aborted|ts(X)=2026-11-18 16:31:56.473928-08| t1 rows=0 ``` minRecoveryPoint stayed at the end of the base backup although the standby had replayed (and written to pg_commit_ts) X's commit, so the earlier target was accepted. X is aborted and its row is invisible, but it has a commit timestamp. Expected: either the restart is refused (minRecoveryPoint past X's commit record), or pg_xact_commit_timestamp() returns NULL for X. With the xid burn removed (no pg_commit_ts eviction) the same script prints ts(X)=NULL. Same result with hot_standby = off, and with X being a prepared transaction (target just before COMMIT PREPARED): it is still prepared after promotion, has a commit timestamp, and keeps it after ROLLBACK PREPARED. ## Analysis - src/backend/access/transam/commit_ts.c: the commit_ts SLRU is registered without nlsns, so shared->group_lsn is NULL and SlruPhysicalWritePage() never calls XLogFlush() for its pages. During recovery XLogFlush() is what calls UpdateMinRecoveryPoint(). - xact_redo_commit() (also used for COMMIT PREPARED) calls TransactionTreeSetCommitTsData() unconditionally, before the pg_xact update; after 8303f2e13c5c2fab539e11054073a58942cb0f30 only the pg_xact update carries the commit LSN. - CheckPointGuts() writes pg_commit_ts (CheckPointCommitTs()) before CheckPointBuffers(), so a restartpoint also writes pages holding timestamps of commits beyond minRecoveryPoint; only the later data buffer flush happens to advance it. - TransactionIdGetCommitTsData() does not consult pg_xact; it only checks the xid against oldestCommitTsXid/newestCommitTsXid, and newestCommitTsXid is advanced by any later-xid commit that was replayed before the target. - Unlike pg_multixact (TrimMultiXact() zeroes successors of nextMXact, with a comment that these writes ignore "write xlog before data"), nothing at end of recovery clears entries for xids below nextXid that did not commit. Blame is the commit that introduced commit timestamps (73c986adde5d73a5e2555da9b5c8facedb146dcd), which used the pg_xact-like SLRU without LSN tracking. ## Notes Tested on master 8303f2e13c5c2fab539e11054073a58942cb0f30 (cassert build). Runs: plain commit with hot_standby = on (bug), same without the xid burn (correct, NULL), hot_standby = off with a prepared transaction (bug), hot_standby = on with a prepared transaction (bug). Not tested (code read only): a primary-side variant with synchronous_commit = off, where a pg_commit_ts page could be written before the async commit's WAL is flushed and then lost in a crash. Related reports on the pg_xact form of this problem, fixed by 8303f2e13c5c2fab539e11054073a58942cb0f30: https://ai-annex.j-davis.com/report/01a13852-5989-7e23-9aeb-6caa2b7480e3 https://ai-annex.j-davis.com/report/01a13843-4f20-7b8d-8056-cbfa39af901c https://ai-annex.j-davis.com/report/01a13816-7147-78e9-9fb0-5f6f2d3eb91e The same missing interlock for pg_twophase state files: https://ai-annex.j-davis.com/report/01a1f157-7641-715d-82db-bcf87b553485 Back branches were not tested.