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 1w9YFU-001X1K-0m for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Apr 2026 00:55:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w9YFR-005jGp-1B for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Apr 2026 00:55:25 +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 1w9YFR-005jGh-0D for pgsql-hackers@lists.postgresql.org; Mon, 06 Apr 2026 00:55:25 +0000 Received: from fhigh-b3-smtp.messagingengine.com ([202.12.124.154]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w9YFO-00000000os4-3Ndl for pgsql-hackers@postgresql.org; Mon, 06 Apr 2026 00:55:25 +0000 Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfhigh.stl.internal (Postfix) with ESMTP id 770BA7A0071; Sun, 5 Apr 2026 20:55:20 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Sun, 05 Apr 2026 20:55:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1775436920; x=1775523320; bh=g9x2xiZVOy 0pHdacoPJyTzNiXwwfsZMQp4NLKXFX6Wo=; b=dB1udz6KJbItbG0VGTvyoR4FI3 C/IF4kgRmhCktRC5jb1F0fHmuzHyPZuLzWpsUS02GrbMr4UfKbNwYy8E9LAuxEaA wGSbOQmIWb7GI5V/NdDR02ISDhh3lhpruxF1pbt5YVkSOr0K22JycKGmE6+1VeU9 MVODHHs/AbhDWm6t20qshOgtTMUyHTU6yKbQM2hh0V7tvst0eAv4uco80nIlbMQ/ +RHqjNZBxbexcXIa1bL89MSiBzg34fpaOhf/pEe7sJZD+DfTt+/dramDDTP8If9/ OaSzR3lFUbQR4YgqxNwwFbAOdlyTgEDH6TLEQNi0ti/6oUlUt0XmdRutUx8A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1775436920; x=1775523320; bh=g9x2xiZVOy0pHdacoPJyTzNiXwwfsZMQp4N LKXFX6Wo=; b=TuDnxK35FizLAUHcgPffjV+PZgxeCGfj9SK+Iy66xVW3cgCBVav zPlJJgXxEfgw9xId+GsDNV/uc9n7Z10Z6dAAsq05FlwHgV7/98WNJavC17SWQdgA C+ZiElKchiPGY2O5414L8/JmX6TZ5VuYTd/ufVSILI6gfErmVEpNCQYg+El7pcKF 5lLKvsKDICDyGOIo9ZoofXnPwCWO03CyCPHFqojvbWXBG2PCxdemZTrJ9/3kyetY XeQko0MluhkkkzfaeB1+qKF2k8Bkxu+FmuN7OFVsKDIOc7Ely0Qvmxun3tKefzay PFUwquHDYzlnkG7D3m7opUe7Uv2K+Bi0Iig== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefhedrtddtgdduiedvlecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfghrlh cuvffnffculdejtddmnecujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvden ucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprghquh hivghrrdighiiiqeenucggtffrrghtthgvrhhnpeetleeifedufffhhfdtteelgeeggeff hfekueevteeigfduudevudetgfegiedvjeenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpehmihgthhgrvghlsehprghquhhivghrrdighiiipdhn sggprhgtphhtthhopedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehhlhhinh hnrghkrgesihhkihdrfhhipdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrshesphho shhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 5 Apr 2026 20:55:18 -0400 (EDT) Date: Mon, 6 Apr 2026 09:55:14 +0900 From: Michael Paquier To: Heikki Linnakangas Cc: "pgsql-hackers@postgresql.org" Subject: Re: Shmem allocated wrong for custom cumulative stats Message-ID: References: <04b04387-92f5-476c-90b0-4064e71c5f37@iki.fi> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="wT0aYElsq84JPFht" Content-Disposition: inline In-Reply-To: <04b04387-92f5-476c-90b0-4064e71c5f37@iki.fi> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --wT0aYElsq84JPFht Content-Type: multipart/mixed; boundary="WEWEZlC2FFGmLdWJ" Content-Disposition: inline --WEWEZlC2FFGmLdWJ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Apr 06, 2026 at 03:16:47AM +0300, Heikki Linnakangas wrote: > We should use the memory that we've reserved, per the attached patch. One > consequence of this fix though is that the allocations are now only > MAXALIGNed, while ShmemAlloc() uses CACHELINEALIGN(). Not sure which we > want. Indeed, it's not right. Thanks for the report. I am pretty sure that I intended each chunk to be MAXALIGN()-d for each custom stats kind registered, allocated in a non-anonymous way, without cache alignment. > I noticed this while working on the new shmem allocation functions, but it's > a pre-existing bug in stable branches too. Right, down to v18 where this has been introduced. That's my bug, so I'd be OK to take care of it myself, if you are OK with that of course. Actually, shouldn't StatsShmemSize() use an add_size() for each shared_size? Noted while passing through the code, extra error from the same commit. -- Michael --WEWEZlC2FFGmLdWJ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v2-0001-Use-the-allocated-space-properly-for-custom-stats.patch Content-Transfer-Encoding: quoted-printable =46rom 9d0c00e96d8776d21f556e1457abf635fe211f9c Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Mon, 6 Apr 2026 09:47:02 +0900 Subject: [PATCH v2] Use the allocated space properly for custom stats kind --- src/backend/utils/activity/pgstat_shmem.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/= activity/pgstat_shmem.c index 955faf5ebc7d..b8f354c818a0 100644 --- a/src/backend/utils/activity/pgstat_shmem.c +++ b/src/backend/utils/activity/pgstat_shmem.c @@ -150,8 +150,7 @@ StatsShmemSize(void) continue; =20 Assert(kind_info->shared_size !=3D 0); - - sz +=3D MAXALIGN(kind_info->shared_size); + sz =3D add_size(sz, MAXALIGN(kind_info->shared_size)); } =20 return sz; @@ -189,6 +188,7 @@ StatsShmemInit(void *arg) * efficiency win. */ ctl->raw_dsa_area =3D p; + p +=3D pgstat_dsa_init_size(); dsa =3D dsa_create_in_place(ctl->raw_dsa_area, pgstat_dsa_init_size(), LWTRANCHE_PGSTATS_DSA, NULL); @@ -242,7 +242,8 @@ StatsShmemInit(void *arg) int idx =3D kind - PGSTAT_KIND_CUSTOM_MIN; =20 Assert(kind_info->shared_size !=3D 0); - ctl->custom_data[idx] =3D ShmemAlloc(kind_info->shared_size); + ctl->custom_data[idx] =3D p; + p +=3D MAXALIGN(kind_info->shared_size); ptr =3D ctl->custom_data[idx]; } =20 --=20 2.53.0 --WEWEZlC2FFGmLdWJ-- --wT0aYElsq84JPFht Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmnTBHIACgkQnvQgOdby QH1Hwg/+LFEVp9aXC9NWqUww84LNXePElxZD99unV2aFGfPj6oPAotmkRz/eaWND tWYfE0p91rFsak8tsPaaqPvvImwFL319kefCKDMUJzPa2FIVIx0zg6gvhlsrSr2U rGU+F8+f3Jo8zkOsq/I0fa+Ck1gKA4gfVF3Fxig6sAZHTzSyDNgCfafMJ0LDJPaK yUGhv5TjKjTZN5WiWgwGwOQqtLkN+iHDy0gUYmyL/nF4lA4+Aboz4k6mB7MauAdW aC1JsBfzRDtH2V1dzvY/6ZA9tAfQyG1yi9yyg9FcK+Jr0U5cGtOgcRwPguCo+Tzs yazqvduCSR81keqhbMjpPznCuYs9iSfUBU0wiUzleULih7E7eQNBqspZ5YHhnbVT ml73srbaEmu2rYP1UPXMyQ728URV8ykP+VRDEy4RMBq8B9MecHj/DObhZfchwvyd vtYM2K5oPcLBh63CKed9gCiQjZeTS6Hyzk43QYg4LG1wxcuUugBjFcSnPpQK1hxt 3TCIBRFBI/B4Alk+W9+NzQtlp700KPZhHdhwliywkj0PDt0KLDeeUd99PV1Hjvf7 lBR1vbgKtf8oRYujN/79lO57CcquUmmpDQGjtuKHpIlxlgCHtU/HudebxS6SbI0N jja/YC7ykrxVfzEkFP1/N1uGJbtLFEh9BhujDnCIBeN5LiEUQRg= =W4Y6 -----END PGP SIGNATURE----- --wT0aYElsq84JPFht--