Plug leak in BuildTupleHashTable by creating ExprContext in correct context.
authorAndres Freund
Sat, 9 Feb 2019 08:35:57 +0000 (00:35 -0800)
committerAndres Freund
Sat, 9 Feb 2019 09:05:50 +0000 (01:05 -0800)
In bf6c614a2f2c5 I added a expr context to evaluate the grouping
expression. Unfortunately the code I added initialized them while in
the calling context, rather the table context.  Additionally, I used
CreateExprContext() rather than CreateStandaloneExprContext(), which
creates the econtext in the estate's query context.

Fix that by using CreateStandaloneExprContext when in the table's
tablecxt. As we rely on the memory being freed by a memory context
reset that means that the econtext's shutdown callbacks aren't being
called, but that seems ok as the expressions are tightly controlled
due to ExecBuildGroupingEqual().

Bug: #15592
Reported-By: Dmitry Marakasov
Author: Andres Freund
Discussion: https://postgr.es/m/20190114222838[email protected]
Backpatch: 11, where I broke this in bf6c614a2f2c5

src/backend/executor/execGrouping.c

index c4d0e0405873517dcf9b6e9cc0d2c4c1b5b72596..271bba17e4065070bc6453ecacfcf085c71f1371 100644 (file)
@@ -208,11 +208,17 @@ BuildTupleHashTable(PlanState *parent,
    hashtable->tab_eq_func = ExecBuildGroupingEqual(inputDesc, inputDesc,
                                                    numCols,
                                                    keyColIdx, eqfuncoids,
-                                                   parent);
+                                                   NULL);
 
-   MemoryContextSwitchTo(oldcontext);
+   /*
+    * 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();
 
-   hashtable->exprcontext = CreateExprContext(parent->state);
+   MemoryContextSwitchTo(oldcontext);
 
    return hashtable;
 }