Skip to content

Commit b568d38

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 0ae841a commit b568d38

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
@@ -1991,6 +1991,7 @@ ExecMakeFunctionResultNoSets(FuncExprState *fcache,
19911991
Tuplestorestate *
19921992
ExecMakeTableFunctionResult(ExprState *funcexpr,
19931993
ExprContext *econtext,
1994+
MemoryContext argContext,
19941995
TupleDesc expectedDesc,
19951996
bool randomAccess)
19961997
{
@@ -2072,12 +2073,18 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
20722073
/*
20732074
* Evaluate the function's argument list.
20742075
*
2075-
* Note: ideally, we'd do this in the per-tuple context, but then the
2076-
* argument values would disappear when we reset the context in the
2077-
* inner loop. So do it in caller context. Perhaps we should make a
2078-
* separate context just to hold the evaluated arguments?
2076+
* We can't do this in the per-tuple context: the argument values
2077+
* would disappear when we reset that context in the inner loop. And
2078+
* the caller's CurrentMemoryContext is typically a query-lifespan
2079+
* context, so we don't want to leak memory there. We require the
2080+
* caller to pass a separate memory context that can be used for this,
2081+
* and can be reset each time through to avoid bloat.
20792082
*/
2083+
MemoryContextReset(argContext);
2084+
oldcontext = MemoryContextSwitchTo(argContext);
20802085
argDone = ExecEvalFuncArgs(&fcinfo, fcache->args, econtext);
2086+
MemoryContextSwitchTo(oldcontext);
2087+
20812088
/* We don't allow sets in the arguments of the table function */
20822089
if (argDone != ExprSingleResult)
20832090
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 "nodes/nodeFuncs.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
}
@@ -227,6 +229,19 @@ ExecInitFunctionScan(FunctionScan *node, EState *estate, int eflags)
227229
ExecAssignResultTypeFromTL(&scanstate->ss.ps);
228230
ExecAssignScanProjectionInfo(&scanstate->ss);
229231

232+
/*
233+
* Create a memory context that ExecMakeTableFunctionResult can use to
234+
* evaluate function arguments in. We can't use the per-tuple context for
235+
* this because it gets reset too often; but we don't want to leak
236+
* evaluation results into the query-lifespan context either. We just
237+
* need one context, because we evaluate each function separately.
238+
*/
239+
scanstate->argcontext = AllocSetContextCreate(CurrentMemoryContext,
240+
"Table function arguments",
241+
ALLOCSET_DEFAULT_MINSIZE,
242+
ALLOCSET_DEFAULT_INITSIZE,
243+
ALLOCSET_DEFAULT_MAXSIZE);
244+
230245
return scanstate;
231246
}
232247

src/include/executor/executor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
228228
bool *isNull);
229229
extern Tuplestorestate *ExecMakeTableFunctionResult(ExprState *funcexpr,
230230
ExprContext *econtext,
231+
MemoryContext argContext,
231232
TupleDesc expectedDesc,
232233
bool randomAccess);
233234
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
@@ -1391,6 +1391,7 @@ typedef struct SubqueryScanState
13911391
* tupdesc expected return tuple description
13921392
* tuplestorestate private state of tuplestore.c
13931393
* funcexpr state for function expression being evaluated
1394+
* argcontext memory context to evaluate function arguments in
13941395
* ----------------
13951396
*/
13961397
typedef struct FunctionScanState
@@ -1400,6 +1401,7 @@ typedef struct FunctionScanState
14001401
TupleDesc tupdesc;
14011402
Tuplestorestate *tuplestorestate;
14021403
ExprState *funcexpr;
1404+
MemoryContext argcontext;
14031405
} FunctionScanState;
14041406

14051407
/* ----------------

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