Re: BRIN: Prevent the heapblk overflow during index summarization on very large tables resulting in an infinite loop

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: sunil s <sunilfeb26(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: BRIN: Prevent the heapblk overflow during index summarization on very large tables resulting in an infinite loop
Date: 2025-10-19 08:52:23
Message-ID: CAApHDvrhCHXUADk6Vm29Lv=-Uy2VCD7NurzSSVf=VM73-3vkHQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, 19 Oct 2025 at 19:03, sunil s <sunilfeb26(at)gmail(dot)com> wrote:
> Previously, heapBlk was defined as an unsigned 32-bit integer. When incremented
> by pagesPerRange on very large tables, it could wrap around, causing the condition
> heapBlk < nblocks to remain true indefinitely — resulting in an infinite loop.

> This was explained very nicely by Tomas Vondra[1] and below two solutions were
>
> suggested.
> i) Change to int64
> ii) Tracking the prevHeapBlk

This is similar to what table_block_parallelscan_nextpage() has to do
to avoid wrapping around when working with tables containing near 2^32
pages. I'd suggest using uint64 rather than int64 and also adding a
comment to mention why that type is being used rather than
BlockNumber. Something like: "We make use of uint64 for heapBlk as a
BlockNumber could wrap for tables with close to 2^32 pages."

You could move the declaration of heapBlk into the for loop line so
that the comment about using uint64 is closer to the declaration.

I suspect you will also want to switch to using uint64 for the
"pageno" variable at the end of the loop. My compiler isn't warning
about the "pageno = heapBlk;", but maybe other compilers will.

Not for this patch, but I wonder why we switch memory contexts so
often in that final tbm_add_page() loop. I think it would be better to
switch once before the loop and back again after it. What's there
seems a bit wasteful for any pagesPerRange > 1.

David

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message sunil s 2025-10-19 06:03:33 BRIN: Prevent the heapblk overflow during index summarization on very large tables resulting in an infinite loop