From a7bd2889b76d702ef39430de64522f2b882fc134 Mon Sep 17 00:00:00 2001 From: Melanie Plageman Date: Thu, 13 Jun 2024 17:24:44 -0400 Subject: [PATCH v21 13/20] BitmapHeapScan: Push primary iterator setup into Heap AM Now that the table AM's are responsible for advancing the iterator for bitmap table scans, it makes sense to also delegate most of the iterator management tasks to them as well. This commit only pushes down the primary iterator which yields the current block. The prefetch iterator will be pushed down in a future commit. --- src/backend/access/heap/heapam.c | 22 ++++++++++++++++++++-- src/backend/access/heap/heapam_handler.c | 2 +- src/backend/executor/nodeBitmapHeapscan.c | 13 +++---------- src/include/access/heapam.h | 12 ++++++++++-- src/include/access/relscan.h | 5 ----- src/include/access/tableam.h | 19 +++++++++++++++---- 6 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 88da737d149..c99ed440a0e 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -1230,7 +1230,9 @@ heap_endscan(TableScanDesc sscan) } BitmapTableScanDesc * -heap_beginscan_bm(Relation relation, Snapshot snapshot, uint32 flags) +heap_beginscan_bm(Relation relation, Snapshot snapshot, uint32 flags, + TIDBitmap *tbm, ParallelBitmapHeapState *pstate, + dsa_area *dsa) { BitmapHeapScanDesc *scan; @@ -1266,14 +1268,23 @@ heap_beginscan_bm(Relation relation, Snapshot snapshot, uint32 flags) scan->vmbuffer = InvalidBuffer; scan->empty_tuples_pending = 0; + tbm_begin_iterate(&scan->iterator, tbm, dsa, + pstate ? + pstate->tbmiterator : + InvalidDsaPointer); + return (BitmapTableScanDesc *) scan; } void -heap_rescan_bm(BitmapTableScanDesc *sscan) +heap_rescan_bm(BitmapTableScanDesc *sscan, TIDBitmap *tbm, + ParallelBitmapHeapState *pstate, + dsa_area *dsa) { BitmapHeapScanDesc *scan = (BitmapHeapScanDesc *) sscan; + tbm_end_iterate(&scan->iterator); + if (BufferIsValid(scan->cbuf)) { ReleaseBuffer(scan->cbuf); @@ -1299,6 +1310,11 @@ heap_rescan_bm(BitmapTableScanDesc *sscan) scan->ctup.t_data = NULL; ItemPointerSetInvalid(&scan->ctup.t_self); + + tbm_begin_iterate(&scan->iterator, tbm, dsa, + pstate ? + pstate->tbmiterator : + InvalidDsaPointer); } void @@ -1306,6 +1322,8 @@ heap_endscan_bm(BitmapTableScanDesc *sscan) { BitmapHeapScanDesc *scan = (BitmapHeapScanDesc *) sscan; + tbm_end_iterate(&scan->iterator); + if (BufferIsValid(scan->cbuf)) ReleaseBuffer(scan->cbuf); diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 4057d0cf22b..936a34dcaa6 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -2140,7 +2140,7 @@ heapam_scan_bitmap_next_block(BitmapTableScanDesc *scan, { CHECK_FOR_INTERRUPTS(); - tbmres = tbm_iterate(&scan->iterator); + tbmres = tbm_iterate(&hscan->iterator); if (tbmres == NULL) return false; diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c index 5f625c1f3e6..22df6314f3f 100644 --- a/src/backend/executor/nodeBitmapHeapscan.c +++ b/src/backend/executor/nodeBitmapHeapscan.c @@ -158,6 +158,8 @@ BitmapHeapNext(BitmapHeapScanState *node) scan = table_beginscan_bm(node->ss.ss_currentRelation, node->ss.ps.state->es_snapshot, + node->tbm, + node->pstate, dsa, need_tuples, node->prefetch_maximum); node->scandesc = scan; @@ -166,15 +168,9 @@ BitmapHeapNext(BitmapHeapScanState *node) else { /* rescan to release any page pin */ - tbm_end_iterate(&scan->iterator); - table_rescan_bm(scan, dsa, node->prefetch_maximum); + table_rescan_bm(scan, node->tbm, pstate, dsa, node->prefetch_maximum); } - tbm_begin_iterate(&scan->iterator, node->tbm, dsa, - pstate ? - pstate->tbmiterator : - InvalidDsaPointer); - node->initialized = true; goto new_page; @@ -593,10 +589,7 @@ ExecEndBitmapHeapScan(BitmapHeapScanState *node) * close heap scan */ if (scanDesc) - { - tbm_end_iterate(&scanDesc->iterator); table_endscan_bm(scanDesc); - } /* * release bitmaps and buffers if any diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h index 1d5583cac22..824092c1d63 100644 --- a/src/include/access/heapam.h +++ b/src/include/access/heapam.h @@ -20,6 +20,7 @@ #include "access/skey.h" #include "access/table.h" /* for backward compatibility */ #include "access/tableam.h" +#include "nodes/execnodes.h" #include "nodes/lockoptions.h" #include "nodes/primnodes.h" #include "storage/bufpage.h" @@ -113,6 +114,7 @@ typedef struct BitmapHeapScanDesc HeapTupleData ctup; /* current tuple in scan, if any */ BlockNumber cblock; /* current block # in scan, if any */ + TBMIterator iterator; /* * These fields are only used for bitmap scans for the "skip fetch" @@ -315,8 +317,14 @@ extern void heap_rescan(TableScanDesc sscan, ScanKey key, bool set_params, extern void heap_endscan(TableScanDesc sscan); extern BitmapTableScanDesc *heap_beginscan_bm(Relation relation, - Snapshot snapshot, uint32 flags); -extern void heap_rescan_bm(BitmapTableScanDesc *sscan); + Snapshot snapshot, uint32 flags, + TIDBitmap *tbm, + ParallelBitmapHeapState *pstate, + dsa_area *dsa); +extern void heap_rescan_bm(BitmapTableScanDesc *sscan, + TIDBitmap *tbm, + ParallelBitmapHeapState *pstate, + dsa_area *dsa); void heap_endscan_bm(BitmapTableScanDesc *sscan); extern HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction); diff --git a/src/include/access/relscan.h b/src/include/access/relscan.h index 086fce35a8b..2c4c57bb983 100644 --- a/src/include/access/relscan.h +++ b/src/include/access/relscan.h @@ -57,11 +57,6 @@ typedef struct BitmapTableScanDesc Relation rel; /* heap relation descriptor */ struct SnapshotData *snapshot; /* snapshot to see */ - /* - * Members common to Parallel and Serial BitmapTableScans - */ - TBMIterator 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 69ccdaee8c4..afb8a9f3d6c 100644 --- a/src/include/access/tableam.h +++ b/src/include/access/tableam.h @@ -36,6 +36,7 @@ extern PGDLLIMPORT bool synchronize_seqscans; struct BulkInsertStateData; struct IndexInfo; +struct ParallelBitmapHeapState; struct SampleScanState; struct VacuumParams; struct ValidateIndexState; @@ -354,9 +355,15 @@ typedef struct TableAmRoutine */ BitmapTableScanDesc *(*scan_begin_bm) (Relation rel, Snapshot snapshot, - uint32 flags); + uint32 flags, + TIDBitmap *tbm, + struct ParallelBitmapHeapState *pstate, + dsa_area *dsa); - void (*scan_rescan_bm) (BitmapTableScanDesc *scan); + void (*scan_rescan_bm) (BitmapTableScanDesc *scan, + TIDBitmap *tbm, + struct ParallelBitmapHeapState *pstate, + dsa_area *dsa); /* * Release resources and deallocate scan. @@ -968,6 +975,8 @@ table_beginscan_strat(Relation rel, Snapshot snapshot, */ static inline BitmapTableScanDesc * table_beginscan_bm(Relation rel, Snapshot snapshot, + TIDBitmap *tbm, + struct ParallelBitmapHeapState *pstate, dsa_area *dsa, bool need_tuple, int prefetch_maximum) @@ -977,7 +986,7 @@ table_beginscan_bm(Relation rel, Snapshot snapshot, if (need_tuple) flags |= SO_NEED_TUPLES; - return rel->rd_tableam->scan_begin_bm(rel, snapshot, flags); + return rel->rd_tableam->scan_begin_bm(rel, snapshot, flags, tbm, pstate, dsa); } /* @@ -985,10 +994,12 @@ table_beginscan_bm(Relation rel, Snapshot snapshot, */ static inline void table_rescan_bm(BitmapTableScanDesc *scan, + TIDBitmap *tbm, + struct ParallelBitmapHeapState *pstate, dsa_area *dsa, int prefetch_maximum) { - scan->rel->rd_tableam->scan_rescan_bm(scan); + scan->rel->rd_tableam->scan_rescan_bm(scan, tbm, pstate, dsa); } /* -- 2.34.1