> On Aug 8, 2026, at 08:00, Chao Li wrote:
>
>
>
>> On Aug 8, 2026, at 03:42, Zsolt Parragi wrote:
>>
>> Hello!
>>
>> Isn't this a generic problem, also present in gzip/lz4 compressed backups?
>
> Maybe, but I haven’t checked them yet. I will do that next week.
I just verified gzip has the same problem using a similar script:
```
workdir=$(mktemp -d /tmp/gzip-trunc.XXXXXX)
mkdir "$workdir/truncated"
dd if=/dev/zero of="$workdir/base.tar" bs=1024 count=2
gzip -c "$workdir/base.tar" > "$workdir/base.tar.gz"
size=$(stat -f %z "$workdir/base.tar.gz")
dd if="$workdir/base.tar.gz" of="$workdir/truncated/base.tar.gz" bs=1 count=$((size - 1))
manifest_prefix=$'{"PostgreSQL-Backup-Manifest-Version": 1,\n "Files": [],\n "WAL-Ranges": [],\n'
printf '%s' "$manifest_prefix" > "$workdir/manifest-prefix"
manifest_checksum=$(shasum -a 256 "$workdir/manifest-prefix" | awk '{print $1}')
printf '%s"Manifest-Checksum": "%s"}\n' "$manifest_prefix" "$manifest_checksum" > "$workdir/truncated/backup_manifest"
gzip -t "$workdir/truncated/base.tar.gz"
pg_verifybackup -F t -s "$workdir/truncated"
```
gzip fails, while pg_verifybackup succeeds.
Looking at the code, astreamer_gzip_decompressor_content() stops processing when it has consumed the input buffer. However, the inflate() doc, see [2] and [3], says that only Z_STREAM_END indicates that decompression has completed. My debugging also shows that, when reading the truncated gzip file, inflate() returns Z_OK, while it returns Z_STREAM_END for a valid gzip file. Therefore, this patch records whether inflate() has returned Z_STREAM_END and checks that state in astreamer_gzip_decompressor_finalize().
Unlike zstd, we don't need to call inflate() again in astreamer_gzip_decompressor_finalize(). Once all input has been consumed without Z_STREAM_END, calling inflate() with no input cannot complete the stream.
See attached 0002 for the fix of gzip streamer. I will check the lz4 streamer next.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Re: Fix detection of truncated zstd-compressed backups
> On Aug 11, 2026, at 00:28, Daniel Gustafsson wrote:
>
>> On 10 Aug 2026, at 10:11, Chao Li wrote:
>>> On Aug 10, 2026, at 14:45, Chao Li wrote:
>
>>> See attached 0002 for the fix of gzip streamer. I will check the lz4 streamer next.
>>
>> Confirmed that lz4 also has the same problem. See the similar repro script:
>
> Thanks for this patchset, I think this is something we should fix. I took the
> liberty to squash the patchset into a single patch to start preparing it for
> the final shape, as well as adding a testcase to verify this.
Thank for taking care of this patch.
>
> + /* Reject empty input, which does not contain a complete zstd frame. */
> + streamer->decompression_ret = 1;
>
> I am not a huge fan of this, we claim that we save the return value but then we
> assign a value which hasn't yet been returned as a sentinel. Given that the
> return is a size_t we also can't really invent a sentinel. Since we don't
> actually use the returned value for anything but "done or not-done", so I
> propose something like the attached which interprets the value and stores a
> named state. What are your thoughts on this?
I agree with the direction. The new proposal can distinguish an empty stream from a truncated stream.
>
>
>
A few comments with v4:
1 - ztsd
```
+ if (mystreamer->state != STREAM_FINISHED)
+ pg_fatal("could not decompress data: compressed stream is incomplete");
+ else if (unlikely(mystreamer->state == STREAM_NEW))
+ pg_fatal("could not decompress data: compressed stream is empty");
```
This is a small logic error. As STREAM_NEW!=STREAM_FINISHED already, the “else if” is unreachable. We should check if (unlikely(mystreamer->state == STREAM_NEW) first.
2 - ztsd
```
+ /* The stream is only done when ZSTD_decompressStream returns 0 */
+ if (ret)
+ mystreamer->state = STREAM_HAS_DATA;
+ else
+ mystreamer->state = STREAM_FINISHED;
```
When ret == 0, that only means the current frame is complete, so the comment “the stream is only done” sounds too strong, I would change to “The frame is only done when …”.
3 - ztsd and lz4
```
+typedef enum
+{
+ STREAM_NEW,
+ STREAM_HAS_DATA,
+ STREAM_FINISHED,
+} pg_stream_state;
```
Similar to comment 2, return == 0 means the current is complete and >0 means the current frame is incomplete, thus I would rename STREAM_HAS_DATA to FRAME_HAS_DATA, and STREAM_FINISHED to FRAME_FINISHED.
I addressed all the 3 comments in v5.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Re: Fix detection of truncated zstd-compressed backups
> On Aug 11, 2026, at 16:32, Osama Abdul Qader wrote:
>
> Hi Chao,
>
> Thanks for the feedback, That's make sense to me, I'll wait for Mr. Daniel's thought on whether the existing 010_client_untar.pl coverage is sufficient.
>
> Thanks,
> Osama Abdul Qader
>
> On Tue, Aug 11, 2026 at 1:28 PM Chao Li wrote:
>
>
> > On Aug 11, 2026, at 15:40, Osama Abdul Qader wrote:
> >
> > Hi Chao,
> >
> > I noticed your commit f80cb3ae0737 ("Fix detection of truncated zstd-compressed backups"), which addresses the truncated ZSTD backup issue I had reported.
> >
> > I added a regression test to src/bin/pg_verifybackup/t/008_untar.pl. The test creates a server-side ZSTD backup, verifies the intact backup, truncates base.tar.zst by one byte, and then verifies that pg_verifybackup rejects the truncated backup.
> >
> > The test passes with your fix:
> > make check -C src/bin/pg_verifybackup TESTS=t/008_untar
> >
> > Result: PASS.
> >
> > I noticed that your commit changes astreamer_zstd.c but does not add a regression test to 008_untar.pl. Would this test be useful to include with the fix?
> > Thanks,
> > Osama Abdul Qader
>
> Hi Osama,
>
> Thanks for your review and for adding the test. I think the test Daniel added to 010_client_untar.pl covers all three astreamer types.
>
> Your test covers server-side compression, but from the astreamer’s perspective, I don't think there is much difference between client side and server side compression. Therefore, I am not sure that an additional server side test is needed. Let’s see what Daniel thinks.
>
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
>
Hi, Osama,
FYI, when you want to attach a patch file, please include “nocfbot” in the file name. Otherwise, CommitFest will pick up your patch file and incorrectly ask for a rebase. See [1].
Resending v5 to recover the CF entry.
[1] https://wiki.postgresql.org/wiki/Cfbot#When_are_patches_tested?
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/