postgresql/src/pl/plpython
Tom Lane cbfd4d0f88 Fix resource leaks in PL/Python error reporting, redux.
Commit c6f7f11d8 intended to prevent leaking any PyObject reference
counts in edge cases (such as out-of-memory during string
construction), but actually it introduced a leak in the normal case.
Repeating an error-trapping operation often enough would lead to
session-lifespan memory bloat.  The problem is that I failed to
think about the fact that PyObject_GetAttrString() increments the
refcount of the returned PyObject, so that simply walking down the
list of error frame objects causes all but the first one to have
their refcount incremented.

I experimented with several more-or-less-complex ways around that,
and eventually concluded that the right fix is simply to drop the
newly-obtained refcount as soon as we walk to the next frame
object in PLy_traceback.  This sounds unsafe, but it's perfectly
okay because the caller holds a refcount on the first frame object
and each frame object holds a refcount on the next one; so the
current frame object can't disappear underneath us.

By the same token, we can simplify the caller's cleanup back to
simply dropping its refcount on the first object.  Cleanup of
each frame object will lead in turn to the refcount of the next
one going to zero.

I also added a couple of comments explaining why PLy_elog_impl()
doesn't try to free the strings acquired from PLy_get_spi_error_data()
or PLy_get_error_data().  That's because I got here by looking at a
Coverity complaint about how those strings might get leaked.  They
are not leaked, but in testing that I discovered this other leak.

Back-patch, as c6f7f11d8 was.  It's a bit nervous-making to be
putting such a fix into v13, which is only a couple weeks from its
final release; but I can't see that leaving a recently-introduced
leak in place is a better idea.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/1203918.1761184159@sss.pgh.pa.us
Backpatch-through: 13
2025-10-23 11:47:46 -04:00
..
expected Fix recursive RECORD-returning plpython functions. 2024-05-09 13:16:21 -04:00
po Translation updates 2025-08-11 14:42:47 +02:00
sql Fix recursive RECORD-returning plpython functions. 2024-05-09 13:16:21 -04:00
.gitignore plpython: Remove regression test infrastructure for Python 2. 2022-03-07 18:20:51 -08:00
generate-spiexceptions.pl Update copyright for 2023 2023-01-02 15:00:37 -05:00
Makefile Don't put library-supplied -L/-I switches before user-supplied ones. 2025-07-29 15:17:41 -04:00
meson.build Fix mistaken file name in plpython's meson recipe. 2023-12-26 17:03:24 -05:00
nls.mk Break up long GETTEXT_FILES lists 2023-03-08 15:05:43 +01:00
plpy_cursorobject.c Repair memory leaks in plpython. 2025-01-11 11:45:56 -05:00
plpy_cursorobject.h Make PL/Python handle domain-type conversions correctly. 2017-11-16 16:23:04 -05:00
plpy_elog.c Fix resource leaks in PL/Python error reporting, redux. 2025-10-23 11:47:46 -04:00
plpy_elog.h Mark all symbols exported from extension libraries PGDLLEXPORT. 2022-07-17 18:50:14 -07:00
plpy_exec.c Fix recursive RECORD-returning plpython functions. 2024-05-09 13:16:21 -04:00
plpy_exec.h Phase 2 of pgindent updates. 2017-06-21 15:19:25 -04:00
plpy_main.c Fix various typos and incorrect/outdated name references 2023-04-19 13:50:33 +12:00
plpy_main.h Phase 2 of pgindent updates. 2017-06-21 15:19:25 -04:00
plpy_planobject.c plpython: Code cleanup related to removal of Python 2 support. 2022-03-07 18:30:28 -08:00
plpy_planobject.h Make PL/Python handle domain-type conversions correctly. 2017-11-16 16:23:04 -05:00
plpy_plpymodule.c plpython: Code cleanup related to removal of Python 2 support. 2022-03-07 18:30:28 -08:00
plpy_plpymodule.h plpython: add missing plpython.h include to plpy_plpymodule.h 2022-03-08 09:47:34 -08:00
plpy_procedure.c Don't corrupt plpython's "TD" dictionary in a recursive trigger call. 2024-05-07 18:15:00 -04:00
plpy_procedure.h Don't corrupt plpython's "TD" dictionary in a recursive trigger call. 2024-05-07 18:15:00 -04:00
plpy_resultobject.c Harmonize some more function parameter names. 2023-04-13 10:15:20 -07:00
plpy_resultobject.h Fix assorted header files that failed to compile standalone. 2019-05-31 11:45:33 -04:00
plpy_spi.c Repair memory leaks in plpython. 2025-01-11 11:45:56 -05:00
plpy_spi.h Fix SPI's handling of errors during transaction commit. 2022-02-28 12:45:36 -05:00
plpy_subxactobject.c Clear some style deviations. 2020-05-21 08:31:16 -07:00
plpy_subxactobject.h Make the order of the header file includes consistent. 2019-11-25 08:08:57 +05:30
plpy_typeio.c Tighten array dimensionality checks in Python -> SQL array conversion. 2023-05-04 11:00:33 -04:00
plpy_typeio.h Mark all symbols exported from extension libraries PGDLLEXPORT. 2022-07-17 18:50:14 -07:00
plpy_util.c plpython: Code cleanup related to removal of Python 2 support. 2022-03-07 18:30:28 -08:00
plpy_util.h Mark all symbols exported from extension libraries PGDLLEXPORT. 2022-07-17 18:50:14 -07:00
plpython.h Hide warnings from Python headers when using gcc-compatible compiler. 2023-12-26 16:16:29 -05:00
plpython3u--1.0.sql Invent "trusted" extensions, and remove the pg_pltemplate catalog. 2020-01-29 18:42:43 -05:00
plpython3u.control Create extension infrastructure for the core procedural languages. 2011-03-04 21:51:14 -05:00
plpython_system.h Hide warnings from Python headers when using gcc-compatible compiler. 2023-12-26 16:16:29 -05:00