]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Change default value of default_toast_compression to "lz4", when available
authorMichael Paquier <michael@paquier.xyz>
Wed, 4 Mar 2026 04:05:31 +0000 (13:05 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 4 Mar 2026 04:05:31 +0000 (13:05 +0900)
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.

--with-lz4 is removed, replaced by a --without-lz4 to disable LZ4 in the
builds on an option-basis, following a practice similar to readline or
ICU.  References to --with-lz4 are removed from the documentation.

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.

For the reference, a similar switch has been done with ICU in
fcb21b3acdcb.  Some of the changes done in this commit are consistent
with that.

Note: this is going to create some disturbance in the buildfarm, in
environments where lz4 is not installed.

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

13 files changed:
.cirrus.tasks.yml
configure
configure.ac
doc/src/sgml/config.sgml
doc/src/sgml/installation.sgml
doc/src/sgml/ref/alter_table.sgml
doc/src/sgml/ref/create_table.sgml
doc/src/sgml/ref/pg_receivewal.sgml
src/backend/access/common/toast_compression.c
src/backend/utils/misc/guc_parameters.dat
src/backend/utils/misc/postgresql.conf.sample
src/bin/initdb/initdb.c
src/include/access/toast_compression.h

index 4841a2042486ab2d959bc15d10b2f0cceae95e9c..a3107f827305242f26497335a476350a0ca3e31e 100644 (file)
@@ -984,6 +984,7 @@ task:
         --host=x86_64-w64-mingw32ucrt \
         --enable-cassert \
         --without-icu \
+        --without-lz4 \
         CC="ccache x86_64-w64-mingw32ucrt-gcc" \
         CXX="ccache x86_64-w64-mingw32ucrt-g++"
       make -s -j${BUILD_JOBS} clean
index b30b98a586e72d7f6135c20c484d22f7380eaaae..7afd8ba01e847b17d5d64dded2494af0842c2d53 100755 (executable)
--- a/configure
+++ b/configure
@@ -1603,7 +1603,7 @@ Optional Packages:
   --with-system-tzdata=DIR
                           use system time zone data in DIR
   --without-zlib          do not use Zlib
-  --with-lz4              build with LZ4 support
+  --without-lz4           build without LZ4 support
   --with-zstd             build with ZSTD support
   --with-ssl=LIB          use LIB for SSL/TLS support (openssl)
   --with-openssl          obsolete spelling of --with-ssl=openssl
@@ -9570,7 +9570,9 @@ $as_echo "#define USE_LZ4 1" >>confdefs.h
   esac
 
 else
-  with_lz4=no
+  with_lz4=yes
+
+$as_echo "#define USE_LZ4 1" >>confdefs.h
 
 fi
 
index f4e3bd307c8425e923a20aded4f3cb19af147e76..60d1c1900f2d36c420251e9705240dce30a5d02c 100644 (file)
@@ -1188,7 +1188,7 @@ AC_SUBST(with_zlib)
 # LZ4
 #
 AC_MSG_CHECKING([whether to build with LZ4 support])
-PGAC_ARG_BOOL(with, lz4, no, [build with LZ4 support],
+PGAC_ARG_BOOL(with, lz4, yes, [build without LZ4 support],
               [AC_DEFINE([USE_LZ4], 1, [Define to 1 to build with LZ4 support. (--with-lz4)])])
 AC_MSG_RESULT([$with_lz4])
 AC_SUBST(with_lz4)
index f670e2d4c31d2dd2a40e04c9c82c9b1721493544..6779bc844d0058ddd032d46c41cb6375303039f4 100644 (file)
@@ -3498,10 +3498,9 @@ include_dir 'conf.d'
         etc.).
         A compressed page image will be decompressed during WAL replay.
         The supported methods are <literal>pglz</literal>,
-        <literal>lz4</literal> (if <productname>PostgreSQL</productname>
-        was compiled with <option>--with-lz4</option>) and
-        <literal>zstd</literal> (if <productname>PostgreSQL</productname>
-        was compiled with <option>--with-zstd</option>).
+        <literal>lz4</literal> and <literal>zstd</literal> (if
+        <productname>PostgreSQL</productname> was compiled with
+        <option>--with-zstd</option>).
         The default value is <literal>off</literal>.
         Only superusers and users with the appropriate <literal>SET</literal>
         privilege can change this setting.
@@ -10018,9 +10017,9 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
         <command>CREATE TABLE</command> or
         <command>ALTER TABLE</command>.)
         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>.
