diff options
author | Michael Paquier <michael@paquier.xyz> | 2021-06-29 11:17:55 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2021-06-29 11:17:55 +0900 |
commit | 4035cd5d4eee4dae797bfc77ab07f8dcd8781b41 (patch) | |
tree | 6f002f0b1f66bd5e6897ad399508060d77b1b874 /src/backend/access/transam/xloginsert.c | |
parent | cc2c7d65fc27e877c9f407587b0b92d46cd6dd16 (diff) | |
download | postgresql-4035cd5d4eee4dae797bfc77ab07f8dcd8781b41.tar.gz postgresql-4035cd5d4eee4dae797bfc77ab07f8dcd8781b41.zip |
Add support for LZ4 with compression of full-page writes in WAL
The logic is implemented so as there can be a choice in the compression
used when building a WAL record, and an extra per-record bit is used to
track down if a block is compressed with PGLZ, LZ4 or nothing.
wal_compression, the existing parameter, is changed to an enum with
support for the following backward-compatible values:
- "off", the default, to not use compression.
- "pglz" or "on", to compress FPWs with PGLZ.
- "lz4", the new mode, to compress FPWs with LZ4.
Benchmarking has showed that LZ4 outclasses easily PGLZ. ZSTD would be
also an interesting choice, but going just with LZ4 for now makes the
patch minimalistic as toast compression is already able to use LZ4, so
there is no need to worry about any build-related needs for this
implementation.
Author: Andrey Borodin, Justin Pryzby
Reviewed-by: Dilip Kumar, Michael Paquier
Discussion: https://postgr.es/m/3037310D-ECB7-4BF1-AF20-01C10BB33A33@yandex-team.ru
Diffstat (limited to 'src/backend/access/transam/xloginsert.c')
-rw-r--r-- | src/backend/access/transam/xloginsert.c | 73 |
1 files changed, 64 insertions, 9 deletions
diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c index 32b4cc84e79..10b3b090535 100644 --- a/src/backend/access/transam/xloginsert.c +++ b/src/backend/access/transam/xloginsert.c @@ -33,8 +33,20 @@ #include "storage/proc.h" #include "utils/memutils.h" -/* Buffer size required to store a compressed version of backup block image */ -#define PGLZ_MAX_BLCKSZ PGLZ_MAX_OUTPUT(BLCKSZ) +/* + * Guess the maximum buffer size required to store a compressed version of + * backup block image. + */ +#ifdef USE_LZ4 +#include <lz4.h> +#define LZ4_MAX_BLCKSZ LZ4_COMPRESSBOUND(BLCKSZ) +#else +#define LZ4_MAX_BLCKSZ 0 +#endif + +#define PGLZ_MAX_BLCKSZ PGLZ_MAX_OUTPUT(BLCKSZ) + +#define COMPRESS_BUFSIZE Max(PGLZ_MAX_BLCKSZ, LZ4_MAX_BLCKSZ) /* * For each block reference registered with XLogRegisterBuffer, we fill in @@ -58,7 +70,7 @@ typedef struct * backup block data in XLogRecordAssemble() */ /* buffer to store a compressed version of backup block image */ - char compressed_page[PGLZ_MAX_BLCKSZ]; + char compressed_page[COMPRESS_BUFSIZE]; } registered_buffer; static registered_buffer *registered_buffers; @@ -628,7 +640,7 @@ XLogRecordAssemble(RmgrId rmid, uint8 info, /* * Try to compress a block image if wal_compression is enabled */ - if (wal_compression) + if (wal_compression != WAL_COMPRESSION_NONE) { is_compressed = XLogCompressBackupBlock(page, bimg.hole_offset, @@ -665,8 +677,29 @@ XLogRecordAssemble(RmgrId rmid, uint8 info, if (is_compressed) { + /* The current compression is stored in the WAL record */ bimg.length = compressed_len; - bimg.bimg_info |= BKPIMAGE_IS_COMPRESSED; + + /* Set the compression method used for this block */ + switch ((WalCompression) wal_compression) + { + case WAL_COMPRESSION_PGLZ: + bimg.bimg_info |= BKPIMAGE_COMPRESS_PGLZ; + break; + + case WAL_COMPRESSION_LZ4: +#ifdef USE_LZ4 + bimg.bimg_info |= BKPIMAGE_COMPRESS_LZ4; +#else + elog(ERROR, "LZ4 is not supported by this build"); +#endif + break; + + case WAL_COMPRESSION_NONE: + Assert(false); /* cannot happen */ + break; + /* no default case, so that compiler will warn */ + } rdt_datas_last->data = regbuf->compressed_page; rdt_datas_last->len = compressed_len; @@ -853,12 +886,34 @@ XLogCompressBackupBlock(char *page, uint16 hole_offset, uint16 hole_length, else source = page; + switch ((WalCompression) wal_compression) + { + case WAL_COMPRESSION_PGLZ: + len = pglz_compress(source, orig_len, dest, PGLZ_strategy_default); + break; + + case WAL_COMPRESSION_LZ4: +#ifdef USE_LZ4 + len = LZ4_compress_default(source, dest, orig_len, + COMPRESS_BUFSIZE); + if (len <= 0) + len = -1; /* failure */ +#else + elog(ERROR, "LZ4 is not supported by this build"); +#endif + break; + + case WAL_COMPRESSION_NONE: + Assert(false); /* cannot happen */ + break; + /* no default case, so that compiler will warn */ + } + /* - * We recheck the actual size even if pglz_compress() reports success and - * see if the number of bytes saved by compression is larger than the - * length of extra data needed for the compressed version of block image. + * We recheck the actual size even if compression reports success and see + * if the number of bytes saved by compression is larger than the length + * of extra data needed for the compressed version of block image. */ - len = pglz_compress(source, orig_len, dest, PGLZ_strategy_default); if (len >= 0 && len + extra_bytes < orig_len) { |