Skip to content

Commit 6176242

Browse files
committed
Fix a few goofs in new backup compression code.
When we try to set the zstd compression level either on the client or on the server, check for errors. For any algorithm, on the client side, don't try to set the compression level unless the user specified one. This was visibly broken for zstd, which managed to set -1 rather than 0 in this case, but tidy up the code for the other methods, too. On the client side, if we fail to create a ZSTD_CCtx, exit after reporting the error. Otherwise we'll dereference a null pointer. Patch by me, reviewed by Dipesh Pandit. Discussion: http://postgr.es/m/CA+TgmoZK3zLQUCGi1h4XZw4jHiAWtcACc+GsdJR1_Mc19jUjXA@mail.gmail.com
1 parent d226469 commit 6176242

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

src/backend/replication/basebackup_zstd.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,17 @@ bbsink_zstd_begin_backup(bbsink *sink)
9898
{
9999
bbsink_zstd *mysink = (bbsink_zstd *) sink;
100100
size_t output_buffer_bound;
101+
size_t ret;
101102

102103
mysink->cctx = ZSTD_createCCtx();
103104
if (!mysink->cctx)
104105
elog(ERROR, "could not create zstd compression context");
105106

106-
ZSTD_CCtx_setParameter(mysink->cctx, ZSTD_c_compressionLevel,
107-
mysink->compresslevel);
107+
ret = ZSTD_CCtx_setParameter(mysink->cctx, ZSTD_c_compressionLevel,
108+
mysink->compresslevel);
109+
if (ZSTD_isError(ret))
110+
elog(ERROR, "could not set zstd compression level to %d: %s",
111+
mysink->compresslevel, ZSTD_getErrorName(ret));
108112

109113
/*
110114
* We need our own buffer, because we're going to pass different data to

src/bin/pg_basebackup/bbstreamer_gzip.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ bbstreamer_gzip_writer_new(char *pathname, FILE *file,
116116
}
117117
}
118118

119-
if (gzsetparams(streamer->gzfile, compress->level,
119+
if ((compress->options & BACKUP_COMPRESSION_OPTION_LEVEL) != 0 &&
120+
gzsetparams(streamer->gzfile, compress->level,
120121
Z_DEFAULT_STRATEGY) != Z_OK)
121122
{
122123
pg_log_error("could not set compression level %d: %s",

src/bin/pg_basebackup/bbstreamer_lz4.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ bbstreamer_lz4_compressor_new(bbstreamer *next, bc_specification *compress)
8989
prefs = &streamer->prefs;
9090
memset(prefs, 0, sizeof(LZ4F_preferences_t));
9191
prefs->frameInfo.blockSizeID = LZ4F_max256KB;
92-
prefs->compressionLevel = compress->level;
92+
if ((compress->options & BACKUP_COMPRESSION_OPTION_LEVEL) != 0)
93+
prefs->compressionLevel = compress->level;
9394

9495
/*
9596
* Find out the compression bound, it specifies the minimum destination

src/bin/pg_basebackup/bbstreamer_zstd.c

+17-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ bbstreamer_zstd_compressor_new(bbstreamer *next, bc_specification *compress)
6767
{
6868
#ifdef USE_ZSTD
6969
bbstreamer_zstd_frame *streamer;
70+
int compresslevel;
71+
size_t ret;
7072

7173
Assert(next != NULL);
7274

@@ -81,11 +83,24 @@ bbstreamer_zstd_compressor_new(bbstreamer *next, bc_specification *compress)
8183

8284
streamer->cctx = ZSTD_createCCtx();
8385
if (!streamer->cctx)
86+
{
8487
pg_log_error("could not create zstd compression context");
88+
exit(1);
89+
}
8590

8691
/* Initialize stream compression preferences */
87-
ZSTD_CCtx_setParameter(streamer->cctx, ZSTD_c_compressionLevel,
88-
compress->level);
92+
if ((compress->options & BACKUP_COMPRESSION_OPTION_LEVEL) == 0)
93+
compresslevel = 0;
94+
else
95+
compresslevel = compress->level;
96+
ret = ZSTD_CCtx_setParameter(streamer->cctx, ZSTD_c_compressionLevel,
97+
compresslevel);
98+
if (ZSTD_isError(ret))
99+
{
100+
pg_log_error("could not set zstd compression level to %d: %s",
101+
compresslevel, ZSTD_getErrorName(ret));
102+
exit(1);
103+
}
89104

90105
/* Initialize the ZSTD output buffer. */
91106
streamer->zstd_outBuf.dst = streamer->base.bbs_buffer.data;

0 commit comments

Comments
 (0)