Re: Fix unnecessary shared memory page allocation in CalculateShmemSize()

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Nathan Bossart <nathandbossart(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix unnecessary shared memory page allocation in CalculateShmemSize()
Date: 2026-09-15 01:25:33
Message-ID: 88D043BA-2198-44E8-B9B6-D45DF6B5A76A@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Sep 14, 2026, at 21:48, Nathan Bossart <nathandbossart(at)gmail(dot)com> wrote:
>
> On Mon, Sep 14, 2026 at 03:19:03PM +0800, Chao Li wrote:
>> This is a small issue, but it has been there for many years.
>> CalculateShmemSize() has logic to round size to a multiple of a typical
>> page size:
>> ```
>> /* might as well round it off to a multiple of a typical page size */
>> size = add_size(size, 8192 - (size % 8192));
>> ```
>>
>> When size is already a multiple of 8192, this add_size() call is not
>> needed; it only results in an extra 8192 bytes being allocated in shared
>> memory. The fix is simple:
>> ```
>> if (size % 8192 != 0)
>> /* might as well round it off to a multiple of a typical page size */
>> size = add_size(size, 8192 - (size % 8192));
>> ```
>
> IMHO the current code is fine and is unlikely to cause problems for users.
>
> --
> nathan

I agree. Given the 100 KB added to the requested size, it should be extremely rare for this issue to be triggered. Even if it is, only an extra 8192 bytes are allocated, so users are unlikely to notice.

Still, it is a potential issue. It might not be worth back-patching, how about fixing it only on master?

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nikolay Samokhvalov 2026-09-15 01:31:47 Re: REPACK (CONCURRENTLY) backend waits indefinitely when decoding worker fails to start
Previous Message Haibo Yan 2026-09-15 01:24:24 Re: [PATCH] Use Boyer-Moore-Horspool for simple LIKE contains patterns