From 25ec128d3dd5f6eb51fd8b2b45f1fcc36339c4de Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Thu, 29 Oct 2026 22:10:14 -0400 Subject: [PATCH v1 2/4] Add test for a checkpoint that starts during CLOG truncation Add an injection point after TruncateCLOG() removes the old segments, and a TAP test that runs a checkpoint there. After crash recovery, the status of an XID in a removed segment must be NULL, not an error. Backpatch-through: 18 --- src/backend/access/transam/clog.c | 3 + src/test/recovery/Makefile | 3 +- src/test/recovery/meson.build | 1 + .../t/061_clog_truncate_checkpoint.pl | 66 +++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/test/recovery/t/061_clog_truncate_checkpoint.pl diff --git a/src/backend/access/transam/clog.c b/src/backend/access/transam/clog.c index 6f7f6b86eb6..52dafb6046c 100644 --- a/src/backend/access/transam/clog.c +++ b/src/backend/access/transam/clog.c @@ -46,6 +46,7 @@ #include "storage/subsystems.h" #include "storage/sync.h" #include "utils/guc_hooks.h" +#include "utils/injection_point.h" #include "utils/wait_event.h" /* @@ -1015,6 +1016,8 @@ TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid) /* Now we can remove the old CLOG segment(s) */ SimpleLruTruncate(XactCtl, cutoffPage); + + INJECTION_POINT("clog-truncate-after-unlink", NULL); } diff --git a/src/test/recovery/Makefile b/src/test/recovery/Makefile index 9c4102b6b2c..edc50a41923 100644 --- a/src/test/recovery/Makefile +++ b/src/test/recovery/Makefile @@ -13,7 +13,8 @@ EXTRA_INSTALL=contrib/pg_prewarm \ contrib/pg_stat_statements \ contrib/test_decoding \ src/test/modules/injection_points \ - src/test/modules/test_wait_lsn + src/test/modules/test_wait_lsn \ + src/test/modules/xid_wraparound subdir = src/test/recovery top_builddir = ../../.. diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build index b6ac9dd07a7..eb3a1bc8a39 100644 --- a/src/test/recovery/meson.build +++ b/src/test/recovery/meson.build @@ -69,6 +69,7 @@ tests += { 't/058_shutdown_crash_restart.pl', 't/059_remote_apply_status_interval.pl', 't/060_multixact_crashed_updater.pl', + 't/061_clog_truncate_checkpoint.pl', ], }, } diff --git a/src/test/recovery/t/061_clog_truncate_checkpoint.pl b/src/test/recovery/t/061_clog_truncate_checkpoint.pl new file mode 100644 index 00000000000..dc9bd63850e --- /dev/null +++ b/src/test/recovery/t/061_clog_truncate_checkpoint.pl @@ -0,0 +1,66 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# CLOG truncation removes old pg_xact segments. If a checkpoint whose redo +# pointer is after the truncation record still recorded the old oldestXid, +# recovery from it would set oldestClogXid to that value, and status lookups +# of XIDs in the removed segments would fail instead of returning NULL. + +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 $node = PostgreSQL::Test::Cluster->new('main'); +$node->init; +$node->append_conf('postgresql.conf', 'autovacuum = off'); +$node->start; + +if (!$node->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} + +$node->safe_psql('postgres', 'CREATE EXTENSION injection_points'); +$node->safe_psql('postgres', 'CREATE EXTENSION xid_wraparound'); +my $old_xid = $node->safe_psql('postgres', 'SELECT pg_current_xact_id()'); + +# Use up more than one pg_xact segment, and freeze all databases except one. +$node->safe_psql('postgres', 'SELECT consume_xids(1100000)'); +$node->safe_psql('postgres', + 'ALTER DATABASE template0 WITH ALLOW_CONNECTIONS true'); +$node->safe_psql($_, 'VACUUM (FREEZE)') for qw(template0 template1); + +# The last vacuum truncates CLOG, and stops there. +my $vacuum = $node->background_psql('postgres', on_error_stop => 0); +$vacuum->query_safe( + q(SELECT injection_points_set_local(); + SELECT injection_points_attach('clog-truncate-after-unlink', 'wait');)); +$vacuum->query_until( + qr/starting_vacuum/, q( + \echo starting_vacuum + VACUUM (FREEZE); +)); +$node->wait_for_event('client backend', 'clog-truncate-after-unlink'); +ok(!-e $node->data_dir . '/pg_xact/0000', 'oldest pg_xact segment removed'); + +# This checkpoint's redo pointer is after the truncation record. +$node->safe_psql('postgres', 'CHECKPOINT'); + +$node->stop('immediate'); +$vacuum->{run}->finish; +$node->start; + +my ($ret, $stdout, $stderr) = + $node->psql('postgres', qq(SELECT pg_xact_status('$old_xid'::xid8))); +is($ret, 0, 'status of truncated XID can be looked up after crash recovery'); +is($stdout, '', 'status of truncated XID is NULL after crash recovery'); + +$node->stop; +done_testing(); -- 2.37.1 (Apple Git-137.1)