From 5c392b74794065b57adfc02afad0863be849a392 Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Wed, 9 Oct 2024 15:25:03 -0400
Subject: [PATCH v24 10/12] Move BitmapTableScan 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 | 220 +++++++++++-----------
 1 file changed, 106 insertions(+), 114 deletions(-)

diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c
index 21ceff5310..ca22cd4196 100644
--- a/src/backend/executor/nodeBitmapHeapscan.c
+++ b/src/backend/executor/nodeBitmapHeapscan.c
@@ -54,150 +54,142 @@ static inline void BitmapDoneInitializingSharedState(ParallelBitmapHeapState *ps
 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;
-	TableScanDesc scan;
-	TIDBitmap  *tbm;
-	TupleTableSlot *slot;
+	TBMIterator tbmiterator = {0};
+	TBMIterator prefetch_iterator = {0};
+	int			prefetch_maximum;
 	ParallelBitmapHeapState *pstate = node->pstate;
 	dsa_area   *dsa = node->ss.ps.state->es_query_dsa;
+	Relation	rel = node->ss.ss_currentRelation;
 
 	/*
-	 * 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->ss.ss_currentScanDesc;
-	tbm = node->tbm;
+	prefetch_maximum = get_tablespace_io_concurrency(rel->rd_rel->reltablespace);
 
-	/*
-	 * If we haven't yet performed the underlying index scan, do it, and begin
-	 * the iteration over the bitmap.
-	 *
-	 * For prefetching, we use *two* iterators, one for the pages we are
-	 * actually scanning and another that runs ahead of the first for
-	 * prefetching.  node->prefetch_pages tracks exactly how many pages ahead
-	 * the prefetch iterator is.  Also, node->prefetch_target tracks the
-	 * desired prefetch distance, which starts small and increases up to the
-	 * node->prefetch_maximum.  This is to avoid doing a lot of prefetching in
-	 * a scan that stops after a few tuples because of a LIMIT.
-	 */
-	if (!node->initialized)
+	if (!pstate)
 	{
-		TBMIterator tbmiterator;
-		int			prefetch_maximum;
-		Relation	rel = node->ss.ss_currentRelation;
-#ifdef USE_PREFETCH
-		TBMIterator prefetch_iterator = {0};
-#endif							/* USE_PREFETCH */
+		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.
+		 * The leader will immediately come out of the function, but others
+		 * will be blocked until leader populates the TBM and wakes them up.
 		 */
-		prefetch_maximum = get_tablespace_io_concurrency(rel->rd_rel->reltablespace);
+		node->tbm = (TIDBitmap *) MultiExecProcNode(outerPlanState(node));
+		if (!node->tbm || !IsA(node->tbm, TIDBitmap))
+			elog(ERROR, "unrecognized result from subplan");
 
-		if (!pstate)
-		{
-			tbm = (TIDBitmap *) MultiExecProcNode(outerPlanState(node));
+		/*
+		 * 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);
 
-			if (!tbm || !IsA(tbm, TIDBitmap))
-				elog(ERROR, "unrecognized result from subplan");
+		/*
+		 * For prefetching, we use *two* iterators, one for the pages we are
+		 * actually scanning and another that runs ahead of the first for
+		 * prefetching.  node->prefetch_pages tracks exactly how many pages
+		 * ahead the prefetch iterator is.  Also, node->prefetch_target tracks
+		 * the desired prefetch distance, which starts small and increases up
+		 * to the node->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)
+			pstate->prefetch_iterator =
+				tbm_prepare_shared_iterate(node->tbm);
+#endif
 
-			node->tbm = 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.
-			 */
-			tbm = (TIDBitmap *) MultiExecProcNode(outerPlanState(node));
-			if (!tbm || !IsA(tbm, TIDBitmap))
-				elog(ERROR, "unrecognized result from subplan");
+		/* We have initialized the shared state so wake up others. */
+		BitmapDoneInitializingSharedState(pstate);
+	}
 
-			node->tbm = tbm;
-
-			/*
-			 * 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(tbm);
+	tbmiterator = tbm_begin_iterate(node->tbm, dsa,
+									pstate ?
+									pstate->tbmiterator :
+									InvalidDsaPointer);
 
 #ifdef USE_PREFETCH
-			if (prefetch_maximum > 0)
-			{
-				pstate->prefetch_iterator =
-					tbm_prepare_shared_iterate(tbm);
-			}
+	if (prefetch_maximum > 0)
+		prefetch_iterator = tbm_begin_iterate(node->tbm, dsa,
+											  pstate ?
+											  pstate->prefetch_iterator :
+											  InvalidDsaPointer);
 #endif							/* USE_PREFETCH */
 
-			/* We have initialized the shared state so wake up others. */
-			BitmapDoneInitializingSharedState(pstate);
-		}
-
-		tbmiterator = tbm_begin_iterate(tbm, dsa,
-										pstate ?
-										pstate->tbmiterator :
-										InvalidDsaPointer);
-#ifdef USE_PREFETCH
-		if (prefetch_maximum > 0)
-			prefetch_iterator = tbm_begin_iterate(tbm, dsa,
-												  pstate ?
-												  pstate->prefetch_iterator :
-												  InvalidDsaPointer);
-#endif							/* USE_PREFETCH */
+	/*
+	 * If this is the first scan of the underlying table, create the table
+	 * scan descriptor and begin the scan.
+	 */
+	if (!node->ss.ss_currentScanDesc)
+	{
+		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 (!scan)
-		{
-			bool		need_tuples = false;
+		need_tuples = (node->ss.ps.plan->qual != NIL ||
+					   node->ss.ps.plan->targetlist != NIL);
+
+		node->ss.ss_currentScanDesc = table_beginscan_bm(node->ss.ss_currentRelation,
+														 node->ss.ps.state->es_snapshot,
+														 pstate,
+														 0,
+														 NULL,
+														 need_tuples,
+														 prefetch_maximum);
+	}
 
-			/*
-			 * 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,
-									  0,
-									  NULL,
-									  need_tuples,
-									  prefetch_maximum);
-
-			node->ss.ss_currentScanDesc = scan;
-		}
+	node->ss.ss_currentScanDesc->st.bts.tbmiterator = tbmiterator;
+	node->ss.ss_currentScanDesc->st.bts.prefetch_iterator = prefetch_iterator;
+	node->initialized = true;
+}
 
-		scan->st.bts.tbmiterator = tbmiterator;
-		scan->st.bts.prefetch_iterator = prefetch_iterator;
-		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;
 
+	/*
+	 * If we haven't yet performed the underlying index scan, do it, and begin
+	 * the iteration over the bitmap.
+	 */
+	if (!node->initialized)
+	{
+		/* BitmapTableScanSetup sets node->ss.ss_currentScanDesc */
+		BitmapTableScanSetup(node);
 		goto new_page;
 	}
 
 	for (;;)
 	{
-		while (table_scan_bitmap_next_tuple(scan, slot))
+		while (table_scan_bitmap_next_tuple(node->ss.ss_currentScanDesc, slot))
 		{
 			/*
 			 * Continuing in previously obtained page.
@@ -231,7 +223,7 @@ new_page:
 		 * Returns false if the bitmap is exhausted and there are no further
 		 * blocks we need to scan.
 		 */
-		if (!table_scan_bitmap_next_block(scan, &node->recheck,
+		if (!table_scan_bitmap_next_block(node->ss.ss_currentScanDesc, &node->recheck,
 										  &node->stats.lossy_pages, &node->stats.exact_pages))
 			break;
 	}
-- 
2.45.2

