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 <[email protected]> Author: shihao zhong <[email protected]> Reviewed-by: Marcos Castedo <[email protected]> Reviewed-by: Tom Lane <[email protected]> Discussion: https://postgr.es/m/[email protected] 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(-)
