Patch
Advance oldestXid before truncating pg_xact
- Branch
- master
- Risk
- 35
- Base commit
- 32afcc29df98b245d6c2055fd2a21e6cd2d0fd06
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a233b7-dc3d-75d8-9a8a-d6e3e83aa687 | jq -r .body | git am
From e69b62c1ad92bde056522c78913b013297ee00bb Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Tue, 1 Dec 2026 14:25:22 -0800
Subject: [PATCH] Advance oldestXid before truncating pg_xact
vac_truncate_clog() truncated pg_xact, which advances oldestClogXid and
WAL-logs a CLOG_TRUNCATE record, and only afterwards called
SetTransactionIdLimit() to advance oldestXid. A checkpoint that
started in between had its redo pointer after the truncation record,
but recorded the old oldestXid. Checkpoints don't save oldestClogXid;
startup initializes it from the checkpoint's oldestXid, on the
assumption that a truncation that the checkpoint doesn't reflect will
be replayed. So after a crash, or after a crash of a standby that had
created a restartpoint from such a checkpoint, oldestClogXid pointed
into the removed segments, and pg_xact_status() for an XID in them
failed with "could not open file" instead of returning NULL. The
stale value was carried over into later checkpoints until some
database's datfrozenxid advanced again. Tuple visibility was not
affected, only lookups of user-supplied XIDs.
Fix by calling SetTransactionIdLimit() before truncating pg_xact, so
that any checkpoint whose redo pointer follows the truncation record
sees the new oldestXid. This is the same order that is used for
commit timestamps, whose oldest value is advanced before truncating.
SetMultiXactIdLimit() has to stay after TruncateMultiXact(), which
compares the new oldest multixact with the current one.
Add an injection point after the truncation of pg_xact, and a test
that takes a checkpoint there and then crashes the primary and a
standby.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
src/backend/commands/vacuum.c | 20 +++-
src/test/recovery/meson.build | 1 +
.../t/062_clog_truncate_checkpoint.pl | 108 ++++++++++++++++++
3 files changed, 124 insertions(+), 5 deletions(-)
create mode 100644 src/test/recovery/t/062_clog_truncate_checkpoint.pl
diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index 63f6b8a733a..3fa21d7226c 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -2012,20 +2012,30 @@ vac_truncate_clog(TransactionId frozenXID,
*/
AdvanceOldestCommitTsXid(frozenXID);
+ /*
+ * Update the wrap limit for GetNewTransactionId. This must be done
+ * before CLOG truncation is WAL-logged. Checkpoints don't save
+ * oldestClogXid; after a crash, it's initialized from the checkpoint's
+ * oldestXid instead. That's only correct if every checkpoint whose redo
+ * pointer follows the truncation record has the new oldestXid.
+ */
+ SetTransactionIdLimit(frozenXID, oldestxid_datoid);
+
/*
* Truncate CLOG, multixact and CommitTs to the oldest computed value.
*/
TruncateCLOG(frozenXID, oldestxid_datoid);
+ INJECTION_POINT("vacuum-after-clog-truncation", NULL);
TruncateCommitTs(frozenXID);
TruncateMultiXact(minMulti, minmulti_datoid);
/*
- * Update the wrap limit for GetNewTransactionId and creation of new
- * MultiXactIds. Note: these functions will also signal the postmaster
- * for an(other) autovac cycle if needed. XXX should we avoid possibly
- * signaling twice?
+ * Update the wrap limit for creation of new MultiXactIds. This must be
+ * done after TruncateMultiXact(), which compares the new limit with the
+ * old one. Note: this function and SetTransactionIdLimit() will also
+ * signal the postmaster for an(other) autovac cycle if needed. XXX
+ * should we avoid possibly signaling twice?
*/
- SetTransactionIdLimit(frozenXID, oldestxid_datoid);
SetMultiXactIdLimit(minMulti, minmulti_datoid);
LWLockRelease(WrapLimitsVacuumLock);
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index cb3df6ed895..975263f079a 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_checkpoint.pl',
],
},
}
diff --git a/src/test/recovery/t/062_clog_truncate_checkpoint.pl b/src/test/recovery/t/062_clog_truncate_checkpoint.pl
new file mode 100644
index 00000000000..ac97ec239eb
--- /dev/null
+++ b/src/test/recovery/t/062_clog_truncate_checkpoint.pl
@@ -0,0 +1,108 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test that a checkpoint taken while VACUUM is truncating pg_xact records
+# the new oldest XID. Checkpoints don't save the oldest XID that can be
+# looked up in pg_xact; after a crash, it's initialized from the
+# checkpoint's oldest XID instead. If that was stale, 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;
+
+if ($ENV{enable_injection_points} ne 'yes')
+{
+ plan skip_all => 'Injection points not supported by this build';
+}
+
+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
+});
+$primary->start;
+
+if (!$primary->check_extension('injection_points'))
+{
+ plan skip_all => 'Extension injection_points not installed';
+}
+
+# Assign an XID in the first pg_xact segment.
+my $xid = $primary->safe_psql('postgres', 'SELECT pg_current_xact_id()');
+
+# Continue at the beginning of the second pg_xact segment, so that freezing
+# everything allows the first segment to be removed.
+$primary->stop;
+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->safe_psql('postgres', 'CREATE EXTENSION injection_points');
+$primary->safe_psql('postgres',
+ 'ALTER DATABASE template0 ALLOW_CONNECTIONS true');
+$primary->safe_psql($_, 'VACUUM FREEZE') foreach ('template0', 'template1');
+
+$primary->backup('backup');
+my $standby = PostgreSQL::Test::Cluster->new('standby');
+$standby->init_from_backup($primary, 'backup', has_streaming => 1);
+$standby->start;
+
+is($primary->safe_psql('postgres', qq{SELECT pg_xact_status('$xid')}),
+ 'committed', 'status of XID in first pg_xact segment');
+
+# Freezing the last database truncates pg_xact. Make it wait right after
+# that.
+$primary->safe_psql('postgres',
+ "SELECT injection_points_attach('vacuum-after-clog-truncation', 'wait')");
+my $vacuum = $primary->background_psql('postgres');
+$vacuum->query_until(
+ qr/starting_vacuum/, q(
+\echo starting_vacuum
+VACUUM FREEZE;
+));
+$primary->wait_for_event('client backend', 'vacuum-after-clog-truncation');
+ok( !-e $primary->data_dir . '/pg_xact/0000',
+ 'first pg_xact segment removed on primary');
+
+# This checkpoint's redo pointer follows the truncation record, so neither
+# crash recovery nor a restartpoint created from it will replay that record.
+$primary->safe_psql('postgres', 'CHECKPOINT');
+
+# Make the standby create a restartpoint from that checkpoint, then crash
+# and restart it.
+$primary->wait_for_replay_catchup($standby);
+ok( !-e $standby->data_dir . '/pg_xact/0000',
+ 'first pg_xact segment removed on standby');
+$standby->safe_psql('postgres', 'CHECKPOINT');
+$standby->stop('immediate');
+$standby->start;
+($ret, $stdout, $stderr) =
+ $standby->psql('postgres', qq{SELECT pg_xact_status('$xid') IS NULL});
+is("$ret|$stdout|$stderr", '0|t|',
+ 'status of truncated XID is NULL on standby after crash');
+
+# Crash the primary while VACUUM is still waiting, and restart it.
+$primary->stop('immediate');
+$vacuum->{run}->finish;
+$primary->start;
+($ret, $stdout, $stderr) =
+ $primary->psql('postgres', qq{SELECT pg_xact_status('$xid') IS NULL});
+is("$ret|$stdout|$stderr", '0|t|',
+ 'status of truncated XID is NULL on primary after crash');
+
+done_testing();
base-commit: 32afcc29df98b245d6c2055fd2a21e6cd2d0fd06
--
2.53.0