> On 13 Aug 2026, at 07:11, Chao Li <[email protected]> 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

commit f5cec129347c3ef65df791e0693bd7029f895591
Author: Daniel Gustafsson <[email protected]>
Date:   Tue Aug 11 21:34:32 2026 +0200

    wip

diff --git a/src/bin/pg_dump/compress_lz4.c b/src/bin/pg_dump/compress_lz4.c
index 500d5e16a6d..29599773abb 100644
--- a/src/bin/pg_dump/compress_lz4.c
+++ b/src/bin/pg_dump/compress_lz4.c
@@ -193,6 +193,9 @@ ReadDataFromArchiveLZ4(ArchiveHandle *AH, CompressorState 
*cs)
                }
        }
 
+       if (status != 0)
+               pg_fatal("could not decompress: chunk is incomplete");
+
        pg_free(outbuf);
        pg_free(readbuf);
 
diff --git a/src/bin/pg_dump/compress_zstd.c b/src/bin/pg_dump/compress_zstd.c
index 68f1d815917..4ef4ce58746 100644
--- a/src/bin/pg_dump/compress_zstd.c
+++ b/src/bin/pg_dump/compress_zstd.c
@@ -203,6 +203,24 @@ ReadDataFromArchiveZstd(ArchiveHandle *AH, CompressorState 
*cs)
                        if (res == 0)
                                break;                  /* End of frame */
                }
+
+               /*
+                * If ZSTD_decompressStream returned non-zero when all input was
+                * consumed then there is data left buffered, drain any 
remaining data
+                * by calling decompression again with an empty input.
+                */
+               if (res > 0)
+               {
+                       ZSTD_inBuffer empty = {NULL, 0, 0};
+
+                       output->pos = 0;
+
+                       res = ZSTD_decompressStream(zstdcs->dstream, output, 
&empty);
+                       if (ZSTD_isError(res))
+                               pg_fatal("1 could not decompress data: %s", 
ZSTD_getErrorName(res));
+                       ((char *) output->dst)[output->pos] = '\0';
+                       ahwrite(output->dst, 1, output->pos, AH);
+               }
        }
 }
 

Reply via email to