From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: plpython: share the SD dictionary across cache entries of a func
Date: Thu, 12 Nov 2026 16:48:13 +0000
Message-ID: <E1xRXy9-00000000SRd-0Eui@gemulon.postgresql.org> (raw)
plpython: share the SD dictionary across cache entries of a function.
Since commit 9bcb8a694 moved PL/Python onto funccache.c, each cache
entry gets its own SD dictionary. funccache.c builds separate entries
for the same function when the input collation differs, when a trigger
function is used by several triggers, or when the argument or result
types differ (as in polymorphic functions, a case PL/Python doesn't
yet support). This results in an unintentional change of behavior
from previous releases, which intended to share an instance of SD
across all calls of a plain function, or all calls for a given trigger
rel for a trigger function. The documentation does not suggest that
the arguments (including collation) should have anything to do with it.
However, the previous implementation had its own problem: CREATE OR
REPLACE FUNCTION caused a new SD to be generated, which is not what
you'd expect after reading the admittedly-terse documentation.
That appears to be an implementation artifact not an intentional
behavioral choice.
To fix, keep long-lived references to the SD dictionaries in a
separate hash table, keyed by function OID and (for triggers) the
relation OID, and then copy a reference into each function cache
entry. This makes SD semantics independent of what funccache.c does,
which seems like a good future-proofing choice. A function's SD will
now survive CREATE OR REPLACE FUNCTION, since that doesn't change
the function's OID.
As before, an inline DO block has its own one-use SD dictionary.
Bug: #19816
Reported-by: Marcos Castedo <marcos.castedo@anachronics.com>
Author: shihao zhong <zhong950419@gmail.com>
Reviewed-by: Marcos Castedo <marcos.castedo@anachronics.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19816-921b5cbc3ee63146@postgresql.org
Backpatch-through: 19
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/c13c414c6e018fcbd9fa6e36e778a852c76a1562
Modified Files
--------------
src/pl/plpython/expected/plpython_global.out | 24 +++++++++
src/pl/plpython/plpy_main.c | 5 ++
src/pl/plpython/plpy_procedure.c | 80 +++++++++++++++++++++++++---
src/pl/plpython/sql/plpython_global.sql | 18 +++++++
src/tools/pgindent/typedefs.list | 2 +
5 files changed, 123 insertions(+), 6 deletions(-)
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: tgl@sss.pgh.pa.us
Subject: Re: pgsql: plpython: share the SD dictionary across cache entries of a func
In-Reply-To: <E1xRXy9-00000000SRd-0Eui@gemulon.postgresql.org>
References: <E1xRXy9-00000000SRd-0Eui@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