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 1w9aPv-001YzL-1b for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Apr 2026 03:14:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w9aPu-006JLz-0B for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Apr 2026 03:14:22 +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 1w9aPt-006JLp-1G for pgsql-hackers@lists.postgresql.org; Mon, 06 Apr 2026 03:14:22 +0000 Received: from fhigh-b7-smtp.messagingengine.com ([202.12.124.158]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w9aPo-00000000ppQ-2wK9 for pgsql-hackers@postgresql.org; Mon, 06 Apr 2026 03:14:20 +0000 Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfhigh.stl.internal (Postfix) with ESMTP id AE0457A0086; Sun, 5 Apr 2026 23:14:13 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Sun, 05 Apr 2026 23:14:13 -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=1775445253; x=1775531653; bh=A+mtDSqTte HXuKfoYna7vkJGoHfD+Guy5Rr6io8cN9Y=; b=TT+90NANFAyzAqt8uUKn4hFKEp 71DV7ig6p3ASSrOBVR/d3kHRLv8tPdNFhEtUQndC+4hufgBD5VWVXW6Pp9baF/lw qCcM99MRIYa1Ec95Y/SNXBlz6gsPl5RFgTW1/1mlPB7Dz6wT0kR4TtH/lyKW8quP BEpFJzfNcnEj84lS8A4NVA5An7RfCkcGVUZ+SbjxwTu7D00fgh/iziurrw28vKvY T7xaFMjdMni9iJEM9+PaWRHe80cpLjR718F9A1ar28SnHDnCWQBZybOhy8JLQPyp TvMwKLaZXxMPzBx84IKUYeRUe59eQruBL3HGFkHrCHsFBgYQUQj2y6x89FAA== 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= 1775445253; x=1775531653; bh=A+mtDSqTteHXuKfoYna7vkJGoHfD+Guy5Rr 6io8cN9Y=; b=qJx+/sYofIJ+UIdbQLmeuPMLCz/HFmTwaEtvQGSbKFpFN9me4Qz 1vamURV3E23e/nH9dMN1a173UZbsIMQh0R8M7wHzbSsXPMb9VauCzU6DeEQjrMiW +576HTayUwGhY+f9K6WaGYz/kBd5CLq+kV+16Ylqh2OMLiGQY/wdgxI0OA7vyJ/v WLOnqGXMhKvtuC2iK5ECrfAVJuzrB8n9J5fHkLrIHkfvZaziXbSgRR4f88QwfMJn CYHZmi1faLVtplrmQbKmdEnMcb/18OFvvIBlUqajdJcA4GNr7AKZwTWuQWSo6LK4 meaW7gXwQOFeB8A+nN9bdAafdj1MHUGebEg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefhedrtddtgdduieehjecutefuodetggdotefrod 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 23:14:11 -0400 (EDT) Date: Mon, 6 Apr 2026 12:14:06 +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="PgHsY5fpJ4cEV8Ji" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --PgHsY5fpJ4cEV8Ji Content-Type: multipart/mixed; boundary="C6nbC3J1m4NqJXDR" Content-Disposition: inline --C6nbC3J1m4NqJXDR Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Apr 06, 2026 at 11:36:21AM +0900, Michael Paquier wrote: > On Mon, Apr 06, 2026 at 09:55:14AM +0900, Michael Paquier wrote: >> 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. >=20 > Note: something is wrong with -m32, for both patches. Digging into > that.. And I have been puzzled for a few minutes here, trying to figure out if this was something in v18 or something with the new shmem routines. It is nothing of the kind: test_custom_stats has been underestimating its shared_size, using PgStat_StatCustomFixedEntry instead of PgStatShared_CustomFixedEntry. Interesting copy-pasto, HEAD-only, second bug. The attached is working correctly. The v18 flavor is slightly simpler. -- Michael --C6nbC3J1m4NqJXDR Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v3-0001-Use-the-allocated-space-properly-for-custom--HEAD.patch Content-Transfer-Encoding: quoted-printable =46rom 943746c5b430b485e15c44a0b7bf099454cee7dd Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Mon, 6 Apr 2026 09:47:02 +0900 Subject: [PATCH v3] Use the allocated space properly for custom stats kind --- src/backend/utils/activity/pgstat_shmem.c | 7 ++++--- .../modules/test_custom_stats/test_custom_fixed_stats.c | 2 +- 2 files changed, 5 insertions(+), 4 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 diff --git a/src/test/modules/test_custom_stats/test_custom_fixed_stats.c b= /src/test/modules/test_custom_stats/test_custom_fixed_stats.c index f9e7c7172802..a066ce117a6d 100644 --- a/src/test/modules/test_custom_stats/test_custom_fixed_stats.c +++ b/src/test/modules/test_custom_stats/test_custom_fixed_stats.c @@ -50,7 +50,7 @@ static const PgStat_KindInfo custom_stats =3D { .fixed_amount =3D true, /* exactly one entry */ .write_to_file =3D true, /* persist to stats file */ =20 - .shared_size =3D sizeof(PgStat_StatCustomFixedEntry), + .shared_size =3D sizeof(PgStatShared_CustomFixedEntry), .shared_data_off =3D offsetof(PgStatShared_CustomFixedEntry, stats), .shared_data_len =3D sizeof(((PgStatShared_CustomFixedEntry *) 0)->stats), =20 --=20 2.53.0 --C6nbC3J1m4NqJXDR Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v3-0001-Use-the-allocated-space-properly-for-custom-s-v18.patch Content-Transfer-Encoding: quoted-printable =46rom f6787781026c7dcf6c9a38b0e7f8c8427a7b2da6 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Mon, 6 Apr 2026 09:47:02 +0900 Subject: [PATCH v3] Use the allocated space properly for custom stats kind --- src/backend/utils/activity/pgstat_shmem.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/= activity/pgstat_shmem.c index 08ec264baa3a..2b7f783ef7c4 100644 --- a/src/backend/utils/activity/pgstat_shmem.c +++ b/src/backend/utils/activity/pgstat_shmem.c @@ -142,8 +142,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; @@ -227,7 +226,8 @@ StatsShmemInit(void) 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 --C6nbC3J1m4NqJXDR-- --PgHsY5fpJ4cEV8Ji Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmnTJP0ACgkQnvQgOdby QH2q4RAApvGl2kJgg6+FkkzpLbHMMXiD1QJuD/ZWzWqFH267cVu27IVLVh1aZEuA nFR3fdOkp/obx6tMurfsSVOyVS3NNfGJgxr9Gs5v1eDLGo4nuUuPu/IuDNUHUBy5 gkErYJ5oQIDhzgUuDXg9GiL4i6V75AuU4WK/nQqsPBq7icSEY6MstEC9LD8n93lq v6hAFtvRtfGGOwv+iL5i2O9yLUyXEyqom6aLsoRBYhDe5fZqLPmF8PNSSE2kxgtv C2pSNRCCctDKnQL+5B2IYvislTiX0UrBpvazEWChTJCt64T/aOe83RJPipSmcVyg 3oS58PFt6vJdkOIVI6N9pCVzYEoEAHP6TTnZlF5/fZ6l44UpfXDOBzQP3lyX3Rqj wwQtHYc2xVjsQQIE6/KX1QSObOyXYvz9FeTkcxkWLZdkixm51hqKRggQXlWLCYPA T78Me9X/vRHZsmpA7JjIAJ/LH/K4Pepmooq3r53GSo8GXRNw+iWJbfRrMPJ3Iwy7 tqAv/K6Bq9nNUsL/j3w9oI3hp/a/7tqmusAi4Tx+nKoPgT9RHdFiDfZly7U5T+fy VKs4NiKNjNJjjkaOdxCMIVYRGJLDflMxpet06R5zJmXrfA8QTIq7uxcgI8LLWHrZ oqRZIliMHQTHqRwIeDVO/7O8gHLW4TfbBh3c4YJXd3e5iF5vh1I= =SzdE -----END PGP SIGNATURE----- --PgHsY5fpJ4cEV8Ji--