From 2c5ce53e9d70040316c1a545d122b85cfe6a2d17 Mon Sep 17 00:00:00 2001 From: Zsolt Parragi Date: Sat, 21 Nov 2026 08:54:05 +0000 Subject: [PATCH v1] Track a standby's own wal_log_hints in pg_control pg_rewind refuses a target without data checksums or wal_log_hints, based on pg_control. On a standby that field held the primary's value, but hint bits are written according to the standby's own setting. So a standby with wal_log_hints = off behind a primary with it on was accepted, and hint bits it wrote without WAL survived the rewind, causing wrong visibility. To fix, starting archive recovery with wal_log_hints = off clears the field, and replaying XLOG_PARAMETER_CHANGE no longer sets it. Only promotion sets it again, so a standby that ever ran with it off is refused until promoted or re-created. The end of archive recovery now always logs the parameters, as pg_control can no longer tell whether wal_log_hints changed. --- doc/src/sgml/ref/pg_rewind.sgml | 11 ++ src/backend/access/transam/xlog.c | 35 +++++- src/bin/pg_rewind/meson.build | 1 + src/bin/pg_rewind/pg_rewind.c | 10 +- .../pg_rewind/t/012_standby_wal_log_hints.pl | 112 ++++++++++++++++++ 5 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 src/bin/pg_rewind/t/012_standby_wal_log_hints.pl diff --git a/doc/src/sgml/ref/pg_rewind.sgml b/doc/src/sgml/ref/pg_rewind.sgml index ab5c77ab61a..889c640e3aa 100644 --- a/doc/src/sgml/ref/pg_rewind.sgml +++ b/doc/src/sgml/ref/pg_rewind.sgml @@ -124,6 +124,17 @@ PostgreSQL documentation on, but is enabled by default. + + If the target server is a standby, its own setting of + wal_log_hints is what counts, not the setting of its + primary, and it must have been enabled the whole time. A standby that + has run with wal_log_hints disabled, or was copied + from a server that has, is refused as a target unless data checksums are + enabled, even after wal_log_hints has been enabled. + Promoting the standby, or re-creating it from a server that has + wal_log_hints enabled, lifts that restriction. + + The data checksum states of the source and target server must be compatible. pg_rewind refuses to run if an diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fd37ea4f607..b12c870e9fa 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -746,7 +746,7 @@ static void CleanupAfterArchiveRecovery(TimeLineID EndOfLogTLI, XLogRecPtr EndOfLog, TimeLineID newTLI); static void CheckRequiredParameterValues(void); -static void XLogReportParameters(void); +static void XLogReportParameters(bool force); static int LocalSetXLogInsertAllowed(void); static void CreateEndOfRecoveryRecord(void); static XLogRecPtr CreateOverwriteContrecordRecord(XLogRecPtr aborted_lsn, @@ -6411,6 +6411,21 @@ StartupXLOG(void) XLogCtl->SharedRecoveryState = RECOVERY_STATE_CRASH; SpinLockRelease(&XLogCtl->info_lck); + /* + * In archive recovery, wal_log_hints in pg_control tells whether we + * have run with it enabled all along, rather than what the primary + * runs with: clear it if it is off now, and never set it here. Hint + * bits set by hot standby queries reach disk without WAL unless + * wal_log_hints or data checksums are enabled here, whatever the + * primary runs with, and they stay there when wal_log_hints is + * enabled later. pg_rewind relies on pg_control to tell whether that + * could have happened. xlog_redo() leaves the field alone for the + * same reason, and the end of recovery sets it to the current value, + * see XLogReportParameters(). + */ + if (ArchiveRecoveryRequested && !wal_log_hints) + ControlFile->wal_log_hints = false; + /* * Update pg_control to show that we are recovering and to show the * selected checkpoint as the place we are starting from. We also mark @@ -6844,9 +6859,11 @@ StartupXLOG(void) /* * If any of the critical GUCs have changed, log them before we allow - * backends to write WAL. + * backends to write WAL. After archive recovery, wal_log_hints in + * pg_control is not the value last replayed, so it cannot tell whether + * that one changed; log them unconditionally then. */ - XLogReportParameters(); + XLogReportParameters(ArchiveRecoveryRequested); /* If this is archive recovery, perform post-recovery cleanup actions. */ if (ArchiveRecoveryRequested) @@ -9098,11 +9115,13 @@ XLogAssignLSN(void) /* * Check if any of the GUC parameters that are critical for hot standby * have changed, and update the value in pg_control file if necessary. + * If force is true, log them even if they appear unchanged. */ static void -XLogReportParameters(void) +XLogReportParameters(bool force) { - if (wal_level != ControlFile->wal_level || + if (force || + wal_level != ControlFile->wal_level || wal_log_hints != ControlFile->wal_log_hints || MaxConnections != ControlFile->MaxConnections || max_worker_processes != ControlFile->max_worker_processes || @@ -9577,7 +9596,11 @@ xlog_redo(XLogReaderState *record) ControlFile->max_prepared_xacts = xlrec.max_prepared_xacts; ControlFile->max_locks_per_xact = xlrec.max_locks_per_xact; ControlFile->wal_level = xlrec.wal_level; - ControlFile->wal_log_hints = xlrec.wal_log_hints; + + /* + * wal_log_hints is not copied, pg_control keeps track of our own + * setting; see StartupXLOG(). + */ /* * Update minRecoveryPoint to ensure that if recovery is aborted, we diff --git a/src/bin/pg_rewind/meson.build b/src/bin/pg_rewind/meson.build index 52a6ab0a515..b4967b21a13 100644 --- a/src/bin/pg_rewind/meson.build +++ b/src/bin/pg_rewind/meson.build @@ -45,6 +45,7 @@ tests += { 't/009_growing_files.pl', 't/010_keep_recycled_wals.pl', 't/011_wal_copy.pl', + 't/012_standby_wal_log_hints.pl', ], }, } diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c index d1d726ee82d..9a1a7adbdae 100644 --- a/src/bin/pg_rewind/pg_rewind.c +++ b/src/bin/pg_rewind/pg_rewind.c @@ -844,7 +844,15 @@ sanityChecks(void) if (ControlFile_target.data_checksum_version != PG_DATA_CHECKSUM_VERSION && !ControlFile_target.wal_log_hints) { - pg_fatal("target server needs to use either data checksums or \"wal_log_hints = on\""); + pg_log_error("target server needs to use either data checksums or \"wal_log_hints = on\""); + + /* + * A standby is also refused while its configuration has wal_log_hints + * enabled, if it was off earlier; see StartupXLOG(). + */ + if (ControlFile_target.state == DB_SHUTDOWNED_IN_RECOVERY) + pg_log_error_detail("A standby must have run with \"wal_log_hints = on\" ever since it was created or last promoted."); + exit(1); } /* diff --git a/src/bin/pg_rewind/t/012_standby_wal_log_hints.pl b/src/bin/pg_rewind/t/012_standby_wal_log_hints.pl new file mode 100644 index 00000000000..66e2141b746 --- /dev/null +++ b/src/bin/pg_rewind/t/012_standby_wal_log_hints.pl @@ -0,0 +1,112 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group +# +# Check that the control file of a standby tracks the standby's own +# wal_log_hints, not the value of its primary, so that pg_rewind refuses a +# standby target that may have written hint bits without WAL. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +# allows_streaming sets wal_log_hints = on on the primary. +my $node_primary = PostgreSQL::Test::Cluster->new('primary'); +$node_primary->init(allows_streaming => 1, no_data_checksums => 1); +$node_primary->start; + +$node_primary->backup('my_backup'); +my $node_standby = PostgreSQL::Test::Cluster->new('standby'); +$node_standby->init_from_backup($node_primary, 'my_backup', + has_streaming => 1); +$node_standby->append_conf('postgresql.conf', 'wal_log_hints = off'); +$node_standby->start; + +# A second standby that keeps wal_log_hints enabled. +my $node_standby_on = PostgreSQL::Test::Cluster->new('standby_on'); +$node_standby_on->init_from_backup($node_primary, 'my_backup', + has_streaming => 1); +$node_standby_on->start; + +# Have the standby replay a parameter change record, which carries the +# primary's wal_log_hints. +$node_primary->append_conf('postgresql.conf', 'track_commit_timestamp = on'); +$node_primary->restart; +$node_primary->wait_for_replay_catchup($node_standby); +$node_primary->wait_for_replay_catchup($node_standby_on); +$node_standby->stop; +$node_standby_on->stop; + +command_like( + [ 'pg_controldata', $node_standby->data_dir ], + qr/wal_log_hints setting:\s+off/, + 'control file of standby has its own wal_log_hints'); + +my @pg_rewind_cmd = ( + 'pg_rewind', + '--source-server' => $node_primary->connstr('postgres'), + '--target-pgdata' => $node_standby->data_dir); + +command_fails_like( + \@pg_rewind_cmd, + qr/target server needs to use either data checksums or "wal_log_hints = on"/, + 'pg_rewind fails with standby target that ran with wal_log_hints = off'); + +# Enabling wal_log_hints without starting the standby does not help, hint +# bits may already have been written. +$node_standby->append_conf('postgresql.conf', 'wal_log_hints = on'); +command_fails_like( + \@pg_rewind_cmd, + qr/target server needs to use either data checksums or "wal_log_hints = on"/, + 'pg_rewind fails after enabling wal_log_hints on stopped standby'); + +# Nor does running with it enabled from now on, the hint bits written +# before are still there. +$node_standby->start; +$node_standby->stop; +command_like( + [ 'pg_controldata', $node_standby->data_dir ], + qr/wal_log_hints setting:\s+off/, + 'control file of standby remembers that wal_log_hints was off'); +command_fails_like( + \@pg_rewind_cmd, + qr/A standby must have run with "wal_log_hints = on" ever since/, + 'pg_rewind fails after restarting standby with wal_log_hints = on'); + +command_checks_all( + [ + 'pg_rewind', + '--source-server' => $node_primary->connstr('postgres'), + '--target-pgdata' => $node_standby_on->data_dir + ], + 0, + [qr/^$/], + [qr/no rewind required/], + 'pg_rewind runs with standby target that always ran with wal_log_hints = on'); + +# At promotion, the standby logs its own parameters even when they match +# pg_control, which no longer holds the primary's wal_log_hints. +$node_standby->append_conf( + 'postgresql.conf', qq( +wal_log_hints = off +track_commit_timestamp = on +)); +$node_standby->start; +my $switch_lsn = + $node_standby->safe_psql('postgres', 'SELECT pg_last_wal_replay_lsn()'); +$node_standby->promote; +my $end_lsn = + $node_standby->safe_psql('postgres', 'SELECT pg_current_wal_flush_lsn()'); +$node_standby->stop; +command_like( + [ + 'pg_waldump', + '--path' => $node_standby->data_dir . '/pg_wal', + '--timeline' => 2, + '--start' => $switch_lsn, + '--end' => $end_lsn, + ], + qr/PARAMETER_CHANGE .*wal_log_hints=off/, + 'promoted standby logs its own wal_log_hints'); + +done_testing(); -- 2.56.1