Add vacuum_truncate configuration parameter.
authorNathan Bossart <nathan@postgresql.org>
Thu, 20 Mar 2025 15:16:50 +0000 (10:16 -0500)
committerNathan Bossart <nathan@postgresql.org>
Thu, 20 Mar 2025 15:16:50 +0000 (10:16 -0500)
This new parameter works just like the storage parameter of the
same name: if set to true (which is the default), autovacuum and
VACUUM attempt to truncate any empty pages at the end of the table.
It is primarily intended to help users avoid locking issues on hot
standbys.  The setting can be overridden with the storage parameter
or VACUUM's TRUNCATE option.

Since there's presently no way to determine whether a Boolean
storage parameter is explicitly set or has just picked up the
default value, this commit also introduces an isset_offset member
to relopt_parse_elt.

Suggested-by: Will Storey <will@summercat.com>
Author: Nathan Bossart <nathandbossart@gmail.com>
Co-authored-by: Gurjeet Singh <gurjeet@singh.im>
Reviewed-by: Laurenz Albe <laurenz.albe@cybertec.at>
Reviewed-by: Fujii Masao <masao.fujii@oss.nttdata.com>
Reviewed-by: Robert Treat <rob@xzilla.net>
Discussion: https://postgr.es/m/Z2DE4lDX4tHqNGZt%40dev.null

13 files changed:
doc/src/sgml/config.sgml
doc/src/sgml/ref/create_table.sgml
doc/src/sgml/ref/vacuum.sgml
src/backend/access/common/reloptions.c
src/backend/commands/vacuum.c
src/backend/utils/misc/guc_tables.c
src/backend/utils/misc/postgresql.conf.sample
src/include/access/reloptions.h
src/include/commands/vacuum.h
src/include/utils/guc_tables.h
src/include/utils/rel.h
src/test/regress/expected/vacuum.out
src/test/regress/sql/vacuum.sql

index 873290daa613b38b22fded0cb456f60fa6e735d3..bdcefa8140b4982a0677b912a5af042cc9a461c7 100644 (file)
@@ -9311,6 +9311,35 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
      </note>
     </sect2>
 
+    <sect2 id="runtime-config-vacuum-default">
+     <title>Default Behavior</title>
+
+     <variablelist>
+      <varlistentry id="guc-vacuum-truncate" xreflabel="vacuum_truncate">
+       <term><varname>vacuum_truncate</varname> (<type>boolean</type>)
+       <indexterm>
+        <primary><varname>vacuum_truncate</varname> configuration parameter</primary>
+       </indexterm>
+       </term>
+       <listitem>
+        <para>
+         Enables or disables vacuum to try to truncate off any empty pages at
+         the end of the table.  The default value is <literal>true</literal>.
+         If <literal>true</literal>, <command>VACUUM</command> and autovacuum
+         do the truncation and the disk space for the truncated pages is
+         returned to the operating system.  Note that the truncation requires
+         an <literal>ACCESS EXCLUSIVE</literal> lock on the table.  The
+         <literal>TRUNCATE</literal> parameter of
+         <link linkend="sql-vacuum"><command>VACUUM</command></link>, if
+         specified, overrides the value of this parameter.  The setting can
+         also be overridden for individual tables by changing table storage
+         parameters.
+        </para>
+       </listitem>
+      </varlistentry>
+     </variablelist>
+    </sect2>
+
     <sect2 id="runtime-config-vacuum-freezing">
      <title>Freezing</title>
 
index 5304b7383225460a3d41a106a20c0840be89fddc..e5c034d724e488c64753d2b84f53403ac761abf1 100644 (file)
@@ -1692,15 +1692,10 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
     </term>
     <listitem>
      <para>
-      Enables or disables vacuum to try to truncate off any empty pages
-      at the end of this table. The default value is <literal>true</literal>.
-      If <literal>true</literal>, <command>VACUUM</command> and
-      autovacuum do the truncation and the disk space for
-      the truncated pages is returned to the operating system.
-      Note that the truncation requires <literal>ACCESS EXCLUSIVE</literal>
-      lock on the table. The <literal>TRUNCATE</literal> parameter
-      of <link linkend="sql-vacuum"><command>VACUUM</command></link>, if specified, overrides the value
-      of this option.
+      Per-table value for <xref linkend="guc-vacuum-truncate"/> parameter.  The
+      <literal>TRUNCATE</literal> parameter of
+      <link linkend="sql-vacuum"><command>VACUUM</command></link>, if
+      specified, overrides the value of this option.
      </para>
     </listitem>
    </varlistentry>
