Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit de380a6

Browse files
Make table_scan_bitmap_next_block() async-friendly
Move all responsibility for indicating a block is exhuasted into table_scan_bitmap_next_tuple() and advance the main iterator in heap-specific code. This flow control makes more sense and is a step toward using the read stream API for bitmap heap scans. Previously, table_scan_bitmap_next_block() returned false to indicate table_scan_bitmap_next_tuple() should not be called for the tuples on the page. This happened both when 1) there were no visible tuples on the page and 2) when the block returned by the iterator was past the end of the table. BitmapHeapNext() (generic bitmap table scan code) handled the case when the bitmap was exhausted. It makes more sense for table_scan_bitmap_next_tuple() to return false when there are no visible tuples on the page and table_scan_bitmap_next_block() to return false when the bitmap is exhausted or there are no more blocks in the table. As part of this new design, TBMIterateResults are no longer used as a flow control mechanism in BitmapHeapNext(), so we removed table_scan_bitmap_next_tuple's TBMIterateResult parameter. Note that the prefetch iterator is still saved in the BitmapHeapScanState node and advanced in generic bitmap table scan code. This is because 1) it was not necessary to change the prefetch iterator location to change the flow control in BitmapHeapNext() 2) modifying prefetch iterator management requires several more steps better split over multiple commits and 3) the prefetch iterator will be removed once the read stream API is used. Author: Melanie Plageman Reviewed-by: Tomas Vondra, Andres Freund, Heikki Linnakangas, Mark Dilger Discussion: https://postgr.es/m/063e4eb4-32d9-439e-a0b1-75565a9835a8%40iki.fi
1 parent 7bd7aa4 commit de380a6

File tree

6 files changed

+252
-162
lines changed

6 files changed

+252
-162
lines changed

src/backend/access/heap/heapam.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,17 +1387,17 @@ heap_set_tidrange(TableScanDesc sscan, ItemPointer mintid,
13871387
heap_setscanlimits(sscan, startBlk, numBlks);
13881388

13891389
/* Finally, set the TID range in sscan */
1390-
ItemPointerCopy(&lowestItem, &sscan->rs_mintid);
1391-
ItemPointerCopy(&highestItem, &sscan->rs_maxtid);
1390+
ItemPointerCopy(&lowestItem, &sscan->st.tidrange.rs_mintid);
1391+
ItemPointerCopy(&highestItem, &sscan->st.tidrange.rs_maxtid);
13921392
}
13931393

13941394
bool
13951395
heap_getnextslot_tidrange(TableScanDesc sscan, ScanDirection direction,
13961396
TupleTableSlot *slot)
13971397
{
13981398
HeapScanDesc scan = (HeapScanDesc) sscan;
1399-
ItemPointer mintid = &sscan->rs_mintid;
1400-
ItemPointer maxtid = &sscan->rs_maxtid;
1399+
ItemPointer mintid = &sscan->st.tidrange.rs_mintid;
1400+
ItemPointer maxtid = &sscan->st.tidrange.rs_maxtid;
14011401

14021402
/* Note: no locking manipulations needed */
14031403
for (;;)

src/backend/access/heap/heapam_handler.c

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,18 +2115,49 @@ heapam_estimate_rel_size(Relation rel, int32 *attr_widths,
21152115

21162116
static bool
21172117
heapam_scan_bitmap_next_block(TableScanDesc scan,
2118-
TBMIterateResult *tbmres,
2118+
BlockNumber *blockno, bool *recheck,
21192119
uint64 *lossy_pages, uint64 *exact_pages)
21202120
{
21212121
HeapScanDesc hscan = (HeapScanDesc) scan;
2122-
BlockNumber block = tbmres->blockno;
2122+
BlockNumber block;
21232123
Buffer buffer;
21242124
Snapshot snapshot;
21252125
int ntup;
2126+
TBMIterateResult *tbmres;
21262127

21272128
hscan->rs_cindex = 0;
21282129
hscan->rs_ntuples = 0;
21292130

2131+
*blockno = InvalidBlockNumber;
2132+
*recheck = true;
2133+
2134+
do
2135+
{
2136+
CHECK_FOR_INTERRUPTS();
2137+
2138+
if (scan->st.bitmap.rs_shared_iterator)
2139+
tbmres = tbm_shared_iterate(scan->st.bitmap.rs_shared_iterator);
2140+
else
2141+
tbmres = tbm_iterate(scan->st.bitmap.rs_iterator);
2142+
2143+
if (tbmres == NULL)
2144+
return false;
2145+
2146+
/*
2147+
* Ignore any claimed entries past what we think is the end of the
2148+
* relation. It may have been extended after the start of our scan (we
2149+
* only hold an AccessShareLock, and it could be inserts from this
2150+
* backend). We don't take this optimization in SERIALIZABLE
2151+
* isolation though, as we need to examine all invisible tuples
2152+
* reachable by the index.
2153+
*/
2154+
} while (!IsolationIsSerializable() &&
2155+
tbmres->blockno >= hscan->rs_nblocks);
2156+
2157+
/* Got a valid block */
2158+
*blockno = tbmres->blockno;
2159+
*recheck = tbmres->recheck;
2160+
21302161
/*
21312162
* We can skip fetching the heap page if we don't need any fields from the
21322163
* heap, the bitmap entries don't need rechecking, and all tuples on the
@@ -2145,16 +2176,7 @@ heapam_scan_bitmap_next_block(TableScanDesc scan,
21452176
return true;
21462177
}
21472178

2148-
/*
2149-
* Ignore any claimed entries past what we think is the end of the
2150-
* relation. It may have been extended after the start of our scan (we
2151-
* only hold an AccessShareLock, and it could be inserts from this
2152-
* backend). We don't take this optimization in SERIALIZABLE isolation
2153-
* though, as we need to examine all invisible tuples reachable by the
2154-
* index.
2155-
*/
2156-
if (!IsolationIsSerializable() && block >= hscan->rs_nblocks)
2157-
return false;
2179+
block = tbmres->blockno;
21582180

21592181
/*
21602182
* Acquire pin on the target heap page, trading in any pin we held before.
@@ -2249,12 +2271,18 @@ heapam_scan_bitmap_next_block(TableScanDesc scan,
22492271
else
22502272
(*lossy_pages)++;
22512273

2252-
return ntup > 0;
2274+
/*
2275+
* Return true to indicate that a valid block was found and the bitmap is
2276+
* not exhausted. If there are no visible tuples on this page,
2277+
* hscan->rs_ntuples will be 0 and heapam_scan_bitmap_next_tuple() will
2278+
* return false returning control to this function to advance to the next
2279+
* block in the bitmap.
2280+
*/
2281+
return true;
22532282
}
22542283

22552284
static bool
22562285
heapam_scan_bitmap_next_tuple(TableScanDesc scan,
2257-
TBMIterateResult *tbmres,
22582286
TupleTableSlot *slot)
22592287
{
22602288
HeapScanDesc hscan = (HeapScanDesc) scan;

0 commit comments

Comments
 (0)