pgstat_create_memcxt_at_init_2.txt

text/plain

Filename: pgstat_create_memcxt_at_init_2.txt
Type: text/plain
Part: 0
Message: Re: Patch to address creation of PgStat* contexts with null parent context
diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/activity/pgstat_shmem.c
index 89060ef29a..e24a19882b 100644
--- a/src/backend/utils/activity/pgstat_shmem.c
+++ b/src/backend/utils/activity/pgstat_shmem.c
@@ -55,9 +55,6 @@ static void pgstat_release_all_entry_refs(bool discard_pending);
 typedef bool (*ReleaseMatchCB) (PgStat_EntryRefHashEntry *, Datum data);
 static void pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatchCB match, Datum match_data);
 
-static void pgstat_setup_memcxt(void);
-
-
 /* parameter for the shared hash */
 static const dshash_parameters dsh_params = {
 	sizeof(PgStat_HashKey),
@@ -227,6 +224,21 @@ pgstat_attach_shmem(void)
 											pgStatLocal.shmem->hash_handle, 0);
 
 	MemoryContextSwitchTo(oldcontext);
+
+	/*
+	 * memory contexts needed by both reads and writes on stats shared memory
+	 */
+	Assert(pgStatSharedRefContext == NULL);
+	Assert(pgStatEntryRefHashContext == NULL);
+
+	pgStatSharedRefContext =
+		AllocSetContextCreate(TopMemoryContext,
+							  "PgStat Shared Ref",
+							  ALLOCSET_SMALL_SIZES);
+	pgStatEntryRefHashContext =
+		AllocSetContextCreate(TopMemoryContext,
+							  "PgStat Shared Ref Hash",
+							  ALLOCSET_SMALL_SIZES);
 }
 
 void
@@ -407,7 +419,6 @@ pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create,
 	Assert(pgStatLocal.shared_hash != NULL);
 	Assert(!pgStatLocal.shmem->is_shutdown);
 
-	pgstat_setup_memcxt();
 	pgstat_setup_shared_refs();
 
 	if (created_entry != NULL)
@@ -970,18 +981,3 @@ pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
 {
 	pgstat_reset_matching_entries(match_kind, Int32GetDatum(kind), ts);
 }
-
-static void
-pgstat_setup_memcxt(void)
-{
-	if (unlikely(!pgStatSharedRefContext))
-		pgStatSharedRefContext =
-			AllocSetContextCreate(CacheMemoryContext,
-								  "PgStat Shared Ref",
-								  ALLOCSET_SMALL_SIZES);
-	if (unlikely(!pgStatEntryRefHashContext))
-		pgStatEntryRefHashContext =
-			AllocSetContextCreate(CacheMemoryContext,
-								  "PgStat Shared Ref Hash",
-								  ALLOCSET_SMALL_SIZES);
-}