Skip to content

Commit c28f8ca

Browse files
committed
Fix BRIN 32-bit counter wrap issue with huge tables
A BlockNumber (32-bit) might not be large enough to add bo_pagesPerRange to when the table contains close to 2^32 pages. At worst, this could result in a cancellable infinite loop during the BRIN index scan with power-of-2 pagesPerRange, and slow (inefficient) BRIN index scans and scanning of unneeded heap blocks for non power-of-2 pagesPerRange. Backpatch to all supported versions. Author: sunil s <sunilfeb26@gmail.com> Reviewed-by: David Rowley <dgrowleyml@gmail.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/CAOG6S4-tGksTQhVzJM19NzLYAHusXsK2HmADPZzGQcfZABsvpA@mail.gmail.com Backpatch-through: 13
1 parent 33276cd commit c28f8ca

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/backend/access/brin/brin.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
421421
Relation heapRel;
422422
BrinOpaque *opaque;
423423
BlockNumber nblocks;
424-
BlockNumber heapBlk;
425424
int64 totalpages = 0;
426425
FmgrInfo *consistentFn;
427426
MemoryContext oldcxt;
@@ -465,9 +464,10 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
465464
/*
466465
* Now scan the revmap. We start by querying for heap page 0,
467466
* incrementing by the number of pages per range; this gives us a full
468-
* view of the table.
467+
* view of the table. We make use of uint64 for heapBlk as a BlockNumber
468+
* could wrap for tables with close to 2^32 pages.
469469
*/
470-
for (heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
470+
for (uint64 heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
471471
{
472472
bool addrange;
473473
bool gottuple = false;
@@ -479,7 +479,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
479479

480480
MemoryContextResetAndDeleteChildren(perRangeCxt);
481481

482-
tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, heapBlk, &buf,
482+
tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, (BlockNumber ) heapBlk, &buf,
483483
&off, &size, BUFFER_LOCK_SHARE,
484484
scan->xs_snapshot);
485485
if (tup)
@@ -585,7 +585,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
585585
/* add the pages in the range to the output bitmap, if needed */
586586
if (addrange)
587587
{
588-
BlockNumber pageno;
588+
uint64 pageno;
589589

590590
for (pageno = heapBlk;
591591
pageno <= Min(nblocks, heapBlk + opaque->bo_pagesPerRange) - 1;

0 commit comments

Comments
 (0)