Re: Fix detection of truncated zstd-compressed backups
Re: Fix detection of truncated zstd-compressed backups
> On Aug 14, 2026, at 06:59, Chao Li wrote: > > > >> On Aug 14, 2026, at 04:57, Daniel Gustafsson wrote: >> >>> On 13 Aug 2026, at 07:11, Chao Li wrote: >> >>> PFA v7: addressed Jipan’s comment. >> >> Sorry for being slow on this, things are quite busy but I hope to have a review >> soon. While poking at this I realized that our compression code in pg_dump >> likely has the same issue. I hacked up a quick PoC diff (attached) but it's >> untested (can one actually test the data-in-zstd-internal-buffers case at all?) >> and mainly a sketch. If you want to pick it up and rework into this patchset >> to tackle it treewide then that would be fantastic. >> >> -- >> Daniel Gustafsson >> >> > > I can work on this today. I just checked pg_dump/pg_restore. The problem exists only with zstd and lz4, gzip doesn't have the problem. Daniel’s PoC covers the custom-archive-format path, but not the directory-format path. For the directory-format path, we can reproduce the problem by simply truncating one byte from a compressed data file. For the custom-archive path, reproducing the problem is less straightforward because the compressed data is stored inside length-prefixed archive blocks. Simply truncating the file can make archive parsing fail before the decompressor sees the truncated frame. I created a repro script, see the attached shell script. Before the fix, the output contains: ``` custom zstd: exit status 0 directory zstd: exit status 0 custom lz4: exit status 0 directory lz4: exit status 0 pg_restore: error: could not uncompress data: (null) custom gzip: exit status 1 pg_restore: error: could not close data file "/tmp/pgdump-trunc.1rxbf0/gzip-dir-bad/3931.dat": Undefined error: 0 directory gzip: exit status 1 ``` This shows that gzip reports failure, while zstd and lz4 silently accept the truncated dump files. After the fix, zstd and lz4 report failures as well: ``` pg_restore: error: could not decompress data: compressed stream is incomplete custom zstd: exit status 1 pg_restore: error: could not decompress data: compressed stream is incomplete directory zstd: exit status 1 pg_restore: error: could not decompress data: compressed stream is incomplete custom lz4: exit status 1 pg_restore: error: could not read from input file: Input/output error directory lz4: exit status 1 pg_restore: error: could not uncompress data: (null) custom gzip: exit status 1 pg_restore: error: could not close data file "/tmp/pgdump-trunc.XjUiJ4/gzip-dir-bad/3931.dat": Undefined error: 0 directory gzip: exit status 1 ``` While testing, I also found a small issue in LZ4Stream_read_internal(). Its error branches call pg_log_error() and then return -1, but callers immediately call pg_fatal() when the return value <0. This results in duplicate error messages. So, I removed those pg_log_error() calls. See 0002 for the fix. I added tests only for zstd and lz4, since gzip is not changed. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: Fix detection of truncated zstd-compressed backups
> On 7 Sep 2026, at 05:13, Chao Li wrote:
> PFA v9.
Sorry for returning to this late, things have been busy. I think v9 is pretty
much ready to go in, but I have a few small comments/questions.
- pg_fatal("could not uncompress data: %s", zp->msg);
+ pg_fatal("could not uncompress data: %s",
+ zp->msg ? zp->msg : "unknown error");
Is this change a precaution or fixing a known issue which can happen?
/* Lazy init */
if (!LZ4Stream_init(state, false /* decompressing */ ))
- {
- pg_log_error("unable to initialize LZ4 library: %s",
- LZ4F_getErrorName(state->errcode));
return -1;
- }
I understand the rationale for this change, but I don't think it's improving
things. As it stands now we are losing context in the messaging when all
errors say "can't read from input file", and the fread error cannot be captured
with LZ4Stream_get_error since fread doesn't set errno. The double log isn't
pretty, but getting worse errormessages isn't better so we need to stick with
it for now. A refactoring for 20 could be to pass down an error buffer and
psprintf the message there for the caller to log. This close to v20 RC I don't
want to redesign any part though.
The above point, along with minor cleanup and polish here and there is in the
attached v10.
--
Daniel Gustafsson
Re: Fix detection of truncated zstd-compressed backups
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Oct 9, 2026, at 06:36, Daniel Gustafsson wrote:
>
>> On 7 Sep 2026, at 05:13, Chao Li wrote:
>
>> PFA v9.
>
> Sorry for returning to this late, things have been busy.
No worries at all.
> I think v9 is pretty
> much ready to go in, but I have a few small comments/questions.
>
> - pg_fatal("could not uncompress data: %s", zp->msg);
> + pg_fatal("could not uncompress data: %s",
> + zp->msg ? zp->msg : "unknown error");
>
> Is this change a precaution or fixing a known issue which can happen?
This change was to address one of your previous comments:
```
>> pg_restore: error: could not uncompress data: (null)
>
> While not strictly related we should take the opportunity to fix this while in
> here. Perhaps use "unkown error" in case zp->msg is null when erroring out?
Fixed by replacing (null) with “unknown error” in 0002.
```
> /* Lazy init */
> if (!LZ4Stream_init(state, false /* decompressing */ ))
> - {
> - pg_log_error("unable to initialize LZ4 library: %s",
> - LZ4F_getErrorName(state->errcode));
> return -1;
> - }
>
> I understand the rationale for this change, but I don't think it's improving
> things. As it stands now we are losing context in the messaging when all
> errors say "can't read from input file", and the fread error cannot be captured
> with LZ4Stream_get_error since fread doesn't set errno. The double log isn't
> pretty, but getting worse errormessages isn't better so we need to stick with
> it for now. A refactoring for 20 could be to pass down an error buffer and
> psprintf the message there for the caller to log. This close to v20 RC I don't
> want to redesign any part though.
Sounds fair.
>
> The above point, along with minor cleanup and polish here and there is in the
> attached v10.
>
> --
> Daniel Gustafsson
>
>
I compared v10 with v9:
* v10-0001 has only a formatting change and an updated commit message.
* v10-0002 restores the original error messages, as discussed in your second comment. It also has some formatting changes on tests.
So, v10 looks good to me.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Re: Fix detection of truncated zstd-compressed backups
Hello
An automatic post-commit claude review pointed out that this broke
restoring directory format dumps with an empty large object, when
compressed with zstd or lz4:
SELECT lo_create(0);
$ pg_dump -Fd -Z zstd -f dump postgres
$ pg_restore -f /dev/null dump
pg_restore: error: could not decompress data: compressed stream is
incomplete
$ pg_dump -Fd -Z lz4 -f dump postgres
$ pg_restore -f /dev/null dump
pg_restore: error: could not read from input file: Input/output error
pg_restore: error: could not read from input file: Input/output error
The compressed file handles only start a frame on the first write, so
the blob_NNN.dat.zst/.lz4 file of an empty large object is 0 bytes,
and the new check treats that as a truncated stream. Custom format and
gzip are fine.
The attached patch makes the reader accept a file with no input at all
as a complete stream, a stream that started a frame and then stopped
is still rejected.
Re: Fix detection of truncated zstd-compressed backups
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Oct 10, 2026, at 19:03, Zsolt Parragi wrote: > > Hello > > An automatic post-commit claude review pointed out that this broke > restoring directory format dumps with an empty large object, when > compressed with zstd or lz4: > > SELECT lo_create(0); > > $ pg_dump -Fd -Z zstd -f dump postgres > $ pg_restore -f /dev/null dump > pg_restore: error: could not decompress data: compressed stream is > incomplete > > $ pg_dump -Fd -Z lz4 -f dump postgres > $ pg_restore -f /dev/null dump > pg_restore: error: could not read from input file: Input/output error > pg_restore: error: could not read from input file: Input/output error > > The compressed file handles only start a frame on the first write, so > the blob_NNN.dat.zst/.lz4 file of an empty large object is 0 bytes, > and the new check treats that as a truncated stream. Custom format and > gzip are fine. > > The attached patch makes the reader accept a file with no input at all > as a complete stream, a stream that started a frame and then stopped > is still rejected. > Thanks for the post-commit verification. I agree with your analysis and the fix. Initializing frame_finished to true allows the reader to handle an empty input file. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/