pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Aidar Imamov <imamovaj22@gmail.com>
To: Michael Paquier <michael@paquier.xyz>
Cc: Chao Li <li.evan.chao@gmail.com>
Cc: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: Refactor code around GUC default_toast_compression
Date: Mon, 14 Sep 2026 16:48:26 +0300
Message-ID: <45654909-903F-42BD-A767-61B701992DE3@gmail.com> (raw)
In-Reply-To: <agGOFTwvOG3FugCn@paquier.xyz>
References: <afRbVhoYuw4RStIO@paquier.xyz>
	<CAJTYsWX2erCHdEV1NoPdGP5fOvPjHYf=MQ2C-N+h6yE45oKihA@mail.gmail.com>
	<afUsoKiQ6yriscvT@paquier.xyz>
	<7A329D19-A0F2-4558-A391-6E82699B9BC0@gmail.com>
	<agGOFTwvOG3FugCn@paquier.xyz>


> On May 11, 2026, at 11:06, Michael Paquier <michael@paquier.xyz> wrote:
> 
> On Sat, May 02, 2026 at 09:55:30AM +0800, Chao Li wrote:
>> Otherwise, a third-party extension that relies on this variable
>> could silently misbehave. I understand that a major release is
>> allowed to change API/ABI contracts, but a build failure would be
>> better than silent misbehavior. Or at least we should document this
>> change somewhere.
>> 
>> Would it better to also rename DEFAULT_TOAST_COMPRESSION to DEFAULT_TOAST_COMPRESSION_GUC.
> 
> After pondering about this point, I think that you are touching
> something sensible here, but not for the reason you mention: the _GUC
> bits serve no actual purpose and we can keep using attcompression in
> the GUC.
> 
>> 3
>> ```
>> +#define TOAST_COMPRESS_PGLZ		0
>> +#define TOAST_COMPRESS_LZ4		1
>> +#define TOAST_COMPRESS_INVALID	2
>> ```
>> 
>> Now TOAST_COMPRESS_PGLZ is 0, and TOAST_PGLZ_COMPRESSION is
>> ‘p’. When they appear together in the code, it’s hard to guess which
>> is 0 and which is ‘p’. So, would it better to rename
>> TOAST_COMPRESS_PGLZ to TOAST_PGLZ_COMPRESS_ID, and rename
>> TOAST_PGLZ_COMPRESSION to TOAST_PGLZ_COMPRESS_METHOD?
> 
> Here as well, I can get some of the confusion.  We can just reuse the
> same names, with _ID instead.
> 
>> As the switch/default explicitly rejects invalid cmethod, I feel
>> slightly better for readability to place "cmid =
>> MethodToCompressionId(cmethod);" after the switch clause.
> 
> WFM.
> 
> At the end I have the updated version attached, which still does the
> job I want it to do, just simpler.
> 
> One extra thing to keep in mind is that we may want to make
> CompressionIdIsValid() smarter in the future, especially across
> multiple vartag_external or varlena types if the same ID values are
> shared across multiple compression methods, but would be simpler after
> this patch with all this knowledge kept local to toast_compression.c.
> Something similar could be said about toast_compress_datum() at some
> point, once/if we get there.  Another argument would be to just switch
> ToastCompressionId to a uint32 and move the numbers to varatt.h, but
> I'd like to be more ambitious.  This patch is just my take on the
> matter.
> 
> What do you think?
> --
> Michael
> <v3-0001-Refactor-some-code-logic-around-GUC-default_toast.patch>

Hi Michael,

I rebased this onto current master (a23ab4862cf). It doesn't apply
cleanly anymore — amcheck picked up the Oid8 TOAST-value-ID rework
(toast_pointer_valueid / OID8_FORMAT) on top of the
VARATT_EXTERNAL_IS_COMPRESSED -> VARATT_EXTERNAL_OID_IS_COMPRESSED
rename. Both were easy to sort out, and the rebased patch is attached.

A couple of small things I noticed while reading through it.

1) varatt.h

