pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedRefactor code around GUC default_toast_compression
7+ messages / 4 participants
[nested] [flat]
* Refactor code around GUC default_toast_compression
@ 2026-05-01 07:50 Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 7+ messages in thread
From: Michael Paquier @ 2026-05-01 07:50 UTC (permalink / raw)
To: Postgres hackers <pgsql-hackers@lists.postgresql.org>
Hi all,
While hacking on the TOAST code, I have been annoyed more than once
with the following piece in toast_compression.h:
/*
* 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;
This is due the fact that we have only two bits that can be used in
va_tcinfo or va_extinfo. While looking at the addition of a new
compression method, this was causing a mess, so I have hacked the
attached patch, that makes the addition of more compression methods
easier. The idea is centralized in toast_compression.c, with the
addition of a registry that knows about all the TOAST compression
methods and its meta-data:
- name
- GUC enum values.
- attcompression char value.
- varatt on-disk value.
This is coupled with a set of translation routines, used in other code
paths. This has also the merit to remove TOAST_INVALID_COMPRESSION_ID
from the list of GUC values, which did not really make sense to begin
with. I don't deny that the addition of a new compression method
would require more tweaks, particularly for the decompression part,
but I think that this is a nice cleanup anyway. This is added to the
next commit fest, to be considered for v20.
Thanks,
--
Michael
Attachments:
0001-Refactor-some-code-logic-around-GUC-default_toast_co.patch (text/plain, 19.5K) download
From ce018b1b4ceaafc2c5e2fc74c4378cdd09a93fb2 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Fri, 1 May 2026 16:31:45 +0900
Subject: [PATCH] Refactor some code logic around GUC default_toast_compression
The TOAST compression code links the GUC values of
default_toast_compression to the on-disk varatt attributes, limiting the
number of elements in the enum GUC structure to 4 elements to cover for
the 2 bits that can be saved in va_tcinfo or va_extinfo, depending on if
we are dealing with an inline compressible entry, or an external TOAST
pointer.
This commit refactors the code so as we have a clean split between the
various concepts related to TOAST compression, for:
- The on-disk varatt values.
- The GUC enum values. These are not anymore limited to 4 values.
- The catalog attribute char values.
- 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. Lifting the GUC enum
limitation offers this benefit, perhaps it could be useful for other
purposes.
---
src/include/access/toast_compression.h | 39 +++--
src/include/access/toast_internals.h | 4 +-
src/include/varatt.h | 13 +-
src/backend/access/common/detoast.c | 14 +-
src/backend/access/common/toast_compression.c | 137 +++++++++++++++---
src/backend/access/common/toast_internals.c | 13 +-
src/backend/utils/adt/varlena.c | 8 +-
src/backend/utils/misc/guc_tables.c | 4 +-
doc/src/sgml/ref/alter_index.sgml | 5 +
contrib/amcheck/verify_heapam.c | 19 +--
src/tools/pgindent/typedefs.list | 3 +-
11 files changed, 179 insertions(+), 80 deletions(-)
diff --git a/src/include/access/toast_compression.h b/src/include/access/toast_compression.h
index 3265f10b734f..3d46be67f756 100644
--- a/src/include/access/toast_compression.h
+++ b/src/include/access/toast_compression.h
@@ -16,30 +16,21 @@
/*
* GUC support.
*
- * default_toast_compression is an integer for purposes of the GUC machinery,
+ * default_toast_compression is an integer for purposes of the GUC machinery.
* but the value is one of the char values defined below, as they appear in
- * pg_attribute.attcompression, e.g. TOAST_PGLZ_COMPRESSION.
+ * pg_attribute.attcompression, e.g. TOAST_PGLZ_COMPRESSION. The on-disk
+ * compression ID values (TOAST_COMPRESS_*) are defined in varatt.h.
*/
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.
+ * Values for GUC default_toast_compression.
*/
-typedef enum ToastCompressionId
+typedef enum ToastCompressionGucValue
{
- TOAST_PGLZ_COMPRESSION_ID = 0,
- TOAST_LZ4_COMPRESSION_ID = 1,
- TOAST_INVALID_COMPRESSION_ID = 2,
-} ToastCompressionId;
+ TOAST_PGLZ_COMPRESSION_GUC = 0,
+ TOAST_LZ4_COMPRESSION_GUC = 1,
+} ToastCompressionGucValue;
/*
* Built-in compression methods. pg_attribute will store these in the
@@ -57,9 +48,9 @@ typedef enum ToastCompressionId
* compiled-in, use it, otherwise use pglz.
*/
#ifdef USE_LZ4
-#define DEFAULT_TOAST_COMPRESSION TOAST_LZ4_COMPRESSION
+#define DEFAULT_TOAST_COMPRESSION TOAST_LZ4_COMPRESSION_GUC
#else
-#define DEFAULT_TOAST_COMPRESSION TOAST_PGLZ_COMPRESSION
+#define DEFAULT_TOAST_COMPRESSION TOAST_PGLZ_COMPRESSION_GUC
#endif
/* pglz compression/decompression routines */
@@ -75,8 +66,16 @@ 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 char ToastCompressionGucToMethod(ToastCompressionGucValue guc_value);
+extern uint32 MethodToCompressionId(char method);
+extern char CompressionIdToMethod(uint32 cmid);
+extern bool CompressionIdIsValid(uint32 cmid);
+
#endif /* TOAST_COMPRESSION_H */
diff --git a/src/include/access/toast_internals.h b/src/include/access/toast_internals.h
index bf45889a6428..dcf6131c7b24 100644
--- a/src/include/access/toast_internals.h
+++ b/src/include/access/toast_internals.h
@@ -39,8 +39,8 @@ typedef struct toast_compress_header
#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); \
+ Assert((cm_method) == TOAST_COMPRESS_PGLZ || \
+ (cm_method) == TOAST_COMPRESS_LZ4); \
((toast_compress_header *) (ptr))->tcinfo = \
(len) | ((uint32) (cm_method) << VARLENA_EXTSIZE_BITS); \
} while (0)
diff --git a/src/include/varatt.h b/src/include/varatt.h
index 000bdf33b923..fb4199e6cac1 100644
--- a/src/include/varatt.h
+++ b/src/include/varatt.h
@@ -45,6 +45,15 @@ typedef struct varatt_external
#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_COMPRESS_PGLZ 0
+#define TOAST_COMPRESS_LZ4 1
+#define TOAST_COMPRESS_INVALID 2
+
/*
* varatt_indirect is a "TOAST pointer" representing an out-of-line
* Datum that's stored in memory, not in an external toast relation.
@@ -519,8 +528,8 @@ VARATT_EXTERNAL_GET_COMPRESS_METHOD(varatt_external toast_pointer)
/* This has to remain a macro; beware multiple evaluations! */
#define VARATT_EXTERNAL_SET_SIZE_AND_COMPRESS_METHOD(toast_pointer, len, cm) \
do { \
- Assert((cm) == TOAST_PGLZ_COMPRESSION_ID || \
- (cm) == TOAST_LZ4_COMPRESSION_ID); \
+ Assert((cm) == TOAST_COMPRESS_PGLZ || \
+ (cm) == TOAST_COMPRESS_LZ4); \
((toast_pointer).va_extinfo = \
(len) | ((uint32) (cm) << VARLENA_EXTSIZE_BITS)); \
} while (0)
diff --git a/src/backend/access/common/detoast.c b/src/backend/access/common/detoast.c
index a6c1f3a734b2..20111d6b275d 100644
--- a/src/backend/access/common/detoast.c
+++ b/src/backend/access/common/detoast.c
@@ -252,7 +252,7 @@ detoast_attr_slice(varlena *attr,
* able to decompress the required slice.
*/
if (VARATT_EXTERNAL_GET_COMPRESS_METHOD(toast_pointer) ==
- TOAST_PGLZ_COMPRESSION_ID)
+ TOAST_COMPRESS_PGLZ)
max_size = pglz_maximum_compressed_size(slicelimit, max_size);
/*
@@ -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));
@@ -481,9 +481,9 @@ toast_decompress_datum(varlena *attr)
cmid = TOAST_COMPRESS_METHOD(attr);
switch (cmid)
{
- case TOAST_PGLZ_COMPRESSION_ID:
+ case TOAST_COMPRESS_PGLZ:
return pglz_decompress_datum(attr);
- case TOAST_LZ4_COMPRESSION_ID:
+ case TOAST_COMPRESS_LZ4:
return lz4_decompress_datum(attr);
default:
elog(ERROR, "invalid compression method id %d", cmid);
@@ -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));
@@ -524,9 +524,9 @@ toast_decompress_datum_slice(varlena *attr, int32 slicelength)
cmid = TOAST_COMPRESS_METHOD(attr);
switch (cmid)
{
- case TOAST_PGLZ_COMPRESSION_ID:
+ case TOAST_COMPRESS_PGLZ:
return pglz_decompress_datum_slice(attr, slicelength);
- case TOAST_LZ4_COMPRESSION_ID:
+ case TOAST_COMPRESS_LZ4:
return lz4_decompress_datum_slice(attr, slicelength);
default:
elog(ERROR, "invalid compression method id %d", cmid);
diff --git a/src/backend/access/common/toast_compression.c b/src/backend/access/common/toast_compression.c
index 5a5d579494a2..9881d9242624 100644
--- a/src/backend/access/common/toast_compression.c
+++ b/src/backend/access/common/toast_compression.c
@@ -31,6 +31,28 @@ 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, on-disk compression ID values
+ * in varlenas, and GUC enum values.
+ */
+typedef struct ToastCompressionRegistryEntry
+{
+ const char *name; /* method name */
+ char method; /* attcompression */
+ uint32 cmid; /* varlena on-disk ID */
+ ToastCompressionGucValue guc_value; /* GUC enum */
+} ToastCompressionRegistryEntry;
+
+static const ToastCompressionRegistryEntry toast_compression_registry[] = {
+ {"pglz", TOAST_PGLZ_COMPRESSION, TOAST_COMPRESS_PGLZ, TOAST_PGLZ_COMPRESSION_GUC},
+ {"lz4", TOAST_LZ4_COMPRESSION, TOAST_COMPRESS_LZ4, TOAST_LZ4_COMPRESSION_GUC},
+};
+
+#define TOAST_NUM_COMPRESSIONS lengthof(toast_compression_registry)
+
/*
* Compress a varlena using PGLZ.
*
@@ -248,12 +270,12 @@ lz4_decompress_datum_slice(const varlena *value, int32 slicelength)
/*
* Extract compression ID from a varlena.
*
- * Returns TOAST_INVALID_COMPRESSION_ID if the varlena is not compressed.
+ * Returns TOAST_COMPRESS_INVALID if the varlena is not compressed.
*/
-ToastCompressionId
+uint32
toast_get_compression_id(varlena *attr)
{
- ToastCompressionId cmid = TOAST_INVALID_COMPRESSION_ID;
+ uint32 cmid = TOAST_COMPRESS_INVALID;
/*
* If it is stored externally then fetch the compression method id from
@@ -278,39 +300,116 @@ 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++)
{
- 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].method == method)
+ return toast_compression_registry[i].name;
}
+
+ elog(ERROR, "invalid compression method %c", method);
+ return NULL; /* keep compiler quiet */
+}
+
+/*
+ * ToastCompressionGucToMethod
+ *
+ * Translate a GUC value to a compression method char, for attcompression.
+ */
+char
+ToastCompressionGucToMethod(ToastCompressionGucValue guc_value)
+{
+ for (int i = 0; i < TOAST_NUM_COMPRESSIONS; i++)
+ {
+ if (toast_compression_registry[i].guc_value == guc_value)
+ return toast_compression_registry[i].method;
+ }
+
+ elog(ERROR, "invalid compression GUC value %d", guc_value);
+ return InvalidCompressionMethod; /* 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_COMPRESS_INVALID; /* keep compiler quiet */
+}
+
+/*
+ * CompressionIdToMethod
+ *
+ * Translate an on-disk varatt ID to the corresponding catalog compression
+ * method char (attcompression).
+ */
+char
+CompressionIdToMethod(uint32 cmid)
+{
+ for (int i = 0; i < TOAST_NUM_COMPRESSIONS; i++)
+ {
+ if (toast_compression_registry[i].cmid == cmid)
+ return toast_compression_registry[i].method;
+ }
+
+ elog(ERROR, "invalid compression method id %d", cmid);
+ return InvalidCompressionMethod; /* 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++)
+ {
+ 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 77d42e7ed65a..abae7c497b9a 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_COMPRESS_INVALID;
Assert(!VARATT_IS_EXTERNAL(DatumGetPointer(value)));
Assert(!VARATT_IS_COMPRESSED(DatumGetPointer(value)));
@@ -56,20 +56,21 @@ toast_compress_datum(Datum value, char cmethod)
/* If the compression method is not valid, use the current default */
if (!CompressionMethodIsValid(cmethod))
- cmethod = default_toast_compression;
+ cmethod = ToastCompressionGucToMethod(default_toast_compression);
/*
- * Call appropriate compression routine for the compression method.
+ * Translate the compression method char to the on-disk compression ID
+ * via the Method Registry, then dispatch to the appropriate compression
+ * routine.
*/
+ cmid = MethodToCompressionId(cmethod);
switch (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);
@@ -91,7 +92,7 @@ toast_compress_datum(Datum value, char cmethod)
if (VARSIZE(tmp) < valsize - 2)
{
/* successful compression */
- Assert(cmid != TOAST_INVALID_COMPRESSION_ID);
+ Assert(cmid != TOAST_COMPRESS_INVALID);
TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD(tmp, valsize, cmid);
return PointerGetDatum(tmp);
}
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index c0ff51bd2fc1..13c6ba25234e 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -4205,7 +4205,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)
@@ -4230,16 +4230,16 @@ pg_column_compression(PG_FUNCTION_ARGS)
/* get the compression method id stored in the compressed varlena */
cmid = toast_get_compression_id((varlena *)
DatumGetPointer(PG_GETARG_DATUM(0)));
- if (cmid == TOAST_INVALID_COMPRESSION_ID)
+ if (cmid == TOAST_COMPRESS_INVALID)
PG_RETURN_NULL();
/* convert compression method id to compression method name */
switch (cmid)
{
- case TOAST_PGLZ_COMPRESSION_ID:
+ case TOAST_COMPRESS_PGLZ:
result = "pglz";
break;
- case TOAST_LZ4_COMPRESSION_ID:
+ case TOAST_COMPRESS_LZ4:
result = "lz4";
break;
default:
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 290ccbc543e2..0fadc907e79d 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -469,9 +469,9 @@ static const struct config_enum_entry shared_memory_options[] = {
};
static const struct config_enum_entry default_toast_compression_options[] = {
- {"pglz", TOAST_PGLZ_COMPRESSION, false},
+ {"pglz", TOAST_PGLZ_COMPRESSION_GUC, false},
#ifdef USE_LZ4
- {"lz4", TOAST_LZ4_COMPRESSION, false},
+ {"lz4", TOAST_LZ4_COMPRESSION_GUC, false},
#endif
{NULL, 0, false}
};
diff --git a/doc/src/sgml/ref/alter_index.sgml b/doc/src/sgml/ref/alter_index.sgml
index 1d42d05d8581..fb7096c16ea6 100644
--- a/doc/src/sgml/ref/alter_index.sgml
+++ b/doc/src/sgml/ref/alter_index.sgml
@@ -97,6 +97,11 @@ ALTER INDEX ALL IN TABLESPACE <replaceable class="parameter">name</replaceable>
index cannot be dropped by itself, and will automatically be dropped
if its parent index is dropped.
</para>
+ <para>
+ If the named index is already attached to the altered index, the
+ command will attempt to validate the parent index if the parent is
+ currently invalid.
+ </para>
</listitem>
</varlistentry>
diff --git a/contrib/amcheck/verify_heapam.c b/contrib/amcheck/verify_heapam.c
index 20ff58aa7825..2d665a572754 100644
--- a/contrib/amcheck/verify_heapam.c
+++ b/contrib/amcheck/verify_heapam.c
@@ -1784,26 +1784,11 @@ check_tuple_attribute(HeapCheckContext *ctx)
if (VARATT_EXTERNAL_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 %u has invalid compression method id %d",
toast_pointer.va_valueid, cmid));
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 0abdb2d37e2d..f83aaac00f17 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -3198,7 +3198,8 @@ TimingClockSourceType
TmFromChar
TmToChar
ToastAttrInfo
-ToastCompressionId
+ToastCompressionGucValue
+ToastCompressionRegistryEntry
ToastTupleContext
ToastedAttribute
TocEntry
--
2.54.0
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Refactor code around GUC default_toast_compression
@ 2026-05-01 09:14 Ayush Tiwari <ayushtiwari.slg01@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 7+ messages in thread
From: Ayush Tiwari @ 2026-05-01 09:14 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>
Hi,
On Fri, 1 May 2026 at 13:21, Michael Paquier <michael@paquier.xyz> wrote:
> Hi all,
>
> While hacking on the TOAST code, I have been annoyed more than once
> with the following piece in toast_compression.h:
> /*
> * 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;
>
> This is due the fact that we have only two bits that can be used in
> va_tcinfo or va_extinfo. While looking at the addition of a new
> compression method, this was causing a mess, so I have hacked the
> attached patch, that makes the addition of more compression methods
> easier. The idea is centralized in toast_compression.c, with the
> addition of a registry that knows about all the TOAST compression
> methods and its meta-data:
> - name
> - GUC enum values.
> - attcompression char value.
> - varatt on-disk value.
>
>
I looked at the patch, and the refactoring direction looks reasonable
to me. I noticed a few small things worth cleaning up (although
it is for v20, just wanted to drop it here for future)
1. The patch includes an unrelated hunk in
doc/src/sgml/ref/alter_index.sgml, adding text about `ALTER INDEX ...
ATTACH PARTITION`. That looks like an accidental carry-over from another
patch and shouldn't be there ig.
2. The comment in src/include/access/toast_compression.h describing
default_toast_compression looks stale after this change? It still says
that the GUC value is one of the char values stored in
pg_attribute.attcompression, but the patch changes it to use the new
ToastCompressionGucValue enum values instead.(Maybe I'm
missing something)
3. One minor point: CompressionIdToMethod() seems to be added as a public
helper, but I could not find any callers in this patch. Also,
pg_column_compression() still keeps its own cmid-to-name switch. If the
intent is to centralize these mappings in the registry, perhaps that code
could use the new helper path as well, otherwise the unused helper may
not
be necessary yet (though it might be in future).
Thanks for the patch!
Regards,
Ayush
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Refactor code around GUC default_toast_compression
@ 2026-05-01 22:43 Michael Paquier <michael@paquier.xyz>
parent: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Michael Paquier @ 2026-05-01 22:43 UTC (permalink / raw)
To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>
On Fri, May 01, 2026 at 02:44:07PM +0530, Ayush Tiwari wrote:
> 1. The patch includes an unrelated hunk in
> doc/src/sgml/ref/alter_index.sgml, adding text about `ALTER INDEX ...
> ATTACH PARTITION`. That looks like an accidental carry-over from another
> patch and shouldn't be there ig.
Sorry about that. That feels like a rebase fart.
> 2. The comment in src/include/access/toast_compression.h describing
> default_toast_compression looks stale after this change? It still says
> that the GUC value is one of the char values stored in
> pg_attribute.attcompression, but the patch changes it to use the new
> ToastCompressionGucValue enum values instead.(Maybe I'm
> missing something)
Nope, you are missing nothing. I was re-reading the patch and I think
that we could just remove the whole paragraph. Even by doing so we
lose no information.
> 3. One minor point: CompressionIdToMethod() seems to be added as a public
> helper, but I could not find any callers in this patch.
Oops, removed. I may have used it at some point.
> Also,
> pg_column_compression() still keeps its own cmid-to-name switch. If the
> intent is to centralize these mappings in the registry, perhaps that code
> could use the new helper path as well, otherwise the unused helper may
> not be necessary yet (though it might be in future).
Yes, this is part of the extra tweaks that would be needed when added
a new compression method. This part looks at a varlena pointer,
retrieves the on-disk ID. So this is left as-is on purpose, like the
direct TOAST decompress business based on varlena pointers.
Attached is a v2, to keep the CI happy for as long as we can use it.
--
Michael
Attachments:
v2-0001-Refactor-some-code-logic-around-GUC-default_toast.patch (text/plain, 18.0K) download
From 7fd5d4665ec31ce24ffaaf24a1c504933e9c276b Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Sat, 2 May 2026 07:33:26 +0900
Subject: [PATCH v2] Refactor some code logic around GUC
default_toast_compression
The TOAST compression code links the GUC values of
default_toast_compression to the on-disk varatt attributes, limiting the
number of elements in the enum GUC structure to 4 elements to cover for
the 2 bits that can be saved in va_tcinfo or va_extinfo, depending on if
we are dealing with an inline compressible entry, or an external TOAST
pointer.
This commit refactors the code so as we have a clean split between the
various concepts related to TOAST compression, for:
- The on-disk varatt values.
- The GUC enum values. These are not anymore limited to 4 values.
- The catalog attribute char values.
- 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. Lifting the GUC enum
limitation offers this benefit, perhaps it could be useful for other
purposes.
---
src/include/access/toast_compression.h | 37 +++---
src/include/access/toast_internals.h | 4 +-
src/include/varatt.h | 13 +-
src/backend/access/common/detoast.c | 14 +--
src/backend/access/common/toast_compression.c | 118 +++++++++++++++---
src/backend/access/common/toast_internals.c | 13 +-
src/backend/utils/adt/varlena.c | 8 +-
src/backend/utils/misc/guc_tables.c | 4 +-
contrib/amcheck/verify_heapam.c | 19 +--
src/tools/pgindent/typedefs.list | 3 +-
10 files changed, 151 insertions(+), 82 deletions(-)
diff --git a/src/include/access/toast_compression.h b/src/include/access/toast_compression.h
index 3265f10b734f..f46d0239ba42 100644
--- a/src/include/access/toast_compression.h
+++ b/src/include/access/toast_compression.h
@@ -15,31 +15,17 @@
/*
* GUC support.
- *
- * default_toast_compression is an integer for purposes of the GUC machinery,
- * but the value is one of the char values defined below, as they appear in
- * pg_attribute.attcompression, e.g. TOAST_PGLZ_COMPRESSION.
*/
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.
+ * Values for GUC default_toast_compression.
*/
-typedef enum ToastCompressionId
+typedef enum ToastCompressionGucValue
{
- TOAST_PGLZ_COMPRESSION_ID = 0,
- TOAST_LZ4_COMPRESSION_ID = 1,
- TOAST_INVALID_COMPRESSION_ID = 2,
-} ToastCompressionId;
+ TOAST_PGLZ_COMPRESSION_GUC = 0,
+ TOAST_LZ4_COMPRESSION_GUC = 1,
+} ToastCompressionGucValue;
/*
* Built-in compression methods. pg_attribute will store these in the
@@ -57,9 +43,9 @@ typedef enum ToastCompressionId
* compiled-in, use it, otherwise use pglz.
*/
#ifdef USE_LZ4
-#define DEFAULT_TOAST_COMPRESSION TOAST_LZ4_COMPRESSION
+#define DEFAULT_TOAST_COMPRESSION TOAST_LZ4_COMPRESSION_GUC
#else
-#define DEFAULT_TOAST_COMPRESSION TOAST_PGLZ_COMPRESSION
+#define DEFAULT_TOAST_COMPRESSION TOAST_PGLZ_COMPRESSION_GUC
#endif
/* pglz compression/decompression routines */
@@ -75,8 +61,15 @@ 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 char ToastCompressionGucToMethod(ToastCompressionGucValue guc_value);
+extern uint32 MethodToCompressionId(char method);
+extern bool CompressionIdIsValid(uint32 cmid);
+
#endif /* TOAST_COMPRESSION_H */
diff --git a/src/include/access/toast_internals.h b/src/include/access/toast_internals.h
index bf45889a6428..dcf6131c7b24 100644
--- a/src/include/access/toast_internals.h
+++ b/src/include/access/toast_internals.h
@@ -39,8 +39,8 @@ typedef struct toast_compress_header
#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); \
+ Assert((cm_method) == TOAST_COMPRESS_PGLZ || \
+ (cm_method) == TOAST_COMPRESS_LZ4); \
((toast_compress_header *) (ptr))->tcinfo = \
(len) | ((uint32) (cm_method) << VARLENA_EXTSIZE_BITS); \
} while (0)
diff --git a/src/include/varatt.h b/src/include/varatt.h
index 000bdf33b923..fb4199e6cac1 100644
--- a/src/include/varatt.h
+++ b/src/include/varatt.h
@@ -45,6 +45,15 @@ typedef struct varatt_external
#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_COMPRESS_PGLZ 0
+#define TOAST_COMPRESS_LZ4 1
+#define TOAST_COMPRESS_INVALID 2
+
/*
* varatt_indirect is a "TOAST pointer" representing an out-of-line
* Datum that's stored in memory, not in an external toast relation.
@@ -519,8 +528,8 @@ VARATT_EXTERNAL_GET_COMPRESS_METHOD(varatt_external toast_pointer)
/* This has to remain a macro; beware multiple evaluations! */
#define VARATT_EXTERNAL_SET_SIZE_AND_COMPRESS_METHOD(toast_pointer, len, cm) \
do { \
- Assert((cm) == TOAST_PGLZ_COMPRESSION_ID || \
- (cm) == TOAST_LZ4_COMPRESSION_ID); \
+ Assert((cm) == TOAST_COMPRESS_PGLZ || \
+ (cm) == TOAST_COMPRESS_LZ4); \
((toast_pointer).va_extinfo = \
(len) | ((uint32) (cm) << VARLENA_EXTSIZE_BITS)); \
} while (0)
diff --git a/src/backend/access/common/detoast.c b/src/backend/access/common/detoast.c
index a6c1f3a734b2..20111d6b275d 100644
--- a/src/backend/access/common/detoast.c
+++ b/src/backend/access/common/detoast.c
@@ -252,7 +252,7 @@ detoast_attr_slice(varlena *attr,
* able to decompress the required slice.
*/
if (VARATT_EXTERNAL_GET_COMPRESS_METHOD(toast_pointer) ==
- TOAST_PGLZ_COMPRESSION_ID)
+ TOAST_COMPRESS_PGLZ)
max_size = pglz_maximum_compressed_size(slicelimit, max_size);
/*
@@ -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));
@@ -481,9 +481,9 @@ toast_decompress_datum(varlena *attr)
cmid = TOAST_COMPRESS_METHOD(attr);
switch (cmid)
{
- case TOAST_PGLZ_COMPRESSION_ID:
+ case TOAST_COMPRESS_PGLZ:
return pglz_decompress_datum(attr);
- case TOAST_LZ4_COMPRESSION_ID:
+ case TOAST_COMPRESS_LZ4:
return lz4_decompress_datum(attr);
default:
elog(ERROR, "invalid compression method id %d", cmid);
@@ -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));
@@ -524,9 +524,9 @@ toast_decompress_datum_slice(varlena *attr, int32 slicelength)
cmid = TOAST_COMPRESS_METHOD(attr);
switch (cmid)
{
- case TOAST_PGLZ_COMPRESSION_ID:
+ case TOAST_COMPRESS_PGLZ:
return pglz_decompress_datum_slice(attr, slicelength);
- case TOAST_LZ4_COMPRESSION_ID:
+ case TOAST_COMPRESS_LZ4:
return lz4_decompress_datum_slice(attr, slicelength);
default:
elog(ERROR, "invalid compression method id %d", cmid);
diff --git a/src/backend/access/common/toast_compression.c b/src/backend/access/common/toast_compression.c
index 5a5d579494a2..8082e5189ec3 100644
--- a/src/backend/access/common/toast_compression.c
+++ b/src/backend/access/common/toast_compression.c
@@ -31,6 +31,28 @@ 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, on-disk compression ID values
+ * in varlenas, and GUC enum values.
+ */
+typedef struct ToastCompressionRegistryEntry
+{
+ const char *name; /* method name */
+ char method; /* attcompression */
+ uint32 cmid; /* varlena on-disk ID */
+ ToastCompressionGucValue guc_value; /* GUC enum */
+} ToastCompressionRegistryEntry;
+
+static const ToastCompressionRegistryEntry toast_compression_registry[] = {
+ {"pglz", TOAST_PGLZ_COMPRESSION, TOAST_COMPRESS_PGLZ, TOAST_PGLZ_COMPRESSION_GUC},
+ {"lz4", TOAST_LZ4_COMPRESSION, TOAST_COMPRESS_LZ4, TOAST_LZ4_COMPRESSION_GUC},
+};
+
+#define TOAST_NUM_COMPRESSIONS lengthof(toast_compression_registry)
+
/*
* Compress a varlena using PGLZ.
*
@@ -248,12 +270,12 @@ lz4_decompress_datum_slice(const varlena *value, int32 slicelength)
/*
* Extract compression ID from a varlena.
*
- * Returns TOAST_INVALID_COMPRESSION_ID if the varlena is not compressed.
+ * Returns TOAST_COMPRESS_INVALID if the varlena is not compressed.
*/
-ToastCompressionId
+uint32
toast_get_compression_id(varlena *attr)
{
- ToastCompressionId cmid = TOAST_INVALID_COMPRESSION_ID;
+ uint32 cmid = TOAST_COMPRESS_INVALID;
/*
* If it is stored externally then fetch the compression method id from
@@ -278,39 +300,97 @@ 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++)
{
- 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].method == method)
+ return toast_compression_registry[i].name;
}
+
+ elog(ERROR, "invalid compression method %c", method);
+ return NULL; /* keep compiler quiet */
+}
+
+/*
+ * ToastCompressionGucToMethod
+ *
+ * Translate a GUC value to a compression method char, for attcompression.
+ */
+char
+ToastCompressionGucToMethod(ToastCompressionGucValue guc_value)
+{
+ for (int i = 0; i < TOAST_NUM_COMPRESSIONS; i++)
+ {
+ if (toast_compression_registry[i].guc_value == guc_value)
+ return toast_compression_registry[i].method;
+ }
+
+ elog(ERROR, "invalid compression GUC value %d", guc_value);
+ return InvalidCompressionMethod; /* 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_COMPRESS_INVALID; /* 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++)
+ {
+ 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 77d42e7ed65a..abae7c497b9a 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_COMPRESS_INVALID;
Assert(!VARATT_IS_EXTERNAL(DatumGetPointer(value)));
Assert(!VARATT_IS_COMPRESSED(DatumGetPointer(value)));
@@ -56,20 +56,21 @@ toast_compress_datum(Datum value, char cmethod)
/* If the compression method is not valid, use the current default */
if (!CompressionMethodIsValid(cmethod))
- cmethod = default_toast_compression;
+ cmethod = ToastCompressionGucToMethod(default_toast_compression);
/*
- * Call appropriate compression routine for the compression method.
+ * Translate the compression method char to the on-disk compression ID
+ * via the Method Registry, then dispatch to the appropriate compression
+ * routine.
*/
+ cmid = MethodToCompressionId(cmethod);
switch (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);
@@ -91,7 +92,7 @@ toast_compress_datum(Datum value, char cmethod)
if (VARSIZE(tmp) < valsize - 2)
{
/* successful compression */
- Assert(cmid != TOAST_INVALID_COMPRESSION_ID);
+ Assert(cmid != TOAST_COMPRESS_INVALID);
TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD(tmp, valsize, cmid);
return PointerGetDatum(tmp);
}
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index c0ff51bd2fc1..13c6ba25234e 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -4205,7 +4205,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)
@@ -4230,16 +4230,16 @@ pg_column_compression(PG_FUNCTION_ARGS)
/* get the compression method id stored in the compressed varlena */
cmid = toast_get_compression_id((varlena *)
DatumGetPointer(PG_GETARG_DATUM(0)));
- if (cmid == TOAST_INVALID_COMPRESSION_ID)
+ if (cmid == TOAST_COMPRESS_INVALID)
PG_RETURN_NULL();
/* convert compression method id to compression method name */
switch (cmid)
{
- case TOAST_PGLZ_COMPRESSION_ID:
+ case TOAST_COMPRESS_PGLZ:
result = "pglz";
break;
- case TOAST_LZ4_COMPRESSION_ID:
+ case TOAST_COMPRESS_LZ4:
result = "lz4";
break;
default:
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 290ccbc543e2..0fadc907e79d 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -469,9 +469,9 @@ static const struct config_enum_entry shared_memory_options[] = {
};
static const struct config_enum_entry default_toast_compression_options[] = {
- {"pglz", TOAST_PGLZ_COMPRESSION, false},
+ {"pglz", TOAST_PGLZ_COMPRESSION_GUC, false},
#ifdef USE_LZ4
- {"lz4", TOAST_LZ4_COMPRESSION, false},
+ {"lz4", TOAST_LZ4_COMPRESSION_GUC, false},
#endif
{NULL, 0, false}
};
diff --git a/contrib/amcheck/verify_heapam.c b/contrib/amcheck/verify_heapam.c
index 20ff58aa7825..2d665a572754 100644
--- a/contrib/amcheck/verify_heapam.c
+++ b/contrib/amcheck/verify_heapam.c
@@ -1784,26 +1784,11 @@ check_tuple_attribute(HeapCheckContext *ctx)
if (VARATT_EXTERNAL_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 %u has invalid compression method id %d",
toast_pointer.va_valueid, cmid));
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 0abdb2d37e2d..f83aaac00f17 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -3198,7 +3198,8 @@ TimingClockSourceType
TmFromChar
TmToChar
ToastAttrInfo
-ToastCompressionId
+ToastCompressionGucValue
+ToastCompressionRegistryEntry
ToastTupleContext
ToastedAttribute
TocEntry
--
2.54.0
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Refactor code around GUC default_toast_compression
@ 2026-05-02 01:55 Chao Li <li.evan.chao@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 7+ messages in thread
From: Chao Li @ 2026-05-02 01:55 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; Postgres hackers <pgsql-hackers@lists.postgresql.org>
> On May 2, 2026, at 06:43, Michael Paquier <michael@paquier.xyz> wrote:
>
> On Fri, May 01, 2026 at 02:44:07PM +0530, Ayush Tiwari wrote:
>> 1. The patch includes an unrelated hunk in
>> doc/src/sgml/ref/alter_index.sgml, adding text about `ALTER INDEX ...
>> ATTACH PARTITION`. That looks like an accidental carry-over from another
>> patch and shouldn't be there ig.
>
> Sorry about that. That feels like a rebase fart.
>
>> 2. The comment in src/include/access/toast_compression.h describing
>> default_toast_compression looks stale after this change? It still says
>> that the GUC value is one of the char values stored in
>> pg_attribute.attcompression, but the patch changes it to use the new
>> ToastCompressionGucValue enum values instead.(Maybe I'm
>> missing something)
>
> Nope, you are missing nothing. I was re-reading the patch and I think
> that we could just remove the whole paragraph. Even by doing so we
> lose no information.
>
>> 3. One minor point: CompressionIdToMethod() seems to be added as a public
>> helper, but I could not find any callers in this patch.
>
> Oops, removed. I may have used it at some point.
>
>> Also,
>> pg_column_compression() still keeps its own cmid-to-name switch. If the
>> intent is to centralize these mappings in the registry, perhaps that code
>> could use the new helper path as well, otherwise the unused helper may
>> not be necessary yet (though it might be in future).
>
> Yes, this is part of the extra tweaks that would be needed when added
> a new compression method. This part looks at a varlena pointer,
> retrieves the on-disk ID. So this is left as-is on purpose, like the
> direct TOAST decompress business based on varlena pointers.
>
> Attached is a v2, to keep the CI happy for as long as we can use it.
> --
> Michael
> <v2-0001-Refactor-some-code-logic-around-GUC-default_toast.patch>
Overall looks good. A few small comments:
1
```
/*
* GUC support.
- *
- * default_toast_compression is an integer for purposes of the GUC machinery,
- * but the value is one of the char values defined below, as they appear in
- * pg_attribute.attcompression, e.g. TOAST_PGLZ_COMPRESSION.
*/
extern PGDLLIMPORT int default_toast_compression;
```
Before this patch, default_toast_compression stores 'p'/'l'. With this patch, it is changed to store 0/1. Would it be better to rename this variable?
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.
2
```
#ifdef USE_LZ4
-#define DEFAULT_TOAST_COMPRESSION TOAST_LZ4_COMPRESSION
+#define DEFAULT_TOAST_COMPRESSION TOAST_LZ4_COMPRESSION_GUC
#else
-#define DEFAULT_TOAST_COMPRESSION TOAST_PGLZ_COMPRESSION
+#define DEFAULT_TOAST_COMPRESSION TOAST_PGLZ_COMPRESSION_GUC
#endif
```
Would it better to also rename DEFAULT_TOAST_COMPRESSION to DEFAULT_TOAST_COMPRESSION_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?
4
```
/*
- * Call appropriate compression routine for the compression method.
+ * Translate the compression method char to the on-disk compression ID
+ * via the Method Registry, then dispatch to the appropriate compression
+ * routine.
*/
+ cmid = MethodToCompressionId(cmethod);
switch (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);
```
As the switch/default explicitly rejects invalid cmethod, I feel slightly better for readability to place "cmid = MethodToCompressionId(cmethod);" after the switch clause.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Refactor code around GUC default_toast_compression
@ 2026-05-11 08:06 Michael Paquier <michael@paquier.xyz>
parent: Chao Li <li.evan.chao@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Michael Paquier @ 2026-05-11 08:06 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; Postgres hackers <pgsql-hackers@lists.postgresql.org>
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
Attachments:
v3-0001-Refactor-some-code-logic-around-GUC-default_toast.patch (text/plain, 11.9K) download
From 1488368c6fb2671e267eeaf9b004c74e1eb989cd Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Mon, 11 May 2026 16:54:04 +0900
Subject: [PATCH v3] 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.
---
src/include/access/toast_compression.h | 27 ++----
src/include/varatt.h | 9 ++
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 +-
contrib/amcheck/verify_heapam.c | 19 +---
src/tools/pgindent/typedefs.list | 2 +-
8 files changed, 105 insertions(+), 62 deletions(-)
diff --git a/src/include/access/toast_compression.h b/src/include/access/toast_compression.h
index 3265f10b734f..433a33467b0e 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 000bdf33b923..4df676d90888 100644
--- a/src/include/varatt.h
+++ b/src/include/varatt.h
@@ -45,6 +45,15 @@ typedef struct varatt_external
#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/backend/access/common/detoast.c b/src/backend/access/common/detoast.c
index a6c1f3a734b2..58be8081e915 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 5a5d579494a2..4e20407e62a3 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++)
{
- 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].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++)
+ {
+ 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 77d42e7ed65a..f30d132c1ff7 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 c0ff51bd2fc1..d7144a045bc6 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -4205,7 +4205,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/contrib/amcheck/verify_heapam.c b/contrib/amcheck/verify_heapam.c
index 20ff58aa7825..2d665a572754 100644
--- a/contrib/amcheck/verify_heapam.c
+++ b/contrib/amcheck/verify_heapam.c
@@ -1784,26 +1784,11 @@ check_tuple_attribute(HeapCheckContext *ctx)
if (VARATT_EXTERNAL_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 %u has invalid compression method id %d",
toast_pointer.va_valueid, cmid));
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 06532bf71526..7e506c246e34 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -3200,7 +3200,7 @@ TimingClockSourceType
TmFromChar
TmToChar
ToastAttrInfo
-ToastCompressionId
+ToastCompressionRegistryEntry
ToastTupleContext
ToastedAttribute
TocEntry
--
2.54.0
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Refactor code around GUC default_toast_compression
@ 2026-09-14 13:48 Aidar Imamov <imamovaj22@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 7+ messages in thread
From: Aidar Imamov @ 2026-09-14 13:48 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Chao Li <li.evan.chao@gmail.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; Postgres hackers <pgsql-hackers@lists.postgresql.org>
> 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
=
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Refactor code around GUC default_toast_compression
@ 2026-09-16 05:17 Michael Paquier <michael@paquier.xyz>
parent: Aidar Imamov <imamovaj22@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Michael Paquier @ 2026-09-16 05:17 UTC (permalink / raw)
To: Aidar Imamov <imamovaj22@gmail.com>; +Cc: Chao Li <li.evan.chao@gmail.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; Postgres hackers <pgsql-hackers@lists.postgresql.org>
On Mon, Sep 14, 2026 at 04:48:26PM +0300, Aidar Imamov wrote:
> 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.
Yep, my fault for breaking my own patch. Thanks for looking at it.
> 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.
Yeah, I am not sure at this point. What I am pretty sure about is
that this patch may not be the best path forward, actually. I was
playing with a few more things related to the extensiblity of the
compression methods in varlenas and this patch was not feeling like
optimal all the time.
Withdrawn for now.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2026-09-16 05:17 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-05-01 07:50 Refactor code around GUC default_toast_compression Michael Paquier <michael@paquier.xyz>
2026-05-01 09:14 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-05-01 22:43 ` Michael Paquier <michael@paquier.xyz>
2026-05-02 01:55 ` Chao Li <li.evan.chao@gmail.com>
2026-05-11 08:06 ` Michael Paquier <michael@paquier.xyz>
2026-09-14 13:48 ` Aidar Imamov <imamovaj22@gmail.com>
2026-09-16 05:17 ` Michael Paquier <michael@paquier.xyz>
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