Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x62pt-000RNT-0d for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Sep 2026 09:18:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x62pr-00C8hE-0k for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Sep 2026 09:18:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x62pq-00C8h4-31 for pgsql-hackers@lists.postgresql.org; Mon, 14 Sep 2026 09:18:46 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x62pn-00000000L7P-1iYq for pgsql-hackers@lists.postgresql.org; Mon, 14 Sep 2026 09:18:46 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-2dd020a2e44so27631335ad.1 for ; Mon, 14 Sep 2026 02:18:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789377522; x=1789982322; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:content-type:from:to:cc :subject:date:message-id:reply-to:content-type; bh=AJTcEo1htN6YB5RXQF0S9lC0sH4YNdwD4v9mhlovFGA=; b=bO5p/qd8Ydeg+sIm5CQAuhDhbCJYa7zLqDXDizSiICgNK6iO8IqtSnQ2AieTCLBH0L gVJSOUwu4jM6FDs5NdjXG8M84ndvV9Bc/1/vPqi4BQYEIpSb4rsH0eXQA2RG9WJwtrnD cAxHBCW9lZJNXA+TPqwtvMVyAWK+F1//rpIkTffwOIPu+bPbdINw3sb4jJZqwEqhp+FZ Ri77RB4pQgE/dLi8r959D5CigbXMLgpE3fTmSAndTZWaR+mh5aT78bLtWElBp51jMrSn Kd0PYQgmsZvsX/IqKZ+xh6DN7CGs98B84aWJsuF5Y/i2b3qDdNuBfvFphq3mtGcwB/V0 orEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789377522; x=1789982322; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:content-type:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=AJTcEo1htN6YB5RXQF0S9lC0sH4YNdwD4v9mhlovFGA=; b=cxGkTn+B0BKbEaWhXha+ljHrbWCLjRYrmmqE57LUZxoR5TQrajim2kssxqNQavm6lv 3vhwe61g1vIj540ecCCVj3VmFmtSdtptvcZMuMZg6Wwk/W3lajXRpf9bdUJvMhRILVg/ XnruY3tTFSKc+oghL29VPwVrw1Qt+jOB4RsHHshRTpqdekmKzpmy6VOlQAwmYs12GJ+s 7iIcwKYhytu8MAbSYagXToVOSckvkCRc+L0rluq3FQLNMd0cX6dH087dPgMpGXeimvHf D5tnYETHlnjNLXfkZ/1H6RimrrwhljTGxwxlsqnUMsiopHgYMcU6Xd9Bv66AaMp/Vrjd +CWw== X-Gm-Message-State: AFuF++kALowYHNlGtD5riWoTzxdkcY/sIPMzbY4A0dFG92Gxoz9b6aUz vKm8UzGgGReqA/EMvs8oguEGPqlBQcfYwpH5voLi04u5Yf/toKFonMWS X-Gm-Gg: AYBFou0wr+GBsNnCqMWSGKtgBgMYsoId0lOzZRmo4SWb+TzB39CSL3GXJl0wgWuaUJF lnihkFvPhYHj/9mdXrSnybCNcgio932dppS513x/HEbzld0+NWV1WnpupyEwWdDMrqkBmPftOLx gaJRDJMYiJaLnzLclHpsPCKC+PrzYllZRDm+ljPiFzfDum8A+sLgPmst5bNtTSK0xiknia9Fi3e 89PI6XCqw1q1S9hdIrH6hnrRpe3YgHZVWwZn0qQO4eyD4NmxEJBcf4oRsFW5tGLFABi0sJBnTbJ ITR+58ftLDyiVayr369lTODCRs5ga2rZcfua65hSKBXeNw6mhQ49SWa6gVlh1PNQU/+dlnVqoHX aiiMOAjkli1BylrAcR7YHyvn0SgY+jpwurOrDcv8MYDg6Z+ydLHUL7uwgkEKJ/hdhBrI5vb/cyJ 5T6PH325ilOaC/P++7lAwlL4A16KZhSI2QyxJ8KPq7NsZiJb9SosDybdrhZAVQbC4QFEzAeFhoV 3QldCNa X-Received: by 2002:a17:903:178e:b0:2d8:d4ce:9f37 with SMTP id d9443c01a7336-2dd6c75fa86mr37142475ad.21.1789377522424; Mon, 14 Sep 2026 02:18:42 -0700 (PDT) Received: from smtpclient.apple ([185.135.79.161]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2dd2ceb3514sm44602225ad.32.2026.09.14.02.18.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Sep 2026 02:18:41 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.700.51.1.1\)) Subject: Re: Fix unnecessary shared memory page allocation in CalculateShmemSize() From: Chao Li In-Reply-To: Date: Mon, 14 Sep 2026 17:18:06 +0800 Cc: pgsql-hackers Content-Transfer-Encoding: quoted-printable Message-Id: <714F9AFE-51BC-4A08-8082-24C01EED5025@gmail.com> References: To: Matthias van de Meent X-Mailer: Apple Mail (2.3864.700.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Sep 14, 2026, at 16:14, Matthias van de Meent = wrote: >=20 > On Mon, 14 Sept 2026 at 09:19, Chao Li wrote: >>=20 >> Hi, >>=20 >> 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. >>=20 >> 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 =3D add_size(size, 8192 - (size % 8192)); >> ``` >>=20 >> 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 !=3D 0) >> /* might as well round it off to a multiple of a = typical page size */ >> size =3D add_size(size, 8192 - (size % 8192)); >> ``` >>=20 >> I put the comment within the if clause because I remember Tom once = mentioning that this would be the preferred style. >=20 > Shouldn't a TYPEALIGN(8192, size) do the trick here, and do it more > concise and better? >=20 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/