When the IDs moved here, the comment that used to sit above the
ToastCompressionId enum got lost. It was the one warning that these raw
bits shouldn't be used to identify a method (that's what
TOAST_PGLZ_COMPRESSION etc. are for), and since both are just small
integers in C, nothing else stops a mixup. Maybe it'd be worth keeping
that note next to the defines (adjusted a bit, since the constants are
now above it):

     * Don't use these values for anything other than understanding the
     * meaning of the raw bits from a varlena; in particular, if the
     * goal is to identify a compression method, use the constants
     * TOAST_PGLZ_COMPRESSION, etc. instead.

2) CompressionNameToMethod()

I can see the hardcoded "lz4" next to the #ifndef makes the intent
obvious, so it might well be deliberate. But the loop already has the
matching registry entry in hand, and keeping the name in exactly one
place was kind of the point of the registry. So I wonder whether it'd
be cleaner to compare the method and reuse the entry's name — the error
message then comes from the registry too:

    #ifndef USE_LZ4
            if (toast_compression_registry[i].method == TOAST_LZ4_COMPRESSION)
                NO_COMPRESSION_SUPPORT(toast_compression_registry[i].name);
    #endif

3) %d vs %u

cmid is uint32 now, and elsewhere in the tree unsigned ids are
generally printed with %u — these four %d's (detoast.c twice,
varlena.c, verify_heapam.c) look like the odd ones out. Would it make
sense to switch them to %u? Purely cosmetic, though.

4) toast_internals.h

TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD() uses
TOAST_*_COMPRESSION_ID, which now live in varatt.h, but the header
doesn't include it — it only works through transitive includes today
(the only file using the macro happens to get varatt.h via
heaptoast.h -> htup_details.h). Maybe add a direct #include "varatt.h"
so the header is self-contained?

I also wondered about going further and moving the compress/decompress
dispatch into the registry (function pointers + a cmid->name lookup).
Probably out of scope for this patch, though — the registry centralizes
the properties, and the dispatch part overlaps with the vartag
direction you mention. Just flagging it in case it's worth a follow-up
at some point.

Regards,
Aidar Imamov

Attachments:

