public inbox for [email protected]  
help / color / mirror / Atom feed
From: Heikki Linnakangas <[email protected]>
To: [email protected] <[email protected]>
Cc: Michael Paquier <[email protected]>
Subject: Shmem allocated wrong for custom cumulative stats
Date: Mon, 6 Apr 2026 03:16:47 +0300
Message-ID: <[email protected]> (raw)

There's only one call of ShmemAlloc() left in the tree outside shmem.c, 
in StatsShmemInit(), and that call doesn't look right:

The StatsShmemSize() function takes those allocations into account when 
it calculates the size for the "Shared Memory Stats" shmem area, but 
ShmemAlloc() doesn't use that reservation, it uses the general-purpose 
unreserved shmem that then shows up as "<anonymous>" in 
pg_shmem_allocations. The space reserved with ShmemRequestStruct() / 
ShmemInitStruct() goes unused.

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.

I noticed this while working on the new shmem allocation functions, but 
it's a pre-existing bug in stable branches too.

- Heikki


Attachments:

  [text/x-patch] 0001-Use-the-allocated-space-properly-for-custom-stats-ki.patch (1.1K, 2-0001-Use-the-allocated-space-properly-for-custom-stats-ki.patch)
  download | inline diff:
From 62d9a2fc8a56751fcd8a8ab780db7781374db5f0 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Mon, 6 Apr 2026 02:54:18 +0300
Subject: [PATCH 1/1] Use the allocated space properly for custom stats kind

---
 src/backend/utils/activity/pgstat_shmem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/activity/pgstat_shmem.c
index 955faf5ebc7..5e65a21fa83 100644
--- a/src/backend/utils/activity/pgstat_shmem.c
+++ b/src/backend/utils/activity/pgstat_shmem.c
@@ -189,6 +189,7 @@ StatsShmemInit(void *arg)
 	 * efficiency win.
 	 */
 	ctl->raw_dsa_area = p;
+	p += pgstat_dsa_init_size();
 	dsa = dsa_create_in_place(ctl->raw_dsa_area,
 							  pgstat_dsa_init_size(),
 							  LWTRANCHE_PGSTATS_DSA, NULL);
@@ -242,7 +243,8 @@ StatsShmemInit(void *arg)
 				int			idx = kind - PGSTAT_KIND_CUSTOM_MIN;
 
 				Assert(kind_info->shared_size != 0);
-				ctl->custom_data[idx] = ShmemAlloc(kind_info->shared_size);
+				ctl->custom_data[idx] = p;
+				p += MAXALIGN(kind_info->shared_size);
 				ptr = ctl->custom_data[idx];
 			}
 
-- 
2.47.3



view thread (5+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Shmem allocated wrong for custom cumulative stats
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox