From 91b04fc00567fe4395c9820eb95e7ee3e10af2ae Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Fri, 5 Apr 2024 18:05:02 -0400
Subject: [PATCH v16 14/18] 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         | 9 ++++++++-
 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             | 8 --------
 5 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index a876a34b74..c35dc3c4e7 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -973,6 +973,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;
 
 	return (TableScanDesc) scan;
@@ -1078,6 +1081,8 @@ heap_endscan_bm(TableScanDesc sscan)
 	if (BufferIsValid(scan->vmbuffer))
 		ReleaseBuffer(scan->vmbuffer);
 
+	bhs_end_iterate(&scan->iterator);
+
 	/*
 	 * decrement relation reference count and free scan descriptor storage
 	 */
@@ -1396,6 +1401,8 @@ heap_rescan_bm(TableScanDesc sscan, TIDBitmap *tbm,
 		ReleaseBuffer(scan->vmbuffer);
 	scan->vmbuffer = InvalidBuffer;
 
+	bhs_end_iterate(&scan->iterator);
+
 	scan->empty_tuples_pending = 0;
 
 	/*
@@ -1403,7 +1410,7 @@ heap_rescan_bm(TableScanDesc sscan, TIDBitmap *tbm,
 	 */
 	initscan(&scan->heap_common, NULL, true);
 
-	bhs_begin_iterate(&scan->heap_common.rs_base.rs_bhs_iterator, tbm, dsa,
+	bhs_begin_iterate(&scan->iterator, tbm, dsa,
 					  pstate ?
 					  pstate->tbmiterator :
 					  InvalidDsaPointer);
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 43721cbd42..cc32d77100 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 = bhs_iterate(&sscan->rs_bhs_iterator);
+		tbmres = bhs_iterate(&scan->iterator);
 
 		if (tbmres == NULL)
 		{
diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h
index 7b44c94d29..e33e4433a7 100644
--- a/src/include/access/heapam.h
+++ b/src/include/access/heapam.h
@@ -92,6 +92,7 @@ typedef struct BitmapHeapScanDescData
 	/*
 	 * Members common to Parallel and Serial BitmapHeapScan
 	 */
+	BitmapHeapIterator 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 e520186b41..855b9558bf 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 */
-	BitmapHeapIterator 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 d02f5055b0..f344d97f1f 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -1020,10 +1020,6 @@ table_beginscan_tid(Relation rel, Snapshot snapshot)
 static inline void
 table_endscan(TableScanDesc scan)
 {
-	if (scan->rs_flags & SO_TYPE_BITMAPSCAN &&
-		!scan->rs_bhs_iterator.exhausted)
-		bhs_end_iterate(&scan->rs_bhs_iterator);
-
 	scan->rs_rd->rd_tableam->scan_end(scan);
 }
 
@@ -1034,10 +1030,6 @@ static inline void
 table_rescan(TableScanDesc scan,
 			 struct ScanKeyData *key)
 {
-	if (scan->rs_flags & SO_TYPE_BITMAPSCAN &&
-		!scan->rs_bhs_iterator.exhausted)
-		bhs_end_iterate(&scan->rs_bhs_iterator);
-
 	scan->rs_rd->rd_tableam->scan_rescan(scan, key, false, false, false, false);
 }
 
-- 
2.40.1

