From bf4777672024963eaf7582d23a7274f2261cd76b Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Sat, 6 Apr 2024 15:18:57 -0400
Subject: [PATCH v19 17/21] Move BitmapHeapScan current block iterator to
 BitmapHeapScanDesc

After beginning iteration in heap AM code, we can move the current block
iterator into the BitmapHeapScanDesc and take it out of the generic
TableScanDesc.

Author: Melanie Plageman
Discussion: https://postgr.es/m/CAAKRu_ZwCwWFeL_H3ia26bP2e7HiKLWt0ZmGXPVwPO6uXq0vaA%40mail.gmail.com
---
 src/backend/access/heap/heapam.c         | 13 ++++++-------
 src/backend/access/heap/heapam_handler.c |  2 +-
 src/include/access/heapam.h              |  1 +
 src/include/access/relscan.h             |  3 ---
 src/include/access/tableam.h             |  4 ----
 5 files changed, 8 insertions(+), 15 deletions(-)

diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 4fa4c29f90f..a45fdb50285 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -1049,9 +1049,6 @@ heap_beginscan_bm(Relation relation, Snapshot snapshot, uint32 flags)
 	scan->heap_common.rs_base.rs_parallel = NULL;
 	scan->heap_common.rs_strategy = NULL;
 
-	scan->heap_common.rs_base.rs_bhs_iterator.serial = NULL;
-	scan->heap_common.rs_base.rs_bhs_iterator.parallel = NULL;
-
 	Assert(snapshot && IsMVCCSnapshot(snapshot));
 
 	/* we only need to set this up once */
@@ -1062,6 +1059,9 @@ heap_beginscan_bm(Relation relation, Snapshot snapshot, uint32 flags)
 
 	initscan(&scan->heap_common, NULL, false);
 
+	scan->iterator.serial = NULL;
+	scan->iterator.parallel = NULL;
+
 	scan->vmbuffer = InvalidBuffer;
 	scan->empty_tuples_pending = 0;
 
@@ -1120,15 +1120,14 @@ heap_rescan_bm(TableScanDesc sscan, TIDBitmap *tbm,
 	scan->vmbuffer = InvalidBuffer;
 
 	scan->empty_tuples_pending = 0;
-
-	unified_tbm_end_iterate(&scan->heap_common.rs_base.rs_bhs_iterator);
+	unified_tbm_end_iterate(&scan->iterator);
 
 	/*
 	 * reinitialize heap scan descriptor
 	 */
 	initscan(&scan->heap_common, NULL, true);
 
-	unified_tbm_begin_iterate(&scan->heap_common.rs_base.rs_bhs_iterator, tbm, dsa,
+	unified_tbm_begin_iterate(&scan->iterator, tbm, dsa,
 							  pstate ?
 							  pstate->tbmiterator :
 							  InvalidDsaPointer);
@@ -1182,7 +1181,7 @@ heap_endscan_bm(TableScanDesc sscan)
 	if (BufferIsValid(scan->vmbuffer))
 		ReleaseBuffer(scan->vmbuffer);
 
-	unified_tbm_end_iterate(&scan->heap_common.rs_base.rs_bhs_iterator);
+	unified_tbm_end_iterate(&scan->iterator);
 
 	/*
 	 * decrement relation reference count and free scan descriptor storage
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index a2cbd7a24d6..15fc58f4fb7 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -2209,7 +2209,7 @@ heapam_scan_bitmap_next_block(TableScanDesc sscan,
 	{
 		CHECK_FOR_INTERRUPTS();
 
-		tbmres = unified_tbm_iterate(&sscan->rs_bhs_iterator);
+		tbmres = unified_tbm_iterate(&scan->iterator);
 
 		if (tbmres == NULL)
 		{
diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h
index 700205b9149..853a042c418 100644
--- a/src/include/access/heapam.h
+++ b/src/include/access/heapam.h
@@ -91,6 +91,7 @@ typedef struct BitmapHeapScanDescData
 	/*
 	 * Members common to Parallel and Serial BitmapHeapScan
 	 */
+	UnifiedTBMIterator iterator;
 
 	/*
 	 * These fields are only used for bitmap scans for the "skip fetch"
diff --git a/src/include/access/relscan.h b/src/include/access/relscan.h
index 7fec4ff8acb..149b0c1cdda 100644
--- a/src/include/access/relscan.h
+++ b/src/include/access/relscan.h
@@ -41,9 +41,6 @@ typedef struct TableScanDescData
 	ItemPointerData rs_mintid;
 	ItemPointerData rs_maxtid;
 
-	/* Only used for Bitmap table scans */
-	UnifiedTBMIterator rs_bhs_iterator;
-
 	/*
 	 * Information about type and behaviour of the scan, a bitmask of members
 	 * of the ScanOptions enum (see tableam.h).
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index e1df701c9a1..aebcecad30d 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -967,11 +967,7 @@ table_beginscan_bm(TableScanDesc scan, Relation rel, Snapshot snapshot,
 	 * scan descriptor and begin the scan.
 	 */
 	if (!scan)
-	{
 		scan = rel->rd_tableam->scan_begin_bm(rel, snapshot, flags);
-		scan->rs_bhs_iterator.serial = NULL;
-		scan->rs_bhs_iterator.parallel = NULL;
-	}
 
 	scan->rs_rd->rd_tableam->scan_rescan_bm(scan, tbm, pstate, dsa);
 
-- 
2.40.1

