Skip to content

Commit 4348738

Browse files
committed
Fix some oversights in BRIN patch.
Remove HeapScanDescData.rs_initblock, which wasn't being used for anything in the final version of the patch. Fix IndexBuildHeapScan so that it supports syncscan again; the patch broke synchronous scanning for index builds by forcing rs_startblk to zero even when the caller did not care about that and had asked for syncscan. Add some commentary and usage defenses to heap_setscanlimits(). Fix heapam so that asking for rs_numblocks == 0 does what you would reasonably expect. As coded it amounted to requesting a whole-table scan, because those "--x <= 0" tests on an unsigned variable would behave surprisingly.
1 parent 9faa6ae commit 4348738

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/backend/access/heap/heapam.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ initscan(HeapScanDesc scan, ScanKey key, bool is_rescan)
277277
scan->rs_startblock = 0;
278278
}
279279

280-
scan->rs_initblock = 0;
281280
scan->rs_numblocks = InvalidBlockNumber;
282281
scan->rs_inited = false;
283282
scan->rs_ctup.t_data = NULL;
@@ -302,11 +301,22 @@ initscan(HeapScanDesc scan, ScanKey key, bool is_rescan)
302301
pgstat_count_heap_scan(scan->rs_rd);
303302
}
304303

304+
/*
305+
* heap_setscanlimits - restrict range of a heapscan
306+
*
307+
* startBlk is the page to start at
308+
* numBlks is number of pages to scan (InvalidBlockNumber means "all")
309+
*/
305310
void
306311
heap_setscanlimits(HeapScanDesc scan, BlockNumber startBlk, BlockNumber numBlks)
307312
{
313+
Assert(!scan->rs_inited); /* else too late to change */
314+
Assert(!scan->rs_syncscan); /* else rs_startblock is significant */
315+
316+
/* Check startBlk is valid (but allow case of zero blocks...) */
317+
Assert(startBlk == 0 || startBlk < scan->rs_nblocks);
318+
308319
scan->rs_startblock = startBlk;
309-
scan->rs_initblock = startBlk;
310320
scan->rs_numblocks = numBlks;
311321
}
312322

