diff options
author | Michael Paquier <michael@paquier.xyz> | 2022-09-14 12:16:57 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2022-09-14 12:16:57 +0900 |
commit | f352e2d08ac048d7407dd6098fc6b344ff85c2dd (patch) | |
tree | 7053b7b6b2be9a218e5eca11048da7d9ec8bc1d6 /src/common/compression.c | |
parent | 0a20ff54f5e66158930d5328f89f087d4e9ab400 (diff) | |
download | postgresql-f352e2d08ac048d7407dd6098fc6b344ff85c2dd.tar.gz postgresql-f352e2d08ac048d7407dd6098fc6b344ff85c2dd.zip |
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
Diffstat (limited to 'src/common/compression.c')
-rw-r--r-- | src/common/compression.c | 105 |
1 files changed, 84 insertions, 21 deletions
diff --git a/src/common/compression.c b/src/common/compression.c index da3c291c0ff..e40ce98ef3a 100644 --- a/src/common/compression.c +++ b/src/common/compression.c @@ -27,6 +27,13 @@ #include "postgres_fe.h" #endif +#ifdef USE_ZSTD +#include <zstd.h> +#endif +#ifdef HAVE_LIBZ +#include <zlib.h> +#endif + #include "common/compression.h" static int expect_integer_value(char *keyword, char *value, @@ -88,6 +95,9 @@ get_compress_algorithm_name(pg_compress_algorithm algorithm) * Note, however, even if there's no parse error, the string might not make * sense: e.g. for gzip, level=12 is not sensible, but it does parse OK. * + * The compression level is assigned by default if not directly specified + * by the specification. + * * Use validate_compress_specification() to find out whether a compression * specification is semantically sensible. */ @@ -101,9 +111,46 @@ parse_compress_specification(pg_compress_algorithm algorithm, char *specificatio /* Initial setup of result object. */ result->algorithm = algorithm; result->options = 0; - result->level = -1; result->parse_error = NULL; + /* + * Assign a default level depending on the compression method. This may + * be enforced later. + */ + switch (result->algorithm) + { + case PG_COMPRESSION_NONE: + result->level = 0; + break; + case PG_COMPRESSION_LZ4: +#ifdef USE_LZ4 + result->level = 0; /* fast compression mode */ +#else + result->parse_error = + psprintf(_("this build does not support compression with %s"), + "LZ4"); +#endif + break; + case PG_COMPRESSION_ZSTD: +#ifdef USE_ZSTD + result->level = ZSTD_CLEVEL_DEFAULT; +#else + result->parse_error = + psprintf(_("this build does not support compression with %s"), + "ZSTD"); +#endif + break; + case PG_COMPRESSION_GZIP: +#ifdef HAVE_LIBZ + result->level = Z_DEFAULT_COMPRESSION; +#else + result->parse_error = + psprintf(_("this build does not support compression with %s"), + "gzip"); +#endif + break; + } + /* If there is no specification, we're done already. */ if (specification == NULL) return; @@ -113,7 +160,6 @@ parse_compress_specification(pg_compress_algorithm algorithm, char *specificatio if (specification != bare_level_endp && *bare_level_endp == '\0') { result->level = bare_level; - result->options |= PG_COMPRESSION_OPTION_LEVEL; return; } @@ -175,7 +221,11 @@ parse_compress_specification(pg_compress_algorithm algorithm, char *specificatio if (strcmp(keyword, "level") == 0) { result->level = expect_integer_value(keyword, value, result); - result->options |= PG_COMPRESSION_OPTION_LEVEL; + + /* + * No need to set a flag in "options", there is a default level + * set at least thanks to the logic above. + */ } else if (strcmp(keyword, "workers") == 0) { @@ -249,36 +299,49 @@ expect_integer_value(char *keyword, char *value, pg_compress_specification *resu char * validate_compress_specification(pg_compress_specification *spec) { + int min_level = 1; + int max_level = 1; + int default_level = 0; + /* If it didn't even parse OK, it's definitely no good. */ if (spec->parse_error != NULL) return spec->parse_error; /* - * If a compression level was specified, check that the algorithm expects - * a compression level and that the level is within the legal range for - * the algorithm. + * Check that the algorithm expects a compression level and it is within + * the legal range for the algorithm. */ - if ((spec->options & PG_COMPRESSION_OPTION_LEVEL) != 0) + switch (spec->algorithm) { - int min_level = 1; - int max_level; - - if (spec->algorithm == PG_COMPRESSION_GZIP) + case PG_COMPRESSION_GZIP: max_level = 9; - else if (spec->algorithm == PG_COMPRESSION_LZ4) +#ifdef HAVE_LIBZ + default_level = Z_DEFAULT_COMPRESSION; +#endif + break; + case PG_COMPRESSION_LZ4: max_level = 12; - else if (spec->algorithm == PG_COMPRESSION_ZSTD) + default_level = 0; /* fast mode */ + break; + case PG_COMPRESSION_ZSTD: max_level = 22; - else - return psprintf(_("compression algorithm \"%s\" does not accept a compression level"), - get_compress_algorithm_name(spec->algorithm)); - - if (spec->level < min_level || spec->level > max_level) - return psprintf(_("compression algorithm \"%s\" expects a compression level between %d and %d"), - get_compress_algorithm_name(spec->algorithm), - min_level, max_level); +#ifdef USE_ZSTD + default_level = ZSTD_CLEVEL_DEFAULT; +#endif + break; + case PG_COMPRESSION_NONE: + if (spec->level != 0) + return psprintf(_("compression algorithm \"%s\" does not accept a compression level"), + get_compress_algorithm_name(spec->algorithm)); + break; } + if ((spec->level < min_level || spec->level > max_level) && + spec->level != default_level) + return psprintf(_("compression algorithm \"%s\" expects a compression level between %d and %d (default at %d)"), + get_compress_algorithm_name(spec->algorithm), + min_level, max_level, default_level); + /* * Of the compression algorithms that we currently support, only zstd * allows parallel workers. |