Skip to content

Commit 9fd29d7

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 <[email protected]> Reviewed-by: David Rowley <[email protected]> Reviewed-by: Michael Paquier <[email protected]> Discussion: https://postgr.es/m/CAOG6S4-tGksTQhVzJM19NzLYAHusXsK2HmADPZzGQcfZABsvpA@mail.gmail.com Backpatch-through: 13
1 parent e4e496e commit 9fd29d7

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
@@ -573,7 +573,6 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
573573
Relation heapRel;
574574
BrinOpaque *opaque;
575575
BlockNumber nblocks;
576-
BlockNumber heapBlk;
577576
int64 totalpages = 0;
578577
FmgrInfo *consistentFn;
579578
MemoryContext oldcxt;
@@ -735,9 +734,10 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
735734
/*
736735
* Now scan the revmap. We start by querying for heap page 0,
737736
* incrementing by the number of pages per range; this gives us a full
738-
* view of the table.
737+
* view of the table. We make use of uint64 for heapBlk as a BlockNumber
738+
* could wrap for tables with close to 2^32 pages.
739739
*/
740-
for (heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
740+
for (uint64 heapBlk = 0; heapBlk < nblocks; heapBlk += opaque->bo_pagesPerRange)
741741
{
742742
bool addrange;
743743
bool gottuple = false;
@@ -749,7 +749,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
749749

750750
MemoryContextReset(perRangeCxt);
751751

752-
tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, heapBlk, &buf,
752+
tup = brinGetTupleForHeapBlock(opaque->bo_rmAccess, (BlockNumber ) heapBlk, &buf,
753753
&off, &size, BUFFER_LOCK_SHARE);
754754
if (tup)
755755
{
@@ -924,7 +924,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
924924
/* add the pages in the range to the output bitmap, if needed */
925925
if (addrange)
926926
{
927-
BlockNumber pageno;
927+
uint64 pageno;
928928

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

0 commit comments

Comments
 (0)