index 971b1237d47e0eed732178ce63b06a8a40c9bb82..bd5dcaf86a5ccccf5d821a723185312a713c4948 100644 (file)
@@ -265,7 +265,8 @@ VACUUM [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] [ <re
       truncate off any empty pages at the end of the table and allow
       the disk space for the truncated pages to be returned to
       the operating system. This is normally the desired behavior
-      and is the default unless the <literal>vacuum_truncate</literal>
+      and is the default unless <xref linkend="guc-vacuum-truncate"/>
+      is set to false or the <literal>vacuum_truncate</literal>
       option has been set to false for the table to be vacuumed.
       Setting this option to false may be useful to avoid
       <literal>ACCESS EXCLUSIVE</literal> lock on the table that
index 59fb53e770713f4df7e95923219581c88fa52069..645b5c00467d11ffb42f32fd404e22a89760a467 100644 (file)
@@ -1779,6 +1779,17 @@ fillRelOptions(void *rdopts, Size basesize,
                char       *itempos = ((char *) rdopts) + elems[j].offset;
                char       *string_val;
 
+               /*
+                * If isset_offset is provided, store whether the reloption is
+                * set there.
+                */
+               if (elems[j].isset_offset > 0)
+               {
+                   char       *setpos = ((char *) rdopts) + elems[j].isset_offset;
+
+                   *(bool *) setpos = options[i].isset;
+               }
+
                switch (options[i].gen->type)
                {
                    case RELOPT_TYPE_BOOL:
@@ -1901,7 +1912,7 @@ default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
        {"vacuum_index_cleanup", RELOPT_TYPE_ENUM,
        offsetof(StdRdOptions, vacuum_index_cleanup)},
        {"vacuum_truncate", RELOPT_TYPE_BOOL,
-       offsetof(StdRdOptions, vacuum_truncate)},
+       offsetof(StdRdOptions, vacuum_truncate), offsetof(StdRdOptions, vacuum_truncate_set)},
        {"vacuum_max_eager_freeze_failure_rate", RELOPT_TYPE_REAL,
        offsetof(StdRdOptions, vacuum_max_eager_freeze_failure_rate)}
    };
@@ -1981,6 +1992,7 @@ build_local_reloptions(local_relopts *relopts, Datum options, bool validate)
        elems[i].optname = opt->option->name;
        elems[i].opttype = opt->option->type;
        elems[i].offset = opt->offset;
+       elems[i].isset_offset = 0;  /* not supported for local relopts yet */
 
        i++;
    }
index e81c9a8aba3e1e7a078200d159ae113fe2027be1..f0a7b87808d1bf3a7c053bd4f8fdec764d82dadb 100644 (file)
@@ -78,6 +78,7 @@ int           vacuum_failsafe_age;
 int            vacuum_multixact_failsafe_age;
 double     vacuum_max_eager_freeze_failure_rate;
 bool       track_cost_delay_timing;
