Patch
Advance oldestXid when replaying a pg_xact truncation
- Branch
- master
- Risk
- 25
- Base commit
- 458c77d74d2bab2014e97f01df19dc805dc7b42a
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a23318-98dd-7dc4-b67d-bda1ede17826 | jq -r .body | git am
From 95346624609b3ef6def6752761c6df37b5ee86e9 Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Tue, 1 Dec 2026 11:32:09 -0800
Subject: [PATCH] Advance oldestXid when replaying a pg_xact truncation
Replay of a CLOG_TRUNCATE record removed the old pg_xact segments and
advanced oldestClogXid, but left TransamVariables->oldestXid alone
until a later checkpoint record of the originating server was
replayed. If recovery ended before that, either by promoting a
standby or by finishing crash recovery, the next checkpoint recorded
the stale oldestXid even though its redo pointer was past the
truncation record. oldestClogXid is not saved in checkpoints but is
initialized from the checkpoint's oldestXid at startup, on the
assumption that a truncation the checkpoint doesn't know about will be
replayed. So after one more crash, oldestClogXid pointed into the
removed segments, and pg_xact_status() for such an XID failed with
"could not open file" instead of returning NULL. The stale value
persisted until some database's datfrozenxid advanced again.
Fix by having the redo routine set the transaction ID limits from the
values in the truncation record, as vac_truncate_clog() does right
after truncating, unless oldestXid is already newer. Replay of a
multixact truncation already does the equivalent.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
---
src/backend/access/transam/clog.c | 16 ++++
src/test/recovery/meson.build | 1 +
.../t/062_clog_truncate_oldest_xid.pl | 81 +++++++++++++++++++
3 files changed, 98 insertions(+)
create mode 100644 src/test/recovery/t/062_clog_truncate_oldest_xid.pl
diff --git a/src/backend/access/transam/clog.c b/src/backend/access/transam/clog.c
index ab84c345543..97469122fb1 100644
--- a/src/backend/access/transam/clog.c
+++ b/src/backend/access/transam/clog.c
@@ -1103,6 +1103,22 @@ clog_redo(XLogReaderState *record)
AdvanceOldestClogXid(xlrec.oldestXact);
+ /*
+ * Advance oldestXid as well, like vac_truncate_clog() does right
+ * after truncating. Checkpoints don't save oldestClogXid; instead,
+ * oldestClogXid is initialized from the checkpoint's oldestXid at
+ * startup. If we left oldestXid alone here, then a checkpoint
+ * created by this server before it has replayed a later checkpoint
+ * record carrying the new value (for example, the checkpoint at the
+ * end of recovery) would begin after this record but still contain
+ * the old oldestXid. A crash recovery starting from that checkpoint
+ * would not replay this record, and would thus make oldestClogXid
+ * point into the removed segments.
+ */
+ if (TransactionIdPrecedes(TransamVariables->oldestXid,
+ xlrec.oldestXact))
+ SetTransactionIdLimit(xlrec.oldestXact, xlrec.oldestXactDb);
+
SimpleLruTruncate(XactCtl, xlrec.pageno);
}
else
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index cb3df6ed895..eeed72a54f1 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -70,6 +70,7 @@ tests += {
't/059_remote_apply_status_interval.pl',
't/060_multixact_crashed_updater.pl',
't/061_recovery_target_prefetch.pl',
+ 't/062_clog_truncate_oldest_xid.pl',
],
},
}
diff --git a/src/test/recovery/t/062_clog_truncate_oldest_xid.pl b/src/test/recovery/t/062_clog_truncate_oldest_xid.pl
new file mode 100644
index 00000000000..30202d3b2d0
--- /dev/null
+++ b/src/test/recovery/t/062_clog_truncate_oldest_xid.pl
@@ -0,0 +1,81 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test that replaying a truncation of pg_xact also advances the oldest XID
+# that later checkpoints record. Otherwise, a server that replayed the
+# truncation, without also replaying a later checkpoint record of the
+# server that performed it, would remember a stale oldest XID, and after
+# its next crash pg_xact_status() would try to read the removed segments.
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my ($ret, $stdout, $stderr);
+
+my $primary = PostgreSQL::Test::Cluster->new('primary');
+$primary->init(allows_streaming => 1);
+$primary->append_conf(
+ 'postgresql.conf', qq{
+autovacuum = off
+checkpoint_timeout = 1h
+});
+
+# Start assigning XIDs at the beginning of the second pg_xact segment, so
+# that freezing everything allows the first segment to be removed.
+my ($controldata) = run_command([ 'pg_controldata', $primary->data_dir ]);
+$controldata =~ /^Database block size:\s+(\d+)$/m
+ or die "could not determine block size";
+my $xacts_per_segment = 32 * 4 * $1;
+command_ok(
+ [
+ 'pg_resetwal',
+ '--next-transaction-id' => $xacts_per_segment,
+ $primary->data_dir
+ ],
+ 'set next transaction ID to the start of the second pg_xact segment');
+$primary->start;
+
+$primary->backup('backup');
+my $standby = PostgreSQL::Test::Cluster->new('standby');
+$standby->init_from_backup($primary, 'backup', has_streaming => 1);
+$standby->start;
+
+# Freeze all databases. The last VACUUM truncates pg_xact.
+$primary->safe_psql('postgres',
+ 'ALTER DATABASE template0 ALLOW_CONNECTIONS true');
+$primary->safe_psql($_, 'VACUUM FREEZE')
+ foreach ('template0', 'template1', 'postgres');
+ok(!-e $primary->data_dir . '/pg_xact/0000',
+ 'first pg_xact segment removed on primary');
+
+$primary->wait_for_replay_catchup($standby);
+ok(!-e $standby->data_dir . '/pg_xact/0000',
+ 'first pg_xact segment removed on standby');
+
+# Crash the primary before it writes a checkpoint, and fail over.
+$primary->stop('immediate');
+$standby->promote;
+
+# Checkpoint the promoted standby, crash it, and restart it. This is a
+# crash recovery that begins after the truncation record.
+$standby->safe_psql('postgres', 'CHECKPOINT');
+$standby->stop('immediate');
+$standby->start;
+($ret, $stdout, $stderr) =
+ $standby->psql('postgres', q{SELECT pg_xact_status('1000'::xid8) IS NULL});
+is("$ret|$stdout|$stderr", '0|t|',
+ 'status of truncated XID is NULL on promoted standby after crash');
+
+# The same applies to the old primary: its crash recovery replays the
+# truncation and ends with a checkpoint.
+$primary->start;
+$primary->stop('immediate');
+$primary->start;
+($ret, $stdout, $stderr) =
+ $primary->psql('postgres', q{SELECT pg_xact_status('1000'::xid8) IS NULL});
+is("$ret|$stdout|$stderr", '0|t|',
+ 'status of truncated XID is NULL on old primary after second crash');
+
+done_testing();
base-commit: 458c77d74d2bab2014e97f01df19dc805dc7b42a
--
2.53.0