From 8c75ad436f75fc629b61f601ba884c8f9313c9af Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 5 Nov 2015 13:52:30 -0500 Subject: Fix memory leaks in PL/Python. Previously, plpython was in the habit of allocating a lot of stuff in TopMemoryContext, and it was very slipshod about making sure that stuff got cleaned up; in particular, use of TopMemoryContext as fn_mcxt for function calls represents an unfixable leak, since we generally don't know what the called function might have allocated in fn_mcxt. This results in session-lifespan leakage in certain usage scenarios, as for example in a case reported by Ed Behn back in July. To fix, get rid of all the retail allocations in TopMemoryContext. All long-lived allocations are now made in sub-contexts that are associated with specific objects (either pl/python procedures, or Python-visible objects such as cursors and plans). We can clean these up when the associated object is deleted. I went so far as to get rid of PLy_malloc completely. There were a couple of places where it could still have been used safely, but on the whole it was just an invitation to bad coding. Haribabu Kommi, based on a draft patch by Heikki Linnakangas; some further work by me --- src/pl/plpython/plpy_planobject.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'src/pl/plpython/plpy_planobject.c') diff --git a/src/pl/plpython/plpy_planobject.c b/src/pl/plpython/plpy_planobject.c index 8305bd68e96..a9040efb502 100644 --- a/src/pl/plpython/plpy_planobject.c +++ b/src/pl/plpython/plpy_planobject.c @@ -11,6 +11,7 @@ #include "plpy_planobject.h" #include "plpy_elog.h" +#include "utils/memutils.h" static void PLy_plan_dealloc(PyObject *arg); @@ -80,6 +81,7 @@ PLy_plan_new(void) ob->types = NULL; ob->values = NULL; ob->args = NULL; + ob->mcxt = NULL; return (PyObject *) ob; } @@ -96,20 +98,15 @@ PLy_plan_dealloc(PyObject *arg) PLyPlanObject *ob = (PLyPlanObject *) arg; if (ob->plan) + { SPI_freeplan(ob->plan); - if (ob->types) - PLy_free(ob->types); - if (ob->values) - PLy_free(ob->values); - if (ob->args) + ob->plan = NULL; + } + if (ob->mcxt) { - int i; - - for (i = 0; i < ob->nargs; i++) - PLy_typeinfo_dealloc(&ob->args[i]); - PLy_free(ob->args); + MemoryContextDelete(ob->mcxt); + ob->mcxt = NULL; } - arg->ob_type->tp_free(arg); } -- cgit v1.2.3