From 4cd9317cdec9c30e97bee30b194c75455c0afc99 Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Mon, 3 Apr 2023 17:48:22 -0400
Subject: [PATCH v14 4/4] VACUUM reloads config file more often

A previous commit allowed autovacuum workers to reload the configuration
file more often. Now, enable this behavior for VACUUM and ANALYZE, when
not nested in a transaction command.

Note that, for now, this only applies to the primary VACUUM stage and
not to parallel vacuum workers vacuuming indexes.
---
 src/backend/commands/vacuum.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index a51a3f78a0..3c426ed501 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -84,6 +84,7 @@ int			vacuum_cost_limit;
  */
 int			VacuumCostLimit = 0;
 double		VacuumCostDelay = -1;
+static bool vacuum_can_reload_config = false;
 
 /*
  * VacuumFailsafeActive is a defined as a global so that we can determine
@@ -355,6 +356,8 @@ vacuum(List *relations, VacuumParams *params,
 	else
 		in_outer_xact = IsInTransactionBlock(isTopLevel);
 
+	vacuum_can_reload_config = !in_outer_xact;
+
 	/*
 	 * Check for and disallow recursive calls.  This could happen when VACUUM
 	 * FULL or ANALYZE calls a hostile index expression that itself calls
@@ -581,6 +584,7 @@ vacuum(List *relations, VacuumParams *params,
 		VacuumCostActive = false;
 		VacuumFailsafeActive = false;
 		VacuumCostBalance = 0;
+		vacuum_can_reload_config = false;
 	}
 	PG_END_TRY();
 
@@ -2253,10 +2257,12 @@ vacuum_delay_point(void)
 
 	/*
 	 * Reload the configuration file if requested. This allows changes to
-	 * autovacuum_vacuum_cost_limit and autovacuum_vacuum_cost_delay to take
-	 * effect while a table is being vacuumed or analyzed.
+	 * [autovacuum_]vacuum_cost_limit and [autovacuum}_vacuum_cost_delay to
+	 * take effect while a table is being vacuumed or analyzed. Analyze should
+	 * not reload configuration file if it is in an outer transaction, as we
+	 * currently only allow configuration reload when in top-level statements.
 	 */
-	if (ConfigReloadPending && IsAutoVacuumWorkerProcess())
+	if (ConfigReloadPending && vacuum_can_reload_config)
 	{
 		ConfigReloadPending = false;
 		ProcessConfigFile(PGC_SIGHUP);
-- 
2.37.2

