2022-03-07 21:08:45 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* bbstreamer_zstd.c
|
|
|
|
*
|
2023-01-02 21:00:37 +01:00
|
|
|
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
|
2022-03-07 21:08:45 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/bin/pg_basebackup/bbstreamer_zstd.c
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-03-15 18:06:25 +01:00
|
|
|
#ifdef USE_ZSTD
|
2022-03-07 21:08:45 +01:00
|
|
|
#include <zstd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "bbstreamer.h"
|
|
|
|
#include "common/logging.h"
|
|
|
|
|
2022-03-15 18:06:25 +01:00
|
|
|
#ifdef USE_ZSTD
|
2022-03-07 21:08:45 +01:00
|
|
|
|
|
|
|
typedef struct bbstreamer_zstd_frame
|
|
|
|
{
|
|
|
|
bbstreamer base;
|
|
|
|
|
|
|
|
ZSTD_CCtx *cctx;
|
|
|
|
ZSTD_DCtx *dctx;
|
|
|
|
ZSTD_outBuffer zstd_outBuf;
|
|
|
|
} bbstreamer_zstd_frame;
|
|
|
|
|
|
|
|
static void bbstreamer_zstd_compressor_content(bbstreamer *streamer,
|
|
|
|
bbstreamer_member *member,
|
|
|
|
const char *data, int len,
|
|
|
|
bbstreamer_archive_context context);
|
|
|
|
static void bbstreamer_zstd_compressor_finalize(bbstreamer *streamer);
|
|
|
|
static void bbstreamer_zstd_compressor_free(bbstreamer *streamer);
|
|
|
|
|
|
|
|
const bbstreamer_ops bbstreamer_zstd_compressor_ops = {
|
|
|
|
.content = bbstreamer_zstd_compressor_content,
|
|
|
|
.finalize = bbstreamer_zstd_compressor_finalize,
|
|
|
|
.free = bbstreamer_zstd_compressor_free
|
|
|
|
};
|
|
|
|
|
|
|
|
static void bbstreamer_zstd_decompressor_content(bbstreamer *streamer,
|
|
|
|
bbstreamer_member *member,
|
|
|
|
const char *data, int len,
|
|
|
|
bbstreamer_archive_context context);
|
|
|
|
static void bbstreamer_zstd_decompressor_finalize(bbstreamer *streamer);
|
|
|
|
static void bbstreamer_zstd_decompressor_free(bbstreamer *streamer);
|
|
|
|
|
|
|
|
const bbstreamer_ops bbstreamer_zstd_decompressor_ops = {
|
|
|
|
.content = bbstreamer_zstd_decompressor_content,
|
|
|
|
.finalize = bbstreamer_zstd_decompressor_finalize,
|
|
|
|
.free = bbstreamer_zstd_decompressor_free
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new base backup streamer that performs zstd compression of tar
|
|
|
|
* blocks.
|
|
|
|
*/
|
|
|
|
bbstreamer *
|
Rename backup_compression.{c,h} to compression.{c,h}
Compression option handling (level, algorithm or even workers) can be
used across several parts of the system and not only base backups.
Structures, objects and routines are renamed in consequence, to remove
the concept of base backups from this part of the code making this
change straight-forward.
pg_receivewal, that has gained support for LZ4 since babbbb5, will make
use of this infrastructure for its set of compression options, bringing
more consistency with pg_basebackup. This cleanup needs to be done
before releasing a beta of 15. pg_dump is a potential future target, as
well, and adding more compression options to it may happen in 16~.
Author: Michael Paquier
Reviewed-by: Robert Haas, Georgios Kokolatos
Discussion: https://postgr.es/m/YlPQGNAAa04raObK@paquier.xyz
2022-04-12 06:38:54 +02:00
|
|
|
bbstreamer_zstd_compressor_new(bbstreamer *next, pg_compress_specification *compress)
|
2022-03-07 21:08:45 +01:00
|
|
|
{
|
2022-03-15 18:06:25 +01:00
|
|
|
#ifdef USE_ZSTD
|
2022-03-07 21:08:45 +01:00
|
|
|
bbstreamer_zstd_frame *streamer;
|
2022-03-28 18:19:05 +02:00
|
|
|
size_t ret;
|
2022-03-07 21:08:45 +01:00
|
|
|
|
|
|
|
Assert(next != NULL);
|
|
|
|
|
|
|
|
streamer = palloc0(sizeof(bbstreamer_zstd_frame));
|
|
|
|
|
|
|
|
*((const bbstreamer_ops **) &streamer->base.bbs_ops) =
|
|
|
|
&bbstreamer_zstd_compressor_ops;
|
|
|
|
|
|
|
|
streamer->base.bbs_next = next;
|
|
|
|
initStringInfo(&streamer->base.bbs_buffer);
|
|
|
|
enlargeStringInfo(&streamer->base.bbs_buffer, ZSTD_DStreamOutSize());
|
|
|
|
|
|
|
|
streamer->cctx = ZSTD_createCCtx();
|
|
|
|
if (!streamer->cctx)
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not create zstd compression context");
|
2022-03-07 21:08:45 +01:00
|
|
|
|
Simplify handling of compression level with compression specifications
PG_COMPRESSION_OPTION_LEVEL is removed from the compression
specification logic, and instead the compression level is always
assigned with each library's default if nothing is directly given. This
centralizes the checks on the compression methods supported by a given
build, and always assigns a default compression level when parsing a
compression specification. This results in complaining at an earlier
stage than previously if a build supports a compression method or not,
aka when parsing a specification in the backend or the frontend, and not
when processing it. zstd, lz4 and zlib are able to handle in their
respective routines setting up the compression level the case of a
default value, hence the backend or frontend code (pg_receivewal or
pg_basebackup) has now no need to know what the default compression
level should be if nothing is specified: the logic is now done so as the
specification parsing assigns it. It can also be enforced by passing
down a "level" set to the default value, that the backend will accept
(the replication protocol is for example able to handle a command like
BASE_BACKUP (COMPRESSION_DETAIL 'gzip:level=-1')).
This code simplification fixes an issue with pg_basebackup --gzip
introduced by ffd5365, where the tarball of the streamed WAL segments
would be created as of pg_wal.tar.gz with uncompressed contents, while
the intention is to compress the segments with gzip at a default level.
The origin of the confusion comes from the handling of the default
compression level of gzip (-1 or Z_DEFAULT_COMPRESSION) and the value of
0 was getting assigned, which is what walmethods.c would consider
as equivalent to no compression when streaming WAL segments with its tar
methods. Assigning always the compression level removes the confusion
of some code paths considering a value of 0 set in a specification as
either no compression or a default compression level.
Note that 010_pg_basebackup.pl has to be adjusted to skip a few tests
where the shape of the compression detail string for client and
server-side compression was checked using gzip. This is a result of the
code simplification, as gzip specifications cannot be used if a build
does not support it.
Reported-by: Tom Lane
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/1400032.1662217889@sss.pgh.pa.us
Backpatch-through: 15
2022-09-14 05:16:57 +02:00
|
|
|
/* Set compression level */
|
|
|
|
ret = ZSTD_CCtx_setParameter(streamer->cctx, ZSTD_c_compressionLevel,
|
|
|
|
compress->level);
|
|
|
|
if (ZSTD_isError(ret))
|
|
|
|
pg_fatal("could not set zstd compression level to %d: %s",
|
|
|
|
compress->level, ZSTD_getErrorName(ret));
|
2022-03-30 15:35:14 +02:00
|
|
|
|
|
|
|
/* Set # of workers, if specified */
|
Rename backup_compression.{c,h} to compression.{c,h}
Compression option handling (level, algorithm or even workers) can be
used across several parts of the system and not only base backups.
Structures, objects and routines are renamed in consequence, to remove
the concept of base backups from this part of the code making this
change straight-forward.
pg_receivewal, that has gained support for LZ4 since babbbb5, will make
use of this infrastructure for its set of compression options, bringing
more consistency with pg_basebackup. This cleanup needs to be done
before releasing a beta of 15. pg_dump is a potential future target, as
well, and adding more compression options to it may happen in 16~.
Author: Michael Paquier
Reviewed-by: Robert Haas, Georgios Kokolatos
Discussion: https://postgr.es/m/YlPQGNAAa04raObK@paquier.xyz
2022-04-12 06:38:54 +02:00
|
|
|
if ((compress->options & PG_COMPRESSION_OPTION_WORKERS) != 0)
|
2022-03-30 15:35:14 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* On older versions of libzstd, this option does not exist, and
|
|
|
|
* trying to set it will fail. Similarly for newer versions if they
|
|
|
|
* are compiled without threading support.
|
|
|
|
*/
|
|
|
|
ret = ZSTD_CCtx_setParameter(streamer->cctx, ZSTD_c_nbWorkers,
|
|
|
|
compress->workers);
|
|
|
|
if (ZSTD_isError(ret))
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not set compression worker count to %d: %s",
|
|
|
|
compress->workers, ZSTD_getErrorName(ret));
|
2022-03-28 18:19:05 +02:00
|
|
|
}
|
2022-03-07 21:08:45 +01:00
|
|
|
|
2023-04-06 17:18:38 +02:00
|
|
|
if ((compress->options & PG_COMPRESSION_OPTION_LONG_DISTANCE) != 0)
|
|
|
|
{
|
|
|
|
ret = ZSTD_CCtx_setParameter(streamer->cctx,
|
|
|
|
ZSTD_c_enableLongDistanceMatching,
|
|
|
|
compress->long_distance);
|
|
|
|
if (ZSTD_isError(ret))
|
|
|
|
{
|
|
|
|
pg_log_error("could not set compression flag for %s: %s",
|
|
|
|
"long", ZSTD_getErrorName(ret));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 21:08:45 +01:00
|
|
|
/* Initialize the ZSTD output buffer. */
|
|
|
|
streamer->zstd_outBuf.dst = streamer->base.bbs_buffer.data;
|
|
|
|
streamer->zstd_outBuf.size = streamer->base.bbs_buffer.maxlen;
|
|
|
|
streamer->zstd_outBuf.pos = 0;
|
|
|
|
|
|
|
|
return &streamer->base;
|
|
|
|
#else
|
2022-12-21 02:39:06 +01:00
|
|
|
pg_fatal("this build does not support compression with %s", "ZSTD");
|
2022-04-09 00:14:24 +02:00
|
|
|
return NULL; /* keep compiler quiet */
|
2022-03-07 21:08:45 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-03-15 18:06:25 +01:00
|
|
|
#ifdef USE_ZSTD
|
2022-03-07 21:08:45 +01:00
|
|
|
/*
|
|
|
|
* Compress the input data to output buffer.
|
|
|
|
*
|
|
|
|
* Find out the compression bound based on input data length for each
|
|
|
|
* invocation to make sure that output buffer has enough capacity to
|
|
|
|
* accommodate the compressed data. In case if the output buffer
|
|
|
|
* capacity falls short of compression bound then forward the content
|
|
|
|
* of output buffer to next streamer and empty the buffer.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bbstreamer_zstd_compressor_content(bbstreamer *streamer,
|
|
|
|
bbstreamer_member *member,
|
|
|
|
const char *data, int len,
|
|
|
|
bbstreamer_archive_context context)
|
|
|
|
{
|
|
|
|
bbstreamer_zstd_frame *mystreamer = (bbstreamer_zstd_frame *) streamer;
|
|
|
|
ZSTD_inBuffer inBuf = {data, len, 0};
|
|
|
|
|
|
|
|
while (inBuf.pos < inBuf.size)
|
|
|
|
{
|
|
|
|
size_t yet_to_flush;
|
|
|
|
size_t max_needed = ZSTD_compressBound(inBuf.size - inBuf.pos);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the output buffer is not left with enough space, send the
|
|
|
|
* compressed bytes to the next streamer, and empty the buffer.
|
|
|
|
*/
|
|
|
|
if (mystreamer->zstd_outBuf.size - mystreamer->zstd_outBuf.pos <
|
|
|
|
max_needed)
|
|
|
|
{
|
|
|
|
bbstreamer_content(mystreamer->base.bbs_next, member,
|
|
|
|
mystreamer->zstd_outBuf.dst,
|
|
|
|
mystreamer->zstd_outBuf.pos,
|
|
|
|
context);
|
|
|
|
|
|
|
|
/* Reset the ZSTD output buffer. */
|
|
|
|
mystreamer->zstd_outBuf.dst = mystreamer->base.bbs_buffer.data;
|
|
|
|
mystreamer->zstd_outBuf.size = mystreamer->base.bbs_buffer.maxlen;
|
|
|
|
mystreamer->zstd_outBuf.pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
yet_to_flush =
|
|
|
|
ZSTD_compressStream2(mystreamer->cctx, &mystreamer->zstd_outBuf,
|
|
|
|
&inBuf, ZSTD_e_continue);
|
|
|
|
|
|
|
|
if (ZSTD_isError(yet_to_flush))
|
|
|
|
pg_log_error("could not compress data: %s",
|
|
|
|
ZSTD_getErrorName(yet_to_flush));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* End-of-stream processing.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bbstreamer_zstd_compressor_finalize(bbstreamer *streamer)
|
|
|
|
{
|
|
|
|
bbstreamer_zstd_frame *mystreamer = (bbstreamer_zstd_frame *) streamer;
|
|
|
|
size_t yet_to_flush;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ZSTD_inBuffer in = {NULL, 0, 0};
|
|
|
|
size_t max_needed = ZSTD_compressBound(0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the output buffer is not left with enough space, send the
|
|
|
|
* compressed bytes to the next streamer, and empty the buffer.
|
|
|
|
*/
|
|
|
|
if (mystreamer->zstd_outBuf.size - mystreamer->zstd_outBuf.pos <
|
|
|
|
max_needed)
|
|
|
|
{
|
|
|
|
bbstreamer_content(mystreamer->base.bbs_next, NULL,
|
|
|
|
mystreamer->zstd_outBuf.dst,
|
|
|
|
mystreamer->zstd_outBuf.pos,
|
|
|
|
BBSTREAMER_UNKNOWN);
|
|
|
|
|
|
|
|
/* Reset the ZSTD output buffer. */
|
|
|
|
mystreamer->zstd_outBuf.dst = mystreamer->base.bbs_buffer.data;
|
|
|
|
mystreamer->zstd_outBuf.size = mystreamer->base.bbs_buffer.maxlen;
|
|
|
|
mystreamer->zstd_outBuf.pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
yet_to_flush = ZSTD_compressStream2(mystreamer->cctx,
|
|
|
|
&mystreamer->zstd_outBuf,
|
|
|
|
&in, ZSTD_e_end);
|
|
|
|
|
|
|
|
if (ZSTD_isError(yet_to_flush))
|
|
|
|
pg_log_error("could not compress data: %s",
|
|
|
|
ZSTD_getErrorName(yet_to_flush));
|
|
|
|
|
|
|
|
} while (yet_to_flush > 0);
|
|
|
|
|
|
|
|
/* Make sure to pass any remaining bytes to the next streamer. */
|
|
|
|
if (mystreamer->zstd_outBuf.pos > 0)
|
|
|
|
bbstreamer_content(mystreamer->base.bbs_next, NULL,
|
|
|
|
mystreamer->zstd_outBuf.dst,
|
|
|
|
mystreamer->zstd_outBuf.pos,
|
|
|
|
BBSTREAMER_UNKNOWN);
|
|
|
|
|
|
|
|
bbstreamer_finalize(mystreamer->base.bbs_next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free memory.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bbstreamer_zstd_compressor_free(bbstreamer *streamer)
|
|
|
|
{
|
|
|
|
bbstreamer_zstd_frame *mystreamer = (bbstreamer_zstd_frame *) streamer;
|
|
|
|
|
|
|
|
bbstreamer_free(streamer->bbs_next);
|
|
|
|
ZSTD_freeCCtx(mystreamer->cctx);
|
|
|
|
pfree(streamer->bbs_buffer.data);
|
|
|
|
pfree(streamer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new base backup streamer that performs decompression of zstd
|
|
|
|
* compressed blocks.
|
|
|
|
*/
|
|
|
|
bbstreamer *
|
|
|
|
bbstreamer_zstd_decompressor_new(bbstreamer *next)
|
|
|
|
{
|
2022-03-15 18:06:25 +01:00
|
|
|
#ifdef USE_ZSTD
|
2022-03-07 21:08:45 +01:00
|
|
|
bbstreamer_zstd_frame *streamer;
|
|
|
|
|
|
|
|
Assert(next != NULL);
|
|
|
|
|
|
|
|
streamer = palloc0(sizeof(bbstreamer_zstd_frame));
|
|
|
|
*((const bbstreamer_ops **) &streamer->base.bbs_ops) =
|
|
|
|
&bbstreamer_zstd_decompressor_ops;
|
|
|
|
|
|
|
|
streamer->base.bbs_next = next;
|
|
|
|
initStringInfo(&streamer->base.bbs_buffer);
|
|
|
|
enlargeStringInfo(&streamer->base.bbs_buffer, ZSTD_DStreamOutSize());
|
|
|
|
|
|
|
|
streamer->dctx = ZSTD_createDCtx();
|
|
|
|
if (!streamer->dctx)
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not create zstd decompression context");
|
2022-03-07 21:08:45 +01:00
|
|
|
|
|
|
|
/* Initialize the ZSTD output buffer. */
|
|
|
|
streamer->zstd_outBuf.dst = streamer->base.bbs_buffer.data;
|
|
|
|
streamer->zstd_outBuf.size = streamer->base.bbs_buffer.maxlen;
|
|
|
|
streamer->zstd_outBuf.pos = 0;
|
|
|
|
|
|
|
|
return &streamer->base;
|
|
|
|
#else
|
2022-12-21 02:39:06 +01:00
|
|
|
pg_fatal("this build does not support compression with %s", "ZSTD");
|
2022-04-09 00:14:24 +02:00
|
|
|
return NULL; /* keep compiler quiet */
|
2022-03-07 21:08:45 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-03-15 18:06:25 +01:00
|
|
|
#ifdef USE_ZSTD
|
2022-03-07 21:08:45 +01:00
|
|
|
/*
|
|
|
|
* Decompress the input data to output buffer until we run out of input
|
|
|
|
* data. Each time the output buffer is full, pass on the decompressed data
|
|
|
|
* to the next streamer.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bbstreamer_zstd_decompressor_content(bbstreamer *streamer,
|
|
|
|
bbstreamer_member *member,
|
|
|
|
const char *data, int len,
|
|
|
|
bbstreamer_archive_context context)
|
|
|
|
{
|
|
|
|
bbstreamer_zstd_frame *mystreamer = (bbstreamer_zstd_frame *) streamer;
|
|
|
|
ZSTD_inBuffer inBuf = {data, len, 0};
|
|
|
|
|
|
|
|
while (inBuf.pos < inBuf.size)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If output buffer is full then forward the content to next streamer
|
|
|
|
* and update the output buffer.
|
|
|
|
*/
|
|
|
|
if (mystreamer->zstd_outBuf.pos >= mystreamer->zstd_outBuf.size)
|
|
|
|
{
|
|
|
|
bbstreamer_content(mystreamer->base.bbs_next, member,
|
|
|
|
mystreamer->zstd_outBuf.dst,
|
|
|
|
mystreamer->zstd_outBuf.pos,
|
|
|
|
context);
|
|
|
|
|
|
|
|
/* Reset the ZSTD output buffer. */
|
|
|
|
mystreamer->zstd_outBuf.dst = mystreamer->base.bbs_buffer.data;
|
|
|
|
mystreamer->zstd_outBuf.size = mystreamer->base.bbs_buffer.maxlen;
|
|
|
|
mystreamer->zstd_outBuf.pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ZSTD_decompressStream(mystreamer->dctx,
|
|
|
|
&mystreamer->zstd_outBuf, &inBuf);
|
|
|
|
|
|
|
|
if (ZSTD_isError(ret))
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_log_error("could not decompress data: %s",
|
|
|
|
ZSTD_getErrorName(ret));
|
2022-03-07 21:08:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* End-of-stream processing.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bbstreamer_zstd_decompressor_finalize(bbstreamer *streamer)
|
|
|
|
{
|
|
|
|
bbstreamer_zstd_frame *mystreamer = (bbstreamer_zstd_frame *) streamer;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* End of the stream, if there is some pending data in output buffers then
|
|
|
|
* we must forward it to next streamer.
|
|
|
|
*/
|
|
|
|
if (mystreamer->zstd_outBuf.pos > 0)
|
|
|
|
bbstreamer_content(mystreamer->base.bbs_next, NULL,
|
|
|
|
mystreamer->base.bbs_buffer.data,
|
|
|
|
mystreamer->base.bbs_buffer.maxlen,
|
|
|
|
BBSTREAMER_UNKNOWN);
|
|
|
|
|
|
|
|
bbstreamer_finalize(mystreamer->base.bbs_next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free memory.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bbstreamer_zstd_decompressor_free(bbstreamer *streamer)
|
|
|
|
{
|
|
|
|
bbstreamer_zstd_frame *mystreamer = (bbstreamer_zstd_frame *) streamer;
|
|
|
|
|
|
|
|
bbstreamer_free(streamer->bbs_next);
|
|
|
|
ZSTD_freeDCtx(mystreamer->dctx);
|
|
|
|
pfree(streamer->bbs_buffer.data);
|
|
|
|
pfree(streamer);
|
|
|
|
}
|
|
|
|
#endif
|