From 43f795ef933d1661e15f4d758e05167e10dc3887 Mon Sep 17 00:00:00 2001
From: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
Date: Thu, 22 Jan 2026 15:16:21 +0100
Subject: Fix rounding method used to compute shared_memory_size_in_huge_pages

When computing the dynamic value of shared_memory_size_in_huge_pages,
(1+size_b/hp_size) is currently used. This works when size_b is not
divisible by hp_size. However, it will yield an additional huge page
when size_b is divisible by hp_size.

This patch replicates the same rounding method used for the mmap
allocation in CreateAnonymousSegment, only rounding up when the value
is not divisible by hp_size.
---
 src/backend/storage/ipc/ipci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 85c67b2c183..268040dbec5 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -363,7 +363,9 @@ InitializeShmemGUCs(void)
 	{
 		Size		hp_required;
 
-		hp_required = add_size(size_b / hp_size, 1);
+		if (size_b % hp_size != 0)
+			size_b = add_size(size_b, hp_size - (size_b % hp_size));
+		hp_required = size_b / hp_size;
 		sprintf(buf, "%zu", hp_required);
 		SetConfigOption("shared_memory_size_in_huge_pages", buf,
 						PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
-- 
2.52.0

