postgres.git / summary / log / commit / refs
commit b74de75cc8c434eeb5d167d6a7ff4c5d08efe5db
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu Nov 12 16:48:06 2026 +0000
First released in: 19.0
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
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(-)
diff --git a/src/pl/plpython/expected/plpython_global.out b/src/pl/plpython/expected/plpython_global.out
index a4cfb1483f9..a3f33f9cba0 100644
--- a/src/pl/plpython/expected/plpython_global.out
+++ b/src/pl/plpython/expected/plpython_global.out
@@ -50,3 +50,27 @@ SELECT global_test_two();
SD: set by global_test_two, GD: set by global_test_one
(1 row)
+-- SD is shared by all calls of a function, whatever the input collation,
+-- and survives CREATE OR REPLACE FUNCTION
+CREATE FUNCTION static_test_coll(t text) RETURNS int4
+AS $$
+SD["n"] = SD.get("n", 0) + 1
+return SD["n"]
+$$ LANGUAGE plpython3u;
+SELECT static_test_coll('a'), static_test_coll('a' COLLATE "C");
+ static_test_coll | static_test_coll
+------------------+------------------
+ 1 | 2
+(1 row)
+
+CREATE OR REPLACE FUNCTION static_test_coll(t text) RETURNS int4
+AS $$
+SD["n"] = SD.get("n", 0) + 1
+return SD["n"]
+$$ LANGUAGE plpython3u;
+SELECT static_test_coll('a');
+ static_test_coll
+------------------
+ 3
+(1 row)
+
diff --git a/src/pl/plpython/plpy_main.c b/src/pl/plpython/plpy_main.c
index df08d3fbd1b..649f72f7f3b 100644
--- a/src/pl/plpython/plpy_main.c
+++ b/src/pl/plpython/plpy_main.c
@@ -309,6 +309,11 @@ plpython3_inline_handler(PG_FUNCTION_ARGS)
plerrcontext.previous = error_context_stack;
error_context_stack = &plerrcontext;
+ /* An inline block has its own SD, used for this call only */
+ proc.statics = PyDict_New();
+ if (!proc.statics)
+ PLy_elog(ERROR, NULL);
+
PLy_procedure_compile(&proc, codeblock->source_text);
exec_ctx->curr_proc = &proc;
PLy_exec_function(fake_fcinfo, &pcache);
diff --git a/src/pl/plpython/plpy_procedure.c b/src/pl/plpython/plpy_procedure.c
index 688ebbbf6e2..d06c5505a91 100644
--- a/src/pl/plpython/plpy_procedure.c
+++ b/src/pl/plpython/plpy_procedure.c
@@ -18,12 +18,34 @@
#include "plpy_util.h"
#include "utils/builtins.h"
#include "utils/funccache.h"
+#include "utils/hsearch.h"
#include "utils/syscache.h"
+/*
+ * SD dictionaries, one per function (and per relation, for DML triggers).
+ * funccache.c may build several PLyProcedures for one function, for example
+ * one per input collation, and they must all see the same SD.
+ */
+typedef struct PLyStaticsKey
+{
+ Oid fn_oid; /* function OID */
+ Oid fn_rel; /* triggered-on relation or InvalidOid */
+} PLyStaticsKey;
+
+typedef struct PLyStaticsEntry
+{
+ PLyStaticsKey key; /* hash key, must be first */
+ PyObject *statics; /* the SD dictionary */
+} PLyStaticsEntry;
+
+static HTAB *PLy_statics_hash = NULL;
+
static void PLy_procedure_create(PLyProcedure *proc,
HeapTuple procTup,
Oid fn_oid,
+ Oid fn_rel,
PLyTrigType is_trigger);
+static PyObject *PLy_procedure_get_statics(Oid fn_oid, Oid fn_rel);
static char *PLy_procedure_munge_source(const char *name, const char *src);
static void PLy_compile_callback(FunctionCallInfo fcinfo,
HeapTuple procTup,
@@ -145,6 +167,7 @@ static void
PLy_procedure_create(PLyProcedure *proc,
HeapTuple procTup,
Oid fn_oid,
+ Oid fn_rel,
PLyTrigType is_trigger)
{
char procName[NAMEDATALEN + 256];
@@ -340,6 +363,9 @@ PLy_procedure_create(PLyProcedure *proc,
Anum_pg_proc_prosrc);
procSource = TextDatumGetCString(prosrcdatum);
+ /* SD is shared with other PLyProcedures of the function */
+ proc->statics = PLy_procedure_get_statics(fn_oid, fn_rel);
+
PLy_procedure_compile(proc, procSource);
pfree(procSource);
@@ -369,11 +395,9 @@ PLy_procedure_compile(PLyProcedure *proc, const char *src)
/*
* SD is private preserved data between calls. GD is global data shared by
- * all functions
+ * all functions. The caller supplies SD.
*/
- proc->statics = PyDict_New();
- if (!proc->statics)
- PLy_elog(ERROR, NULL);
+ Assert(proc->statics != NULL);
PyDict_SetItemString(proc->globals, "SD", proc->statics);
/*
@@ -483,20 +507,64 @@ PLy_compile_callback(FunctionCallInfo fcinfo,
{
PLyProcedure *proc = (PLyProcedure *) cfunc;
Oid fn_oid = fcinfo->flinfo->fn_oid;
+ Oid fn_rel = InvalidOid;
PLyTrigType is_trigger;
/*
* Derive the trigger type from the call context, matching what
- * plpython3_call_handler dispatches on.
+ * plpython3_call_handler dispatches on. DML triggers get one SD per
+ * relation. The validator has no relation.
*/
if (CALLED_AS_TRIGGER(fcinfo))
+ {
is_trigger = PLPY_TRIGGER;
+ if (!forValidator)
+ fn_rel = RelationGetRelid(((TriggerData *) fcinfo->context)->tg_relation);
+ }
else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
is_trigger = PLPY_EVENT_TRIGGER;
else
is_trigger = PLPY_NOT_TRIGGER;
- PLy_procedure_create(proc, procTup, fn_oid, is_trigger);
+ PLy_procedure_create(proc, procTup, fn_oid, fn_rel, is_trigger);
+}
+
+/*
+ * Get the SD dictionary of a function, as a new reference.
+ */
+static PyObject *
+PLy_procedure_get_statics(Oid fn_oid, Oid fn_rel)
+{
+ PLyStaticsKey key;
+ PLyStaticsEntry *entry;
+ bool found;
+
+ if (PLy_statics_hash == NULL)
+ {
+ HASHCTL ctl;
+
+ ctl.keysize = sizeof(PLyStaticsKey);
+ ctl.entrysize = sizeof(PLyStaticsEntry);
+ PLy_statics_hash = hash_create("PL/Python SD dictionaries", 32,
+ &ctl, HASH_ELEM | HASH_BLOBS);
+ }
+
+ key.fn_oid = fn_oid;
+ key.fn_rel = fn_rel;
+ entry = hash_search(PLy_statics_hash, &key, HASH_ENTER, &found);
+ if (!found)
+ entry->statics = NULL;
+
+ /* A failed PyDict_New leaves the entry empty, so retry it here */
+ if (entry->statics == NULL)
+ {
+ entry->statics = PyDict_New();
+ if (entry->statics == NULL)
+ PLy_elog(ERROR, NULL);
+ }
+
+ Py_INCREF(entry->statics);
+ return entry->statics;
}
/*
diff --git a/src/pl/plpython/sql/plpython_global.sql b/src/pl/plpython/sql/plpython_global.sql
index 96d20492861..6f59600b455 100644
--- a/src/pl/plpython/sql/plpython_global.sql
+++ b/src/pl/plpython/sql/plpython_global.sql
@@ -36,3 +36,21 @@ SELECT static_test();
SELECT static_test();
SELECT global_test_one();
SELECT global_test_two();
+
+-- SD is shared by all calls of a function, whatever the input collation,
+-- and survives CREATE OR REPLACE FUNCTION
+CREATE FUNCTION static_test_coll(t text) RETURNS int4
+AS $$
+SD["n"] = SD.get("n", 0) + 1
+return SD["n"]
+$$ LANGUAGE plpython3u;
+
+SELECT static_test_coll('a'), static_test_coll('a' COLLATE "C");
+
+CREATE OR REPLACE FUNCTION static_test_coll(t text) RETURNS int4
+AS $$
+SD["n"] = SD.get("n", 0) + 1
+return SD["n"]
+$$ LANGUAGE plpython3u;
+
+SELECT static_test_coll('a');
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index afce25bb0b6..712ed773857 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2053,6 +2053,8 @@ PLyResultObject
PLySRFState
PLySavedArgs
PLyScalarToOb
+PLyStaticsEntry
+PLyStaticsKey
PLySubtransactionData
PLySubtransactionObject
PLyTransformToOb
[parent: 52ecf2af068c]