From e62b06d8d649f2b5a1cfe3e7925f84c446fc9add Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Mon, 14 Jan 2019 12:02:34 -0800
Subject: [PATCH v1 1/4] Plug leak by creating BuildTupleHashTable's
 ExprContext in correct context.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:
---
 src/backend/executor/execGrouping.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/backend/executor/execGrouping.c b/src/backend/executor/execGrouping.c
index 4e7600e4ed6..7cf001518a1 100644
--- a/src/backend/executor/execGrouping.c
+++ b/src/backend/executor/execGrouping.c
@@ -212,12 +212,18 @@ BuildTupleHashTable(PlanState *parent,
 													&TTSOpsMinimalTuple, &TTSOpsMinimalTuple,
 													numCols,
 													keyColIdx, eqfuncoids,
-													parent);
+													NULL);
+
+	/*
+	 * While not pretty, it's ok to not shut down this context, but instead
+	 * rely on the containing memory context being reset, as
+	 * ExecBuildGroupingEqual() only builds a very simple expression calling
+	 * functions (i.e. nothing that'd employ RegisterExprContextCallback()).
+	 */
+	hashtable->exprcontext = CreateStandaloneExprContext();
 
 	MemoryContextSwitchTo(oldcontext);
 
-	hashtable->exprcontext = CreateExprContext(parent->state);
-
 	return hashtable;
 }
 
-- 
2.18.0.rc2.dirty