+bool       vacuum_truncate;
 
 /*
  * Variables for cost-based vacuum delay. The defaults differ between
@@ -2198,13 +2199,21 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
            ((StdRdOptions *) rel->rd_options)->vacuum_max_eager_freeze_failure_rate;
 
    /*
-    * Set truncate option based on truncate reloption if it wasn't specified
-    * in VACUUM command, or when running in an autovacuum worker
+    * Set truncate option based on truncate reloption or GUC if it wasn't
+    * specified in VACUUM command, or when running in an autovacuum worker
     */
    if (params->truncate == VACOPTVALUE_UNSPECIFIED)
    {
-       if (rel->rd_options == NULL ||
-           ((StdRdOptions *) rel->rd_options)->vacuum_truncate)
+       StdRdOptions *opts = (StdRdOptions *) rel->rd_options;
+
+       if (opts && opts->vacuum_truncate_set)
+       {
+           if (opts->vacuum_truncate)
+               params->truncate = VACOPTVALUE_ENABLED;
+           else
+               params->truncate = VACOPTVALUE_DISABLED;
+       }
+       else if (vacuum_truncate)
            params->truncate = VACOPTVALUE_ENABLED;
        else
            params->truncate = VACOPTVALUE_DISABLED;
index cc8f2b1230a2b56b04808efc7c42a64153c58427..97cfd6e5a822b0229830a5cf9c014025a4f4d0c4 100644 (file)
@@ -712,6 +712,7 @@ const char *const config_group_names[] =
    [STATS_CUMULATIVE] = gettext_noop("Statistics / Cumulative Query and Index Statistics"),
    [VACUUM_AUTOVACUUM] = gettext_noop("Vacuuming / Automatic Vacuuming"),
    [VACUUM_COST_DELAY] = gettext_noop("Vacuuming / Cost-Based Vacuum Delay"),
+   [VACUUM_DEFAULT] = gettext_noop("Vacuuming / Default Behavior"),
    [VACUUM_FREEZING] = gettext_noop("Vacuuming / Freezing"),
    [CLIENT_CONN_STATEMENT] = gettext_noop("Client Connection Defaults / Statement Behavior"),
    [CLIENT_CONN_LOCALE] = gettext_noop("Client Connection Defaults / Locale and Formatting"),
@@ -2131,6 +2132,15 @@ struct config_bool ConfigureNamesBool[] =
        NULL, NULL, NULL
    },
 
