From d6ae56bbbc060835c2d3a4606fcf788b72ee154d Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Thu, 13 Jun 2024 13:38:46 -0400
Subject: [PATCH v22 10/19] Add scan_in_progress to BitmapHeapScanState

Instead of checking that the scan descriptor hasn't been allocated yet,
use a dedicated variable, scan_in_progress. This feels cleaner and also
allows us to more easily extract the per scan setup into a separate
function.
---
 src/backend/executor/nodeBitmapHeapscan.c | 5 ++++-
 src/include/nodes/execnodes.h             | 2 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c
index 84ad3a92585..3774379cc69 100644
--- a/src/backend/executor/nodeBitmapHeapscan.c
+++ b/src/backend/executor/nodeBitmapHeapscan.c
@@ -141,7 +141,7 @@ BitmapHeapNext(BitmapHeapScanState *node)
 		 * If this is the first scan of the underlying table, create the table
 		 * scan descriptor and begin the scan.
 		 */
-		if (!scan)
+		if (!node->scan_in_progress)
 		{
 			bool		need_tuples = false;
 
@@ -163,8 +163,10 @@ BitmapHeapNext(BitmapHeapScanState *node)
 									  need_tuples);
 
 			node->scandesc = scan;
+			node->scan_in_progress = true;
 		}
 
+		Assert(scan);
 		tbm_begin_iterate(&scan->tbmiterator, node->tbm, dsa,
 						  pstate ?
 						  pstate->tbmiterator :
@@ -640,6 +642,7 @@ ExecInitBitmapHeapScan(BitmapHeapScan *node, EState *estate, int eflags)
 	scanstate->prefetch_pages = 0;
 	scanstate->prefetch_target = -1;
 	scanstate->initialized = false;
+	scanstate->scan_in_progress = false;
 	scanstate->pstate = NULL;
 	scanstate->recheck = true;
 	scanstate->blockno = InvalidBlockNumber;
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index ebba73d9b8b..5a9a187771a 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -1804,6 +1804,7 @@ typedef struct ParallelBitmapHeapState
  *		prefetch_target    current target prefetch distance
  *		prefetch_maximum   maximum value for prefetch_target
  *		initialized		   is node is ready to iterate
+ *		scan_in_progress   is this a rescan
  *		pstate			   shared state for parallel bitmap scan
  *		recheck			   do current page's tuples need recheck
  *		blockno			   used to validate pf and current block in sync
@@ -1823,6 +1824,7 @@ typedef struct BitmapHeapScanState
 	int			prefetch_target;
 	int			prefetch_maximum;
 	bool		initialized;
+	bool		scan_in_progress;
 	TBMIterator prefetch_iterator;
 	ParallelBitmapHeapState *pstate;
 	bool		recheck;
-- 
2.34.1

