From d16551c7330822ccd0132565773ceab7569aa6b5 Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Fri, 5 Apr 2024 17:48:08 -0400
Subject: [PATCH v16 13/18] BitmapHeapScan begin iteration in rescan

Now that BitmapHeapScan has its own dedicated rescan function, it is
easy to begin iteration there instead. Doing this in heap AM code will
allow us to move the current block iterator from the TableScanDesc to
the BitmapHeapScanDesc.

Author: Melanie Plageman
Discussion: https://postgr.es/m/CAAKRu_ZwCwWFeL_H3ia26bP2e7HiKLWt0ZmGXPVwPO6uXq0vaA%40mail.gmail.com
---
 src/backend/access/heap/heapam.c          |  8 +++++++-
 src/backend/executor/nodeBitmapHeapscan.c | 10 ++++------
 src/include/access/heapam.h               |  3 ++-
 src/include/access/tableam.h              |  8 +++++---
 4 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 82faf447e9..a876a34b74 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -1384,7 +1384,8 @@ heap_getnextslot_tidrange(TableScanDesc sscan, ScanDirection direction,
 }
 
 void
-heap_rescan_bm(TableScanDesc sscan)
+heap_rescan_bm(TableScanDesc sscan, TIDBitmap *tbm,
+			   ParallelBitmapHeapState *pstate, dsa_area *dsa)
 {
 	BitmapHeapScanDesc scan = (BitmapHeapScanDesc) sscan;
 
@@ -1401,6 +1402,11 @@ heap_rescan_bm(TableScanDesc sscan)
 	 * reinitialize heap scan descriptor
 	 */
 	initscan(&scan->heap_common, NULL, true);
+
+	bhs_begin_iterate(&scan->heap_common.rs_base.rs_bhs_iterator, tbm, dsa,
+					  pstate ?
+					  pstate->tbmiterator :
+					  InvalidDsaPointer);
 }
 
 
diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c
index 4f7138bbd9..f8627991d0 100644
--- a/src/backend/executor/nodeBitmapHeapscan.c
+++ b/src/backend/executor/nodeBitmapHeapscan.c
@@ -205,15 +205,13 @@ BitmapHeapNext(BitmapHeapScanState *node)
 		scan = table_beginscan_bm(node->ss.ss_currentScanDesc,
 								  node->ss.ss_currentRelation,
 								  node->ss.ps.state->es_snapshot,
-								  extra_flags);
+								  extra_flags,
+								  node->tbm,
+								  node->pstate,
+								  node->ss.ps.state->es_query_dsa);
 
 		node->ss.ss_currentScanDesc = scan;
 
-		bhs_begin_iterate(&scan->rs_bhs_iterator, tbm, dsa,
-						  pstate ?
-						  pstate->tbmiterator :
-						  InvalidDsaPointer);
-
 #ifdef USE_PREFETCH
 		if (node->prefetch_maximum > 0)
 		{
diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h
index 545760aab5..7b44c94d29 100644
--- a/src/include/access/heapam.h
+++ b/src/include/access/heapam.h
@@ -305,7 +305,8 @@ extern bool heap_getnextslot_tidrange(TableScanDesc sscan,
 extern TableScanDesc heap_beginscan_bm(Relation relation, Snapshot snapshot, uint32 flags);
 
 extern void heap_endscan_bm(TableScanDesc scan);
-extern void heap_rescan_bm(TableScanDesc sscan);
+extern void heap_rescan_bm(TableScanDesc sscan, TIDBitmap *tbm,
+						   ParallelBitmapHeapState *pstate, dsa_area *dsa);
 
 extern bool heap_fetch(Relation relation, Snapshot snapshot,
 					   HeapTuple tuple, Buffer *userbuf, bool keep_buf);
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index 3b0fa0610b..d02f5055b0 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -398,7 +398,8 @@ typedef struct TableAmRoutine
 									Snapshot snapshot,
 									uint32 flags);
 
-	void		(*scan_rescan_bm) (TableScanDesc scan);
+	void		(*scan_rescan_bm) (TableScanDesc scan, TIDBitmap *tbm,
+								   ParallelBitmapHeapState *pstate, dsa_area *dsa);
 
 	/*
 	 * Release resources and deallocate scan. If TableScanDesc.temp_snap,
@@ -949,7 +950,8 @@ table_beginscan_strat(Relation rel, Snapshot snapshot,
  */
 static inline TableScanDesc
 table_beginscan_bm(TableScanDesc scan, Relation rel, Snapshot snapshot,
-				   uint32 extra_flags)
+				   uint32 extra_flags, TIDBitmap *tbm,
+				   ParallelBitmapHeapState *pstate, dsa_area *dsa)
 {
 	uint32		flags = SO_TYPE_BITMAPSCAN | SO_ALLOW_PAGEMODE | extra_flags;
 
@@ -960,7 +962,7 @@ table_beginscan_bm(TableScanDesc scan, Relation rel, Snapshot snapshot,
 	if (!scan)
 		scan = rel->rd_tableam->scan_begin_bm(rel, snapshot, flags);
 
-	scan->rs_rd->rd_tableam->scan_rescan_bm(scan);
+	scan->rs_rd->rd_tableam->scan_rescan_bm(scan, tbm, pstate, dsa);
 
 	return scan;
 }
-- 
2.40.1

