From: Michael Paquier Date: Thu, 5 Mar 2026 00:24:35 +0000 (+0900) Subject: Change default value of default_toast_compression to "lz4", take two X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=34dfca293432e206b8f80431f81535aff69782ca;p=thirdparty%2Fpostgresql.git Change default value of default_toast_compression to "lz4", take two The default value for default_toast_compression was "pglz". The main reason for this choice is that this option is always available, pglz code being embedded in Postgres. However, it is known that LZ4 is more efficient than pglz: less CPU required, more compression on average. As of this commit, the default value of default_toast_compression becomes "lz4", if available. By switching to LZ4 as the default, users should see natural speedups on TOAST data reads and/or writes. Support for LZ4 in TOAST compression was added in Postgres v14, or 5 releases ago. This should be long enough to consider this feature as stable. While at it, quotes are removed from default_toast_compression in postgresql.conf.sample. Quotes are not required in this case. The in-place value replacement done by initdb if the build supports LZ4 would not use them in the postgresql.conf file added to a freshly-initialized cluster. Note that this is a version lighter than 7c1849311e49, that included a replacement of --with-lz4 by --without-lz4 in configure builds, forcing a requirement for LZ4 in all environments. The buildfarm did not like it, at all. This commit switches default_toast_compression to lz4 as default only when --with-lz4 is defined, which should keep the buildfarm at bay while still allowing users to benefit from LZ4 compression in TOAST as long as the code is compiled with it. Author: Euler Taveira Reviewed-by: Peter Eisentraut Reviewed-by: Aleksander Alekseev Discussion: https://posgr.es/m/435df33a-129e-4f0c-a803-f3935c5a5ecb@eisentraut.org --- diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index f670e2d4c31..cb7afca9079 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -10020,7 +10020,8 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv; The supported compression methods are pglz and (if PostgreSQL was compiled with ) lz4. - The default is pglz. + The default is lz4 (if available); otherwise, + pglz. diff --git a/src/backend/access/common/toast_compression.c b/src/backend/access/common/toast_compression.c index 4d00537049e..5a5d579494a 100644 --- a/src/backend/access/common/toast_compression.c +++ b/src/backend/access/common/toast_compression.c @@ -23,7 +23,7 @@ #include "varatt.h" /* GUC */ -int default_toast_compression = TOAST_PGLZ_COMPRESSION; +int default_toast_compression = DEFAULT_TOAST_COMPRESSION; #define NO_COMPRESSION_SUPPORT(method) \ ereport(ERROR, \ diff --git a/src/backend/utils/misc/guc_parameters.dat b/src/backend/utils/misc/guc_parameters.dat index 9507778415d..5ee84a639d8 100644 --- a/src/backend/utils/misc/guc_parameters.dat +++ b/src/backend/utils/misc/guc_parameters.dat @@ -735,7 +735,7 @@ { name => 'default_toast_compression', type => 'enum', context => 'PGC_USERSET', group => 'CLIENT_CONN_STATEMENT', short_desc => 'Sets the default compression method for compressible values.', variable => 'default_toast_compression', - boot_val => 'TOAST_PGLZ_COMPRESSION', + boot_val => 'DEFAULT_TOAST_COMPRESSION', options => 'default_toast_compression_options', }, diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index f938cc65a3a..e686d88afc4 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -779,7 +779,7 @@ #row_security = on #default_table_access_method = 'heap' #default_tablespace = '' # a tablespace name, '' uses the default -#default_toast_compression = 'pglz' # 'pglz' or 'lz4' +#default_toast_compression = pglz # pglz or lz4 #temp_tablespaces = '' # a list of tablespace names, '' uses # only default tablespace #check_function_bodies = on diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 53ec1544ff3..6387c66289e 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -1426,6 +1426,11 @@ setup_config(void) "0640", false); } +#if USE_LZ4 + conflines = replace_guc_value(conflines, "default_toast_compression", + "lz4", true); +#endif + /* * Now replace anything that's overridden via -c switches. */ diff --git a/src/include/access/toast_compression.h b/src/include/access/toast_compression.h index 5f3ffa9ab2d..3265f10b734 100644 --- a/src/include/access/toast_compression.h +++ b/src/include/access/toast_compression.h @@ -52,6 +52,15 @@ typedef enum ToastCompressionId #define CompressionMethodIsValid(cm) ((cm) != InvalidCompressionMethod) +/* + * Choose an appropriate default toast compression method. If lz4 is + * compiled-in, use it, otherwise use pglz. + */ +#ifdef USE_LZ4 +#define DEFAULT_TOAST_COMPRESSION TOAST_LZ4_COMPRESSION +#else +#define DEFAULT_TOAST_COMPRESSION TOAST_PGLZ_COMPRESSION +#endif /* pglz compression/decompression routines */ extern varlena *pglz_compress_datum(const varlena *value);