From eda66e24fbf9357e9c4af262ebb8e2e0d2c46ac0 Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <sawada.mshk@gmail.com>
Date: Fri, 13 Dec 2024 15:54:32 -0800
Subject: [PATCH v5 2/8] Remember the number of times parallel index
 vacuuming/cleanup is executed in ParallelVacuumState.

Previously, the caller can passes an arbitrary value for
'num_index_scans' to parallel index vacuuming or cleaning up APIs, but
it didn't make sense since if the caller needs to be careful about
counting how many times it executed index vacuuming or cleaning
up. Otherwise, it fails to reinitialize parallel DSM.

This commit changes parallel vacuum APIs so that ParallelVacuumState
has the counter num_index_scans and re-initialize parallel DSM based
on that.

An upcoming patch for parallel table scan will do a similar thing.

Author:
Reviewed-by:
Discussion: https://postgr.es/m/
Backpatch-through:
---
 src/backend/access/heap/vacuumlazy.c  |  4 +---
 src/backend/commands/vacuumparallel.c | 27 +++++++++++++++------------
 src/include/commands/vacuum.h         |  4 +---
 3 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index 05406a0bc5a..61b77af09b1 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -2143,8 +2143,7 @@ lazy_vacuum_all_indexes(LVRelState *vacrel)
 	else
 	{
 		/* Outsource everything to parallel variant */
-		parallel_vacuum_bulkdel_all_indexes(vacrel->pvs, old_live_tuples,
-											vacrel->num_index_scans);
+		parallel_vacuum_bulkdel_all_indexes(vacrel->pvs, old_live_tuples);
 
 		/*
 		 * Do a postcheck to consider applying wraparound failsafe now.  Note
@@ -2514,7 +2513,6 @@ lazy_cleanup_all_indexes(LVRelState *vacrel)
 	{
 		/* Outsource everything to parallel variant */
 		parallel_vacuum_cleanup_all_indexes(vacrel->pvs, reltuples,
-											vacrel->num_index_scans,
 											estimated_count);
 	}
 
diff --git a/src/backend/commands/vacuumparallel.c b/src/backend/commands/vacuumparallel.c
index 67cba17a564..50dd3d7d14d 100644
--- a/src/backend/commands/vacuumparallel.c
+++ b/src/backend/commands/vacuumparallel.c
@@ -200,6 +200,9 @@ struct ParallelVacuumState
 	 */
 	bool	   *will_parallel_vacuum;
 
+	/* How many time index vacuuming or cleaning up is executed? */
+	int			num_index_scans;
+
 	/*
 	 * The number of indexes that support parallel index bulk-deletion and
 	 * parallel index cleanup respectively.
@@ -223,8 +226,7 @@ struct ParallelVacuumState
 
 static int	parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested,
 											bool *will_parallel_vacuum);
-static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans,
-												bool vacuum);
+static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, bool vacuum);
 static void parallel_vacuum_process_safe_indexes(ParallelVacuumState *pvs);
 static void parallel_vacuum_process_unsafe_indexes(ParallelVacuumState *pvs);
 static void parallel_vacuum_process_one_index(ParallelVacuumState *pvs, Relation indrel,
@@ -497,8 +499,7 @@ parallel_vacuum_reset_dead_items(ParallelVacuumState *pvs)
  * Do parallel index bulk-deletion with parallel workers.
  */
 void
-parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, long num_table_tuples,
-									int num_index_scans)
+parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, long num_table_tuples)
 {
 	Assert(!IsParallelWorker());
 
@@ -509,7 +510,7 @@ parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, long num_table_tup
 	pvs->shared->reltuples = num_table_tuples;
 	pvs->shared->estimated_count = true;
 
-	parallel_vacuum_process_all_indexes(pvs, num_index_scans, true);
+	parallel_vacuum_process_all_indexes(pvs, true);
 }
 
 /*
@@ -517,7 +518,7 @@ parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, long num_table_tup
  */
 void
 parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, long num_table_tuples,
-									int num_index_scans, bool estimated_count)
+									bool estimated_count)
 {
 	Assert(!IsParallelWorker());
 
@@ -529,7 +530,7 @@ parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, long num_table_tup
 	pvs->shared->reltuples = num_table_tuples;
 	pvs->shared->estimated_count = estimated_count;
 
-	parallel_vacuum_process_all_indexes(pvs, num_index_scans, false);
+	parallel_vacuum_process_all_indexes(pvs, false);
 }
 
 /*
@@ -608,8 +609,7 @@ parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested,
  * must be used by the parallel vacuum leader process.
  */
 static void
-parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans,
-									bool vacuum)
+parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, bool vacuum)
 {
 	int			nworkers;
 	PVIndVacStatus new_status;
@@ -631,7 +631,7 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan
 		nworkers = pvs->nindexes_parallel_cleanup;
 
 		/* Add conditionally parallel-aware indexes if in the first time call */
-		if (num_index_scans == 0)
+		if (pvs->num_index_scans == 0)
 			nworkers += pvs->nindexes_parallel_condcleanup;
 	}
 
@@ -659,7 +659,7 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan
 		indstats->parallel_workers_can_process =
 			(pvs->will_parallel_vacuum[i] &&
 			 parallel_vacuum_index_is_parallel_safe(pvs->indrels[i],
-													num_index_scans,
+													pvs->num_index_scans,
 													vacuum));
 	}
 
@@ -670,7 +670,7 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan
 	if (nworkers > 0)
 	{
 		/* Reinitialize parallel context to relaunch parallel workers */
-		if (num_index_scans > 0)
+		if (pvs->num_index_scans > 0)
 			ReinitializeParallelDSM(pvs->pcxt);
 
 		/*
@@ -764,6 +764,9 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan
 		VacuumSharedCostBalance = NULL;
 		VacuumActiveNWorkers = NULL;
 	}
+
+	/* Increment the counter */
+	pvs->num_index_scans++;
 }
 
 /*
diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h
index 759f9a87d38..7613d00e26f 100644
--- a/src/include/commands/vacuum.h
+++ b/src/include/commands/vacuum.h
@@ -366,11 +366,9 @@ extern TidStore *parallel_vacuum_get_dead_items(ParallelVacuumState *pvs,
 												VacDeadItemsInfo **dead_items_info_p);
 extern void parallel_vacuum_reset_dead_items(ParallelVacuumState *pvs);
 extern void parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs,
-												long num_table_tuples,
-												int num_index_scans);
+												long num_table_tuples);
 extern void parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs,
 												long num_table_tuples,
-												int num_index_scans,
 												bool estimated_count);
 extern void parallel_vacuum_main(dsm_segment *seg, shm_toc *toc);
 
-- 
2.43.5