@@ -477,7 +487,7 @@ heapgettup(HeapScanDesc scan,
477487
/*
478488
* return null immediately if relation is empty
479489
*/
480-
if (scan->rs_nblocks == 0)
490+
if (scan->rs_nblocks == 0 || scan->rs_numblocks == 0)
481491
{
482492
Assert(!BufferIsValid(scan->rs_cbuf));
483493
tuple->t_data = NULL;
@@ -511,7 +521,7 @@ heapgettup(HeapScanDesc scan,
511521
/*
512522
* return null immediately if relation is empty
513523
*/
514-
if (scan->rs_nblocks == 0)
524+
if (scan->rs_nblocks == 0 || scan->rs_numblocks == 0)
515525
{
516526
Assert(!BufferIsValid(scan->rs_cbuf));
517527
tuple->t_data = NULL;
@@ -651,7 +661,7 @@ heapgettup(HeapScanDesc scan,
651661
if (backward)
652662
{
653663
finished = (page == scan->rs_startblock) ||
654-
(scan->rs_numblocks != InvalidBlockNumber ? --scan->rs_numblocks <= 0 : false);
664+
(scan->rs_numblocks != InvalidBlockNumber ? --scan->rs_numblocks == 0 : false);
655665
if (page == 0)
656666
page = scan->rs_nblocks;
657667
page--;
@@ -662,7 +672,7 @@ heapgettup(HeapScanDesc scan,
662672
if (page >= scan->rs_nblocks)
663673
page = 0;
664674
finished = (page == scan->rs_startblock) ||
665-
(scan->rs_numblocks != InvalidBlockNumber ? --scan->rs_numblocks <= 0 : false);
675+
(scan->rs_numblocks != InvalidBlockNumber ? --scan->rs_numblocks == 0 : false);
666676

667677
/*
668678
* Report our new scan position for synchronization purposes. We
@@ -754,7 +764,7 @@ heapgettup_pagemode(HeapScanDesc scan,
754764
/*
755765
* return null immediately if relation is empty
756766
*/
757-
if (scan->rs_nblocks == 0)
767+
if (scan->rs_nblocks == 0 || scan->rs_numblocks == 0)
758768
{
759769
Assert(!BufferIsValid(scan->rs_cbuf));
760770
tuple->t_data = NULL;
@@ -785,7 +795,7 @@ heapgettup_pagemode(HeapScanDesc scan,
785795
/*
786796
* return null immediately if relation is empty
787797
*/
788-
if (scan->rs_nblocks == 0)
798+
if (scan->rs_nblocks == 0 || scan->rs_numblocks == 0)
789799
{
790800
Assert(!BufferIsValid(scan->rs_cbuf));
791801
tuple->t_data = NULL;
@@ -914,7 +924,7 @@ heapgettup_pagemode(HeapScanDesc scan,
914924
if (backward)
915925
{
916926
finished = (page == scan->rs_startblock) ||
917-
(scan->rs_numblocks != InvalidBlockNumber ? --scan->rs_numblocks <= 0 : false);
927+
(scan->rs_numblocks != InvalidBlockNumber ? --scan->rs_numblocks == 0 : false);
918928
if (page == 0)
919929
page = scan->rs_nblocks;
920930
page--;
@@ -925,7 +935,7 @@ heapgettup_pagemode(HeapScanDesc scan,
925935
if (page >= scan->rs_nblocks)
926936
page = 0;
927937
finished = (page == scan->rs_startblock) ||
928-
(scan->rs_numblocks != InvalidBlockNumber ? --scan->rs_numblocks <= 0 : false);
938+
(scan->rs_numblocks != InvalidBlockNumber ? --scan->rs_numblocks == 0 : false);
929939

930940
/*
931941
* Report our new scan position for synchronization purposes. We

src/backend/catalog/index.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,7 +2168,8 @@ IndexBuildHeapScan(Relation heapRelation,
21682168
/*
21692169
* As above, except that instead of scanning the complete heap, only the given
21702170
* number of blocks are scanned. Scan to end-of-rel can be signalled by
2171-
* passing InvalidBlockNumber as numblocks.
2171+
* passing InvalidBlockNumber as numblocks. Note that restricting the range
2172+
* to scan cannot be done when requesting syncscan.
21722173
*/
21732174
double
21742175
IndexBuildHeapRangeScan(Relation heapRelation,
@@ -2251,7 +2252,14 @@ IndexBuildHeapRangeScan(Relation heapRelation,
22512252
allow_sync); /* syncscan OK? */
22522253

22532254
/* set our scan endpoints */
2254-
heap_setscanlimits(scan, start_blockno, numblocks);
2255+
if (!allow_sync)
2256+
heap_setscanlimits(scan, start_blockno, numblocks);
2257+
else
2258+
{
2259+
/* syncscan can only be requested on whole relation */
2260+
Assert(start_blockno == 0);
2261+
Assert(numblocks == InvalidBlockNumber);
2262+
}
22552263

22562264
reltuples = 0;
22572265

src/include/access/relscan.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ typedef struct HeapScanDescData
3838
/* state set up at initscan time */
3939
BlockNumber rs_nblocks; /* total number of blocks in rel */
4040
BlockNumber rs_startblock; /* block # to start at */
41-
BlockNumber rs_initblock; /* block # to consider initial of rel */
42-
BlockNumber rs_numblocks; /* number of blocks to scan */
41+
BlockNumber rs_numblocks; /* max number of blocks to scan */
42+
/* rs_numblocks is usually InvalidBlockNumber, meaning "scan whole rel" */
4343
BufferAccessStrategy rs_strategy; /* access strategy for reads */
4444
bool rs_syncscan; /* report location to syncscan logic? */
4545

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy