Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() - Mailing list pgsql-hackers

From Chao Li
Subject Re: Fix unnecessary shared memory page allocation in CalculateShmemSize()
Date
Msg-id 714F9AFE-51BC-4A08-8082-24C01EED5025@gmail.com
Whole thread
In response to Fix unnecessary shared memory page allocation in CalculateShmemSize()  (Chao Li <li.evan.chao@gmail.com>)
List pgsql-hackers

> On Sep 14, 2026, at 16:14, Matthias van de Meent <boekewurm+postgres@gmail.com> wrote:
>
> On Mon, 14 Sept 2026 at 09:19, Chao Li <li.evan.chao@gmail.com> wrote:
>>
>> Hi,
>>
>> I just noticed this item when I went through my TODO list today. I remember finding this issue a few months ago, but
atthat time, only bugs new to PG19 were being processed, so I put it on my TODO list. 
>>
>> This is a small issue, but it has been there for many years. CalculateShmemSize() has logic to round size to a
multipleof 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
beingallocated 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));
>> ```
>>
>> I put the comment within the if clause because I remember Tom once mentioning that this would be the preferred
style.
>
> Shouldn't a TYPEALIGN(8192, size) do the trick here, and do it more
> concise and better?
>

TYPEALIGN(8192, size) would handle rounding more concisely, but I see add_size() has an overflow protection, maybe that
matters?

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







Attachment

pgsql-hackers by date:

Previous
From: Antonin Houska
Date:
Subject: Re: REPACK (CONCURRENTLY) backend waits indefinitely when decoding worker fails to start
Next
From: Jan Nidzwetzki
Date:
Subject: Re: [PATCH] Speed up repeat() for larger counts