From 0926bd5aa9c57550c5b3687937d1252c57bee56d Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Thu, 3 Dec 2026 17:26:05 +0800
Subject: [PATCH v1] Remove useless memory context switch in
 ExecHashTableCreate().

---
 src/backend/executor/nodeHash.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index 8825bb6fa23..dfd15094943 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -479,7 +479,6 @@ ExecHashTableCreate(HashState *state)
 	double		rows;
 	int			num_skew_mcvs;
 	int			log2_nbuckets;
-	MemoryContext oldcxt;
 
 	/*
 	 * Get information about the size of the relation to be hashed (it's the
@@ -570,32 +569,26 @@ ExecHashTableCreate(HashState *state)
 												"HashSpillContext",
 												ALLOCSET_DEFAULT_SIZES);
 
-	/* Allocate data that will live for the life of the hashjoin */
-
-	oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
-
 	if (nbatch > 1 && hashtable->parallel_state == NULL)
 	{
-		MemoryContext oldctx;
+		MemoryContext oldcxt;
 
 		/*
-		 * allocate and initialize the file arrays in hashCxt (not needed for
+		 * allocate and initialize the file arrays in spillCxt (not needed for
 		 * parallel case which uses shared tuplestores instead of raw files)
 		 */
-		oldctx = MemoryContextSwitchTo(hashtable->spillCxt);
+		oldcxt = MemoryContextSwitchTo(hashtable->spillCxt);
 
 		hashtable->innerBatchFile = palloc0_array(BufFile *, nbatch);
 		hashtable->outerBatchFile = palloc0_array(BufFile *, nbatch);
 
-		MemoryContextSwitchTo(oldctx);
+		MemoryContextSwitchTo(oldcxt);
 
 		/* The files will not be opened until needed... */
 		/* ... but make sure we have temp tablespaces established for them */
 		PrepareTempTablespaces();
 	}
 
-	MemoryContextSwitchTo(oldcxt);
-
 	if (hashtable->parallel_state)
 	{
 		ParallelHashJoinState *pstate = hashtable->parallel_state;
@@ -646,11 +639,13 @@ ExecHashTableCreate(HashState *state)
 	}
 	else
 	{
+		MemoryContext oldcxt;
+
 		/*
 		 * Prepare context for the first-scan space allocations; allocate the
 		 * hashbucket array therein, and set each bucket "empty".
 		 */
-		MemoryContextSwitchTo(hashtable->batchCxt);
+		oldcxt = MemoryContextSwitchTo(hashtable->batchCxt);
 
 		hashtable->buckets.unshared = palloc0_array(HashJoinTuple, nbuckets);
 
-- 
2.56.1