v3-0001-Refactor-some-code-logic-around-GUC-default_toast-rebased-master-20260914.patch (application/octet-stream, 12.0K) download From c42eed3cb1c4cf6052152a6649597ce319cf7000 Mon Sep 17 00:00:00 2001 From: Michael Paquier <michael@paquier.xyz> Date: Mon, 11 May 2026 16:54:04 +0900 Subject: [PATCH] Refactor some code logic around GUC default_toast_compression This commit refactors the code around TOAST compression methods so as we have a cleaner split between the various concepts related to TOAST compression, for: - The on-disk varatt values. - The catalog attribute char values (attcompression). - The compression method names. The knowledge of each compression method is now localized in a single "registry", in toast_compression.c, with a set of routines that are able to retrieve some of the properties. The goal of this patch is to ease the addition of future methods. One idea would be split the on-disk varatt properties across multiple vartags. --- contrib/amcheck/verify_heapam.c | 19 +--- src/backend/access/common/detoast.c | 4 +- src/backend/access/common/toast_compression.c | 97 +++++++++++++++---- src/backend/access/common/toast_internals.c | 7 +- src/backend/utils/adt/varlena.c | 2 +- src/include/access/toast_compression.h | 27 ++---- src/include/varatt.h | 9 ++ src/tools/pgindent/typedefs.list | 2 +- 8 files changed, 105 insertions(+), 62 deletions(-) diff --git a/contrib/amcheck/verify_heapam.c b/contrib/amcheck/verify_heapam.c index 33b00fa4dc5..4aaed846f0d 100644 --- a/contrib/amcheck/verify_heapam.c +++ b/contrib/amcheck/verify_heapam.c @@ -1792,26 +1792,11 @@ check_tuple_attribute(HeapCheckContext *ctx) if (VARATT_EXTERNAL_OID_IS_COMPRESSED(toast_pointer)) { - ToastCompressionId cmid; - bool valid = false; + uint32 cmid; /* Compressed attributes should have a valid compression method */ cmid = TOAST_COMPRESS_METHOD(&toast_pointer); - switch (cmid) - { - /* List of all valid compression method IDs */ - case TOAST_PGLZ_COMPRESSION_ID: - case TOAST_LZ4_COMPRESSION_ID: - valid = true; - break; - - /* Recognized but invalid compression method ID */ - case TOAST_INVALID_COMPRESSION_ID: - break; - - /* Intentionally no default here */ - } - if (!valid) + if (!CompressionIdIsValid(cmid)) report_corruption(ctx, psprintf("toast value " OID8_FORMAT " has invalid compression method id %d", toast_pointer_valueid, cmid)); diff --git a/src/backend/access/common/detoast.c b/src/backend/access/common/detoast.c index a50aea7b6b1..15d8aa8b5a8 100644 --- a/src/backend/access/common/detoast.c +++ b/src/backend/access/common/detoast.c @@ -470,7 +470,7 @@ toast_fetch_datum_slice(varlena *attr, int32 sliceoffset, static varlena * toast_decompress_datum(varlena *attr) { - ToastCompressionId cmid; + uint32 cmid; Assert(VARATT_IS_COMPRESSED(attr)); @@ -502,7 +502,7 @@ toast_decompress_datum(varlena *attr) static varlena * toast_decompress_datum_slice(varlena *attr, int32 slicelength) { - ToastCompressionId cmid; + uint32 cmid; Assert(VARATT_IS_COMPRESSED(attr)); diff --git a/src/backend/access/common/toast_compression.c b/src/backend/access/common/toast_compression.c index 3b1bd651926..74822e64374 100644 --- a/src/backend/access/common/toast_compression.c +++ b/src/backend/access/common/toast_compression.c @@ -31,6 +31,27 @@ int default_toast_compression = DEFAULT_TOAST_COMPRESSION; errmsg("compression method %s not supported", method), \ errdetail("This functionality requires the server to be built with %s support.", method))) +/* + * Compression Method Registry for TOAST. + * + * This holds the metadata associated to each compression method supported + * by TOAST: name, values in attcompression, and on-disk compression ID + * values in varlenas. + */ +typedef struct ToastCompressionRegistryEntry +{ + const char *name; /* method name */ + char method; /* attcompression */ + uint32 cmid; /* varlena on-disk ID */ +} ToastCompressionRegistryEntry; + +static const ToastCompressionRegistryEntry toast_compression_registry[] = { + {"pglz", TOAST_PGLZ_COMPRESSION, TOAST_PGLZ_COMPRESSION_ID}, + {"lz4", TOAST_LZ4_COMPRESSION, TOAST_LZ4_COMPRESSION_ID}, +}; + +#define TOAST_NUM_COMPRESSIONS lengthof(toast_compression_registry) + /* * Compress a varlena using PGLZ. * @@ -250,10 +271,10 @@ lz4_decompress_datum_slice(const varlena *value, int32 slicelength) * * Returns TOAST_INVALID_COMPRESSION_ID if the varlena is not compressed. */ -ToastCompressionId +uint32 toast_get_compression_id(varlena *attr) { - ToastCompressionId cmid = TOAST_INVALID_COMPRESSION_ID; + uint32 cmid = TOAST_INVALID_COMPRESSION_ID; /* * If it is stored externally then fetch the compression method id from @@ -278,39 +299,79 @@ toast_get_compression_id(varlena *attr) /* * CompressionNameToMethod - Get compression method from compression name * - * Search in the available built-in methods. If the compression not found - * in the built-in methods then return InvalidCompressionMethod. + * Search the compression registry by name. If the method name is not found + * then return InvalidCompressionMethod. */ char CompressionNameToMethod(const char *compression) { - if (strcmp(compression, "pglz") == 0) - return TOAST_PGLZ_COMPRESSION; - else if (strcmp(compression, "lz4") == 0) + for (int i = 0; i < TOAST_NUM_COMPRESSIONS; i++) { + if (strcmp(compression, toast_compression_registry[i].name) == 0) + { #ifndef USE_LZ4 - NO_COMPRESSION_SUPPORT("lz4"); + if (strcmp(compression, "lz4") == 0) + NO_COMPRESSION_SUPPORT("lz4"); #endif - return TOAST_LZ4_COMPRESSION; + return toast_compression_registry[i].method; + } } return InvalidCompressionMethod; } /* - * GetCompressionMethodName - Get compression method name + * GetCompressionMethodName + * + * Get compression method name, based on a compression method char, or + * attcompression. */ const char * GetCompressionMethodName(char method) { - switch (method) + for (int i = 0; i < TOAST_NUM_COMPRESSIONS; i++) + { + if (toast_compression_registry[i].method == method) + return toast_compression_registry[i].name; + } + + elog(ERROR, "invalid compression method %c", method); + return NULL; /* keep compiler quiet */ +} + +/* + * MethodToCompressionId + * + * Translate a catalog compression method char (attcompression) to the + * corresponding on-disk varatt ID. + */ +uint32 +MethodToCompressionId(char method) +{ + for (int i = 0; i < TOAST_NUM_COMPRESSIONS; i++) + { + if (toast_compression_registry[i].method == method) + return toast_compression_registry[i].cmid; + } + + elog(ERROR, "invalid compression method %c", method); + return TOAST_INVALID_COMPRESSION_ID; /* keep compiler quiet */ +} + +/* + * CompressionIdIsValid + * + * Check whether a compression ID is registered. Returns true if the + * ID corresponds to a known compression method, false otherwise. + */ +bool +CompressionIdIsValid(uint32 cmid) +{ + for (int i = 0; i < TOAST_NUM_COMPRESSIONS; i++) { - case TOAST_PGLZ_COMPRESSION: - return "pglz"; - case TOAST_LZ4_COMPRESSION: - return "lz4"; - default: - elog(ERROR, "invalid compression method %c", method); - return NULL; /* keep compiler quiet */ + if (toast_compression_registry[i].cmid == cmid) + return true; } + + return false; } diff --git a/src/backend/access/common/toast_internals.c b/src/backend/access/common/toast_internals.c index ca5ed8dfabc..bf9dbd8d623 100644 --- a/src/backend/access/common/toast_internals.c +++ b/src/backend/access/common/toast_internals.c @@ -47,7 +47,7 @@ toast_compress_datum(Datum value, char cmethod) { varlena *tmp = NULL; int32 valsize; - ToastCompressionId cmid = TOAST_INVALID_COMPRESSION_ID; + uint32 cmid = TOAST_INVALID_COMPRESSION_ID; Assert(!VARATT_IS_EXTERNAL(DatumGetPointer(value))); Assert(!VARATT_IS_COMPRESSED(DatumGetPointer(value))); @@ -65,11 +65,9 @@ toast_compress_datum(Datum value, char cmethod) { case TOAST_PGLZ_COMPRESSION: tmp = pglz_compress_datum((const varlena *) DatumGetPointer(value)); - cmid = TOAST_PGLZ_COMPRESSION_ID; break; case TOAST_LZ4_COMPRESSION: tmp = lz4_compress_datum((const varlena *) DatumGetPointer(value)); - cmid = TOAST_LZ4_COMPRESSION_ID; break; default: elog(ERROR, "invalid compression method %c", cmethod); @@ -78,6 +76,9 @@ toast_compress_datum(Datum value, char cmethod) if (tmp == NULL) return PointerGetDatum(NULL); + /* translate attcompression to the on-disk compression ID */ + cmid = MethodToCompressionId(cmethod); + /* * We recheck the actual size even if compression reports success, because * it might be satisfied with having saved as little as one byte in the diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4533dac11a6..ae09f45fdbf 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -4208,7 +4208,7 @@ pg_column_compression(PG_FUNCTION_ARGS) { int typlen; char *result; - ToastCompressionId cmid; + uint32 cmid; /* On first call, get the input type's typlen, and save at *fn_extra */ if (fcinfo->flinfo->fn_extra == NULL) diff --git a/src/include/access/toast_compression.h b/src/include/access/toast_compression.h index 3265f10b734..433a33467b0 100644 --- a/src/include/access/toast_compression.h +++ b/src/include/access/toast_compression.h @@ -22,25 +22,6 @@ */ extern PGDLLIMPORT int default_toast_compression; -/* - * Built-in compression method ID. The toast compression header will store - * this in the first 2 bits of the raw length. These built-in compression - * method IDs are directly mapped to the built-in compression methods. - * - * Don't use these values for anything other than understanding the meaning - * of the raw bits from a varlena; in particular, if the goal is to identify - * a compression method, use the constants TOAST_PGLZ_COMPRESSION, etc. - * below. We might someday support more than 4 compression methods, but - * we can never have more than 4 values in this enum, because there are - * only 2 bits available in the places where this is stored. - */ -typedef enum ToastCompressionId -{ - TOAST_PGLZ_COMPRESSION_ID = 0, - TOAST_LZ4_COMPRESSION_ID = 1, - TOAST_INVALID_COMPRESSION_ID = 2, -} ToastCompressionId; - /* * Built-in compression methods. pg_attribute will store these in the * attcompression column. In attcompression, InvalidCompressionMethod @@ -75,8 +56,14 @@ extern varlena *lz4_decompress_datum_slice(const varlena *value, int32 slicelength); /* other stuff */ -extern ToastCompressionId toast_get_compression_id(varlena *attr); +extern uint32 toast_get_compression_id(varlena *attr); extern char CompressionNameToMethod(const char *compression); extern const char *GetCompressionMethodName(char method); +/* + * Registry translation functions. "cmid" is the on-disk varatt value. + */ +extern uint32 MethodToCompressionId(char method); +extern bool CompressionIdIsValid(uint32 cmid); + #endif /* TOAST_COMPRESSION_H */ diff --git a/src/include/varatt.h b/src/include/varatt.h index 89b47db0833..0de641fce47 100644 --- a/src/include/varatt.h +++ b/src/include/varatt.h @@ -49,6 +49,15 @@ StaticAssertDecl((sizeof(int32) + sizeof(uint32) + 2 * sizeof(Oid)) == #define VARLENA_EXTSIZE_BITS 30 #define VARLENA_EXTSIZE_MASK ((1U << VARLENA_EXTSIZE_BITS) - 1) +/* + * On-disk compression method IDs stored in the high bits of va_tcinfo + * and va_extinfo. Only 2 bits are available, so at most 4 values should + * be used here. + */ +#define TOAST_PGLZ_COMPRESSION_ID 0 +#define TOAST_LZ4_COMPRESSION_ID 1 +#define TOAST_INVALID_COMPRESSION_ID 2 + /* * varatt_indirect is a "TOAST pointer" representing an out-of-line * Datum that's stored in memory, not in an external toast relation. diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index db45ee8881e..b580ffd385a 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -3207,7 +3207,7 @@ TimingClockSourceType TmFromChar TmToChar ToastAttrInfo -ToastCompressionId +ToastCompressionRegistryEntry ToastTupleContext ToastedAttribute TocEntry -- 2.47.1 =

view thread (7+ messages)  latest in thread

Message-ID: <45654909-903F-42BD-A767-61B701992DE3@gmail.com>
Permalink:  ../45654909-903F-42BD-A767-61B701992DE3@gmail.com/
Also on:    postgresql.org/message-id/45654909-903F-42BD-A767-61B701992DE3@gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: imamovaj22@gmail.com, michael@paquier.xyz, li.evan.chao@gmail.com, ayushtiwari.slg01@gmail.com
  Subject: Re: Refactor code around GUC default_toast_compression
  In-Reply-To: <45654909-903F-42BD-A767-61B701992DE3@gmail.com>
  References: <afRbVhoYuw4RStIO@paquier.xyz> <CAJTYsWX2erCHdEV1NoPdGP5fOvPjHYf=MQ2C-N+h6yE45oKihA@mail.gmail.com> <afUsoKiQ6yriscvT@paquier.xyz> <7A329D19-A0F2-4558-A391-6E82699B9BC0@gmail.com> <agGOFTwvOG3FugCn@paquier.xyz> <45654909-903F-42BD-A767-61B701992DE3@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox