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

Commitc8c3e07

Browse files
committed
Clean up possible memory leakage in nodeSubplan
1 parentf68e11f commitc8c3e07

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

‎src/backend/executor/nodeSubplan.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 1994, Regents of the University of California
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/executor/nodeSubplan.c,v 1.16 1999/11/1502:00:01 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/executor/nodeSubplan.c,v 1.17 1999/11/1503:28:05 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -107,9 +107,18 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext, bool *isNull)
107107
if (found)
108108
elog(ERROR,"ExecSubPlan: more than one tuple returned by expression subselect");
109109
found= true;
110-
/* XXX need to copy tuple in case pass by ref */
111-
/* XXX need to ref-count the tuple to avoid mem leak! */
110+
/*
111+
* We need to copy the subplan's tuple in case the result is of
112+
* pass-by-ref type --- our return value will point into this
113+
* copied tuple! Can't use the subplan's instance of the tuple
114+
* since it won't still be valid after next ExecProcNode() call.
115+
* node->curTuple keeps track of the copied tuple for eventual
116+
* freeing.
117+
*/
112118
tup=heap_copytuple(tup);
119+
if (node->curTuple)
120+
pfree(node->curTuple);
121+
node->curTuple=tup;
113122
result=heap_getattr(tup,col,tdesc,isNull);
114123
/* keep scanning subplan to make sure there's only one tuple */
115124
continue;
@@ -253,10 +262,13 @@ ExecInitSubPlan(SubPlan *node, EState *estate, Plan *parent)
253262
ExecCreateTupleTable(ExecCountSlotsNode(node->plan)+10);
254263
sp_estate->es_snapshot=estate->es_snapshot;
255264

265+
node->shutdown= false;
266+
node->curTuple=NULL;
267+
256268
if (!ExecInitNode(node->plan,sp_estate,NULL))
257269
return false;
258270

259-
node->shutdown= true;
271+
node->shutdown= true;/* now we need to shutdown the subplan */
260272

261273
/*
262274
* If this plan is un-correlated or undirect correlated one and want
@@ -332,13 +344,15 @@ ExecSetParamPlan(SubPlan *node)
332344
found= true;
333345

334346
/*
335-
* If this is uncorrelated subquery then its plan will be closed
336-
* (see below) and this tuple will be free-ed - bad for not byval
337-
* types... But is free-ing possible in the next ExecProcNode in
338-
* this loop ? Who knows... Someday we'll keep track of saved
339-
* tuples...
347+
* We need to copy the subplan's tuple in case any of the params
348+
* are pass-by-ref type --- the pointers stored in the param structs
349+
* will point at this copied tuple! node->curTuple keeps track
350+
* of the copied tuple for eventual freeing.
340351
*/
341352
tup=heap_copytuple(tup);
353+
if (node->curTuple)
354+
pfree(node->curTuple);
355+
node->curTuple=tup;
342356

343357
foreach(lst,node->setParam)
344358
{
@@ -387,13 +401,16 @@ ExecSetParamPlan(SubPlan *node)
387401
void
388402
ExecEndSubPlan(SubPlan*node)
389403
{
390-
391404
if (node->shutdown)
392405
{
393406
ExecEndNode(node->plan,node->plan);
394407
node->shutdown= false;
395408
}
396-
409+
if (node->curTuple)
410+
{
411+
pfree(node->curTuple);
412+
node->curTuple=NULL;
413+
}
397414
}
398415

399416
void

‎src/backend/nodes/copyfuncs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.95 1999/11/1502:00:01 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.96 1999/11/1503:28:06 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -551,6 +551,10 @@ _copySubPlan(SubPlan *from)
551551
newnode->parParam=listCopy(from->parParam);
552552
Node_Copy(from,newnode,sublink);
553553

554+
/* do not copy execution state */
555+
newnode->shutdown= false;
556+
newnode->curTuple=NULL;
557+
554558
returnnewnode;
555559
}
556560

‎src/backend/nodes/equalfuncs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.50 1999/10/07 04:23:04 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.51 1999/11/15 03:28:06 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -406,9 +406,13 @@ _equalIndexScan(IndexScan *a, IndexScan *b)
406406
staticbool
407407
_equalSubPlan(SubPlan*a,SubPlan*b)
408408
{
409+
/* should compare plans, but have to settle for comparing plan IDs */
409410
if (a->plan_id!=b->plan_id)
410411
return false;
411412

413+
if (!equal(a->rtable,b->rtable))
414+
return false;
415+
412416
if (!equal(a->sublink,b->sublink))
413417
return false;
414418

‎src/backend/nodes/freefuncs.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/Attic/freefuncs.c,v 1.26 1999/08/21 03:48:57 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/Attic/freefuncs.c,v 1.27 1999/11/15 03:28:07 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -441,6 +441,9 @@ _freeSubPlan(SubPlan *node)
441441
freeList(node->parParam);
442442
freeObject(node->sublink);
443443

444+
if (node->curTuple)
445+
pfree(node->curTuple);
446+
444447
pfree(node);
445448
}
446449

‎src/include/nodes/plannodes.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: plannodes.h,v 1.32 1999/11/1502:00:13 tgl Exp $
9+
* $Id: plannodes.h,v 1.33 1999/11/1503:28:06 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -352,13 +352,18 @@ typedef struct SubPlan
352352
* funcs for plan nodes... actually, we
353353
* could put *plan itself somewhere else
354354
* (TopPlan node ?)... */
355-
List*rtable;/* range table */
355+
List*rtable;/* range table for subselect */
356+
/* setParam and parParam are lists of integers (param IDs) */
356357
List*setParam;/* non-correlated EXPR & EXISTS subqueries
357358
* have to set some Params for paren Plan */
358359
List*parParam;/* indices of corr. Vars from parent plan */
359360
SubLink*sublink;/* SubLink node from parser; holds info about
360361
* what to do with subselect's results */
361-
boolshutdown;/* shutdown plan if TRUE */
362+
/*
363+
* Remaining fields are working state for executor; not used in planning
364+
*/
365+
boolshutdown;/* TRUE = need to shutdown plan */
366+
HeapTuplecurTuple;/* copy of most recent tuple from subplan */
362367
}SubPlan;
363368

364369
#endif/* PLANNODES_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp