pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedFix unnecessary shared memory page allocation in CalculateShmemSize()
7+ messages / 4 participants
[nested] [flat]
* Fix unnecessary shared memory page allocation in CalculateShmemSize()
@ 2026-09-14 07:19 Chao Li <li.evan.chao@gmail.com>
2026-09-14 07:26 ` Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
2026-09-14 08:14 ` Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-09-14 13:48 ` Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() Nathan Bossart <nathandbossart@gmail.com>
0 siblings, 3 replies; 7+ messages in thread
From: Chao Li @ 2026-09-14 07:19 UTC (permalink / raw)
To: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi,
I just noticed this item when I went through my TODO list today. I remember finding this issue a few months ago, but at that 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 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));
```
I put the comment within the if clause because I remember Tom once mentioning that this would be the preferred style.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v1-0001-Avoid-extra-shared-memory-page-in-CalculateShmemS.patch (application/octet-stream, 1.2K) download
From a1023326b5895f2820424f96612c2fd945d5f424 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Mon, 14 Sep 2026 15:16:20 +0800
Subject: [PATCH v1] Avoid extra shared-memory page in CalculateShmemSize()
CalculateShmemSize() rounds the requested shared-memory size up to a
multiple of 8192 bytes. When the size is already aligned, however, the
calculation adds another 8192 bytes unnecessarily.
Only add the padding when the size is not already a multiple of 8192.
Author: Chao Li <lic@highgo.com>
---
src/backend/storage/ipc/ipci.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index e149a738c8d..3a221b88f37 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -73,8 +73,9 @@ CalculateShmemSize(void)
/* include additional requested shmem from preload libraries */
size = add_size(size, total_addin_request);
- /* might as well round it off to a multiple of a typical page size */
- size = add_size(size, 8192 - (size % 8192));
+ 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));
return size;
}
--
2.50.1 (Apple Git-155)
=
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Fix unnecessary shared memory page allocation in CalculateShmemSize()
2026-09-14 07:19 Fix unnecessary shared memory page allocation in CalculateShmemSize() Chao Li <li.evan.chao@gmail.com>
@ 2026-09-14 07:26 ` Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
2 siblings, 0 replies; 7+ messages in thread
From: Osama Abdul Qader @ 2026-09-14 07:26 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hello Chao,
I'm interested and I'll look into this and get back to you.
With Regards,
Osama Abdul Qader
On Mon, 14 Sept, 2026, 12:49 pm 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 at that 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 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));
> ```
>
> I put the comment within the if clause because I remember Tom once
> mentioning that this would be the preferred style.
>
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
>
>
>
>
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Fix unnecessary shared memory page allocation in CalculateShmemSize()
2026-09-14 07:19 Fix unnecessary shared memory page allocation in CalculateShmemSize() Chao Li <li.evan.chao@gmail.com>
@ 2026-09-14 08:14 ` Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-09-14 09:18 ` Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() Chao Li <li.evan.chao@gmail.com>
2 siblings, 1 reply; 7+ messages in thread
From: Matthias van de Meent @ 2026-09-14 08:14 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
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 at that 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 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));
> ```
>
> 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?
Kind regards,
Matthias van de Meent
Databricks (https://www.databricks.com)
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Fix unnecessary shared memory page allocation in CalculateShmemSize()
2026-09-14 07:19 Fix unnecessary shared memory page allocation in CalculateShmemSize() Chao Li <li.evan.chao@gmail.com>
2026-09-14 08:14 ` Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() Matthias van de Meent <boekewurm+postgres@gmail.com>
@ 2026-09-14 09:18 ` Chao Li <li.evan.chao@gmail.com>
2026-09-14 09:59 ` Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Chao Li @ 2026-09-14 09:18 UTC (permalink / raw)
To: Matthias van de Meent <boekewurm+postgres@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
> 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 at that 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 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));
>> ```
>>
>> 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/
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Fix unnecessary shared memory page allocation in CalculateShmemSize()
2026-09-14 07:19 Fix unnecessary shared memory page allocation in CalculateShmemSize() Chao Li <li.evan.chao@gmail.com>
2026-09-14 08:14 ` Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-09-14 09:18 ` Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() Chao Li <li.evan.chao@gmail.com>
@ 2026-09-14 09:59 ` Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Osama Abdul Qader @ 2026-09-14 09:59 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Matthias van de Meent <boekewurm+postgres@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Greetings of the day,
I was able to reproduce the bug locally and have applied both *TYPEALIGN(8192,
size) *and the patch that Chao attached in his email.
I have tested both approaches locally.
*TYPEALIGN(8192, size) *does handle the rounding more concisely, and I
confirmed that it leaves an already 8192-byte-aligned value unchanged.
The proposed conditional *add_size() *change also fixes the issue, while
retaining the overflow protection provided by *add_size()*.
So I think the main question is whether overflow protection is important
here. If it is, I would prefer the proposed conditional change; otherwise,
*TYPEALIGN()* seems cleaner.
With regards,
Osama Abdul Qader
On Mon, Sep 14, 2026 at 2:48 PM Chao Li <li.evan.chao@gmail.com> wrote:
>
>
> > 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 at that 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 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));
> >> ```
> >>
> >> 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/
>
>
>
>
>
>
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Fix unnecessary shared memory page allocation in CalculateShmemSize()
2026-09-14 07:19 Fix unnecessary shared memory page allocation in CalculateShmemSize() Chao Li <li.evan.chao@gmail.com>
@ 2026-09-14 13:48 ` Nathan Bossart <nathandbossart@gmail.com>
2026-09-15 01:25 ` Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() Chao Li <li.evan.chao@gmail.com>
2 siblings, 1 reply; 7+ messages in thread
From: Nathan Bossart @ 2026-09-14 13:48 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Fix unnecessary shared memory page allocation in CalculateShmemSize()
2026-09-14 07:19 Fix unnecessary shared memory page allocation in CalculateShmemSize() Chao Li <li.evan.chao@gmail.com>
2026-09-14 13:48 ` Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() Nathan Bossart <nathandbossart@gmail.com>
@ 2026-09-15 01:25 ` Chao Li <li.evan.chao@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Chao Li @ 2026-09-15 01:25 UTC (permalink / raw)
To: Nathan Bossart <nathandbossart@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
> On Sep 14, 2026, at 21:48, Nathan Bossart <nathandbossart@gmail.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/
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2026-09-15 01:25 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-14 07:19 Fix unnecessary shared memory page allocation in CalculateShmemSize() Chao Li <li.evan.chao@gmail.com>
2026-09-14 07:26 ` Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
2026-09-14 08:14 ` Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-09-14 09:18 ` Chao Li <li.evan.chao@gmail.com>
2026-09-14 09:59 ` Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
2026-09-14 13:48 ` Nathan Bossart <nathandbossart@gmail.com>
2026-09-15 01:25 ` Chao Li <li.evan.chao@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox