From 44c10d802f29bf49f7317b17d46ab6f9d132b7bd Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu@ip-172-31-46-230.ec2.internal>
Date: Thu, 4 Sep 2025 16:00:23 +0000
Subject: [PATCH v1 1/1] Fix PgStat_HashKey padding issue when passed by
 reference

Adding a new OID field to PgStat_HashKey introduced 4 bytes of hole
padding. When the key was passed by value to
pgstat_get_entry_ref_cached(), builds with -O2 or higher optimization
sometimes left the padding uninitialized. Since hash/compare functions
of the key rely on the the full struct contents, inconsistent padding
caused spurious errors such as "entry ref vanished before deletion".

Passing the key by reference ensures padding is preserved and avoids
mismatches under optimization. Other hash key uses were reviewed and do
not appear to be affected.
---
 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 62de3474453..4492f3f9890 100644
--- a/src/backend/utils/activity/pgstat_shmem.c
+++ b/src/backend/utils/activity/pgstat_shmem.c
@@ -385,7 +385,7 @@ pgstat_acquire_entry_ref(PgStat_EntryRef *entry_ref,
  * Helper function for pgstat_get_entry_ref().
  */
 static bool
-pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
+pgstat_get_entry_ref_cached(PgStat_HashKey *key, PgStat_EntryRef **entry_ref_p)
 {
 	bool		found;
 	PgStat_EntryRefHashEntry *cache_entry;
@@ -396,7 +396,7 @@ pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
 	 * out-of-memory errors after incrementing PgStatShared_Common->refcount.
 	 */
 
-	cache_entry = pgstat_entry_ref_hash_insert(pgStatEntryRefHash, key, &found);
+	cache_entry = pgstat_entry_ref_hash_insert(pgStatEntryRefHash, *key, &found);
 
 	if (!found || !cache_entry->entry_ref)
 	{
@@ -485,7 +485,7 @@ pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create,
 	 * First check the lookup cache hashtable in local memory. If we find a
 	 * match here we can avoid taking locks / causing contention.
 	 */
-	if (pgstat_get_entry_ref_cached(key, &entry_ref))
+	if (pgstat_get_entry_ref_cached(&key, &entry_ref))
 		return entry_ref;
 
 	Assert(entry_ref != NULL);
-- 
2.43.0

