From: Chao Li <li.evan.chao@gmail.com>
To: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Subject: Fix unnecessary shared memory page allocation in CalculateShmemSize()
Date: Mon, 14 Sep 2026 15:19:03 +0800
Message-ID: <F132EBB6-47EB-4DF8-8C8D-3DE698265A2B@gmail.com> (raw)
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.cindex 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)
=
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: li.evan.chao@gmail.com
Subject: Re: Fix unnecessary shared memory page allocation in CalculateShmemSize()
In-Reply-To: <F132EBB6-47EB-4DF8-8C8D-3DE698265A2B@gmail.com>
References: <F132EBB6-47EB-4DF8-8C8D-3DE698265A2B@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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