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

Commit4a67565

Browse files
committed
Fix within-function memory leaks in the various PLs' interfaces to
SPI_prepare: they all save the prepared plan into topCxt, and so theprocCxt copy that's actually returned by SPI_prepare ought to be freed.Diagnosis and plpython fix by Nigel Andrews, followup for other PLsby Tom Lane.
1 parent30c2b5e commit4a67565

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

‎src/pl/plpgsql/src/pl_exec.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* procedural language
44
*
55
* IDENTIFICATION
6-
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.64 2002/09/05 00:43:07 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.65 2002/10/19 22:10:58 tgl Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -2018,6 +2018,7 @@ exec_prepare_plan(PLpgSQL_execstate * estate,
20182018
expr->plan_simple_expr=NULL;
20192019
exec_simple_check_plan(expr);
20202020

2021+
SPI_freeplan(plan);
20212022
pfree(argtypes);
20222023
}
20232024

@@ -2544,10 +2545,14 @@ exec_stmt_open(PLpgSQL_execstate * estate, PLpgSQL_stmt_open * stmt)
25442545
* ----------
25452546
*/
25462547
curplan=SPI_prepare(querystr,0,NULL);
2548+
if (curplan==NULL)
2549+
elog(ERROR,"SPI_prepare() failed for dynamic query \"%s\"",
2550+
querystr);
25472551
portal=SPI_cursor_open(curname,curplan,NULL,NULL);
25482552
if (portal==NULL)
25492553
elog(ERROR,"Failed to open cursor");
25502554
pfree(querystr);
2555+
SPI_freeplan(curplan);
25512556

25522557
/* ----------
25532558
* Store the eventually assigned cursor name in the cursor variable

‎src/pl/plpython/plpython.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
3030
*
3131
* IDENTIFICATION
32-
*$Header: /cvsroot/pgsql/src/pl/plpython/plpython.c,v 1.25 2002/10/14 04:20:52 momjian Exp $
32+
*$Header: /cvsroot/pgsql/src/pl/plpython/plpython.c,v 1.26 2002/10/19 22:10:58 tgl Exp $
3333
*
3434
*********************************************************************
3535
*/
@@ -1837,21 +1837,7 @@ PLy_plan_dealloc(PyObject * arg)
18371837
enter();
18381838

18391839
if (ob->plan)
1840-
{
1841-
/*
1842-
* free the plan... pfree(ob->plan);
1843-
*
1844-
* FIXME -- leaks saved plan on object destruction. can this be
1845-
* avoided?
1846-
* I think so. A function prepares and then execp's a statement.
1847-
* When we come to deallocate the 'statement' object we obviously
1848-
* no long need the plan. Even if we did, without the object
1849-
* we're never going to be able to use it again.
1850-
* In the against arguments: SPI_saveplan has stuck this under
1851-
* the top context so there must be a reason for doing that.
1852-
*/
1853-
pfree(ob->plan);
1854-
}
1840+
SPI_freeplan(ob->plan);
18551841
if (ob->types)
18561842
PLy_free(ob->types);
18571843
if (ob->args)
@@ -2023,6 +2009,7 @@ PLy_spi_prepare(PyObject * self, PyObject * args)
20232009
PyObject*list=NULL;
20242010
PyObject*volatileoptr=NULL;
20252011
char*query;
2012+
void*tmpplan;
20262013

20272014
enter();
20282015

@@ -2062,7 +2049,6 @@ PLy_spi_prepare(PyObject * self, PyObject * args)
20622049
intnargs,
20632050
i;
20642051

2065-
20662052
nargs=PySequence_Length(list);
20672053
if (nargs>0)
20682054
{
@@ -2125,7 +2111,10 @@ PLy_spi_prepare(PyObject * self, PyObject * args)
21252111
RAISE_EXC(1);
21262112
}
21272113

2128-
plan->plan=SPI_saveplan(plan->plan);
2114+
/* transfer plan from procCxt to topCxt */
2115+
tmpplan=plan->plan;
2116+
plan->plan=SPI_saveplan(tmpplan);
2117+
SPI_freeplan(tmpplan);
21292118
if (plan->plan==NULL)
21302119
{
21312120
PLy_exception_set(PLy_exc_spi_error,

‎src/pl/tcl/pltcl.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* ENHANCEMENTS, OR MODIFICATIONS.
3232
*
3333
* IDENTIFICATION
34-
* $Header: /cvsroot/pgsql/src/pl/tcl/pltcl.c,v 1.65 2002/10/14 04:20:52 momjian Exp $
34+
* $Header: /cvsroot/pgsql/src/pl/tcl/pltcl.c,v 1.66 2002/10/19 22:10:58 tgl Exp $
3535
*
3636
**********************************************************************/
3737

@@ -1285,7 +1285,7 @@ pltcl_elog(ClientData cdata, Tcl_Interp *interp,
12851285
elseif (strcmp(argv[1],"NOTICE")==0)
12861286
level=NOTICE;
12871287
elseif (strcmp(argv[1],"WARNING")==0)
1288-
level=ERROR;
1288+
level=WARNING;
12891289
elseif (strcmp(argv[1],"ERROR")==0)
12901290
level=ERROR;
12911291
elseif (strcmp(argv[1],"FATAL")==0)
@@ -1837,7 +1837,8 @@ pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
18371837
}
18381838

18391839
/************************************************************
1840-
* Save the plan
1840+
* Save the plan into permanent memory (right now it's in the
1841+
* SPI procCxt, which will go away at function end).
18411842
************************************************************/
18421843
qdesc->plan=SPI_saveplan(plan);
18431844
if (qdesc->plan==NULL)
@@ -1866,6 +1867,8 @@ pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
18661867

18671868
elog(ERROR,"pltcl: SPI_saveplan() failed - %s",reason);
18681869
}
1870+
/* Release the procCxt copy to avoid within-function memory leak */
1871+
SPI_freeplan(plan);
18691872

18701873
/************************************************************
18711874
* Insert a hashtable entry for the plan and return

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp