Skip to content

Commit d297c91

Browse files
committed
Avoid leaking memory while evaluating arguments for a table function.
ExecMakeTableFunctionResult evaluated the arguments for a function-in-FROM in the query-lifespan memory context. This is insignificant in simple cases where the function relation is scanned only once; but if the function is in a sub-SELECT or is on the inside of a nested loop, any memory consumed during argument evaluation can add up quickly. (The potential for trouble here had been foreseen long ago, per existing comments; but we'd not previously seen a complaint from the field about it.) To fix, create an additional temporary context just for this purpose. Per an example from MauMau. Back-patch to all active branches.
1 parent 2f69338 commit d297c91

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/backend/executor/execQual.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,7 @@ ExecMakeFunctionResultNoSets(FuncExprState *fcache,
19351935
Tuplestorestate *
19361936
ExecMakeTableFunctionResult(ExprState *funcexpr,
19371937
ExprContext *econtext,
1938+
MemoryContext argContext,
19381939
TupleDesc expectedDesc,
19391940
bool randomAccess)
19401941
{
@@ -2013,13 +2014,19 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
20132014
/*
20142015
* Evaluate the function's argument list.
20152016
*
2016-
* Note: ideally, we'd do this in the per-tuple context, but then the
2017-
* argument values would disappear when we reset the context in the
2018-
* inner loop. So do it in caller context. Perhaps we should make a
2019-
* separate context just to hold the evaluated arguments?
2017+
* We can't do this in the per-tuple context: the argument values
2018+
* would disappear when we reset that context in the inner loop. And
2019+
* the caller's CurrentMemoryContext is typically a query-lifespan
2020+
* context, so we don't want to leak memory there. We require the
2021+
* caller to pass a separate memory context that can be used for this,
2022+
* and can be reset each time through to avoid bloat.
20202023
*/
2024+
MemoryContextReset(argContext);
2025+
oldcontext = MemoryContextSwitchTo(argContext);
20212026
fcinfo.flinfo = &(fcache->func);
20222027
argDone = ExecEvalFuncArgs(&fcinfo, fcache->args, econtext);
2028+
MemoryContextSwitchTo(oldcontext);
2029+
20232030
/* We don't allow sets in the arguments of the table function */
20242031
if (argDone != ExprSingleResult)
20252032
ereport(ERROR,

src/backend/executor/nodeFunctionscan.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "executor/nodeFunctionscan.h"
2626
#include "funcapi.h"
2727
#include "utils/builtins.h"
28+
#include "utils/memutils.h"
2829

2930

3031
static TupleTableSlot *FunctionNext(FunctionScanState *node);
@@ -64,6 +65,7 @@ FunctionNext(FunctionScanState *node)
6465
node->tuplestorestate = tuplestorestate =
6566
ExecMakeTableFunctionResult(node->funcexpr,
6667
node->ss.ps.ps_ExprContext,
68+
node->argcontext,
6769
node->tupdesc,
6870
node->eflags & EXEC_FLAG_BACKWARD);
6971
}
@@ -217,6 +219,19 @@ ExecInitFunctionScan(FunctionScan *node, EState *estate, int eflags)
217219
ExecAssignResultTypeFromTL(&scanstate->ss.ps);
218220
ExecAssignScanProjectionInfo(&scanstate->ss);
219221

222+
/*
223+
* Create a memory context that ExecMakeTableFunctionResult can use to
224+
* evaluate function arguments in. We can't use the per-tuple context for
225+
* this because it gets reset too often; but we don't want to leak
226+
* evaluation results into the query-lifespan context either. We just
227+
* need one context, because we evaluate each function separately.
228+
*/
229+
scanstate->argcontext = AllocSetContextCreate(CurrentMemoryContext,
230+
"Table function arguments",
231+
ALLOCSET_DEFAULT_MINSIZE,
232+
ALLOCSET_DEFAULT_INITSIZE,
233+
ALLOCSET_DEFAULT_MAXSIZE);
234+
220235
return scanstate;
221236
}
222237

src/include/executor/executor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
188188
bool *isNull);
189189
extern Tuplestorestate *ExecMakeTableFunctionResult(ExprState *funcexpr,
190190
ExprContext *econtext,
191+
MemoryContext argContext,
191192
TupleDesc expectedDesc,
192193
bool randomAccess);
193194
extern Datum ExecEvalExprSwitchContext(ExprState *expression, ExprContext *econtext,

src/include/nodes/execnodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,7 @@ typedef struct SubqueryScanState
12401240
* tupdesc expected return tuple description
12411241
* tuplestorestate private state of tuplestore.c
12421242
* funcexpr state for function expression being evaluated
1243+
* argcontext memory context to evaluate function arguments in
12431244
* ----------------
12441245
*/
12451246
typedef struct FunctionScanState
@@ -1249,6 +1250,7 @@ typedef struct FunctionScanState
12491250
TupleDesc tupdesc;
12501251
Tuplestorestate *tuplestorestate;
12511252
ExprState *funcexpr;
1253+
MemoryContext argcontext;
12521254
} FunctionScanState;
12531255

12541256
/* ----------------

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy