From 0d3f8e7762df262b78d4e16985e33359c8b42d22 Mon Sep 17 00:00:00 2001 From: Andrey Borodin Date: Tue, 6 Oct 2026 11:27:54 +0500 Subject: [PATCH v11 1/3] Reuse the zstd context when compressing WAL images Compressing each full-page image currently creates and destroys a zstd compression context. Keep a backend-local context, allocated on first use, to avoid repeating those allocations. Fall back to an uncompressed image if the context cannot be allocated, as for compression failures. Compression is optional, and raising an error here could turn an allocation failure into a PANIC in a critical section. Author: Andrey Borodin Reviewed-by: Michael Paquier Reviewed-by: Anthonin Bonnefoy Discussion: https://www.postgresql.org/message-id/asRsOl73nf8GeBFd@paquier.xyz --- src/backend/access/transam/xloginsert.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c index c9aff944a2e..3c87a9183de 100644 --- a/src/backend/access/transam/xloginsert.c +++ b/src/backend/access/transam/xloginsert.c @@ -116,6 +116,11 @@ static uint8 curinsert_flags = 0; static XLogRecData hdr_rdt; static char *hdr_scratch = NULL; +#ifdef USE_ZSTD +/* Reuse the compression context for the lifetime of the backend. */ +static ZSTD_CCtx *zstd_cctx = NULL; +#endif + #define SizeOfXlogOrigin (sizeof(ReplOriginId) + sizeof(char)) #define SizeOfXLogTransactionId (sizeof(TransactionId) + sizeof(char)) @@ -1063,8 +1068,18 @@ XLogCompressBackupBlock(const PageData *page, uint16 hole_offset, uint16 hole_le case WAL_COMPRESSION_ZSTD: #ifdef USE_ZSTD - len = ZSTD_compress(dest, COMPRESS_BUFSIZE, source, orig_len, - ZSTD_CLEVEL_DEFAULT); + if (zstd_cctx == NULL) + zstd_cctx = ZSTD_createCCtx(); + + /* Compression is optional, and we may be in a critical section. */ + if (zstd_cctx == NULL) + { + len = -1; + break; + } + + len = ZSTD_compressCCtx(zstd_cctx, dest, COMPRESS_BUFSIZE, + source, orig_len, ZSTD_CLEVEL_DEFAULT); if (ZSTD_isError(len)) len = -1; /* failure */ #else base-commit: cb1eef81c9bc8d1a2adc1e3a63669e315b6232bd -- That's all, folks. May the source be with you.