pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid memory leak in validation of a PL/Python trigger function.
Date: Thu, 23 Oct 2025 18:23:31 +0000
Message-ID: <E1vBzyF-0039wc-2y@gemulon.postgresql.org> (raw)

Avoid memory leak in validation of a PL/Python trigger function.

If we're trying to perform check_function_bodies validation
of a PL/Python trigger function, we create a new PLyProcedure,
but we don't put it into the PLy_procedure_cache hash table.
(Doing so would be useless, since we don't have the relation
OID that is part of the cache key for a trigger function, so
we could not make an entry that would be found by later uses.)
However, we didn't think through what to do instead, with the
result that the PLyProcedure was simply leaked.

It would take a pretty large number of CREATE FUNCTION operations
for this to amount to a serious problem, but it's easy to see the
memory bloat if you do CREATE OR REPLACE FUNCTION in a loop.

To fix, have PLy_procedure_get delete the new PLyProcedure
and return NULL if it's not going to cache the PLyProcedure.
I considered making plpython3_validator do the cleanup instead,
which would be more natural.  But then plpython3_validator would
have to know the rules under which PLy_procedure_get returns a
non-cached PLyProcedure, else it risks deleting something that's
pointed to by a cache entry.  On the whole it seems more robust
to deal with the case inside PLy_procedure_get.

Found by the new version of Coverity (nice catch!).  In the end
I feel this fix is more about satisfying Coverity than about
fixing a real-world problem, so I'm not going to back-patch.

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/e557c82759a3362d8fb618c798ca99ee90be96c5

Modified Files
--------------
src/pl/plpython/plpy_main.c      |  2 +-
src/pl/plpython/plpy_procedure.c | 13 ++++++++++++-
2 files changed, 13 insertions(+), 2 deletions(-)



Message-ID: <E1vBzyF-0039wc-2y@gemulon.postgresql.org>
Permalink:  ../E1vBzyF-0039wc-2y@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1vBzyF-0039wc-2y@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Avoid memory leak in validation of a PL/Python trigger function.
  In-Reply-To: <E1vBzyF-0039wc-2y@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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