Fix memory leaks in PL/Python.
Tom Lane <tgl@sss.pgh.pa.us>
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
Files
| Path | Change | +/− |
|---|---|---|
| src/pl/plpython/plpy_cursorobject.c | modified | +21 −9 |
| src/pl/plpython/plpy_cursorobject.h | modified | +1 −0 |
| src/pl/plpython/plpy_exec.c | modified | +1 −1 |
| src/pl/plpython/plpy_main.c | modified | +30 −9 |
| src/pl/plpython/plpy_main.h | modified | +3 −0 |
| src/pl/plpython/plpy_planobject.c | modified | +8 −11 |
| src/pl/plpython/plpy_planobject.h | modified | +1 −0 |
| src/pl/plpython/plpy_procedure.c | modified | +52 −66 |
| src/pl/plpython/plpy_procedure.h | modified | +2 −0 |
| src/pl/plpython/plpy_spi.c | modified | +23 −7 |
| src/pl/plpython/plpy_subxactobject.c | modified | +11 −4 |
| src/pl/plpython/plpy_typeio.c | modified | +64 −73 |
| src/pl/plpython/plpy_typeio.h | modified | +4 −2 |
| src/pl/plpython/plpy_util.c | modified | +0 −36 |
| src/pl/plpython/plpy_util.h | modified | +0 −5 |