From d267c3c0cb26ea97616ca1184ab13664acb4feb8 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 11 May 2021 11:11:15 +1200
Subject: [PATCH 1/3] Clobber memory released by HASH_REMOVE.

---
 src/backend/utils/hash/dynahash.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c
index 6546e3c7c7..b977d0395c 100644
--- a/src/backend/utils/hash/dynahash.c
+++ b/src/backend/utils/hash/dynahash.c
@@ -102,6 +102,7 @@
 #include "storage/shmem.h"
 #include "storage/spin.h"
 #include "utils/dynahash.h"
+#include "utils/memdebug.h"
 #include "utils/memutils.h"
 
 
@@ -1076,10 +1077,12 @@ hash_search_with_hash_value(HTAB *hashp,
 					SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
 
 				/*
-				 * better hope the caller is synchronizing access to this
-				 * element, because someone else is going to reuse it the next
-				 * time something is added to the table
+				 * Clobber the memory, to find bugs in code that accesses
+				 * it after removing.
 				 */
+#ifdef CLOBBER_FREED_MEMORY
+				wipe_mem(ELEMENTKEY(currBucket), hctl->entrysize);
+#endif
 				return (void *) ELEMENTKEY(currBucket);
 			}
 			return NULL;
-- 
2.30.2

