From ff47e927bb7f57b03720c610ecf963faa640a82a Mon Sep 17 00:00:00 2001 From: Ayush Tiwari Date: Thu, 26 Nov 2026 18:46:41 +0000 Subject: [PATCH v1 2/2] Add a test for resuming into a compressed partial segment This appends some junk to an LZ4 .partial segment and resumes streaming into it. The next start of pg_receivewal then fails if any of the junk is left in the completed segment, which is what happens without the previous patch. This works by tweaking files on disk, so it's not meant for commit. Author: Ayush Tiwari --- src/bin/pg_basebackup/t/020_pg_receivewal.pl | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/bin/pg_basebackup/t/020_pg_receivewal.pl b/src/bin/pg_basebackup/t/020_pg_receivewal.pl index 9f3b0a12e9c..cb5162d9989 100644 --- a/src/bin/pg_basebackup/t/020_pg_receivewal.pl +++ b/src/bin/pg_basebackup/t/020_pg_receivewal.pl @@ -215,6 +215,56 @@ SKIP: "lz4 verified the integrity of compressed WAL segments"); } +# Resume streaming into an LZ4 partial segment that has some junk at its end, +# as if an earlier run had written a longer compressed stream there. +SKIP: +{ + skip "postgres was not built with LZ4 support", 3 + if (!check_pg_config("#define USE_LZ4 1")); + + my @lz4_partial_wals = glob "$stream_dir/*.lz4.partial"; + is(scalar(@lz4_partial_wals), + 1, "one partial WAL segment compressed with LZ4 exists"); + my $lz4_partial = $lz4_partial_wals[0]; + + open(my $fh, '>>', $lz4_partial) + or die "could not open \"$lz4_partial\": $!"; + binmode($fh); + print $fh 'stale data ' x 10000; + close($fh) or die "could not close \"$lz4_partial\": $!"; + + $primary->psql('postgres', 'SELECT pg_switch_wal();'); + $nextlsn = + $primary->safe_psql('postgres', 'SELECT pg_current_wal_insert_lsn();'); + chomp($nextlsn); + $primary->psql('postgres', 'INSERT INTO test_table VALUES (100);'); + $primary->command_ok( + [ + 'pg_receivewal', + '--directory' => $stream_dir, + '--verbose', + '--endpos' => $nextlsn, + '--no-loop', + '--compress' => 'lz4' + ], + 'resume streaming into a partial LZ4 segment'); + + # pg_receivewal decompresses completed LZ4 segments while looking for + # its start position, which fails if the junk is still there. With + # such an early --endpos, it stops right after that. + $primary->command_ok( + [ + 'pg_receivewal', + '--directory' => $stream_dir, + '--endpos' => '0/1', + '--no-loop' + ], + 'completed LZ4 segment can be decompressed'); + + # Update the list of partial wals with the current one. + @partial_wals = glob "$stream_dir/*.lz4.partial"; +} + # 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.43.0