From 206cff7a169626dc32c411751f472c2147cd7a98 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 11 May 2025 13:14:52 -0400
Subject: [PATCH v1 05/11] Silence complaints about leaked CatCache structs.

In USE_VALGRIND builds, don't attempt to cache-align CatCache
structs.  That prevents them from being reported as
"possibly lost" because all the pointers to them look like
interior pointers to Valgrind.

This is a hack, not meant for final commit.  The same hazard exists
for every other use of MemoryContextAllocAligned, so we really ought
to fix it there instead of touching this code.  But this seems to be
enough to silence all such Valgrind complaints in simple test cases,
so just do this much for now.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/285483.1746756246@sss.pgh.pa.us
---
 src/backend/utils/cache/catcache.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index 657648996c2..9e82d9fec68 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -923,12 +923,19 @@ InitCatCache(int id,
 	}
 
 	/*
-	 * Allocate a new cache structure, aligning to a cacheline boundary
+	 * Allocate a new cache structure.  Normally we align it to a cacheline
+	 * boundary, but skip that in USE_VALGRIND builds because it will confuse
+	 * Valgrind.  (XXX really we should fix that problem in
+	 * MemoryContextAllocAligned, not here.)
 	 *
 	 * Note: we rely on zeroing to initialize all the dlist headers correctly
 	 */
+#ifndef USE_VALGRIND
 	cp = (CatCache *) palloc_aligned(sizeof(CatCache), PG_CACHE_LINE_SIZE,
 									 MCXT_ALLOC_ZERO);
+#else
+	cp = (CatCache *) palloc0(sizeof(CatCache));
+#endif
 	cp->cc_bucket = palloc0(nbuckets * sizeof(dlist_head));
 
 	/*
-- 
2.43.5

