From 950bcbbe8f2528d57438d7bc2be335ca6464ab3d Mon Sep 17 00:00:00 2001 From: Ayush Tiwari Date: Tue, 24 Nov 2026 14:32:03 +0530 Subject: [PATCH v1] Flush WAL before writing commit timestamp pages A dirty pg_commit_ts page can reach disk before the commit record for a transaction on that page. If an asynchronous commit is lost in a crash, pg_xact_status() will report that the transaction aborted, but pg_xact_commit_timestamp() can still return its commit time. On a standby, writing the page doesn't advance minRecoveryPoint. If the standby crashes and replay stops before that commit on restart, the timestamp from the first replay is still on disk. Commit 8303f2e13c5 fixed the corresponding problem for pg_xact. Pass the end LSN of the commit record to pg_commit_ts and keep the highest LSN for each dirty page. The SLRU writer already flushes WAL to that LSN, or advances minRecoveryPoint during recovery, before it writes the page. One LSN per page is enough since timestamp lookups don't need it. --- src/backend/access/transam/commit_ts.c | 17 ++++++++++----- src/backend/access/transam/twophase.c | 2 +- src/backend/access/transam/xact.c | 4 ++-- src/include/access/commit_ts.h | 2 +- src/include/access/slru.h | 9 ++++---- src/test/modules/commit_ts/t/002_standby.pl | 24 +++++++++++++++++++++ 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/backend/access/transam/commit_ts.c b/src/backend/access/transam/commit_ts.c index 9e6fd5d4657..1dfecff2c36 100644 --- a/src/backend/access/transam/commit_ts.c +++ b/src/backend/access/transam/commit_ts.c @@ -122,7 +122,7 @@ bool track_commit_timestamp; static void SetXidCommitTsInPage(TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz ts, - ReplOriginId nodeid, int64 pageno); + ReplOriginId nodeid, int64 pageno, XLogRecPtr lsn); static void TransactionIdSetCommitTs(TransactionId xid, TimestampTz ts, ReplOriginId nodeid, int slotno); static void error_commit_ts_disabled(void); @@ -137,6 +137,7 @@ static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXid); * for a transaction and its subtransaction tree, as efficiently as possible. * * xid is the top level transaction id. + * lsn is the end of the local commit record, not the replication origin's LSN. * * subxids is an array of xids of length nsubxids, representing subtransactions * in the tree of xid. In various cases nsubxids may be zero. @@ -149,7 +150,7 @@ static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXid); void TransactionTreeSetCommitTsData(TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz timestamp, - ReplOriginId nodeid) + ReplOriginId nodeid, XLogRecPtr lsn) { int i; TransactionId headxid; @@ -166,6 +167,8 @@ TransactionTreeSetCommitTsData(TransactionId xid, int nsubxids, if (!commitTsShared->commitTsActive) return; + Assert(XLogRecPtrIsValid(lsn)); + /* * Figure out the latest Xid in this batch: either the last subxid if * there's any, otherwise the parent xid. @@ -197,7 +200,7 @@ TransactionTreeSetCommitTsData(TransactionId xid, int nsubxids, /* subxids[i..j] are on the same page as the head */ SetXidCommitTsInPage(headxid, j - i, subxids + i, timestamp, nodeid, - pageno); + pageno, lsn); /* if we wrote out all subxids, we're done. */ if (j >= nsubxids) @@ -230,7 +233,7 @@ TransactionTreeSetCommitTsData(TransactionId xid, int nsubxids, static void SetXidCommitTsInPage(TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz ts, - ReplOriginId nodeid, int64 pageno) + ReplOriginId nodeid, int64 pageno, XLogRecPtr lsn) { LWLock *lock = SimpleLruGetBankLock(CommitTsCtl, pageno); int slotno; @@ -238,12 +241,15 @@ SetXidCommitTsInPage(TransactionId xid, int nsubxids, LWLockAcquire(lock, LW_EXCLUSIVE); - slotno = SimpleLruReadPage(CommitTsCtl, pageno, true, &xid); + /* A write in progress may already have checked the page's LSN. */ + slotno = SimpleLruReadPage(CommitTsCtl, pageno, false, &xid); TransactionIdSetCommitTs(xid, ts, nodeid, slotno); for (i = 0; i < nsubxids; i++) TransactionIdSetCommitTs(subxids[i], ts, nodeid, slotno); + if (CommitTsCtl->shared->group_lsn[slotno] < lsn) + CommitTsCtl->shared->group_lsn[slotno] = lsn; CommitTsCtl->shared->page_dirty[slotno] = true; LWLockRelease(lock); @@ -554,6 +560,7 @@ CommitTsShmemRequest(void *arg) .long_segment_names = false, .nslots = CommitTsShmemBuffers(), + .nlsns = 1, .PagePrecedes = CommitTsPagePrecedes, .errdetail_for_io_error = commit_ts_errdetail_for_io_error, diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c index d89afb09320..635d2239774 100644 --- a/src/backend/access/transam/twophase.c +++ b/src/backend/access/transam/twophase.c @@ -2436,7 +2436,7 @@ RecordTransactionCommitPrepared(TransactionId xid, TransactionTreeSetCommitTsData(xid, nchildren, children, replorigin_xact_state.origin_timestamp, - replorigin_xact_state.origin); + replorigin_xact_state.origin, recptr); /* * We don't currently try to sleep before flush here ... nor is there any diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index d8d91bed427..aba45480f3c 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -1513,7 +1513,7 @@ RecordTransactionCommit(void) TransactionTreeSetCommitTsData(xid, nchildren, children, replorigin_xact_state.origin_timestamp, - replorigin_xact_state.origin); + replorigin_xact_state.origin, XactLastRecEnd); } /* @@ -6256,7 +6256,7 @@ xact_redo_commit(xl_xact_parsed_commit *parsed, /* Set the transaction commit timestamp and metadata */ TransactionTreeSetCommitTsData(xid, parsed->nsubxacts, parsed->subxacts, - commit_time, origin_id); + commit_time, origin_id, lsn); if (standbyState == STANDBY_DISABLED) { diff --git a/src/include/access/commit_ts.h b/src/include/access/commit_ts.h index 825ccda90ed..e19422d0caf 100644 --- a/src/include/access/commit_ts.h +++ b/src/include/access/commit_ts.h @@ -21,7 +21,7 @@ extern PGDLLIMPORT bool track_commit_timestamp; extern void TransactionTreeSetCommitTsData(TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz timestamp, - ReplOriginId nodeid); + ReplOriginId nodeid, XLogRecPtr lsn); extern bool TransactionIdGetCommitTsData(TransactionId xid, TimestampTz *ts, ReplOriginId *nodeid); extern TransactionId GetLatestCommitTsData(TimestampTz *ts, diff --git a/src/include/access/slru.h b/src/include/access/slru.h index b4adb1789c7..b79b6fcfc4e 100644 --- a/src/include/access/slru.h +++ b/src/include/access/slru.h @@ -85,11 +85,10 @@ typedef struct SlruSharedData /* * Optional array of WAL flush LSNs associated with entries in the SLRU - * pages. If not zero/NULL, we must flush WAL before writing pages (true - * for pg_xact, false for everything else). group_lsn[] has - * lsn_groups_per_page entries per buffer slot, each containing the - * highest LSN known for a contiguous group of SLRU entries on that slot's - * page. + * pages. If present, WAL must be flushed before writing the page. + * group_lsn[] has lsn_groups_per_page entries per buffer slot, each + * containing the highest LSN known for a contiguous group of SLRU entries + * on that slot's page. */ XLogRecPtr *group_lsn; int lsn_groups_per_page; diff --git a/src/test/modules/commit_ts/t/002_standby.pl b/src/test/modules/commit_ts/t/002_standby.pl index 3b64628e677..4f531e8d001 100644 --- a/src/test/modules/commit_ts/t/002_standby.pl +++ b/src/test/modules/commit_ts/t/002_standby.pl @@ -24,6 +24,7 @@ $primary->backup($bkplabel); my $standby = PostgreSQL::Test::Cluster->new('standby'); $standby->init_from_backup($primary, $bkplabel, has_streaming => 1); +$standby->append_conf('postgresql.conf', 'commit_timestamp_buffers = 16'); $standby->start; for my $i (1 .. 10) @@ -44,6 +45,29 @@ my $standby_ts = $standby->safe_psql('postgres', ); is($primary_ts, $standby_ts, "standby gives same value as primary"); +my $before_commits = $primary->safe_psql('postgres', + "SELECT pg_create_restore_point('before_commits')"); + +# Fill more than 16 pages of timestamps (10 bytes per XID) to force eviction. +my $after_commits = $primary->safe_psql( + 'postgres', q{ + DO $$ + BEGIN + FOR counter IN 1..17 * (current_setting('block_size')::int / 10) LOOP + PERFORM pg_current_xact_id(); + COMMIT; + END LOOP; + END $$; + SELECT pg_create_restore_point('after_commits'); +}); +$primary->wait_for_catchup($standby, 'replay', $after_commits); +is( $standby->safe_psql( + 'postgres', + "SELECT min_recovery_end_lsn > '$before_commits' FROM pg_control_recovery()" + ), + 't', + 'writing commit timestamps advances the minimum recovery point'); + $primary->append_conf('postgresql.conf', 'track_commit_timestamp = off'); $primary->restart; $primary->safe_psql('postgres', 'checkpoint'); -- 2.34.1