From 5fa08ede2171b68b5b4652743d6401e3f6652f98 Mon Sep 17 00:00:00 2001
From: Daniil Davidov <d.davydov@postgrespro.ru>
Date: Sun, 1 Mar 2026 21:36:00 +0700
Subject: [PATCH 3/3] fixes for 0004 patch

---
 src/backend/commands/vacuumparallel.c         | 33 ++++---------------
 src/backend/postmaster/autovacuum.c           | 18 +++++++---
 src/test/modules/test_autovacuum/meson.build  |  2 +-
 .../t/001_parallel_autovacuum.pl              | 26 ++-------------
 .../modules/test_autovacuum/test_autovacuum.c |  8 +----
 5 files changed, 26 insertions(+), 61 deletions(-)

diff --git a/src/backend/commands/vacuumparallel.c b/src/backend/commands/vacuumparallel.c
index 54210a4971d..1d352b44e44 100644
--- a/src/backend/commands/vacuumparallel.c
+++ b/src/backend/commands/vacuumparallel.c
@@ -301,10 +301,6 @@ static bool parallel_vacuum_index_is_parallel_safe(Relation indrel, int num_inde
 												   bool vacuum);
 static void parallel_vacuum_error_callback(void *arg);
 
-#ifdef USE_INJECTION_POINTS
-static inline void parallel_vacuum_report_cost_based_params(void);
-#endif
-
 /*
  * Try to enter parallel mode and create a parallel context.  Then initialize
  * shared memory state.
@@ -1327,7 +1323,13 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc)
 	 * allows tests to ensure this.
 	 */
 	if (shared->is_autovacuum)
-		parallel_vacuum_report_cost_based_params();
+		elog(DEBUG2,
+			 "parallel autovacuum worker cost params: cost_limit=%d, cost_delay=%g, cost_page_miss=%d, cost_page_dirty=%d, cost_page_hit=%d",
+			 vacuum_cost_limit,
+			 vacuum_cost_delay,
+			 VacuumCostPageMiss,
+			 VacuumCostPageDirty,
+			 VacuumCostPageHit);
 #endif
 
 	/* Report buffer/WAL usage during parallel execution */
@@ -1382,24 +1384,3 @@ parallel_vacuum_error_callback(void *arg)
 			return;
 	}
 }
