pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedLack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
11+ messages / 4 participants
[nested] [flat]
Commits from this thread:
2015-07-20 f8d67ca8d4 b0b6f8d71f 4c11967e73 14ade020ff 3cb6ef9983 49c3000407 b2efbb71df Fix (some of) pltcl memory usage
* Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
@ 2015-06-11 19:22 Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Bill Parker @ 2015-06-11 19:22 UTC (permalink / raw)
To: pgsql-bugs
============================================================================
POSTGRESQL BUG REPORT TEMPLATE
============================================================================
Your name : Bill Parker
Your email address : wp02855 at gmail dot com
System Configuration:
---------------------
Architecture (example: Intel Pentium) : x86/x86-64/AMD
Operating System (example: Linux 2.4.18) : Linux 3.11.6-4
PostgreSQL version (example: PostgreSQL 9.4.3): PostgreSQL 9.4.x
Compiler used (example: gcc 3.3.5) : gcc version 4.8.1
Please enter a FULL description of your problem:
------------------------------------------------
Hello All,
In reviewing some code, in directory 'postgresql-9.4.3/src/pl/tcl',
file 'pltcl.c', there are several instances where calls to malloc()
are made, but no check for a return value of NULL is made, which
would indicate failure. Additionally, it appears when malloc()
returns NULL, previously allocated memory in function 'perm_fmgr_info'
is not released, which could lead to memory leaks (even though the
comment at the top says 'this routine is a crock' :)
If you know how this problem might be fixed, list the solution below:
---------------------------------------------------------------------
The patch file below addresses these issues:
inline patch 1: pltcl.c (+2/-0 lines) download inline-1.patch
--- pltcl.c.orig 2015-06-11 08:41:24.316077095 -0700
+++ pltcl.c 2015-06-11 08:48:49.186617853 -0700
@@ -2136,11 +2136,28 @@
* Allocate the new querydesc structure
************************************************************/
qdesc = (pltcl_query_desc *) malloc(sizeof(pltcl_query_desc));
+ if (qdesc == NULL)
+ ereport(ERROR, ((errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of
memory")));
snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
qdesc->nargs = nargs;
qdesc->argtypes = (Oid *) malloc(nargs * sizeof(Oid));
+ if (qdesc->argtypes == NULL) {
+ free(qdesc);
+ ereport(ERROR, ((errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of
memory")));
+ }
qdesc->arginfuncs = (FmgrInfo *) malloc(nargs * sizeof(FmgrInfo));
+ if (qdesc->arginfuncs == NULL) {
+ free(qdesc->argtypes);
+ free(qdesc);
+ ereport(ERROR, ((errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of
memory")));
+ }
qdesc->argtypioparams = (Oid *) malloc(nargs * sizeof(Oid));
+ if (qdesc->argtypioparams == NULL) {
+ free(qdesc->inargfuncs);
+ free(qdesc->argtypes);
+ free(qdesc);
+ }
+ ereport(ERROR, ((errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of
memory")));
/************************************************************
* Execute the prepare inside a sub-transaction, so we can cope with
Please feel free to review and comment on the above patch file...
I am attaching the patch file to this bug report
Bill Parker (wp02855 at gmail dot com)
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Patches and attachments: download all (2 files, .tar.gz)
pltcl.c.patch (application/octet-stream, 1.3K) download
--- pltcl.c.orig 2015-06-11 08:41:24.316077095 -0700
+++ pltcl.c 2015-06-11 08:48:49.186617853 -0700
@@ -2136,11 +2136,28 @@
* Allocate the new querydesc structure
************************************************************/
qdesc = (pltcl_query_desc *) malloc(sizeof(pltcl_query_desc));
+ if (qdesc == NULL)
+ ereport(ERROR, ((errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory")));
snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
qdesc->nargs = nargs;
qdesc->argtypes = (Oid *) malloc(nargs * sizeof(Oid));
+ if (qdesc->argtypes == NULL) {
+ free(qdesc);
+ ereport(ERROR, ((errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory")));
+ }
qdesc->arginfuncs = (FmgrInfo *) malloc(nargs * sizeof(FmgrInfo));
+ if (qdesc->arginfuncs == NULL) {
+ free(qdesc->argtypes);
+ free(qdesc);
+ ereport(ERROR, ((errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory")));
+ }
qdesc->argtypioparams = (Oid *) malloc(nargs * sizeof(Oid));
+ if (qdesc->argtypioparams == NULL) {
+ free(qdesc->inargfuncs);
+ free(qdesc->argtypes);
+ free(qdesc);
+ }
+ ereport(ERROR, ((errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory")));
/************************************************************
* Execute the prepare inside a sub-transaction, so we can cope with
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
@ 2015-06-12 07:05 ` Michael Paquier <michael.paquier@gmail.com>
2015-06-12 19:47 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 11+ messages in thread
From: Michael Paquier @ 2015-06-12 07:05 UTC (permalink / raw)
To: Bill Parker <wp02855@gmail.com>; +Cc: pgsql-bugs
On Fri, Jun 12, 2015 at 4:22 AM, Bill Parker wrote:
> In reviewing some code, in directory 'postgresql-9.4.3/src/pl/tcl',
> file 'pltcl.c', there are several instances where calls to malloc()
> are made, but no check for a return value of NULL is made, which
> would indicate failure. Additionally, it appears when malloc()
> returns NULL, previously allocated memory in function 'perm_fmgr_info'
> is not released, which could lead to memory leaks (even though the
> comment at the top says 'this routine is a crock' :)
>
> If you know how this problem might be fixed, list the solution below:
> Please feel free to review and comment on the above patch file...
Oh, nice catch again.
> I am attaching the patch file to this bug report
By the way, your patch does not compile properly and is not in-line
with the project's code format. See the updated patch attached ;)
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
20150612_pltcl_oom.patch (text/x-diff, 1.4K) download
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index d6b72f7..d0a5de4 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -2134,11 +2134,39 @@ pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
* Allocate the new querydesc structure
************************************************************/
qdesc = (pltcl_query_desc *) malloc(sizeof(pltcl_query_desc));
+ if (qdesc == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_OUT_OF_MEMORY),
+ errmsg("out of memory")));
snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
qdesc->nargs = nargs;
qdesc->argtypes = (Oid *) malloc(nargs * sizeof(Oid));
+ if (qdesc->argtypes == NULL)
+ {
+ free(qdesc);
+ ereport(ERROR,
+ (errcode(ERRCODE_OUT_OF_MEMORY),
+ errmsg("out of memory")));
+ }
qdesc->arginfuncs = (FmgrInfo *) malloc(nargs * sizeof(FmgrInfo));
+ if (qdesc->arginfuncs == NULL)
+ {
+ free(qdesc->argtypes);
+ free(qdesc);
+ ereport(ERROR,
+ (errcode(ERRCODE_OUT_OF_MEMORY),
+ errmsg("out of memory")));
+ }
qdesc->argtypioparams = (Oid *) malloc(nargs * sizeof(Oid));
+ if (qdesc->argtypioparams == NULL)
+ {
+ free(qdesc->arginfuncs);
+ free(qdesc->argtypes);
+ free(qdesc);
+ ereport(ERROR,
+ (errcode(ERRCODE_OUT_OF_MEMORY),
+ errmsg("out of memory")));
+ }
/************************************************************
* Execute the prepare inside a sub-transaction, so we can cope with
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
@ 2015-06-12 19:47 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-06-13 03:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 11+ messages in thread
From: Alvaro Herrera @ 2015-06-12 19:47 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Bill Parker <wp02855@gmail.com>; pgsql-bugs
Michael Paquier wrote:
> On Fri, Jun 12, 2015 at 4:22 AM, Bill Parker wrote:
> > In reviewing some code, in directory 'postgresql-9.4.3/src/pl/tcl',
> > file 'pltcl.c', there are several instances where calls to malloc()
> > are made, but no check for a return value of NULL is made, which
> > would indicate failure. Additionally, it appears when malloc()
> > returns NULL, previously allocated memory in function 'perm_fmgr_info'
> > is not released, which could lead to memory leaks (even though the
> > comment at the top says 'this routine is a crock' :)
> >
> > If you know how this problem might be fixed, list the solution below:
> > Please feel free to review and comment on the above patch file...
>
> Oh, nice catch again.
>
> > I am attaching the patch file to this bug report
>
> By the way, your patch does not compile properly and is not in-line
> with the project's code format. See the updated patch attached ;)
... or the conventions for allocating memory. Why not just use palloc()?
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-06-12 19:47 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2015-06-13 03:41 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-06-13 11:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Tom Lane @ 2015-06-13 03:41 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Bill Parker <wp02855@gmail.com>; pgsql-bugs
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
> Michael Paquier wrote:
>> By the way, your patch does not compile properly and is not in-line
>> with the project's code format. See the updated patch attached ;)
> ... or the conventions for allocating memory. Why not just use palloc()?
That's hardly the fault of the proposed patch. But yeah, it seems like
much the best fix here is to get rid of the malloc (and strdup) calls in
this code in favor of using the palloc infrastructure. Even the calls
that *do* have manual failure checks are not compliant with our usual
coding standards.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-06-12 19:47 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-06-13 03:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Tom Lane <tgl@sss.pgh.pa.us>
@ 2015-06-13 11:41 ` Michael Paquier <michael.paquier@gmail.com>
2015-07-18 15:54 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 11+ messages in thread
From: Michael Paquier @ 2015-06-13 11:41 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; Bill Parker <wp02855@gmail.com>; pgsql-bugs
On Sat, Jun 13, 2015 at 12:41 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Alvaro Herrera <alvherre@2ndquadrant.com> writes:
>> Michael Paquier wrote:
>>> By the way, your patch does not compile properly and is not in-line
>>> with the project's code format. See the updated patch attached ;)
>
>> ... or the conventions for allocating memory. Why not just use palloc()?
>
> That's hardly the fault of the proposed patch. But yeah, it seems like
> much the best fix here is to get rid of the malloc (and strdup) calls in
> this code in favor of using the palloc infrastructure. Even the calls
> that *do* have manual failure checks are not compliant with our usual
> coding standards.
Hm. Regarding the code path mentioned by Bill something like the patch
attached is enough with a memory context for the query description.
Now, perhaps we could do more efforts with prodesc as well, see for
example compile_pltcl_function for pltcl and similarly for plperl.
Thoughts?
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
20150613_pltcl_malloc.patch (text/x-diff, 2.4K) download
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index d6b72f7..b9f685e 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -2104,6 +2104,7 @@ static int
pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
int argc, CONST84 char *argv[])
{
+ volatile MemoryContext plan_cxt = NULL;
int nargs;
CONST84 char **args;
pltcl_query_desc *qdesc;
@@ -2132,13 +2133,23 @@ pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
/************************************************************
* Allocate the new querydesc structure
+ *
+ * The qdesc struct, as well as all its subsidiary data, lives in its
+ * plan_cxt.
************************************************************/
- qdesc = (pltcl_query_desc *) malloc(sizeof(pltcl_query_desc));
+ plan_cxt = AllocSetContextCreate(TopMemoryContext,
+ "PL/TCL spi_prepare query",
+ ALLOCSET_SMALL_MINSIZE,
+ ALLOCSET_SMALL_INITSIZE,
+ ALLOCSET_SMALL_MAXSIZE);
+ MemoryContextSwitchTo(plan_cxt);
+ qdesc = (pltcl_query_desc *) palloc0(sizeof(pltcl_query_desc));
snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
qdesc->nargs = nargs;
- qdesc->argtypes = (Oid *) malloc(nargs * sizeof(Oid));
- qdesc->arginfuncs = (FmgrInfo *) malloc(nargs * sizeof(FmgrInfo));
- qdesc->argtypioparams = (Oid *) malloc(nargs * sizeof(Oid));
+ qdesc->argtypes = (Oid *) palloc(nargs * sizeof(Oid));
+ qdesc->arginfuncs = (FmgrInfo *) palloc(nargs * sizeof(FmgrInfo));
+ qdesc->argtypioparams = (Oid *) palloc(nargs * sizeof(Oid));
+ MemoryContextSwitchTo(oldcontext);
/************************************************************
* Execute the prepare inside a sub-transaction, so we can cope with
@@ -2166,7 +2177,7 @@ pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
getTypeInputInfo(typId, &typInput, &typIOParam);
qdesc->argtypes[i] = typId;
- perm_fmgr_info(typInput, &(qdesc->arginfuncs[i]));
+ fmgr_info_cxt(typInput, &(qdesc->arginfuncs[i]), plan_cxt);
qdesc->argtypioparams[i] = typIOParam;
}
@@ -2193,10 +2204,11 @@ pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
{
pltcl_subtrans_abort(interp, oldcontext, oldowner);
- free(qdesc->argtypes);
- free(qdesc->arginfuncs);
- free(qdesc->argtypioparams);
- free(qdesc);
+ pfree(qdesc->argtypes);
+ pfree(qdesc->arginfuncs);
+ pfree(qdesc->argtypioparams);
+ pfree(qdesc);
+ MemoryContextDelete(plan_cxt);
ckfree((char *) args);
return TCL_ERROR;
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-06-12 19:47 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-06-13 03:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Tom Lane <tgl@sss.pgh.pa.us>
2015-06-13 11:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
@ 2015-07-18 15:54 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-18 20:19 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 11+ messages in thread
From: Alvaro Herrera @ 2015-07-18 15:54 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Bill Parker <wp02855@gmail.com>; pgsql-bugs
Michael Paquier wrote:
> On Sat, Jun 13, 2015 at 12:41 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> > Alvaro Herrera <alvherre@2ndquadrant.com> writes:
> >> Michael Paquier wrote:
> >>> By the way, your patch does not compile properly and is not in-line
> >>> with the project's code format. See the updated patch attached ;)
> >
> >> ... or the conventions for allocating memory. Why not just use palloc()?
> >
> > That's hardly the fault of the proposed patch. But yeah, it seems like
> > much the best fix here is to get rid of the malloc (and strdup) calls in
> > this code in favor of using the palloc infrastructure. Even the calls
> > that *do* have manual failure checks are not compliant with our usual
> > coding standards.
>
> Hm. Regarding the code path mentioned by Bill something like the patch
> attached is enough with a memory context for the query description.
Right. Note this no longer needs the individual pfree() when aborting,
because the MemoryContextDelete would remove the whole thing. (Don't
bother resubmitting; I have already fixed it.) Will push shortly.
> Now, perhaps we could do more efforts with prodesc as well, see for
> example compile_pltcl_function for pltcl and similarly for plperl.
> Thoughts?
Right. It would simplify the code: create a memory context child of
TopTransactionContext, then compile the function, and if successful,
then MemoryContextSetParent to some longer-lived context. When the
function is invalidated, it's sufficient to delete the context and
create a new one. Creating the context as child of
TopTransactionContext allows you to avoid an explicit
MemoryContextDelete() in the elog(ERROR) cases while compiling.
I do wonder how many users this code has ...
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-06-12 19:47 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-06-13 03:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Tom Lane <tgl@sss.pgh.pa.us>
2015-06-13 11:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-07-18 15:54 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2015-07-18 20:19 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-19 15:20 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Alvaro Herrera @ 2015-07-18 20:19 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Bill Parker <wp02855@gmail.com>; pgsql-bugs
Alvaro Herrera wrote:
> > Now, perhaps we could do more efforts with prodesc as well, see for
> > example compile_pltcl_function for pltcl and similarly for plperl.
> > Thoughts?
>
> Right. It would simplify the code: create a memory context child of
> TopTransactionContext, then compile the function, and if successful,
> then MemoryContextSetParent to some longer-lived context. When the
> function is invalidated, it's sufficient to delete the context and
> create a new one. Creating the context as child of
> TopTransactionContext allows you to avoid an explicit
> MemoryContextDelete() in the elog(ERROR) cases while compiling.
With some additional effort, we could get rid of perm_fmgr_info, at
least in pltcl. (That hack was introduced in a3ed622b63b and
7748e9e7e5a back in 2001 and we never actually fixed it ...)
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-06-12 19:47 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-06-13 03:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Tom Lane <tgl@sss.pgh.pa.us>
2015-06-13 11:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-07-18 15:54 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-18 20:19 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2015-07-19 15:20 ` Michael Paquier <michael.paquier@gmail.com>
2015-07-19 15:44 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-20 12:29 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 2 replies; 11+ messages in thread
From: Michael Paquier @ 2015-07-19 15:20 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Bill Parker <wp02855@gmail.com>; pgsql-bugs
On Sun, Jul 19, 2015 at 5:19 AM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:
> Alvaro Herrera wrote:
>
>> > Now, perhaps we could do more efforts with prodesc as well, see for
>> > example compile_pltcl_function for pltcl and similarly for plperl.
>> > Thoughts?
>>
>> Right. It would simplify the code: create a memory context child of
>> TopTransactionContext, then compile the function, and if successful,
>> then MemoryContextSetParent to some longer-lived context. When the
>> function is invalidated, it's sufficient to delete the context and
>> create a new one. Creating the context as child of
>> TopTransactionContext allows you to avoid an explicit
>> MemoryContextDelete() in the elog(ERROR) cases while compiling.
>
> With some additional effort, we could get rid of perm_fmgr_info, at
> least in pltcl. (That hack was introduced in a3ed622b63b and
> 7748e9e7e5a back in 2001 and we never actually fixed it ...)
Yes it seems so, even for plperl and plpython there seem to be some
room for improvement at quick glance... This looks like a master-only
change to me though (and I am sure we are on the same page). For
back-branches something like the previous patch is definitely safer.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-06-12 19:47 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-06-13 03:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Tom Lane <tgl@sss.pgh.pa.us>
2015-06-13 11:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-07-18 15:54 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-18 20:19 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-19 15:20 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
@ 2015-07-19 15:44 ` Alvaro Herrera <alvherre@2ndquadrant.com>
1 sibling, 0 replies; 11+ messages in thread
From: Alvaro Herrera @ 2015-07-19 15:44 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Bill Parker <wp02855@gmail.com>; pgsql-bugs
Michael Paquier wrote:
> On Sun, Jul 19, 2015 at 5:19 AM, Alvaro Herrera
> <alvherre@2ndquadrant.com> wrote:
> > With some additional effort, we could get rid of perm_fmgr_info, at
> > least in pltcl. (That hack was introduced in a3ed622b63b and
> > 7748e9e7e5a back in 2001 and we never actually fixed it ...)
>
> Yes it seems so, even for plperl and plpython there seem to be some
> room for improvement at quick glance... This looks like a master-only
> change to me though (and I am sure we are on the same page). For
> back-branches something like the previous patch is definitely safer.
Yes, agreed on both counts. I will push the patch when I have a moment.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-06-12 19:47 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-06-13 03:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Tom Lane <tgl@sss.pgh.pa.us>
2015-06-13 11:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-07-18 15:54 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-18 20:19 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-19 15:20 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
@ 2015-07-20 12:29 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-22 07:57 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
1 sibling, 1 reply; 11+ messages in thread
From: Alvaro Herrera @ 2015-07-20 12:29 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Bill Parker <wp02855@gmail.com>; pgsql-bugs
Michael Paquier wrote:
> > With some additional effort, we could get rid of perm_fmgr_info, at
> > least in pltcl. (That hack was introduced in a3ed622b63b and
> > 7748e9e7e5a back in 2001 and we never actually fixed it ...)
>
> Yes it seems so, even for plperl and plpython there seem to be some
> room for improvement at quick glance... This looks like a master-only
> change to me though (and I am sure we are on the same page). For
> back-branches something like the previous patch is definitely safer.
Pushed.
All in all, the whole business of memory allocation in pltcl is pretty
nasty. If people is really interested in pltcl, that should probably be
fixed to avoid memory leaks. For one thing, the query cache hash is
global to the interpreter, not local to the function; whenever a
function is recompiled, the old SPI_prepared plans linger forever in the
interpreter (for the life of the session). It would be nicer if those
were tied to the function lifetime. For this, the hashtable would have
to be moved to the query desc struct. pltcl_spi_prepare would have to
know the currently executing function desc struct ...
Maybe Bill is interested enough to write a patch?
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-06-12 19:47 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-06-13 03:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Tom Lane <tgl@sss.pgh.pa.us>
2015-06-13 11:41 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-07-18 15:54 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-18 20:19 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-19 15:20 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Michael Paquier <michael.paquier@gmail.com>
2015-07-20 12:29 ` Re: Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2015-07-22 07:57 ` Michael Paquier <michael.paquier@gmail.com>
0 siblings, 0 replies; 11+ messages in thread
From: Michael Paquier @ 2015-07-22 07:57 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Bill Parker <wp02855@gmail.com>; pgsql-bugs
On Mon, Jul 20, 2015 at 9:29 PM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:
> Michael Paquier wrote:
>
>> > With some additional effort, we could get rid of perm_fmgr_info, at
>> > least in pltcl. (That hack was introduced in a3ed622b63b and
>> > 7748e9e7e5a back in 2001 and we never actually fixed it ...)
>>
>> Yes it seems so, even for plperl and plpython there seem to be some
>> room for improvement at quick glance... This looks like a master-only
>> change to me though (and I am sure we are on the same page). For
>> back-branches something like the previous patch is definitely safer.
>
> Pushed.
>
> All in all, the whole business of memory allocation in pltcl is pretty
> nasty. If people is really interested in pltcl, that should probably be
> fixed to avoid memory leaks. For one thing, the query cache hash is
> global to the interpreter, not local to the function; whenever a
> function is recompiled, the old SPI_prepared plans linger forever in the
> interpreter (for the life of the session). It would be nicer if those
> were tied to the function lifetime. For this, the hashtable would have
> to be moved to the query desc struct. pltcl_spi_prepare would have to
> know the currently executing function desc struct ...
If this is done, the same can be directly applied to plperl. For
plpython, we are going to visibly need a memory context initialized at
some point and rely on it for allocations instead of TopMemoryContext
when doing the various allocations... Do you think it is worth it in
this case?
For what it's worth, I have been playing for a couple of minutes with
the code of pltcl and plperl to replace the remaining malloc calls by
a memory context and remove the calls to perm_func_ctx. This does not
fix the memory leaks of pltcl when functions are recompiled by this is
working somewhat nicely with plperl by relying on free_plperl_function
and check-world passes. So attached they are. This seem to improve a
bit things to me. Thoughts?
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments: download all (3 files, .tar.gz)
0001-Replace-use-of-malloc-by-an-internal-memory-context-.patch (application/x-patch, 6.9K) download
From 8a5b5f4212ee9089b8fbf500c6fc6c3feb717aa8 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@otacoo.com>
Date: Wed, 22 Jul 2015 15:07:29 +0900
Subject: [PATCH 1/3] Replace use of malloc by an internal memory context in
pltcl
This simplifies the code to not rely anymore on malloc() calls and
removes an old tweak to cache function information in the top memory
context of a backend.
---
src/pl/tcl/pltcl.c | 82 +++++++++++-------------------------------------------
1 file changed, 17 insertions(+), 65 deletions(-)
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index 48a3206..ef0e3fa 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -303,23 +303,6 @@ pltcl_WaitForEvent(CONST86 Tcl_Time *timePtr)
/*
- * This routine is a crock, and so is everyplace that calls it. The problem
- * is that the cached form of pltcl functions/queries is allocated permanently
- * (mostly via malloc()) and never released until backend exit. Subsidiary
- * data structures such as fmgr info records therefore must live forever
- * as well. A better implementation would store all this stuff in a per-
- * function memory context that could be reclaimed at need. In the meantime,
- * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
- * it might allocate, and whatever the eventual function might allocate using
- * fn_mcxt, will live forever too.
- */
-static void
-perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
-{
- fmgr_info_cxt(functionId, finfo, TopMemoryContext);
-}
-
-/*
* _PG_init() - library load-time initialization
*
* DO NOT make this static nor change its name!
@@ -1222,12 +1205,14 @@ static pltcl_proc_desc *
compile_pltcl_function(Oid fn_oid, Oid tgreloid,
bool is_event_trigger, bool pltrusted)
{
+ volatile MemoryContext plan_cxt = NULL;
HeapTuple procTup;
Form_pg_proc procStruct;
pltcl_proc_key proc_key;
pltcl_proc_ptr *proc_ptr;
bool found;
pltcl_proc_desc *prodesc;
+ MemoryContext oldcontext = CurrentMemoryContext;
/* We'll need the pg_proc tuple in any case... */
procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
@@ -1309,21 +1294,21 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid,
/************************************************************
* Allocate a new procedure description block
+ *
+ * struct prodesc and subsidiary data all live in plan_cxt.
************************************************************/
- prodesc = (pltcl_proc_desc *) malloc(sizeof(pltcl_proc_desc));
- if (prodesc == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_OUT_OF_MEMORY),
- errmsg("out of memory")));
- MemSet(prodesc, 0, sizeof(pltcl_proc_desc));
- prodesc->user_proname = strdup(NameStr(procStruct->proname));
- prodesc->internal_proname = strdup(internal_proname);
- if (prodesc->user_proname == NULL || prodesc->internal_proname == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_OUT_OF_MEMORY),
- errmsg("out of memory")));
+ plan_cxt = AllocSetContextCreate(TopMemoryContext,
+ "PL/TCL compile",
+ ALLOCSET_SMALL_MINSIZE,
+ ALLOCSET_SMALL_INITSIZE,
+ ALLOCSET_SMALL_MAXSIZE);
+ MemoryContextSwitchTo(plan_cxt);
+ prodesc = (pltcl_proc_desc *) palloc0(sizeof(pltcl_proc_desc));
+ prodesc->user_proname = pstrdup(NameStr(procStruct->proname));
+ prodesc->internal_proname = pstrdup(internal_proname);
prodesc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
prodesc->fn_tid = procTup->t_self;
+ MemoryContextSwitchTo(oldcontext);
/* Remember if function is STABLE/IMMUTABLE */
prodesc->fn_readonly =
@@ -1347,13 +1332,8 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid,
SearchSysCache1(TYPEOID,
ObjectIdGetDatum(procStruct->prorettype));
if (!HeapTupleIsValid(typeTup))
- {
- free(prodesc->user_proname);
- free(prodesc->internal_proname);
- free(prodesc);
elog(ERROR, "cache lookup failed for type %u",
procStruct->prorettype);
- }
typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
/* Disallow pseudotype result, except VOID */
@@ -1363,37 +1343,22 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid,
/* okay */ ;
else if (procStruct->prorettype == TRIGGEROID ||
procStruct->prorettype == EVTTRIGGEROID)
- {
- free(prodesc->user_proname);
- free(prodesc->internal_proname);
- free(prodesc);
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("trigger functions can only be called as triggers")));
- }
else
- {
- free(prodesc->user_proname);
- free(prodesc->internal_proname);
- free(prodesc);
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("PL/Tcl functions cannot return type %s",
format_type_be(procStruct->prorettype))));
- }
}
if (typeStruct->typtype == TYPTYPE_COMPOSITE)
- {
- free(prodesc->user_proname);
- free(prodesc->internal_proname);
- free(prodesc);
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("PL/Tcl functions cannot return composite types")));
- }
- perm_fmgr_info(typeStruct->typinput, &(prodesc->result_in_func));
+ fmgr_info_cxt(typeStruct->typinput, &(prodesc->result_in_func), plan_cxt);
prodesc->result_typioparam = getTypeIOParam(typeTup);
ReleaseSysCache(typeTup);
@@ -1412,26 +1377,16 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid,
typeTup = SearchSysCache1(TYPEOID,
ObjectIdGetDatum(procStruct->proargtypes.values[i]));
if (!HeapTupleIsValid(typeTup))
- {
- free(prodesc->user_proname);
- free(prodesc->internal_proname);
- free(prodesc);
elog(ERROR, "cache lookup failed for type %u",
procStruct->proargtypes.values[i]);
- }
typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
/* Disallow pseudotype argument */
if (typeStruct->typtype == TYPTYPE_PSEUDO)
- {
- free(prodesc->user_proname);
- free(prodesc->internal_proname);
- free(prodesc);
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("PL/Tcl functions cannot accept type %s",
format_type_be(procStruct->proargtypes.values[i]))));
- }
if (typeStruct->typtype == TYPTYPE_COMPOSITE)
{
@@ -1441,8 +1396,8 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid,
else
{
prodesc->arg_is_rowtype[i] = false;
- perm_fmgr_info(typeStruct->typoutput,
- &(prodesc->arg_out_func[i]));
+ fmgr_info_cxt(typeStruct->typoutput,
+ &(prodesc->arg_out_func[i]), plan_cxt);
snprintf(buf, sizeof(buf), "%d", i + 1);
}
@@ -1541,9 +1496,6 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid,
Tcl_DStringFree(&proc_internal_def);
if (tcl_rc != TCL_OK)
{
- free(prodesc->user_proname);
- free(prodesc->internal_proname);
- free(prodesc);
UTF_BEGIN;
elog(ERROR, "could not create internal procedure \"%s\": %s",
internal_proname, UTF_U2E(Tcl_GetStringResult(interp)));
--
2.4.6
0002-Replace-use-of-malloc-by-an-internal-memory-context-.patch (application/x-patch, 4.8K) download
From 5aeefad98f7929355a231e5782a19402adc583e3 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@otacoo.com>
Date: Wed, 22 Jul 2015 15:39:44 +0900
Subject: [PATCH 2/3] Replace use of malloc by an internal memory context in
plperl
This makes the code more flexible by using the internal memory context
infrastructure instead of a system-level memory call.
---
src/pl/plperl/plperl.c | 54 ++++++++++++++++++++++++--------------------------
1 file changed, 26 insertions(+), 28 deletions(-)
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c
index 78baaac..327d262 100644
--- a/src/pl/plperl/plperl.c
+++ b/src/pl/plperl/plperl.c
@@ -108,6 +108,7 @@ typedef struct plperl_proc_desc
TransactionId fn_xmin; /* xmin/TID of procedure's pg_proc tuple */
ItemPointerData fn_tid;
int refcount; /* reference count of this struct */
+ MemoryContext memcxt; /* memory context where this lives */
SV *reference; /* CODE reference for Perl sub */
plperl_interp_desc *interp; /* interpreter it's created in */
bool fn_readonly; /* is function readonly (not volatile)? */
@@ -2588,18 +2589,16 @@ free_plperl_function(plperl_proc_desc *prodesc)
SvREFCNT_dec(prodesc->reference);
activate_interpreter(oldinterp);
}
- /* Get rid of what we conveniently can of our own structs */
- /* (FmgrInfo subsidiary info will get leaked ...) */
- if (prodesc->proname)
- free(prodesc->proname);
- list_free(prodesc->trftypes);
- free(prodesc);
+
+ /* Finish destroying this data */
+ MemoryContextDelete(prodesc->memcxt);
}
static plperl_proc_desc *
compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
{
+ volatile MemoryContext plan_cxt = NULL;
HeapTuple procTup;
Form_pg_proc procStruct;
plperl_proc_key proc_key;
@@ -2608,6 +2607,7 @@ compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
int i;
plperl_interp_desc *oldinterp = plperl_active_interp;
ErrorContextCallback plperl_error_context;
+ MemoryContext oldcontext = CurrentMemoryContext;
/* We'll need the pg_proc tuple in any case... */
procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
@@ -2662,38 +2662,33 @@ compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
/************************************************************
* Allocate a new procedure description block
+ *
+ * struct prodesc and subsidiary data all live in plan_cxt.
************************************************************/
- prodesc = (plperl_proc_desc *) malloc(sizeof(plperl_proc_desc));
- if (prodesc == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_OUT_OF_MEMORY),
- errmsg("out of memory")));
- /* Initialize all fields to 0 so free_plperl_function is safe */
- MemSet(prodesc, 0, sizeof(plperl_proc_desc));
-
+ plan_cxt = AllocSetContextCreate(TopMemoryContext,
+ "PL/Perl compile",
+ ALLOCSET_SMALL_MINSIZE,
+ ALLOCSET_SMALL_INITSIZE,
+ ALLOCSET_SMALL_MAXSIZE);
+ MemoryContextSwitchTo(plan_cxt);
+ prodesc = (plperl_proc_desc *) palloc0(sizeof(plperl_proc_desc));
prodesc->proname = strdup(NameStr(procStruct->proname));
- if (prodesc->proname == NULL)
- {
- free_plperl_function(prodesc);
- ereport(ERROR,
- (errcode(ERRCODE_OUT_OF_MEMORY),
- errmsg("out of memory")));
- }
+ MemoryContextSwitchTo(oldcontext);
+ prodesc->memcxt = plan_cxt;
prodesc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
prodesc->fn_tid = procTup->t_self;
+
/* Remember if function is STABLE/IMMUTABLE */
prodesc->fn_readonly =
(procStruct->provolatile != PROVOLATILE_VOLATILE);
{
- MemoryContext oldcxt;
-
protrftypes_datum = SysCacheGetAttr(PROCOID, procTup,
Anum_pg_proc_protrftypes, &isnull);
- oldcxt = MemoryContextSwitchTo(TopMemoryContext);
+ MemoryContextSwitchTo(plan_cxt);
prodesc->trftypes = isnull ? NIL : oid_array_to_list(protrftypes_datum);
- MemoryContextSwitchTo(oldcxt);
+ MemoryContextSwitchTo(oldcontext);
}
/************************************************************
@@ -2762,7 +2757,9 @@ compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
prodesc->fn_retisarray =
(typeStruct->typlen == -1 && typeStruct->typelem);
- perm_fmgr_info(typeStruct->typinput, &(prodesc->result_in_func));
+ fmgr_info_cxt(typeStruct->typinput,
+ &(prodesc->result_in_func),
+ plan_cxt);
prodesc->result_typioparam = getTypeIOParam(typeTup);
ReleaseSysCache(typeTup);
@@ -2804,8 +2801,9 @@ compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
else
{
prodesc->arg_is_rowtype[i] = false;
- perm_fmgr_info(typeStruct->typoutput,
- &(prodesc->arg_out_func[i]));
+ fmgr_info_cxt(typeStruct->typoutput,
+ &(prodesc->arg_out_func[i]),
+ plan_cxt);
}
/* Identify array attributes */
--
2.4.6
0003-Remove-the-use-of-perm_fmgr_info-in-pl-Perl.patch (application/x-patch, 2.1K) download
From 26c6048fb69204b5e6800a5e03e7ac701e723846 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@otacoo.com>
Date: Wed, 22 Jul 2015 15:46:02 +0900
Subject: [PATCH 3/3] Remove the use of perm_fmgr_info in pl/Perl
Function information was being accessed from the top memory context
but actually it seems just fine to use the current memory context as
the information where is stored the function information is present
there are well.
---
src/pl/plperl/plperl.c | 21 ++-------------------
1 file changed, 2 insertions(+), 19 deletions(-)
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c
index 327d262..1cf3ab6 100644
--- a/src/pl/plperl/plperl.c
+++ b/src/pl/plperl/plperl.c
@@ -353,23 +353,6 @@ hek2cstr(HE *he)
return ret;
}
-/*
- * This routine is a crock, and so is everyplace that calls it. The problem
- * is that the cached form of plperl functions/queries is allocated permanently
- * (mostly via malloc()) and never released until backend exit. Subsidiary
- * data structures such as fmgr info records therefore must live forever
- * as well. A better implementation would store all this stuff in a per-
- * function memory context that could be reclaimed at need. In the meantime,
- * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
- * it might allocate, and whatever the eventual function might allocate using
- * fn_mcxt, will live forever too.
- */
-static void
-perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
-{
- fmgr_info_cxt(functionId, finfo, TopMemoryContext);
-}
-
/*
* _PG_init() - library load-time initialization
@@ -1431,9 +1414,9 @@ plperl_ref_from_pg_array(Datum arg, Oid typid)
&typdelim, &typioparam, &typoutputfunc);
if ((transform_funcid = get_transform_fromsql(elementtype, current_call_data->prodesc->lang_oid, current_call_data->prodesc->trftypes)))
- perm_fmgr_info(transform_funcid, &info->transform_proc);
+ fmgr_info(transform_funcid, &info->transform_proc);
else
- perm_fmgr_info(typoutputfunc, &info->proc);
+ fmgr_info(typoutputfunc, &info->proc);
info->elem_is_rowtype = type_is_rowtype(elementtype);
--
2.4.6
^ permalink raw reply [nested|flat] 11+ messages in thread
end of thread, other threads:[~2015-07-22 07:57 UTC | newest]
Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-06-11 19:22 Lack of Sanity Checking in file 'pctcl.c' for PostgreSQL 9.4.x Bill Parker <wp02855@gmail.com>
2015-06-12 07:05 ` Michael Paquier <michael.paquier@gmail.com>
2015-06-12 19:47 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-06-13 03:41 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-06-13 11:41 ` Michael Paquier <michael.paquier@gmail.com>
2015-07-18 15:54 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-18 20:19 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-19 15:20 ` Michael Paquier <michael.paquier@gmail.com>
2015-07-19 15:44 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-20 12:29 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-07-22 07:57 ` Michael Paquier <michael.paquier@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox