From efbb311eddc765dd761154e1460e337fc2d29323 Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Tue, 13 Feb 2024 10:05:04 -0500
Subject: [PATCH v3 06/13] BitmapHeapScan scan desc counts lossy and exact
 pages

Future commits will remove the TBMIterateResult from BitmapHeapNext(),
pushing it into the table AM-specific code. So we will have to keep
track of the number of lossy and exact pages in the scan descriptor.
Doing this change to lossy/exact page counting in a separate commit just
simplifies the diff.
---
 src/backend/access/heap/heapam_handler.c  |  9 +++++++++
 src/backend/executor/nodeBitmapHeapscan.c | 19 ++++++++++++++-----
 src/include/access/relscan.h              |  4 ++++
 src/include/access/tableam.h              |  6 +++++-
 4 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 7661acac3a8..9fc99a87fdf 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -2242,6 +2242,15 @@ heapam_scan_bitmap_next_block(TableScanDesc scan,
 	Assert(ntup <= MaxHeapTuplesPerPage);
 	hscan->rs_ntuples = ntup;
 
+	/* Only count exact and lossy pages with visible tuples */
+	if (ntup > 0)
+	{
+		if (tbmres->ntuples >= 0)
+			scan->exact_pages++;
+		else
+			scan->lossy_pages++;
+	}
+
 	return ntup > 0;
 }
 
diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c
index 3439c02e989..eee90b8785b 100644
--- a/src/backend/executor/nodeBitmapHeapscan.c
+++ b/src/backend/executor/nodeBitmapHeapscan.c
@@ -53,6 +53,8 @@
 #include "utils/spccache.h"
 
 static TupleTableSlot *BitmapHeapNext(BitmapHeapScanState *node);
+static inline void BitmapAccumCounters(BitmapHeapScanState *node,
+									   TableScanDesc scan);
 static inline void BitmapDoneInitializingSharedState(ParallelBitmapHeapState *pstate);
 static inline void BitmapAdjustPrefetchIterator(BitmapHeapScanState *node,
 												BlockNumber blockno);
@@ -250,11 +252,6 @@ BitmapHeapNext(BitmapHeapScanState *node)
 				continue;
 			}
 
-			if (tbmres->ntuples >= 0)
-				node->exact_pages++;
-			else
-				node->lossy_pages++;
-
 			/* Adjust the prefetch target */
 			BitmapAdjustPrefetchTarget(node);
 		}
@@ -322,15 +319,27 @@ BitmapHeapNext(BitmapHeapScanState *node)
 		}
 
 		/* OK to return this tuple */
+		BitmapAccumCounters(node, scan);
 		return slot;
 	}
 
 	/*
 	 * if we get here it means we are at the end of the scan..
 	 */
+	BitmapAccumCounters(node, scan);
 	return ExecClearTuple(slot);
 }
 
+static inline void
+BitmapAccumCounters(BitmapHeapScanState *node,
+					TableScanDesc scan)
+{
+	node->exact_pages += scan->exact_pages;
+	scan->exact_pages = 0;
+	node->lossy_pages += scan->lossy_pages;
+	scan->lossy_pages = 0;
+}
+
 /*
  *	BitmapDoneInitializingSharedState - Shared state is initialized
  *
diff --git a/src/include/access/relscan.h b/src/include/access/relscan.h
index 521043304ab..b74e08dd745 100644
--- a/src/include/access/relscan.h
+++ b/src/include/access/relscan.h
@@ -40,6 +40,10 @@ typedef struct TableScanDescData
 	ItemPointerData rs_mintid;
 	ItemPointerData rs_maxtid;
 
+	/* Only used for Bitmap table scans */
+	long		exact_pages;
+	long		lossy_pages;
+
 	/*
 	 * 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 c193ea5db43..7dfb291800c 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -954,9 +954,13 @@ table_beginscan_bm(Relation rel, Snapshot snapshot,
 				   int nkeys, struct ScanKeyData *key,
 				   uint32 extra_flags)
 {
+	TableScanDesc result;
 	uint32		flags = SO_TYPE_BITMAPSCAN | SO_ALLOW_PAGEMODE | extra_flags;
 
-	return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, NULL, flags);
+	result = rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, NULL, flags);
+	result->lossy_pages = 0;
+	result->exact_pages = 0;
+	return result;
 }
 
 /*
-- 
2.37.2

