From 86c718a44cccbacf307ae3935f4f98ac82196b8d Mon Sep 17 00:00:00 2001
From: Maksim Melnikov <m.melnikov@postgrespro.ru>
Date: Thu, 12 Nov 2026 10:18:21 +0300
Subject: [PATCH v1 3/3] Fix 039_end_of_wal.pl WAL manual inserts.

---
 src/test/perl/PostgreSQL/Test/Cluster.pm      | 24 ++++++++---
 src/test/recovery/t/039_end_of_wal.pl         | 40 +++++++++----------
 .../recovery/t/043_no_contrecord_switch.pl    |  6 +--
 3 files changed, 41 insertions(+), 29 deletions(-)

diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm b/src/test/perl/PostgreSQL/Test/Cluster.pm
index a2930fd1f2f8..a5eefe298898 100644
--- a/src/test/perl/PostgreSQL/Test/Cluster.pm
+++ b/src/test/perl/PostgreSQL/Test/Cluster.pm
@@ -3143,18 +3143,30 @@ Returns the end LSN up to which WAL has advanced, in bytes.
 
 sub advance_wal_out_of_record_splitting_zone
 {
-	my ($self, $wal_block_size) = @_;
+	my ($self, $wal_block_size, $end_lsn) = @_;
 
 	my $page_threshold = $wal_block_size / 4;
-	my $end_lsn = $self->_get_insert_lsn();
-	my $page_offset = $end_lsn % $wal_block_size;
+	my $insert_lsn = $self->_get_insert_lsn();
+
+	if (defined($end_lsn))
+	{
+		# both lsns should be on the same page to avoid edge cases when new WAL
+		# page hasn't been initialized yet.
+		while(int($insert_lsn / $wal_block_size) != int($end_lsn / $wal_block_size))
+		{
+			$end_lsn = $self->emit_wal(0);
+			$insert_lsn = $self->_get_insert_lsn();
+		}
+	}
+
+	my $page_offset = $insert_lsn % $wal_block_size;
 	while ($page_offset >= $wal_block_size - $page_threshold)
 	{
 		$self->emit_wal($page_threshold);
-		$end_lsn = $self->_get_insert_lsn();
-		$page_offset = $end_lsn % $wal_block_size;
+		$insert_lsn = $self->_get_insert_lsn();
+		$page_offset = $insert_lsn % $wal_block_size;
 	}
-	return $end_lsn;
+	return $insert_lsn;
 }
 
 =pod
diff --git a/src/test/recovery/t/039_end_of_wal.pl b/src/test/recovery/t/039_end_of_wal.pl
index 47f9bb15e033..50008d70d8b1 100644
--- a/src/test/recovery/t/039_end_of_wal.pl
+++ b/src/test/recovery/t/039_end_of_wal.pl
@@ -141,8 +141,8 @@ note "Single-page end-of-WAL detection";
 ###########################################################################
 
 # xl_tot_len is 0 (a common case, we hit trailing zeroes).
-$node->emit_wal(0);
-$end_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+$end_lsn = $node->emit_wal(0);
+$end_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 $node->stop('immediate');
 my $log_size = -s $node->logfile;
 $node->start;
