Hi,

While testing the new PG 19 support for reading WAL from tar archives, I
found that member_is_wal_file() in src/bin/pg_waldump/archive_waldump.c
accepts WAL files from directories other than pg_wal/.

The comment says "Skip files in subdirectories other than pg_wal/", but the
check only compares the first six characters:

if (pathlen > XLOG_FNAME_LEN &&
strncmp(pathname, XLOGDIR, strlen(XLOGDIR)) != 0)
return false;

So pg_wal.old/000000010000000000000038 or
pg_wal/somedir/000000010000000000000038 is also taken as WAL. If such a
file comes first in the archive, the real pg_wal/ copy is then skipped as a
duplicate.

This also affects pg_verifybackup. With -Ft and no pg_wal.tar (for example
pg_basebackup -Xfetch), it runs pg_waldump on base.tar.

How I reproduced it on REL_19_STABLE:

1. Put a WAL segment from another cluster into the data directory as
pg_wal.old/000000010000000000000038, using the same name as the segment the
next backup starts in.
2. pg_basebackup -D bk -Ft -Xfetch. base.tar has
pg_wal.old/000000010000000000000038 as member 9 and
pg_wal/000000010000000000000038 as member 1004.
3. pg_waldump -p bk/base.tar -s 0/03800028 -e 0/03800128 exits 0 and prints
a COMMIT record from the other cluster. The real records at that LSN are
CHECKPOINT_REDO, RUNNING_XACTS, CHECKPOINT_ONLINE and BACKUP_END.
4. I zeroed the first records of the real pg_wal/000000010000000000000038
in base.tar. Without pg_wal.old/, pg_verifybackup -F t fails with "WAL
parsing failed for timeline 1". With pg_wal.old/ present, it says "backup
successfully verified".

The attached patch accepts a WAL file only at the top level of the archive
or directly in pg_wal/, and adds a test to
https://www.google.com/url?q=http://001_basic.pl&source=gmail&ust=1795274530565000&sa=E.
The test fails without the code change and passes with it.

With the patch, the corrupted backup above fails verification, and
pg_waldump prints the real records. I checked that pg_waldump output is
unchanged compared to reading the directory for tar files with segments in
order, reversed and shuffled, with pg_wal/ and ./ prefixes, with a
duplicate member, and with gzip, lz4 and zstd compression. The pg_waldump
and pg_verifybackup TAP suites pass on master and REL_19_STABLE.

I think this should be backpatched to 19.

Regards,
Sai Pranav
From 1daf8d6c1e11c7c2b87da789cba046969d91155a Mon Sep 17 00:00:00 2001
From: Sai Pranav <[email protected]>
Date: Fri, 20 Nov 2026 15:21:28 +0000
Subject: [PATCH v1] pg_waldump: accept archive WAL members only at top level
 or in pg_wal/

member_is_wal_file() was meant to skip WAL files in subdirectories
other than pg_wal/, but it only checked that the member path starts
with "pg_wal".  So a file such as pg_wal.old/000000010000000000000038
was read as WAL.  If that file came first in the archive, the real
pg_wal/ copy was then skipped as a duplicate.

This matters for pg_verifybackup, which runs pg_waldump on base.tar
when the backup has no pg_wal.tar.  A data directory with a
pg_wal.old/ directory holding a segment of the same name from another
cluster made pg_verifybackup check that file instead of the backup's
own WAL, and report a backup with corrupted WAL as verified.

Accept a WAL file only at the top level of the archive or directly in
pg_wal/, and add a test.

Backpatch to 19, where reading WAL from tar archives was added.
---
 src/bin/pg_waldump/archive_waldump.c | 11 ++++++---
 src/bin/pg_waldump/t/001_basic.pl    | 34 ++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/src/bin/pg_waldump/archive_waldump.c b/src/bin/pg_waldump/archive_waldump.c
index e0c3aa4d25..fe1809b898 100644
--- a/src/bin/pg_waldump/archive_waldump.c
+++ b/src/bin/pg_waldump/archive_waldump.c
@@ -829,9 +829,14 @@ member_is_wal_file(astreamer_waldump *mystreamer, astreamer_member *member,
 	canonicalize_path(pathname);
 	pathlen = strlen(pathname);
 
-	/* Skip files in subdirectories other than pg_wal/ */
-	if (pathlen > XLOG_FNAME_LEN &&
-		strncmp(pathname, XLOGDIR, strlen(XLOGDIR)) != 0)
+	/*
+	 * Accept only a WAL file at the top level of the archive or directly in
+	 * pg_wal/.  Checking just the prefix would also accept a directory such
+	 * as pg_wal.old/ or pg_wal/archive/ that merely starts with "pg_wal".
+	 */
+	if (pathlen != XLOG_FNAME_LEN &&
+		(pathlen != strlen(XLOGDIR) + 1 + XLOG_FNAME_LEN ||
+		 strncmp(pathname, XLOGDIR "/", strlen(XLOGDIR) + 1) != 0))
 		return false;
 
 	/* WAL file may appear with a full path (e.g., pg_wal/<name>) */
diff --git a/src/bin/pg_waldump/t/001_basic.pl b/src/bin/pg_waldump/t/001_basic.pl
index 7b33efc629..244fdd10a3 100644
--- a/src/bin/pg_waldump/t/001_basic.pl
+++ b/src/bin/pg_waldump/t/001_basic.pl
@@ -509,4 +509,38 @@ for my $scenario (@scenarios)
 	}
 }
 
+# A WAL file in a directory whose name only starts with "pg_wal" is not the
+# server's WAL and must not be read from an archive.
+SKIP:
+{
+	skip "tar command is not available", 1
+	  if !defined $tar || $tar eq '';
+
+	my $lookalike_dir = "$tmp_dir/lookalike";
+	mkdir $lookalike_dir;
+	mkdir "$lookalike_dir/pg_wal.old";
+	copy($node->data_dir . '/pg_wal/' . $start_walfile,
+		"$lookalike_dir/pg_wal.old/$start_walfile")
+	  || die "copy: $!";
+
+	my $cwd = getcwd;
+	chdir($lookalike_dir) || die "chdir: $!";
+	command_ok(
+		[
+			$tar, @tar_p_flags, '-cf', "$tmp_dir/lookalike.tar",
+			"pg_wal.old/$start_walfile"
+		]);
+	chdir($cwd) || die "chdir: $!";
+
+	command_fails_like(
+		[
+			'pg_waldump',
+			'--path' => "$tmp_dir/lookalike.tar",
+			'--start' => $start_lsn,
+			'--limit' => 1,
+		],
+		qr/error: could not find WAL/,
+		'WAL file outside pg_wal/ in archive is ignored');
+}
+
 done_testing();
-- 
2.50.1 (Apple Git-155)

Reply via email to