Re: Add LZ4 compression in pg_dump
Justin Pryzby <pryzby@telsasoft.com>
On Thu, Mar 16, 2023 at 11:30:50PM +0100, Tomas Vondra wrote:
> On 3/16/23 01:20, Justin Pryzby wrote:
> > But try reading the diff while looking for the cause of a bug. It's the
> > difference between reading 50, two-line changes, and reading a hunk that
> > replaces 100 lines with a different 100 lines, with empty/unrelated
> > lines randomly thrown in as context.
>
> I don't know, maybe I'm doing something wrong or maybe I just am bad at
> looking at diffs, but if I apply the patch you submitted on 8/3 and do
> the git-diff above (output attached), it seems pretty incomprehensible
> to me :-( I don't see 50 two-line changes (I certainly wouldn't be able
> to identify the root cause of the bug based on that).
It's true that most of the diff is still incomprehensible...
But look at the part relevant to the "empty-data" bug:
[... incomprehensible changes elided ...]
> static void
> -InitCompressorZlib(CompressorState *cs, int level)
> +DeflateCompressorInit(CompressorState *cs)
> {
> + GzipCompressorState *gzipcs;
> z_streamp zp;
>
> - zp = cs->zp = (z_streamp) pg_malloc(sizeof(z_stream));
> + gzipcs = (GzipCompressorState *) pg_malloc0(sizeof(GzipCompressorState));
> + zp = gzipcs->zp = (z_streamp) pg_malloc(sizeof(z_stream));
> zp->zalloc = Z_NULL;
> zp->zfree = Z_NULL;
> zp->opaque = Z_NULL;
>
> /*
> - * zlibOutSize is the buffer size we tell zlib it can output to. We
> - * actually allocate one extra byte because some routines want to append a
> - * trailing zero byte to the zlib output.
> + * outsize is the buffer size we tell zlib it can output to. We actually
> + * allocate one extra byte because some routines want to append a trailing
> + * zero byte to the zlib output.
> */
> - cs->zlibOut = (char *) pg_malloc(ZLIB_OUT_SIZE + 1);
> - cs->zlibOutSize = ZLIB_OUT_SIZE;
> + gzipcs->outbuf = pg_malloc(ZLIB_OUT_SIZE + 1);
> + gzipcs->outsize = ZLIB_OUT_SIZE;
>
> - if (deflateInit(zp, level) != Z_OK)
> - pg_fatal("could not initialize compression library: %s",
> - zp->msg);
> + /* -Z 0 uses the "None" compressor -- not zlib with no compression */
> + Assert(cs->compression_spec.level != 0);
> +
> + if (deflateInit(zp, cs->compression_spec.level) != Z_OK)
> + pg_fatal("could not initialize compression library: %s", zp->msg);
>
> /* Just be paranoid - maybe End is called after Start, with no Write */
> - zp->next_out = (void *) cs->zlibOut;
> - zp->avail_out = cs->zlibOutSize;
> + zp->next_out = gzipcs->outbuf;
> + zp->avail_out = gzipcs->outsize;
> +
> + /* Keep track of gzipcs */
> + cs->private_data = gzipcs;
> }
>
> static void
> -EndCompressorZlib(ArchiveHandle *AH, CompressorState *cs)
> +DeflateCompressorEnd(ArchiveHandle *AH, CompressorState *cs)
> {
> - z_streamp zp = cs->zp;
> + GzipCompressorState *gzipcs = (GzipCompressorState *) cs->private_data;
> + z_streamp zp;
>
> + zp = gzipcs->zp;
> zp->next_in = NULL;
> zp->avail_in = 0;
>
> /* Flush any remaining data from zlib buffer */
> - DeflateCompressorZlib(AH, cs, true);
> + DeflateCompressorCommon(AH, cs, true);
>
> if (deflateEnd(zp) != Z_OK)
> pg_fatal("could not close compression stream: %s", zp->msg);
>
> - free(cs->zlibOut);
> - free(cs->zp);
> + pg_free(gzipcs->outbuf);
> + pg_free(gzipcs->zp);
> + pg_free(gzipcs);
> + cs->private_data = NULL;
> }
>
> static void
> -DeflateCompressorZlib(ArchiveHandle *AH, CompressorState *cs, bool flush)
> +DeflateCompressorCommon(ArchiveHandle *AH, CompressorState *cs, bool flush)
> {
> - z_streamp zp = cs->zp;
> - char *out = cs->zlibOut;
> + GzipCompressorState *gzipcs = (GzipCompressorState *) cs->private_data;
> + z_streamp zp = gzipcs->zp;
> + void *out = gzipcs->outbuf;
> int res = Z_OK;
>
> - while (cs->zp->avail_in != 0 || flush)
> + while (gzipcs->zp->avail_in != 0 || flush)
> {
> res = deflate(zp, flush ? Z_FINISH : Z_NO_FLUSH);
> if (res == Z_STREAM_ERROR)
> pg_fatal("could not compress data: %s", zp->msg);
> - if ((flush && (zp->avail_out < cs->zlibOutSize))
> + if ((flush && (zp->avail_out < gzipcs->outsize))
> || (zp->avail_out == 0)
> || (zp->avail_in != 0)
> )
> @@ -289,18 +122,18 @@ DeflateCompressorZlib(ArchiveHandle *AH, CompressorState *cs, bool flush)
> * chunk is the EOF marker in the custom format. This should never
> * happen but ...
> */
> - if (zp->avail_out < cs->zlibOutSize)
> + if (zp->avail_out < gzipcs->outsize)
> {
> /*
> * Any write function should do its own error checking but to
> * make sure we do a check here as well ...
> */
> - size_t len = cs->zlibOutSize - zp->avail_out;
> + size_t len = gzipcs->outsize - zp->avail_out;
>
> - cs->writeF(AH, out, len);
> + cs->writeF(AH, (char *) out, len);
> }
> - zp->next_out = (void *) out;
> - zp->avail_out = cs->zlibOutSize;
> + zp->next_out = out;
> + zp->avail_out = gzipcs->outsize;
> }
>
> if (res == Z_STREAM_END)
> @@ -309,16 +142,26 @@ DeflateCompressorZlib(ArchiveHandle *AH, CompressorState *cs, bool flush)
> }
>
> static void
> -WriteDataToArchiveZlib(ArchiveHandle *AH, CompressorState *cs,
> - const char *data, size_t dLen)
> +EndCompressorGzip(ArchiveHandle *AH, CompressorState *cs)
> {
> - cs->zp->next_in = (void *) unconstify(char *, data);
> - cs->zp->avail_in = dLen;
> - DeflateCompressorZlib(AH, cs, false);
> + /* If deflation was initialized, finalize it */
> + if (cs->private_data)
> + DeflateCompressorEnd(AH, cs);
> }
>
> static void
> -ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF)
> +WriteDataToArchiveGzip(ArchiveHandle *AH, CompressorState *cs,
> + const void *data, size_t dLen)
> +{
> + GzipCompressorState *gzipcs = (GzipCompressorState *) cs->private_data;
> +
> + gzipcs->zp->next_in = (void *) unconstify(void *, data);
> + gzipcs->zp->avail_in = dLen;
> + DeflateCompressorCommon(AH, cs, false);
> +}
> +
> +static void
> +ReadDataFromArchiveGzip(ArchiveHandle *AH, CompressorState *cs)
> {
> z_streamp zp;
> char *out;
> @@ -342,7 +185,7 @@ ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF)
> zp->msg);
>
> /* no minimal chunk size for zlib */
> - while ((cnt = readF(AH, &buf, &buflen)))
> + while ((cnt = cs->readF(AH, &buf, &buflen)))
> {
> zp->next_in = (void *) buf;
> zp->avail_in = cnt;
> @@ -382,389 +225,196 @@ ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF)
> free(out);
> free(zp);
> }
[... more incomprehensible changes elided ...]
Commits
-
Advance input pointer when LZ4 compressing data
- 1a05c1d25299 16.0 landed
-
Null-terminate the output buffer of LZ4Stream_gets
- 3c18d90f8907 16.0 landed
-
Rework code defining default compression for dir/custom formats in pg_dump
- bedc1f0564d1 16.0 landed
-
pg_dump: Use only LZ4 frame format for compression
- 0070b66fef21 16.0 landed
-
Minor comment improvements for compress_lz4
- d0160ca11e31 16.0 landed
-
Unify buffer sizes in pg_dump compression API
- f081a48f9a91 16.0 landed
-
Improve type handling in pg_dump's compress file API
- d3b57755e60c 16.0 landed
-
Improve wording in pg_dump compression docs
- 6095069b40d7 16.0 landed
-
Fix condition in pg_dump TAP test
- 34ce11437497 16.0 landed
-
Add LZ4 compression to pg_dump
- 0da243fed087 16.0 landed
-
Introduce a generic pg_dump compression API
- e9960732a961 16.0 landed
-
Prepare pg_dump internals for additional compression methods
- 03d02f54a640 16.0 landed
-
Fix behavior with pg_restore -l and compressed dumps
- 783d8abc3b63 16.0 landed
-
Add 250c8ee07ed to git-blame-ignore-revs
- ff23b592ad66 16.0 cited
-
Provide test coverage in pg_dump for default behaviors with compression
- a7885c9bb22d 16.0 landed
-
Switch pg_dump to use compression specifications
- 5e73a6048849 16.0 landed
-
Refactor code parsing compression option values (-Z/--compress)
- d18655cc037a 16.0 landed
-
meson: Add some missing env settings for tests of pg_dump and pg_verifybackup
- 00ae5d6f588e 16.0 landed
-
Extend TAP tests of pg_dump to test for compression with gzip
- 98fe74218d97 15.0 landed
-
Clean up some dead code in pg_dump with tar format and gzip compression
- 8ac4c25a05d1 15.0 landed
-
Add TAP test in pg_dump with --format=tar and --compress
- edcedcc2c7bb 15.0 landed
-
Replace BASE_BACKUP COMPRESSION_LEVEL option with COMPRESSION_DETAIL.
- ffd53659c46a 15.0 cited
-
Refactor the pg_dump zlib code from pg_backup_custom.c to a separate file,
- bf9aa490db24 9.1.0 cited