From 5ed8157fa4c18909687804da70599c01774736a2 Mon Sep 17 00:00:00 2001
From: Sai Pranav <rajasaipranav0@gmail.com>
Date: Sat, 21 Nov 2026 06:29:44 +0000
Subject: [PATCH v1] pg_receivewal: Skip a completed LZ4 segment whose frame is
 incomplete

When looking for where to restart streaming, pg_receivewal decompresses
each completed .lz4 segment and accepts it if it yields exactly one
segment of data.  It did not check that the LZ4 frame was complete, so a
file cut off after its last data block but before the frame's end mark
was taken as a complete segment, and streaming restarted after it.

LZ4F_decompress() returns 0 once a frame is fully decoded.  Skip the
segment, with a warning, if that never happened.

Discussion: https://postgr.es/m/19890-293babc93c520dc8@postgresql.org
---
 src/bin/pg_basebackup/pg_receivewal.c        |  9 ++++
 src/bin/pg_basebackup/t/020_pg_receivewal.pl | 50 ++++++++++++++++++++
 2 files changed, 59 insertions(+)

diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c
index 85f3616beb..e52a70f8db 100644
--- a/src/bin/pg_basebackup/pg_receivewal.c
+++ b/src/bin/pg_basebackup/pg_receivewal.c
@@ -372,6 +372,7 @@ FindStreamingStart(uint32 *tli_p)
 			int			fd;
 			ssize_t		r;
 			size_t		uncompressed_size = 0;
+			bool		frame_complete = false;
 			char		fullpath[MAXPGPATH * 2];
 			char	   *outbuf;
 			char	   *readbuf;
@@ -424,6 +425,8 @@ FindStreamingStart(uint32 *tli_p)
 
 					readp += read_size;
 					uncompressed_size += out_size;
+					/* LZ4F_decompress() returns 0 once the frame is complete */
+					frame_complete = (status == 0);
 				}
 
 				/*
@@ -450,6 +453,12 @@ FindStreamingStart(uint32 *tli_p)
 							   dirent->d_name, uncompressed_size);
 				continue;
 			}
+			if (!frame_complete)
+			{
+				pg_log_warning("compressed segment file \"%s\" is truncated, skipping",
+							   dirent->d_name);
+				continue;
+			}
 #else
 			pg_log_error("cannot check file \"%s\": compression with %s not supported by this build",
 						 dirent->d_name, "LZ4");
diff --git a/src/bin/pg_basebackup/t/020_pg_receivewal.pl b/src/bin/pg_basebackup/t/020_pg_receivewal.pl
index 9f3b0a12e9..3ffcf2cfd7 100644
--- a/src/bin/pg_basebackup/t/020_pg_receivewal.pl
+++ b/src/bin/pg_basebackup/t/020_pg_receivewal.pl
@@ -3,6 +3,8 @@
 
 use strict;
 use warnings FATAL => 'all';
+use File::Basename;
+use File::Copy;
 use PostgreSQL::Test::Utils;
 use PostgreSQL::Test::Cluster;
 use Test::More;
@@ -215,6 +217,54 @@ SKIP:
 		"lz4 verified the integrity of compressed WAL segments");
 }
 
+# A completed LZ4 segment whose frame is cut off after the last data block
+# decompresses to the full segment size, but must still be streamed again.
+SKIP:
+{
+	skip "postgres was not built with LZ4 support", 4
+	  if (!check_pg_config("#define USE_LZ4 1"));
+
+	my $trunc_dir = $primary->basedir . '/lz4_truncated';
+	mkdir($trunc_dir);
+	my ($lz4_wal) = glob "$stream_dir/*.lz4";
+	my $name = basename($lz4_wal);
+	copy($lz4_wal, "$trunc_dir/$name") or die "could not copy $lz4_wal: $!";
+	# Drop the 4-byte end mark of the frame.
+	truncate("$trunc_dir/$name", (-s "$trunc_dir/$name") - 4)
+	  or die "could not truncate $trunc_dir/$name: $!";
+
+	my $segsize = $primary->safe_psql('postgres',
+		"SELECT pg_size_bytes(current_setting('wal_segment_size'))");
+	my ($tli, $logid, $segno) =
+	  $name =~ /^([0-9A-F]{8})([0-9A-F]{8})([0-9A-F]{8})/;
+	my $seg_start = sprintf('%X/%08X', hex($logid), hex($segno) * $segsize);
+
+	# A complete segment just before it, so that skipping the truncated one
+	# makes streaming restart at its start, not at the server's position.
+	# Only the size of an uncompressed segment is checked.
+	my $prev = sprintf('%s%s%08X', $tli, $logid, hex($segno) - 1);
+	open(my $fh, '>', "$trunc_dir/$prev") or die "could not create $prev: $!";
+	truncate($fh, $segsize) or die "could not extend $prev: $!";
+	close($fh);
+
+	$primary->command_checks_all(
+		[
+			'pg_receivewal',
+			'--directory' => $trunc_dir,
+			'--verbose',
+			'--endpos' => $nextlsn,
+			'--no-loop',
+			'--compress' => 'lz4'
+		],
+		0,
+		[qr/^$/],
+		[
+			qr/compressed segment file "\Q$name\E" is truncated, skipping/,
+			qr/starting log streaming at \Q$seg_start\E /
+		],
+		'pg_receivewal streams a truncated LZ4 segment again');
+}
+
 # Verify that the start streaming position is computed and that the value is
 # correct regardless of whether any compression is available.
 $primary->psql('postgres', 'SELECT pg_switch_wal();');
-- 
2.50.1 (Apple Git-155)

