autovacuum_disable_relation_truncation.v1.patch

application/x-patch

Filename: autovacuum_disable_relation_truncation.v1.patch
Type: application/x-patch
Part: 0
Message: Re: Disabling vacuum truncate for autovacuum

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: unified
Series: patch v1
File+
src/backend/postmaster/autovacuum.c 9 4
src/backend/utils/misc/guc_tables.c 10 0
src/include/postmaster/autovacuum.h 1 0
commit 84e8eebb87bc2c58feae847efd995bc055701688
Author: Gurjeet Singh <gurjeet@singh.im>
Date:   Thu Jan 23 19:37:33 2025 -0800

    Version 1

diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 0ab921a169..17e22c08e5 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -115,6 +115,7 @@
  * GUC parameters
  */
 bool		autovacuum_start_daemon = false;
+bool		autovacuum_disable_vacuum_truncate = false;
 int			autovacuum_worker_slots;
 int			autovacuum_max_workers;
 int			autovacuum_work_mem = -1;
@@ -2811,12 +2812,16 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map,
 			(!wraparound ? VACOPT_SKIP_LOCKED : 0);
 
 		/*
-		 * index_cleanup and truncate are unspecified at first in autovacuum.
-		 * They will be filled in with usable values using their reloptions
-		 * (or reloption defaults) later.
+		 * index_cleanup is unspecified at first in autovacuum. truncate is
+		 * unspecified, unless it is disabled via the GUC parameter.
+		 *
+		 * The unspecified options will be filled in with usable values using
+		 * their reloptions (or reloption defaults) later.
 		 */
 		tab->at_params.index_cleanup = VACOPTVALUE_UNSPECIFIED;
-		tab->at_params.truncate = VACOPTVALUE_UNSPECIFIED;
+		tab->at_params.truncate = autovacuum_disable_vacuum_truncate
+									? VACOPTVALUE_DISABLED
+									: VACOPTVALUE_UNSPECIFIED;
 		/* As of now, we don't support parallel vacuum for autovacuum */
 		tab->at_params.nworkers = -1;
 		tab->at_params.freeze_min_age = freeze_min_age;
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 38cb9e970d..40368e339c 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -1509,6 +1509,16 @@ struct config_bool ConfigureNamesBool[] =
 		NULL, NULL, NULL
 	},
 
+	{
+		{"autovacuum_disable_vacuum_truncate", PGC_SIGHUP, VACUUM_AUTOVACUUM,
+			gettext_noop("Disables autovacuum behavior of truncatiing relations."),
+			NULL
+		},
+		&autovacuum_disable_vacuum_truncate,
+		false,
+		NULL, NULL, NULL
+	},
+
 	{
 		{"trace_notify", PGC_USERSET, DEVELOPER_OPTIONS,
 			gettext_noop("Generates debugging output for LISTEN and NOTIFY."),
diff --git a/src/include/postmaster/autovacuum.h b/src/include/postmaster/autovacuum.h
index 54e01c81d6..c7597407d2 100644
--- a/src/include/postmaster/autovacuum.h
+++ b/src/include/postmaster/autovacuum.h
@@ -28,6 +28,7 @@ typedef enum
 
 /* GUC variables */
 extern PGDLLIMPORT bool autovacuum_start_daemon;
+extern PGDLLIMPORT bool autovacuum_disable_vacuum_truncate;
 extern PGDLLIMPORT int autovacuum_worker_slots;
 extern PGDLLIMPORT int autovacuum_max_workers;
 extern PGDLLIMPORT int autovacuum_work_mem;