From 868457ce9d1b227e2bc16b47ed268b63e2fbbf71 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tv@fuzzy.cz>
Date: Tue, 18 Jun 2024 23:39:17 +0200
Subject: [PATCH v22b 23/29] BitmapHeapScan: move per scan setup into a helper

Add BitmapTableScanSetup() which contains all of the code that must be
done on every scan of the table in a bitmap table scan.
This includes scanning the index, building the bitmap, and setting up
the scan descriptors.

Much of BitmapHeapNext() code was this setup, so it made sense to put it
in a helper function.
---
 src/backend/executor/nodeBitmapHeapscan.c | 233 +++++++++++-----------
 1 file changed, 115 insertions(+), 118 deletions(-)

diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c
index 79229f9f538..317cbda857a 100644
--- a/src/backend/executor/nodeBitmapHeapscan.c
+++ b/src/backend/executor/nodeBitmapHeapscan.c
@@ -53,153 +53,150 @@ static TupleTableSlot *BitmapHeapNext(BitmapHeapScanState *node);
 static inline void BitmapDoneInitializingSharedState(ParallelBitmapHeapState *pstate);
 static bool BitmapShouldInitializeSharedState(ParallelBitmapHeapState *pstate);
 
-
-/* ----------------------------------------------------------------
- *		BitmapHeapNext
- *
- *		Retrieve next tuple from the BitmapHeapScan node's currentRelation
- * ----------------------------------------------------------------
+/*
+ * Do the underlying index scan, build the bitmap, set up the parallel state
+ * needed for parallel workers to iterate through the bitmap, and set up the
+ * underlying table scan descriptor.
  */