-
-#ifdef USE_INJECTION_POINTS
-/*
- * Log values related to cost-based vacuum delay parameters. It is used for
- * testing purpose.
- */
-static inline void
-parallel_vacuum_report_cost_based_params(void)
-{
-	const char *msg_format =
-		_("Parallel autovacuum worker cost params: cost_limit=%d, cost_delay=%g, cost_page_miss=%d, cost_page_dirty=%d, cost_page_hit=%d");
-
-	elog(DEBUG2,
-		 msg_format,
-		 vacuum_cost_limit,
-		 vacuum_cost_delay,
-		 VacuumCostPageMiss,
-		 VacuumCostPageDirty,
-		 VacuumCostPageHit);
-}
-#endif
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 7b24a5d6e67..d17fc92e783 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -1409,7 +1409,18 @@ avl_sigusr2_handler(SIGNAL_ARGS)
 static void
 autovacuum_worker_before_shmem_exit(int code, Datum arg)
 {
+	int nreserved_old = av_nworkers_reserved;
+
 	AutoVacuumReleaseAllParallelWorkers();
+
+	if (nreserved_old > 0)
+	{
+		elog(DEBUG2,
+			 ngettext("autovacuum worker before_shmem_exit: %d parallel worker has been released",
+					  "autovacuum worker before_shmem_exit: %d parallel workers has been released",
+						nreserved_old - av_nworkers_reserved),
+			 nreserved_old - av_nworkers_reserved);
+	}
 }
 
 /*
@@ -3656,10 +3667,9 @@ adjust_free_parallel_workers(int prev_max_parallel_workers)
 	AutoVacuumShmem->av_freeParallelWorkers = Max(nfree_workers, 0);
 	AutoVacuumShmem->av_maxParallelWorkers = autovacuum_max_parallel_workers;
 
-	ereport(DEBUG2,
-			(errmsg("number of free parallel autovacuum workers is set to %u due to config reload",
-					AutoVacuumShmem->av_freeParallelWorkers),
-			 errhidecontext(true)));
+	elog(DEBUG2,
+		 "number of free parallel autovacuum workers is set to %u due to config reload",
+		 AutoVacuumShmem->av_freeParallelWorkers);
 
 	LWLockRelease(AutovacuumLock);
 }
diff --git a/src/test/modules/test_autovacuum/meson.build b/src/test/modules/test_autovacuum/meson.build
index 3441e5e49cf..75b24814b13 100644
--- a/src/test/modules/test_autovacuum/meson.build
+++ b/src/test/modules/test_autovacuum/meson.build
@@ -30,7 +30,7 @@ tests += {
        'enable_injection_points': get_option('injection_points') ? 'yes' : 'no',
     },
     'tests': [
-      't/001_basic.pl',
+      't/001_parallel_autovacuum.pl',
     ],
   },
 }
diff --git a/src/test/modules/test_autovacuum/t/001_parallel_autovacuum.pl b/src/test/modules/test_autovacuum/t/001_parallel_autovacuum.pl
index 9b80d371f5c..edfbde73aac 100644
--- a/src/test/modules/test_autovacuum/t/001_parallel_autovacuum.pl
+++ b/src/test/modules/test_autovacuum/t/001_parallel_autovacuum.pl
@@ -161,7 +161,7 @@ $node->wait_for_event(
 # Check whether parallel worker successfully updated all parameters during
 # index processing
 $log_start = $node->wait_for_log(
-	qr/Parallel autovacuum worker cost params: cost_limit=500, cost_delay=2, / .
+	qr/parallel autovacuum worker cost params: cost_limit=500, cost_delay=2, / .
 	qr/cost_page_miss=10, cost_page_dirty=10, cost_page_hit=10/,
 	$log_start
 );
@@ -264,20 +264,11 @@ $node->safe_psql('postgres', qq{
 prepare_for_next_test($node, 5);
 
 $node->safe_psql('postgres', qq{
-	SELECT injection_points_attach('autovacuum-start-parallel-vacuum', 'wait');
 	SELECT injection_points_attach('autovacuum-leader-before-indexes-processing', 'wait');
 	ALTER TABLE test_autovac SET (autovacuum_enabled = true);
 });
 
-# Wait until parallel autovacuum is inited and wake up the leader
-$node->wait_for_event(
-	'autovacuum worker',
-	'autovacuum-start-parallel-vacuum'
-);
-$node->safe_psql('postgres', qq{
-	SELECT injection_points_wakeup('autovacuum-start-parallel-vacuum');
-});
-
+# Wait until parallel workers are reserved autovacuum and kill the leader
 $node->wait_for_event(
 	'autovacuum worker',
 	'autovacuum-leader-before-indexes-processing'
@@ -295,23 +286,12 @@ $node->safe_psql('postgres', qq{
 });
 
 $log_start = $node->wait_for_log(
-	qr/terminating autovacuum process due to administrator command/,
+	qr/autovacuum worker before_shmem_exit: 2 parallel workers has been released/,
 	$log_start
 );
 
-# Now it is safe to check the number of free parallel workers, because even if
-# autovacuum is trying to vacuum table in parallel mode again, the leader
-# worker cannot go any further than "autovacuum-start-parallel-vacuum" point.
-# I.e. no one can interfere and change the number of free parallel workers.
-
-$psql_out = $node->safe_psql('postgres', qq{
-	SELECT get_parallel_autovacuum_free_workers();
-});
-is($psql_out, 10, 'All parallel workers has been released by the leader after FATAL');
-
 # Cleanup
 $node->safe_psql('postgres', qq{
-	SELECT injection_points_detach('autovacuum-start-parallel-vacuum');
 	SELECT injection_points_detach('autovacuum-leader-before-indexes-processing');
 });
 
diff --git a/src/test/modules/test_autovacuum/test_autovacuum.c b/src/test/modules/test_autovacuum/test_autovacuum.c
index 959629c7685..195a6149a5d 100644
--- a/src/test/modules/test_autovacuum/test_autovacuum.c
+++ b/src/test/modules/test_autovacuum/test_autovacuum.c
@@ -3,7 +3,7 @@
  * test_autovacuum.c
  *		Helpers to write tests for parallel autovacuum
  *
- * Copyright (c) 2020-2025, PostgreSQL Global Development Group
+ * Copyright (c) 2020-2026, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
  *	  src/test/modules/test_autovacuum/test_autovacuum.c
@@ -13,14 +13,8 @@
 
 #include "postgres.h"
 
-#include "commands/vacuum.h"
 #include "fmgr.h"
-#include "miscadmin.h"
 #include "postmaster/autovacuum.h"
-#include "storage/shmem.h"
-#include "storage/ipc.h"
-#include "storage/lwlock.h"
-#include "utils/builtins.h"
 #include "utils/injection_point.h"
 
 PG_MODULE_MAGIC;
-- 
2.43.0