+        <literal>lz4</literal>.
+        The default is <literal>lz4</literal> (if available); otherwise,
+        <literal>pglz</literal>.
        </para>
       </listitem>
      </varlistentry>
index b345a1056740a04d3a56a76040db9eb591d63993..a026ffea68d9e8a39f3f4ce35c715a354fda91dd 100644 (file)
@@ -975,15 +975,6 @@ build-postgresql:
        </listitem>
       </varlistentry>
 
-      <varlistentry id="configure-option-with-lz4">
-       <term><option>--with-lz4</option></term>
-       <listitem>
-        <para>
-         Build with <productname>LZ4</productname> compression support.
-        </para>
-       </listitem>
-      </varlistentry>
-
       <varlistentry id="configure-option-with-zstd">
        <term><option>--with-zstd</option></term>
        <listitem>
@@ -1315,6 +1306,18 @@ build-postgresql:
        </listitem>
       </varlistentry>
 
+      <varlistentry id="configure-option-without-lz4">
+       <term><option>--without-lz4</option></term>
+       <listitem>
+        <para>
+         <indexterm>
+          <primary>lz4</primary>
+         </indexterm>
+         Prevents use of the <application>LZ4</application> library.
+        </para>
+       </listitem>
+      </varlistentry>
+
      </variablelist>
 
    </sect3>
index aab2c6eb19ff168e0448200aa7ceed1bd0cd2565..9d2c87b660aecaabaebb6c3d90a6874abd3c53a5 100644 (file)
@@ -462,9 +462,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
       its existing compression method, rather than being recompressed with the
       compression method of the target column.
       The supported compression
-      methods are <literal>pglz</literal> and <literal>lz4</literal>.
-      (<literal>lz4</literal> is available only if <option>--with-lz4</option>
-      was used when building <productname>PostgreSQL</productname>.)  In
+      methods are <literal>pglz</literal> and <literal>lz4</literal>.  In
       addition, <replaceable class="parameter">compression_method</replaceable>
       can be <literal>default</literal>, which selects the default behavior of
       consulting the <xref linkend="guc-default-toast-compression"/> setting
index 982532fe7253336a0fa83bd17d7b6b2c96ee03c7..188bc11e940071b634f3b74771f9060be2eb765e 100644 (file)
@@ -344,9 +344,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
       has no direct effect, because such tables have no storage of their own,
       but the configured value will be inherited by newly-created partitions.
       The supported compression methods are <literal>pglz</literal> and
-      <literal>lz4</literal>.  (<literal>lz4</literal> is available only if
-      <option>--with-lz4</option> was used when building
-      <productname>PostgreSQL</productname>.)  In addition,
+      <literal>lz4</literal>.  In addition,
       <replaceable class="parameter">compression_method</replaceable>
       can be <literal>default</literal> to explicitly specify the default
       behavior, which is to consult the
index 9e353a068e5a3e150049af45bbc096fdde5135c4..1896f4da0e844cf8aa5b81dde3c44b41f1cc7e72 100644 (file)
@@ -275,9 +275,7 @@ PostgreSQL documentation
        </para>
        <para>
         The compression method can be set to <literal>gzip</literal>,
-        <literal>lz4</literal> (if <productname>PostgreSQL</productname>
-        was compiled with <option>--with-lz4</option>) or
-        <literal>none</literal> for no compression.
+        <literal>lz4</literal> or <literal>none</literal> for no compression.
         A compression detail string can optionally be specified.  If the
         detail string is an integer, it specifies the compression level.
         Otherwise, it should be a comma-separated list of items, each of the
index 4d00537049ec1c77d4a09a199bc8f5c863b5af95..5a5d579494a2311065a4877f9d8eaff18d40ec0d 100644 (file)
@@ -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, \
index 9507778415da1a746c0e4a2e1e7038e8b4c6584b..5ee84a639d828b846fff0cd9c68938058bb261ae 100644 (file)
 { 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',
 },
 
index f938cc65a3aa96915187c5790bad11a8bc8ac106..e686d88afc427982ef40bd491f024290bcc1906e 100644 (file)
 #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
index 53ec1544ff3675b337ad14b0f96591582a83fdc6..6387c66289e2640a8e87e46a9d32c5c58f1453ab 100644 (file)
@@ -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.
         */
index 5f3ffa9ab2d46eb7e7383bb9569f336ae42d33ff..3265f10b734f46be1bafdc5bdbaa24a1bc92bcbe 100644 (file)
@@ -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);