v25-0002-table_scan_bitmap_next_block-implementations-cou.patch

application/octet-stream

Filename: v25-0002-table_scan_bitmap_next_block-implementations-cou.patch
Type: application/octet-stream
Part: 4
Message: Re: BitmapHeapScan streaming read user and prelim refactoring

Patch

Format: format-patch
Series: patch v25-0002
Subject: table_scan_bitmap_next_block implementations count lossy and exact pages
File+
src/backend/access/heap/heapam_handler.c 7 1
src/backend/executor/nodeBitmapHeapscan.c 3 10
src/include/access/tableam.h 17 3
From abb7cbcc8761f2a43792763d99d69cc1f1b6ed0f Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Sat, 6 Apr 2024 08:51:40 -0400
Subject: [PATCH v25 02/11] table_scan_bitmap_next_block implementations count
 lossy and exact pages

Keep track of whether or not individual TBMIterateResult's blocks were
represented lossily in the bitmap in table AM-specific code. These
counters are used for EXPLAIN.

This is a step toward removing TBMIterateResults as a flow control
mechanism in BitmapHeapNext(), which is required for the more
asynchronous design required to use the read stream API. It also is more
table AM-agnostic.

Author: Melanie Plageman
Reviewed-by: Tomas Vondra, Heikki Linnakangas
Discussion: https://postgr.es/m/063e4eb4-32d9-439e-a0b1-75565a9835a8%40iki.fi
---
 src/backend/access/heap/heapam_handler.c  |  8 +++++++-
 src/backend/executor/nodeBitmapHeapscan.c | 13 +++----------
 src/include/access/tableam.h              | 20 +++++++++++++++++---
 3 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index bdb7599684b..753d0384ead 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -2115,7 +2115,8 @@ heapam_estimate_rel_size(Relation rel, int32 *attr_widths,
 
 static bool
 heapam_scan_bitmap_next_block(TableScanDesc scan,
-							  TBMIterateResult *tbmres)
+							  TBMIterateResult *tbmres,
+							  uint64 *lossy_pages, uint64 *exact_pages)
 {
 	HeapScanDesc hscan = (HeapScanDesc) scan;
 	BlockNumber block = tbmres->blockno;
@@ -2243,6 +2244,11 @@ heapam_scan_bitmap_next_block(TableScanDesc scan,
 	Assert(ntup <= MaxHeapTuplesPerPage);
 	hscan->rs_ntuples = ntup;
 
+	if (tbmres->ntuples >= 0)
+		(*exact_pages)++;
+	else
+		(*lossy_pages)++;
+
 	return ntup > 0;
 }
 
diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c
index 3c63bdd93df..f4690a20bb1 100644
--- a/src/backend/executor/nodeBitmapHeapscan.c
+++ b/src/backend/executor/nodeBitmapHeapscan.c
@@ -212,8 +212,6 @@ BitmapHeapNext(BitmapHeapScanState *node)
 
 	for (;;)
 	{
-		bool		valid_block;
-
 		CHECK_FOR_INTERRUPTS();
 
 		/*
@@ -233,14 +231,9 @@ BitmapHeapNext(BitmapHeapScanState *node)
 
 			BitmapAdjustPrefetchIterator(node, tbmres->blockno);
 
-			valid_block = table_scan_bitmap_next_block(scan, tbmres);
-
-			if (tbmres->ntuples >= 0)
-				node->stats.exact_pages++;
-			else
-				node->stats.lossy_pages++;
-
-			if (!valid_block)
+			if (!table_scan_bitmap_next_block(scan, tbmres,
+											  &node->stats.lossy_pages,
+											  &node->stats.exact_pages))
 			{
 				/* AM doesn't think this block is valid, skip */
 				continue;
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index da661289c1f..be09d180d45 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -796,6 +796,10 @@ typedef struct TableAmRoutine
 	 * on the page have to be returned, otherwise the tuples at offsets in
 	 * `tbmres->offsets` need to be returned.
 	 *
+	 * `lossy_pages` and `exact_pages` are EXPLAIN counters that can be
+	 * incremented by the table AM to indicate whether or not the block's
+	 * representation in the bitmap is lossy.
+	 *
 	 * XXX: Currently this may only be implemented if the AM uses md.c as its
 	 * storage manager, and uses ItemPointer->ip_blkid in a manner that maps
 	 * blockids directly to the underlying storage. nodeBitmapHeapscan.c
@@ -811,7 +815,9 @@ typedef struct TableAmRoutine
 	 * scan_bitmap_next_tuple need to exist, or neither.
 	 */
 	bool		(*scan_bitmap_next_block) (TableScanDesc scan,
-										   struct TBMIterateResult *tbmres);
+										   struct TBMIterateResult *tbmres,
+										   uint64 *lossy_pages,
+										   uint64 *exact_pages);
 
 	/*
 	 * Fetch the next tuple of a bitmap table scan into `slot` and return true
@@ -1954,12 +1960,18 @@ table_relation_estimate_size(Relation rel, int32 *attr_widths,
  * table_beginscan_bm(). Returns false if there are no tuples to be found on
  * the page, true otherwise.
  *
+ * `lossy_pages` and `exact_pages` are EXPLAIN counters that can be
+ * incremented by the table AM to indicate whether or not the block's
+ * representation in the bitmap is lossy.
+ *
  * Note, this is an optionally implemented function, therefore should only be
  * used after verifying the presence (at plan time or such).
  */
 static inline bool
 table_scan_bitmap_next_block(TableScanDesc scan,
-							 struct TBMIterateResult *tbmres)
+							 struct TBMIterateResult *tbmres,
+							 uint64 *lossy_pages,
+							 uint64 *exact_pages)
 {
 	/*
 	 * We don't expect direct calls to table_scan_bitmap_next_block with valid
@@ -1970,7 +1982,9 @@ table_scan_bitmap_next_block(TableScanDesc scan,
 		elog(ERROR, "unexpected table_scan_bitmap_next_block call during logical decoding");
 
 	return scan->rs_rd->rd_tableam->scan_bitmap_next_block(scan,
-														   tbmres);
+														   tbmres,
+														   lossy_pages,
+														   exact_pages);
 }
 
 /*
-- 
2.45.2