Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc1f8fb9

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

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
@@ -2002,6 +2002,7 @@ ExecMakeFunctionResultNoSets(FuncExprState *fcache,
20022002
Tuplestorestate*
20032003
ExecMakeTableFunctionResult(ExprState*funcexpr,
20042004
ExprContext*econtext,
2005+
MemoryContextargContext,
20052006
TupleDescexpectedDesc,
20062007
boolrandomAccess)
20072008
{
@@ -2083,12 +2084,18 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
20832084
/*
20842085
* Evaluate the function's argument list.
20852086
*
2086-
* Note: ideally, we'd do this in the per-tuple context, but then the
2087-
* argument values would disappear when we reset the context in the
2088-
* inner loop. So do it in caller context. Perhaps we should make a
2089-
* separate context just to hold the evaluated arguments?
2087+
* We can't do this in the per-tuple context: the argument values
2088+
* would disappear when we reset that context in the inner loop. And
2089+
* the caller's CurrentMemoryContext is typically a query-lifespan
2090+
* context, so we don't want to leak memory there. We require the
2091+
* caller to pass a separate memory context that can be used for this,
2092+
* and can be reset each time through to avoid bloat.
20902093
*/
2094+
MemoryContextReset(argContext);
2095+
oldcontext=MemoryContextSwitchTo(argContext);
20912096
argDone=ExecEvalFuncArgs(&fcinfo,fcache->args,econtext);
2097+
MemoryContextSwitchTo(oldcontext);
2098+
20922099
/* We don't allow sets in the arguments of the table function */
20932100
if (argDone!=ExprSingleResult)
20942101
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
staticTupleTableSlot*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
returnscanstate;
231246
}
232247

‎src/include/executor/executor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
229229
bool*isNull);
230230
externTuplestorestate*ExecMakeTableFunctionResult(ExprState*funcexpr,
231231
ExprContext*econtext,
232+
MemoryContextargContext,
232233
TupleDescexpectedDesc,
233234
boolrandomAccess);
234235
externDatumExecEvalExprSwitchContext(ExprState*expression,ExprContext*econtext,

‎src/include/nodes/execnodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,7 @@ typedef struct SubqueryScanState
13921392
*tupdescexpected return tuple description
13931393
*tuplestorestateprivate state of tuplestore.c
13941394
*funcexprstate for function expression being evaluated
1395+
*argcontextmemory context to evaluate function arguments in
13951396
* ----------------
13961397
*/
13971398
typedefstructFunctionScanState
@@ -1401,6 +1402,7 @@ typedef struct FunctionScanState
14011402
TupleDesctupdesc;
14021403
Tuplestorestate*tuplestorestate;
14031404
ExprState*funcexpr;
1405+
MemoryContextargcontext;
14041406
}FunctionScanState;
14051407

14061408
/* ----------------

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp