From 6fa2afb16f8a741dfc6df546af031c5cd112046f Mon Sep 17 00:00:00 2001
From: Zhao Junwang <zhjwpku@gmail.com>
Date: Wed, 13 Sep 2023 14:34:23 +0800
Subject: [PATCH] do not refill the hashkey after hash_search

It's not necessary to fill the key field for most cases, since
hash_search has already done that for you. For developer that
using memset to zero the entry structure after enter it, fill the
key field is a must, but IMHO that is not good coding style, we
really should not touch the key field after insert it into the
dynahash.

This patch fixed some most abnormal ones, instead of refilling the
key field of primitive types, adding some assert might be a better
choice.

Signed-off-by: Zhao Junwang <zhjwpku@gmail.com>
---
 contrib/dblink/dblink.c                               | 1 -
 src/backend/catalog/pg_enum.c                         | 2 +-
 src/backend/commands/async.c                          | 4 ++--
 src/backend/commands/tablecmds.c                      | 2 +-
 src/backend/replication/logical/applyparallelworker.c | 2 +-
 src/backend/replication/logical/relation.c            | 5 ++---
 6 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index 41e1f6c91d..a50786ccda 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -2562,7 +2562,6 @@ createNewConnection(const char *name, remoteConn *rconn)
 	}
 
 	hentry->rconn = rconn;
-	strlcpy(hentry->name, name, sizeof(hentry->name));
 }
 
 static void
diff --git a/src/backend/catalog/pg_enum.c b/src/backend/catalog/pg_enum.c
index 3c328664b2..d5b96bdaea 100644
--- a/src/backend/catalog/pg_enum.c
+++ b/src/backend/catalog/pg_enum.c
@@ -785,6 +785,6 @@ RestoreUncommittedEnums(void *space)
 	init_uncommitted_enums();
 	do
 	{
-		hash_search(uncommitted_enums, serialized++, HASH_ENTER, NULL);
+		(void) hash_search(uncommitted_enums, serialized++, HASH_ENTER, NULL);
 	} while (OidIsValid(*serialized));
 }
diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index d148d10850..5065236ec7 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -2375,7 +2375,7 @@ AddEventToPendingNotifies(Notification *n)
 													  HASH_ENTER,
 													  &found);
 			Assert(!found);
-			hentry->event = oldn;
+			Assert(hentry->event == oldn);
 		}
 	}
 
@@ -2393,7 +2393,7 @@ AddEventToPendingNotifies(Notification *n)
 												  HASH_ENTER,
 												  &found);
 		Assert(!found);
-		hentry->event = n;
+		Assert(hentry->event == n);
 	}
 }
 
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 8a2c671b66..7957771f79 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -2006,7 +2006,7 @@ ExecuteTruncateGuts(List *explicit_rels,
 			ft_info = hash_search(ft_htab, &serverid, HASH_ENTER, &found);
 			if (!found)
 			{
-				ft_info->serverid = serverid;
+				Assert(ft_info->serverid == serverid);
 				ft_info->rels = NIL;
 			}
 
diff --git a/src/backend/replication/logical/applyparallelworker.c b/src/backend/replication/logical/applyparallelworker.c
index 82f48a488e..08dc76f20e 100644
--- a/src/backend/replication/logical/applyparallelworker.c
+++ b/src/backend/replication/logical/applyparallelworker.c
@@ -508,8 +508,8 @@ pa_allocate_worker(TransactionId xid)
 
 	winfo->in_use = true;
 	winfo->serialize_changes = false;
+	Assert(entry->xid == xid);
 	entry->winfo = winfo;
-	entry->xid = xid;
 }
 
 /*
diff --git a/src/backend/replication/logical/relation.c b/src/backend/replication/logical/relation.c
index d62eefed13..8f23847af3 100644
--- a/src/backend/replication/logical/relation.c
+++ b/src/backend/replication/logical/relation.c
@@ -641,8 +641,8 @@ logicalrep_partition_open(LogicalRepRelMapEntry *root,
 
 	if (!found)
 	{
-		memset(part_entry, 0, sizeof(LogicalRepPartMapEntry));
-		part_entry->partoid = partOid;
+		Assert(part_entry->partoid == partOid);
+		memset(entry, 0, sizeof(LogicalRepRelMapEntry));
 	}
 
 	/* Release the no-longer-useful attrmap, if any. */
@@ -657,7 +657,6 @@ logicalrep_partition_open(LogicalRepRelMapEntry *root,
 		int			i;
 
 		/* Remote relation is copied as-is from the root entry. */
-		entry = &part_entry->relmapentry;
 		entry->remoterel.remoteid = remoterel->remoteid;
 		entry->remoterel.nspname = pstrdup(remoterel->nspname);
 		entry->remoterel.relname = pstrdup(remoterel->relname);
-- 
2.41.0

