v3-0001-Use-the-allocated-space-properly-for-custom--HEAD.patch

text/plain

Filename: v3-0001-Use-the-allocated-space-properly-for-custom--HEAD.patch
Type: text/plain
Part: 0
Message: Re: Shmem allocated wrong for custom cumulative stats

Patch

Format: format-patch
Series: patch v3-0001
Subject: Use the allocated space properly for custom stats kind
File+
src/backend/utils/activity/pgstat_shmem.c 4 3
src/test/modules/test_custom_stats/test_custom_fixed_stats.c 1 1
From 943746c5b430b485e15c44a0b7bf099454cee7dd Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
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;
 
 		Assert(kind_info->shared_size != 0);
-
-		sz += MAXALIGN(kind_info->shared_size);
+		sz = add_size(sz, MAXALIGN(kind_info->shared_size));
 	}
 
 	return sz;
@@ -189,6 +188,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 +242,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];
 			}
 
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 = {
 	.fixed_amount = true,		/* exactly one entry */
 	.write_to_file = true,		/* persist to stats file */
 
-	.shared_size = sizeof(PgStat_StatCustomFixedEntry),
+	.shared_size = sizeof(PgStatShared_CustomFixedEntry),
 	.shared_data_off = offsetof(PgStatShared_CustomFixedEntry, stats),
 	.shared_data_len = sizeof(((PgStatShared_CustomFixedEntry *) 0)->stats),
 
-- 
2.53.0