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

From: Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
To: Chao Li <li(dot)evan(dot)chao(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-14 08:14:35
Message-ID: CAEze2Wh56itT63Q9ZN9hRRq_GJfh5ya1Sn53wQs46uXePOf1iQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, 14 Sept 2026 at 09:19, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)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)

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Richard Guo 2026-09-14 08:18:00 Re: Wrong result from JSON constructor in a simple CASE
Previous Message David Geier 2026-09-14 07:32:16 Re: Reducing relcache memory usage: deduping index shapes