postgres.git / summary / log / commit / refs

commit    34dfca293432e206b8f80431f81535aff69782ca
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Thu Mar 05 00:24:35 2026 +0000

    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 <euler@eulerto.com>
    Reviewed-by: Peter Eisentraut <peter@eisentraut.org>
    Reviewed-by: Aleksander Alekseev <aleksander@tigerdata.com>
    Discussion: https://posgr.es/m/435df33a-129e-4f0c-a803-f3935c5a5ecb@eisentraut.org


doc/src/sgml/config.sgml | 3 ++- src/backend/access/common/toast_compression.c | 2 +- src/backend/utils/misc/guc_parameters.dat | 2 +- src/backend/utils/misc/postgresql.conf.sample | 2 +- src/bin/initdb/initdb.c | 5 +++++ src/include/access/toast_compression.h | 9 +++++++++ 6 files changed, 19 insertions(+), 4 deletions(-) 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 <literal>pglz</literal> and (if <productname>PostgreSQL</productname> was compiled with <option>--with-lz4</option>) <literal>lz4</literal>. - The default is <literal>pglz</literal>. + The default is <literal>lz4</literal> (if available); otherwise, + <literal>pglz</literal>. </para> </listitem> </varlistentry> 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); [parent: 4f0b3afab4da]