From 5bcbe74bdb930ce1bd46791419e5fdd195b5f0d4 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Thu, 18 Jul 2024 14:10:35 +0900
Subject: [PATCH v3 2/5] Add helper routines to retrieve custom data for
 fixed-numbered stats

This is useful for extensions to get snapshot and shmem data for custom
statistics, so as these don't need to know about the current snapshots.
---
 src/include/utils/pgstat_internal.h | 32 +++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/src/include/utils/pgstat_internal.h b/src/include/utils/pgstat_internal.h
index 39f63362a3..6bbd49388c 100644
--- a/src/include/utils/pgstat_internal.h
+++ b/src/include/utils/pgstat_internal.h
@@ -525,6 +525,8 @@ static inline int pgstat_cmp_hash_key(const void *a, const void *b, size_t size,
 static inline uint32 pgstat_hash_hash_key(const void *d, size_t size, void *arg);
 static inline size_t pgstat_get_entry_len(PgStat_Kind kind);
 static inline void *pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry);
+static inline void *pgstat_get_custom_shmem_data(PgStat_Kind kind);
+static inline void *pgstat_get_custom_snapshot_data(PgStat_Kind kind);
 
 
 /*
@@ -842,4 +844,34 @@ pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
 	return ((char *) (entry)) + off;
 }
 
+/*
+ * Returns a pointer to the shared memory area of custom stats for
+ * fixed-numbered statistics.
+ */
+static inline void *
+pgstat_get_custom_shmem_data(PgStat_Kind kind)
+{
+	int		idx = kind - PGSTAT_KIND_CUSTOM_MIN;
+
+	Assert(pgstat_is_kind_custom(kind));
+	Assert(pgstat_get_kind_info(kind)->fixed_amount);
+
+	return pgStatLocal.shmem->custom_data[idx];
+}
+
+/*
+ * Returns a pointer to the portion of custom data for fixed-numbered
+ * statistics in the current snapshot.
+ */
+static inline void *
+pgstat_get_custom_snapshot_data(PgStat_Kind kind)
+{
+	int		idx = kind - PGSTAT_KIND_CUSTOM_MIN;
+
+	Assert(pgstat_is_kind_custom(kind));
+	Assert(pgstat_get_kind_info(kind)->fixed_amount);
+
+	return pgStatLocal.snapshot.custom_data[idx];
+}
+
 #endif							/* PGSTAT_INTERNAL_H */
-- 
2.45.2

