From fc9b803e185be700707ae75cf16e9238a9332414 Mon Sep 17 00:00:00 2001 From: Nikhil Kumar Veldanda Date: Thu, 24 Sep 2026 00:28:11 -0700 Subject: [PATCH v3 1/5] Remove duplicate definition of the compressed varlena header toast_internals.h described the header of a compressed-in-line varlena a second time, as toast_compress_header, with its own TOAST_COMPRESS_* macros to read and write the tcinfo word. varatt.h already describes the same bytes as varattrib_4b.va_compressed and provides VARDATA_COMPRESSED_GET_EXTSIZE() and VARDATA_COMPRESSED_GET_COMPRESS_METHOD() to read them. Remove the duplicate. The decompression code reads the header through the varatt.h accessors, and the compression code writes it through a single inline function, toast_compress_set_size_and_method(), so that there is one definition of this layout to maintain when it changes. No behavior change. --- src/backend/access/common/detoast.c | 6 ++-- src/backend/access/common/toast_internals.c | 2 +- src/include/access/toast_internals.h | 39 +++++++++------------ src/tools/pgindent/typedefs.list | 1 - 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/backend/access/common/detoast.c b/src/backend/access/common/detoast.c index 06992de1ede..741421963ba 100644 --- a/src/backend/access/common/detoast.c +++ b/src/backend/access/common/detoast.c @@ -489,7 +489,7 @@ toast_decompress_datum(varlena *attr) * Fetch the compression method id stored in the compression header and * decompress the data using the appropriate decompression routine. */ - cmid = TOAST_COMPRESS_METHOD(attr); + cmid = VARDATA_COMPRESSED_GET_COMPRESS_METHOD(attr); switch (cmid) { case TOAST_PGLZ_COMPRESSION_ID: @@ -525,14 +525,14 @@ toast_decompress_datum_slice(varlena *attr, int32 slicelength) * have been seen to give wrong results if passed an output size that is * more than the data's true decompressed size. */ - if ((uint32) slicelength >= TOAST_COMPRESS_EXTSIZE(attr)) + if ((uint32) slicelength >= VARDATA_COMPRESSED_GET_EXTSIZE(attr)) return toast_decompress_datum(attr); /* * Fetch the compression method id stored in the compression header and * decompress the data slice using the appropriate decompression routine. */ - cmid = TOAST_COMPRESS_METHOD(attr); + cmid = VARDATA_COMPRESSED_GET_COMPRESS_METHOD(attr); switch (cmid) { case TOAST_PGLZ_COMPRESSION_ID: diff --git a/src/backend/access/common/toast_internals.c b/src/backend/access/common/toast_internals.c index b0bfc1bfc9c..c979e4a32a2 100644 --- a/src/backend/access/common/toast_internals.c +++ b/src/backend/access/common/toast_internals.c @@ -92,7 +92,7 @@ toast_compress_datum(Datum value, char cmethod) { /* successful compression */ Assert(cmid != TOAST_INVALID_COMPRESSION_ID); - TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD(tmp, valsize, cmid); + toast_compress_set_size_and_method(tmp, valsize, cmid); return PointerGetDatum(tmp); } else diff --git a/src/include/access/toast_internals.h b/src/include/access/toast_internals.h index e03cc1204be..033bf45f0d1 100644 --- a/src/include/access/toast_internals.h +++ b/src/include/access/toast_internals.h @@ -17,34 +17,29 @@ #include "storage/lockdefs.h" #include "utils/relcache.h" #include "utils/snapshot.h" +#include "varatt.h" /* - * The information at the start of the compressed toast data. + * Fill in the header of a compressed-in-line datum: the original data size + * (excluding header) and the compression method. + * + * The compression routine must already have laid out the datum with + * VARHDRSZ_COMPRESSED bytes of header, since the compressed data starts + * right after it. The varlena length word is not touched here. */ -typedef struct toast_compress_header +static inline void +toast_compress_set_size_and_method(varlena *ptr, uint32 rawsize, + ToastCompressionId cmid) { - int32 vl_len_; /* varlena header (do not touch directly!) */ - uint32 tcinfo; /* 2 bits for compression method and 30 bits - * external size; see va_extinfo */ -} toast_compress_header; + varattrib_4b *va = (varattrib_4b *) ptr; -/* - * Utilities for manipulation of header information for compressed - * toast entries. - */ -#define TOAST_COMPRESS_EXTSIZE(ptr) \ - (((toast_compress_header *) (ptr))->tcinfo & VARLENA_EXTSIZE_MASK) -#define TOAST_COMPRESS_METHOD(ptr) \ - (((toast_compress_header *) (ptr))->tcinfo >> VARLENA_EXTSIZE_BITS) + Assert(rawsize > 0 && rawsize <= VARLENA_EXTSIZE_MASK); + Assert(cmid == TOAST_PGLZ_COMPRESSION_ID || + cmid == TOAST_LZ4_COMPRESSION_ID); -#define TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD(ptr, len, cm_method) \ - do { \ - Assert((len) > 0 && (len) <= VARLENA_EXTSIZE_MASK); \ - Assert((cm_method) == TOAST_PGLZ_COMPRESSION_ID || \ - (cm_method) == TOAST_LZ4_COMPRESSION_ID); \ - ((toast_compress_header *) (ptr))->tcinfo = \ - (len) | ((uint32) (cm_method) << VARLENA_EXTSIZE_BITS); \ - } while (0) + va->va_compressed.va_tcinfo = + rawsize | ((uint32) cmid << VARLENA_EXTSIZE_BITS); +} extern Datum toast_compress_datum(Datum value, char cmethod); extern Oid toast_get_valid_index(Oid toastoid, LOCKMODE lock); diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index 5d432074c2c..656f1f60862 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -4357,7 +4357,6 @@ timeout_handler_proc timeout_params timerCA tlist_vinfo -toast_compress_header toast_external_data tokenize_error_callback_arg transferMode base-commit: 2c10c2ce4d7bcd57543a49ab402af02a39724e0a -- 2.54.0 (Apple Git-157)