@@ -152,8 +152,8 @@ ok( $node->log_contains(
 	"xl_tot_len zero");
 
 # xl_tot_len is < 24 (presumably recycled garbage).
-$node->emit_wal(0);
-$end_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+$end_lsn = $node->emit_wal(0);
+$end_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 $node->stop('immediate');
 $node->write_wal($TLI, $end_lsn, $WAL_SEGMENT_SIZE, build_record_header(23));
 $log_size = -s $node->logfile;
@@ -177,8 +177,8 @@ ok( $node->log_contains(
 	"xl_tot_len short at end-of-page");
 
 # Need more pages, but xl_prev check fails first.
-$node->emit_wal(0);
-$end_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+$end_lsn = $node->emit_wal(0);
+$end_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 $node->stop('immediate');
 $node->write_wal($TLI, $end_lsn, $WAL_SEGMENT_SIZE,
 	build_record_header(2 * 1024 * 1024 * 1024, 0, 0xdeadbeef));
@@ -189,8 +189,8 @@ ok( $node->log_contains(
 	"xl_prev bad");
 
 # xl_crc check fails.
-$node->emit_wal(0);
-$node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+$end_lsn = $node->emit_wal(0);
+$node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 $end_lsn = $node->emit_wal(10);
 $node->stop('immediate');
 # Corrupt a byte in that record, breaking its CRC.
@@ -211,8 +211,8 @@ note "Multi-page end-of-WAL detection, header is not split";
 # written to WAL.
 
 # Good xl_prev, we hit zero page next (zero magic).
-$node->emit_wal(0);
-$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+$end_lsn = $node->emit_wal(0);
+$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 $end_lsn = $node->emit_wal(0);
 $node->stop('immediate');
 $node->write_wal($TLI, $end_lsn, $WAL_SEGMENT_SIZE,
@@ -223,8 +223,8 @@ ok($node->log_contains("invalid magic number 0000 .* LSN .*", $log_size),
 	"xlp_magic zero");
 
 # Good xl_prev, we hit garbage page next (bad magic).
-$node->emit_wal(0);
-$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+$end_lsn = $node->emit_wal(0);
+$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 $end_lsn = $node->emit_wal(0);
 $node->stop('immediate');
 $node->write_wal($TLI, $end_lsn, $WAL_SEGMENT_SIZE,
@@ -238,8 +238,8 @@ ok($node->log_contains("invalid magic number CAFE .* LSN .*", $log_size),
 
 # Good xl_prev, we hit typical recycled page (good xlp_magic, bad
 # xlp_pageaddr).
-$node->emit_wal(0);
-$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+$end_lsn = $node->emit_wal(0);
+$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 $end_lsn = $node->emit_wal(0);
 $node->stop('immediate');
 $node->write_wal($TLI, $end_lsn, $WAL_SEGMENT_SIZE,
@@ -253,8 +253,8 @@ ok( $node->log_contains(
 	"xlp_pageaddr bad");
 
 # Good xl_prev, xlp_magic, xlp_pageaddr, but bogus xlp_info.
-$node->emit_wal(0);
-$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+$end_lsn = $node->emit_wal(0);
+$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 $end_lsn = $node->emit_wal(0);
 $node->stop('immediate');
 $node->write_wal($TLI, $end_lsn, $WAL_SEGMENT_SIZE,
@@ -272,8 +272,8 @@ ok($node->log_contains("invalid info bits 1234 in .*, LSN .*,", $log_size),
 
 # Good xl_prev, xlp_magic, xlp_pageaddr, but xlp_info doesn't mention
 # continuation record.
-$node->emit_wal(0);
-$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+$end_lsn = $node->emit_wal(0);
+$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 $end_lsn = $node->emit_wal(0);
 $node->stop('immediate');
 $node->write_wal($TLI, $end_lsn, $WAL_SEGMENT_SIZE,
@@ -288,8 +288,8 @@ ok($node->log_contains("there is no contrecord flag at .*", $log_size),
 
 # Good xl_prev, xlp_magic, xlp_pageaddr, xlp_info but xlp_rem_len doesn't add
 # up.
-$node->emit_wal(0);
-$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+$end_lsn = $node->emit_wal(0);
+$prev_lsn = $node->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 $end_lsn = $node->emit_wal(0);
 $node->stop('immediate');
 $node->write_wal($TLI, $end_lsn, $WAL_SEGMENT_SIZE,
diff --git a/src/test/recovery/t/043_no_contrecord_switch.pl b/src/test/recovery/t/043_no_contrecord_switch.pl
index ead41e6f40be..12072c5d64d6 100644
--- a/src/test/recovery/t/043_no_contrecord_switch.pl
+++ b/src/test/recovery/t/043_no_contrecord_switch.pl
@@ -79,9 +79,9 @@ $TLI = $primary->safe_psql('postgres',
 # Get close to the end of the current WAL page, enough to fit the
 # beginning of a record that spans on two pages, generating a
 # continuation record.
-$primary->emit_wal(0);
-my $end_lsn =
-  $primary->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
+my $end_lsn = $primary->emit_wal(0);
+$end_lsn =
+  $primary->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE, $end_lsn);
 
 # Do some math to find the record size that will overflow the page, and
 # write it.
-- 
2.43.0

