diff options
author | Tom Lane | 2015-11-05 18:52:30 +0000 |
---|---|---|
committer | Tom Lane | 2015-11-05 18:52:40 +0000 |
commit | 8c75ad436f75fc629b61f601ba884c8f9313c9af (patch) | |
tree | 80d33393d43c659c57021bb129a5c8ba0734329c /src/pl/plpython/plpy_subxactobject.c | |
parent | 64b2e7ad917a9a7814904d0f6dbde52cefbcfa00 (diff) |
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
Diffstat (limited to 'src/pl/plpython/plpy_subxactobject.c')
-rw-r--r-- | src/pl/plpython/plpy_subxactobject.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/pl/plpython/plpy_subxactobject.c b/src/pl/plpython/plpy_subxactobject.c index 2e7ec4fdab4..81fb3a3a4ab 100644 --- a/src/pl/plpython/plpy_subxactobject.c +++ b/src/pl/plpython/plpy_subxactobject.c @@ -8,6 +8,7 @@ #include "access/xact.h" #include "executor/spi.h" +#include "utils/memutils.h" #include "plpython.h" @@ -132,16 +133,22 @@ PLy_subtransaction_enter(PyObject *self, PyObject *unused) subxact->started = true; oldcontext = CurrentMemoryContext; - subxactdata = PLy_malloc(sizeof(*subxactdata)); + subxactdata = (PLySubtransactionData *) + MemoryContextAlloc(TopTransactionContext, + sizeof(PLySubtransactionData)); + subxactdata->oldcontext = oldcontext; subxactdata->oldowner = CurrentResourceOwner; BeginInternalSubTransaction(NULL); - /* Do not want to leave the previous memory context */ - MemoryContextSwitchTo(oldcontext); + /* Be sure that cells of explicit_subtransactions list are long-lived */ + MemoryContextSwitchTo(TopTransactionContext); explicit_subtransactions = lcons(subxactdata, explicit_subtransactions); + /* Caller wants to stay in original memory context */ + MemoryContextSwitchTo(oldcontext); + Py_INCREF(self); return self; } @@ -204,7 +211,7 @@ PLy_subtransaction_exit(PyObject *self, PyObject *args) MemoryContextSwitchTo(subxactdata->oldcontext); CurrentResourceOwner = subxactdata->oldowner; - PLy_free(subxactdata); + pfree(subxactdata); /* * AtEOSubXact_SPI() should not have popped any SPI context, but just in |