-static TupleTableSlot *
-BitmapHeapNext(BitmapHeapScanState *node)
+static void
+BitmapTableScanSetup(BitmapHeapScanState *node)
 {
-	ExprContext *econtext;
-	BitmapTableScanDesc *scan;
-	TupleTableSlot *slot;
 	ParallelBitmapHeapState *pstate = node->pstate;
 	dsa_area   *dsa = node->ss.ps.state->es_query_dsa;
+	int			prefetch_maximum = 0;
 
 	/*
-	 * extract necessary information from index scan node
+	 * Maximum number of prefetches for the tablespace if configured,
+	 * otherwise the current value of the effective_io_concurrency GUC.
 	 */
-	econtext = node->ss.ps.ps_ExprContext;
-	slot = node->ss.ss_ScanTupleSlot;
-	scan = node->scandesc;
+#ifdef USE_PREFETCH
+	Relation	rel = node->ss.ss_currentRelation;
+
+	prefetch_maximum = get_tablespace_io_concurrency(rel->rd_rel->reltablespace);
+#endif
 
 	/*
-	 * If we haven't yet performed the underlying index scan, do it, and begin
-	 * the iteration over the bitmap.
+	 * Scan the index, build the bitmap, and set up shared state for parallel.
 	 */
-	if (!node->initialized)
+	if (!pstate)
 	{
-		int			prefetch_maximum = 0;
+		node->tbm = (TIDBitmap *) MultiExecProcNode(outerPlanState(node));
 
+		if (!node->tbm || !IsA(node->tbm, TIDBitmap))
+			elog(ERROR, "unrecognized result from subplan");
+	}
+	else if (BitmapShouldInitializeSharedState(pstate))
+	{
 		/*
-		 * Maximum number of prefetches for the tablespace if configured,
-		 * otherwise the current value of the effective_io_concurrency GUC.
-		 *
-		 * XXX Why do we protect this with the ifdef? The prefetches are
-		 * still protected by the same ifdef anyway, and we didn't do that
-		 * before this patch.
+		 * The leader will immediately come out of the function, but others
+		 * will be blocked until leader populates the TBM and wakes them up.
 		 */
-#ifdef USE_PREFETCH
-		Relation	rel = node->ss.ss_currentRelation;
+		node->tbm = (TIDBitmap *) MultiExecProcNode(outerPlanState(node));
+		if (!node->tbm || !IsA(node->tbm, TIDBitmap))
+			elog(ERROR, "unrecognized result from subplan");
 
-		prefetch_maximum = get_tablespace_io_concurrency(rel->rd_rel->reltablespace);
-#endif
+		/*
+		 * Two iterators are used -- one for the pages being scanned and one
+		 * for the blocks being prefetched.
+		 */
 
-		if (!pstate)
+		/*
+		 * Prepare to iterate over the TBM. This will return the dsa_pointer
+		 * of the iterator state which will be used by multiple processes to
+		 * iterate jointly.
+		 */
+		pstate->tbmiterator = tbm_prepare_shared_iterate(node->tbm);
+#ifdef USE_PREFETCH
+		if (prefetch_maximum > 0)
 		{
-			node->tbm = (TIDBitmap *) MultiExecProcNode(outerPlanState(node));
-
-			if (!node->tbm || !IsA(node->tbm, TIDBitmap))
-				elog(ERROR, "unrecognized result from subplan");
+			pstate->prefetch_iterator =
+				tbm_prepare_shared_iterate(node->tbm);
 		}
-		else if (BitmapShouldInitializeSharedState(pstate))
-		{
-			/*
-			 * The leader will immediately come out of the function, but
-			 * others will be blocked until leader populates the TBM and wakes
-			 * them up.
-			 */
-			node->tbm = (TIDBitmap *) MultiExecProcNode(outerPlanState(node));
-			if (!node->tbm || !IsA(node->tbm, TIDBitmap))
-				elog(ERROR, "unrecognized result from subplan");
-
-			/*
-			 * Two iterators are used -- one for the pages being scanned and
-			 * one for the blocks being prefetched.
-			 *
-			 * XXX Is this comment needed? The one a bit further down says
-			 * about the same thing, no?
-			 */
-
-			/*
-			 * Prepare to iterate over the TBM. This will return the
-			 * dsa_pointer of the iterator state which will be used by
-			 * multiple processes to iterate jointly.
-			 */
-			pstate->tbmiterator = tbm_prepare_shared_iterate(node->tbm);
-#ifdef USE_PREFETCH
-			if (prefetch_maximum > 0)
-			{
-				pstate->prefetch_iterator =
-					tbm_prepare_shared_iterate(node->tbm);
-			}
 #endif
-			/* We have initialized the shared state so wake up others. */
-			BitmapDoneInitializingSharedState(pstate);
-		}
+		/* We have initialized the shared state so wake up others. */
+		BitmapDoneInitializingSharedState(pstate);
+	}
+
+	/*
+	 * If this is the first scan of the underlying table, create the table
+	 * scan descriptor and begin the scan.
+	 */
+	if (!node->scan_in_progress)
+	{
+		bool		need_tuples = false;
 
 		/*
-		 * If this is the first scan of the underlying table, create the table
-		 * scan descriptor and begin the scan.
+		 * We can potentially skip fetching heap pages if we do not need any
+		 * columns of the table, either for checking non-indexable quals or
+		 * for returning data.  This test is a bit simplistic, as it checks
+		 * the stronger condition that there's no qual or return tlist at all.
+		 * But in most cases it's probably not worth working harder than that.
 		 */
-		if (!node->scan_in_progress)
-		{
-			bool		need_tuples = false;
+		need_tuples = (node->ss.ps.plan->qual != NIL ||
+					   node->ss.ps.plan->targetlist != NIL);
+
+		node->scandesc = table_beginscan_bm(node->ss.ss_currentRelation,
+											node->ss.ps.state->es_snapshot,
+											pstate,
+											need_tuples,
+											prefetch_maximum);
+		node->scan_in_progress = true;
+	}
+	else
+	{
+		Assert(node->scandesc);
+		tbm_end_iterate(&node->scandesc->iterator);
+		/* rescan to release any page pin */
+		table_rescan_bm(node->scandesc);
+	}
 
-			/*
-			 * We can potentially skip fetching heap pages if we do not need
-			 * any columns of the table, either for checking non-indexable
-			 * quals or for returning data.  This test is a bit simplistic, as
-			 * it checks the stronger condition that there's no qual or return
-			 * tlist at all. But in most cases it's probably not worth working
-			 * harder than that.
-			 */
-			need_tuples = (node->ss.ps.plan->qual != NIL ||
-						   node->ss.ps.plan->targetlist != NIL);
-
-			scan = table_beginscan_bm(node->ss.ss_currentRelation,
-									  node->ss.ps.state->es_snapshot,
-									  pstate,
-									  need_tuples,
-									  prefetch_maximum);
-			node->scandesc = scan;
-			node->scan_in_progress = true;
-		}
-		else
-		{
-			Assert(scan);
-			/* rescan to release any page pin */
-			tbm_end_iterate(&scan->iterator);
-			table_rescan_bm(scan);
-		}
+	tbm_begin_iterate(&node->scandesc->iterator, node->tbm, dsa,
+					  pstate ?
+					  pstate->tbmiterator :
+					  InvalidDsaPointer);
 
-		tbm_begin_iterate(&scan->iterator, node->tbm, dsa,
+	/*
+	 * We use *two* iterators, one for the pages we are actually scanning and
+	 * another that runs ahead of the first for prefetching.
+	 * scan->prefetch_pages tracks exactly how many pages ahead the prefetch
+	 * iterator is.  Also, scan->prefetch_target tracks the desired prefetch
+	 * distance, which starts small and increases up to the prefetch_maximum.
+	 * This is to avoid doing a lot of prefetching in a scan that stops after
+	 * a few tuples because of a LIMIT.
+	 */
+#ifdef USE_PREFETCH
+	if (prefetch_maximum > 0)
+		tbm_begin_iterate(&node->scandesc->prefetch_iterator, node->tbm, dsa,
 						  pstate ?
-						  pstate->tbmiterator :
+						  pstate->prefetch_iterator :
 						  InvalidDsaPointer);
-
-		/*
-		 * We use *two* iterators, one for the pages we are actually scanning
-		 * and another that runs ahead of the first for prefetching.
-		 * scan->prefetch_pages tracks exactly how many pages ahead the
-		 * prefetch iterator is.  Also, scan->prefetch_target tracks the
-		 * desired prefetch distance, which starts small and increases up to
-		 * the prefetch_maximum. This is to avoid doing a lot of prefetching
-		 * in a scan that stops after a few tuples because of a LIMIT.
-		 */
-#ifdef USE_PREFETCH
-		if (prefetch_maximum > 0)
-			tbm_begin_iterate(&scan->prefetch_iterator, node->tbm, dsa,
-							  pstate ?
-							  pstate->prefetch_iterator :
-							  InvalidDsaPointer);
 #endif							/* USE_PREFETCH */
 
-		node->initialized = true;
 
+	node->initialized = true;
+}
+
+/* ----------------------------------------------------------------
+ *		BitmapHeapNext
+ *
+ *		Retrieve next tuple from the BitmapHeapScan node's currentRelation
+ * ----------------------------------------------------------------
+ */
+static TupleTableSlot *
+BitmapHeapNext(BitmapHeapScanState *node)
+{
+	ExprContext *econtext = node->ss.ps.ps_ExprContext;
+	TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
+	BitmapTableScanDesc *scan = node->scandesc;
+
+	/*
+	 * If we haven't yet performed the underlying index scan, do it, and begin
+	 * the iteration over the bitmap.
+	 */
+	if (!node->initialized)
+	{
+		BitmapTableScanSetup(node);
+		scan = node->scandesc;
 		goto new_page;
 	}
 
-- 
2.45.2