+   {
+       {"vacuum_truncate", PGC_USERSET, VACUUM_DEFAULT,
+           gettext_noop("Enables vacuum to truncate empty pages at the end of the table."),
+       },
+       &vacuum_truncate,
+       true,
+       NULL, NULL, NULL
+   },
+
    /* End-of-list marker */
    {
        {NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL, NULL
index ad54585cf1d0632d7a23f3b16d9ec200e912520b..9f31e4071c7b1221f34f4640ec0104880528361c 100644 (file)
@@ -714,6 +714,10 @@ autovacuum_worker_slots = 16   # autovacuum worker slots to allocate
 #vacuum_cost_page_dirty = 20       # 0-10000 credits
 #vacuum_cost_limit = 200       # 1-10000 credits
 
+# - Default Behavior -
+
+#vacuum_truncate = on          # enable truncation after vacuum
+
 # - Freezing -
 
 #vacuum_freeze_table_age = 150000000
index 43445cdcc6c087d7b8a9e0fa088a8b3528d3b340..146aed47c2d6e2a8e4673c1d8d8fe3ffb598e37f 100644 (file)
@@ -152,6 +152,7 @@ typedef struct
    const char *optname;        /* option's name */
    relopt_type opttype;        /* option's datatype */
    int         offset;         /* offset of field in result struct */
+   int         isset_offset;   /* if > 0, offset of "is set" field */
 } relopt_parse_elt;
 
 /* Local reloption definition */
index baacc63f590969e92b11314f08846ac45029ee05..bc37a80dc74fab6e021aa87642ce81e840a9f550 100644 (file)
@@ -304,6 +304,7 @@ extern PGDLLIMPORT int vacuum_multixact_freeze_table_age;
 extern PGDLLIMPORT int vacuum_failsafe_age;
 extern PGDLLIMPORT int vacuum_multixact_failsafe_age;
 extern PGDLLIMPORT bool track_cost_delay_timing;
+extern PGDLLIMPORT bool vacuum_truncate;
 
 /*
  * Relevant for vacuums implementing eager scanning. Normal vacuums may
index ab47145ec36c8b8b36fd71c7ad458c759f922e4d..f72ce944d7f9f2c1f17ba18b47ca253bc5be81bc 100644 (file)
@@ -89,6 +89,7 @@ enum config_group
    STATS_CUMULATIVE,
    VACUUM_AUTOVACUUM,
    VACUUM_COST_DELAY,
+   VACUUM_DEFAULT,
    VACUUM_FREEZING,
    CLIENT_CONN_STATEMENT,
    CLIENT_CONN_LOCALE,
index db3e504c3d2d9b96734581c47e6e0dc9db0d59dc..d94fddd7cef702f2c4494affd9dfea55c9415008 100644 (file)
@@ -344,6 +344,7 @@ typedef struct StdRdOptions
    int         parallel_workers;   /* max number of parallel workers */
    StdRdOptIndexCleanup vacuum_index_cleanup;  /* controls index vacuuming */
    bool        vacuum_truncate;    /* enables vacuum to truncate a relation */
+   bool        vacuum_truncate_set;    /* whether vacuum_truncate is set */
 
    /*
     * Fraction of pages in a relation that vacuum can eagerly scan and fail
index 3f91b69b32487eaf9d11192512a9b324acc41c5c..0abcc99989e0725db855d7d085177df9ff3d5377 100644 (file)
@@ -236,6 +236,7 @@ SELECT pg_relation_size('vac_truncate_test') > 0;
  t
 (1 row)
 
+SET vacuum_truncate = false;
 VACUUM (DISABLE_PAGE_SKIPPING) vac_truncate_test;
 SELECT pg_relation_size('vac_truncate_test') = 0;
  ?column? 
@@ -244,6 +245,32 @@ SELECT pg_relation_size('vac_truncate_test') = 0;
 (1 row)
 
 VACUUM (TRUNCATE FALSE, FULL TRUE) vac_truncate_test;
+ALTER TABLE vac_truncate_test RESET (vacuum_truncate);
+INSERT INTO vac_truncate_test VALUES (1, NULL), (NULL, NULL);
+ERROR:  null value in column "i" of relation "vac_truncate_test" violates not-null constraint
+DETAIL:  Failing row contains (null, null).
+VACUUM (DISABLE_PAGE_SKIPPING) vac_truncate_test;
+SELECT pg_relation_size('vac_truncate_test') > 0;
+ ?column? 
+----------
+ t
+(1 row)
+
+RESET vacuum_truncate;
+VACUUM (TRUNCATE FALSE, DISABLE_PAGE_SKIPPING) vac_truncate_test;
+SELECT pg_relation_size('vac_truncate_test') > 0;
+ ?column? 
+----------
+ t
+(1 row)
+
+VACUUM (DISABLE_PAGE_SKIPPING) vac_truncate_test;
+SELECT pg_relation_size('vac_truncate_test') = 0;
+ ?column? 
+----------
+ t
+(1 row)
+
 DROP TABLE vac_truncate_test;
 -- partitioned table
 CREATE TABLE vacparted (a int, b char) PARTITION BY LIST (a);
index 058add027f13e1145a4419c7f6e9f6de5c2fabb4..a72bdb5b619d94f9d3b76b2dc1df839f69cae897 100644 (file)
@@ -194,9 +194,19 @@ CREATE TEMP TABLE vac_truncate_test(i INT NOT NULL, j text)
 INSERT INTO vac_truncate_test VALUES (1, NULL), (NULL, NULL);
 VACUUM (TRUNCATE FALSE, DISABLE_PAGE_SKIPPING) vac_truncate_test;
 SELECT pg_relation_size('vac_truncate_test') > 0;
+SET vacuum_truncate = false;
 VACUUM (DISABLE_PAGE_SKIPPING) vac_truncate_test;
 SELECT pg_relation_size('vac_truncate_test') = 0;
 VACUUM (TRUNCATE FALSE, FULL TRUE) vac_truncate_test;
+ALTER TABLE vac_truncate_test RESET (vacuum_truncate);
+INSERT INTO vac_truncate_test VALUES (1, NULL), (NULL, NULL);
+VACUUM (DISABLE_PAGE_SKIPPING) vac_truncate_test;
+SELECT pg_relation_size('vac_truncate_test') > 0;
+RESET vacuum_truncate;
+VACUUM (TRUNCATE FALSE, DISABLE_PAGE_SKIPPING) vac_truncate_test;
+SELECT pg_relation_size('vac_truncate_test') > 0;
+VACUUM (DISABLE_PAGE_SKIPPING) vac_truncate_test;
+SELECT pg_relation_size('vac_truncate_test') = 0;
 DROP TABLE vac_truncate_test;
 
 -- partitioned table