diff --git a/src/backend/access/common/toast_compression.c b/src/backend/access/common/toast_compression.c index d161634c25b..27ea82ece0d 100644 --- a/src/backend/access/common/toast_compression.c +++ b/src/backend/access/common/toast_compression.c @@ -52,21 +52,6 @@ int default_toast_compression = DEFAULT_TOAST_COMPRESSION; * frame's window plus a block, about 2.5MB at the default compression level; * zstd shrinks that buffer by itself once later uses no longer need it. */ -static ZSTD_DCtx *zstd_dctx = NULL; - -static ZSTD_DCtx * -zstd_get_dctx(void) -{ - if (zstd_dctx == NULL) - { - zstd_dctx = ZSTD_createDCtx(); - if (zstd_dctx == NULL) - elog(ERROR, "could not create zstd decompression context"); - } - else - ZSTD_DCtx_reset(zstd_dctx, ZSTD_reset_session_only); - return zstd_dctx; -} #endif #define NO_COMPRESSION_SUPPORT(method) \ @@ -380,11 +365,10 @@ zstd_decompress_datum(const varlena *value) * content size; also check that it matches the size in our header, as * pglz does. */ - rawsize = ZSTD_decompressDCtx(zstd_get_dctx(), - VARDATA(result), - VARDATA_COMPRESSED_GET_EXTSIZE(value), - (const char *) value + VARHDRSZ_COMPRESSED_LONG, - VARSIZE(value) - VARHDRSZ_COMPRESSED_LONG); + rawsize = ZSTD_decompress(VARDATA(result), + VARDATA_COMPRESSED_GET_EXTSIZE(value), + (const char *) value + VARHDRSZ_COMPRESSED_LONG, + VARSIZE(value) - VARHDRSZ_COMPRESSED_LONG); if (ZSTD_isError(rawsize) || rawsize != VARDATA_COMPRESSED_GET_EXTSIZE(value)) ereport(ERROR, @@ -437,7 +421,9 @@ zstd_decompress_datum_slice(const varlena *value, int32 slicelength) /* allocate memory for the uncompressed slice */ result = (varlena *) palloc(slicelength + VARHDRSZ); - dctx = zstd_get_dctx(); + dctx = ZSTD_createDCtx(); + if (dctx == NULL) + elog(ERROR, "could not create zstd decompression context"); inbuf.src = (const char *) value + VARHDRSZ_COMPRESSED_LONG; inbuf.size = VARSIZE(value) - VARHDRSZ_COMPRESSED_LONG; @@ -470,6 +456,9 @@ zstd_decompress_datum_slice(const varlena *value, int32 slicelength) } } + /* release the context before any possible error is thrown */ + ZSTD_freeDCtx(dctx); + if (failed) ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED),