Re: Fix rounding method used to compute huge pages

From: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
To: Anthonin Bonnefoy <anthonin(dot)bonnefoy(at)datadoghq(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix rounding method used to compute huge pages
Date: 2026-01-22 17:31:01
Message-ID: CAExHW5tSm7=dH9wnV6McXeqw5C6_Kcv1Oa3QPsroSOpeoLA8xA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Jan 22, 2026 at 10:13 PM Anthonin Bonnefoy
<anthonin(dot)bonnefoy(at)datadoghq(dot)com> wrote:
>
> Hi,
>
> When computing the dynamic value of shared_memory_size_in_huge_pages,
> (1+size_b/hp_size) is currently used. This works when size_b is not
> divisible by hp_size. However, it will yield an additional huge page
> when size_b is divisible by hp_size.
>
> On CreateAnonymousSegment's side, the allocation size is rounded up to
> the next required huge pages when necessary. However, there's no
> overflow check when doing this round up.
>
> 0001: This patch replicates CreateAnonymousSegment's rounding method
> to InitializeShmemGUCs, only rounding up when the value is not
> divisible by hp_size.
>
> 0002: This patch uses add_size in CreateAnonymousSegment when the
> allocation size is rounded up, to check for possible overflow.

We have similar incantation in CalculateShmemSize()
size = add_size(size, 8192 - (size % 8192));

I think we should just introduce a method ceil_size() and place it
near add_size() and mul_size() and use wherever we are rounding the
sizes.
Size
ceil_size(size, base)
{
return add_size(size, base - (size % base))
}

InitializeShmemGUCs() also has following line, which can can be
replaced with ceil_size(size_b, 1024 * 1024)
size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);

--
Best Wishes,
Ashutosh Bapat

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Noah Misch 2026-01-22 17:42:50 Re: Inval reliability, especially for inplace updates
Previous Message Sami Imseih 2026-01-22 17:23:29 Re: Flush some statistics within running transactions