From eaa08a98efc3103d49172c89eef705c713f5e230 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Sat, 14 Nov 2026 23:17:17 -0500 Subject: [PATCH] plpython: leave input collation out of the function cache key. Nothing plpython compiles uses the collation, so keying on it only built duplicate cache entries. Discussion: https://postgr.es/m/3245516.1794415242@sss.pgh.pa.us --- src/backend/executor/functions.c | 1 + src/backend/utils/cache/funccache.c | 15 +++++++++++---- src/include/utils/funccache.h | 8 +++++--- src/pl/plpgsql/src/pl_comp.c | 1 + src/pl/plpython/expected/plpython_global.out | 14 ++++++++++++++ src/pl/plpython/plpy_procedure.c | 7 +++++-- src/pl/plpython/sql/plpython_global.sql | 11 +++++++++++ 7 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/backend/executor/functions.c b/src/backend/executor/functions.c index 8fec79a2f76..5eda27d7d25 100644 --- a/src/backend/executor/functions.c +++ b/src/backend/executor/functions.c @@ -605,6 +605,7 @@ init_sql_fcache(FunctionCallInfo fcinfo, bool lazyEvalOK) sql_delete_callback, sizeof(SQLFunctionHashEntry), true, + true, false); /* diff --git a/src/backend/utils/cache/funccache.c b/src/backend/utils/cache/funccache.c index 7b3422a07bf..82a7c960159 100644 --- a/src/backend/utils/cache/funccache.c +++ b/src/backend/utils/cache/funccache.c @@ -252,6 +252,7 @@ compute_function_hashkey(FunctionCallInfo fcinfo, CachedFunctionHashKey *hashkey, Size cacheEntrySize, bool includeResultType, + bool includeInputCollation, bool forValidator) { /* Make sure pad bytes within fixed part of the struct are zero */ @@ -286,8 +287,9 @@ compute_function_hashkey(FunctionCallInfo fcinfo, hashkey->trigOid = trigdata->tg_trigger->tgoid; } - /* get input collation, if known */ - hashkey->inputCollation = fcinfo->fncollation; + /* get input collation, if known and wanted */ + if (includeInputCollation) + hashkey->inputCollation = fcinfo->fncollation; /* * We include only input arguments in the hash key, since output argument @@ -487,6 +489,10 @@ delete_function(CachedFunction *func) * If includeResultType is true and the function returns composite, * include the actual result descriptor in the cache lookup key. * + * If includeInputCollation is true, include the call's input collation in + * the cache lookup key. Languages whose compiled form doesn't depend on the + * collation should pass false, to avoid building redundant cache entries. + * * If forValidator is true, we're only compiling for validation purposes, * and so some checks are skipped. In this case the passed fcinfo need * only provide the function OID and indications of whether the function @@ -507,6 +513,7 @@ cached_function_compile(FunctionCallInfo fcinfo, CachedFunctionDeleteCallback dcallback, Size cacheEntrySize, bool includeResultType, + bool includeInputCollation, bool forValidator) { Oid funcOid = fcinfo->flinfo->fn_oid; @@ -535,7 +542,7 @@ recheck: /* Compute hashkey using function signature and actual arg types */ compute_function_hashkey(fcinfo, procStruct, &hashkey, cacheEntrySize, includeResultType, - forValidator); + includeInputCollation, forValidator); hashkey_valid = true; /* And do the lookup */ @@ -591,7 +598,7 @@ recheck: if (!hashkey_valid) compute_function_hashkey(fcinfo, procStruct, &hashkey, cacheEntrySize, includeResultType, - forValidator); + includeInputCollation, forValidator); /* * Create the new function struct, if not done already. The function diff --git a/src/include/utils/funccache.h b/src/include/utils/funccache.h index ee8f03ee997..38c4b251f7d 100644 --- a/src/include/utils/funccache.h +++ b/src/include/utils/funccache.h @@ -75,9 +75,10 @@ typedef struct CachedFunctionHashKey Oid trigOid; /* - * We must include the input collation as part of the hash key too, - * because we have to generate different plans (with different Param - * collations) for different collation settings. + * Usually we must include the input collation as part of the hash key + * too, because we have to generate different plans (with different Param + * collations) for different collation settings. Languages whose compiled + * form doesn't depend on the collation can ask to leave this zero. */ Oid inputCollation; @@ -123,6 +124,7 @@ extern CachedFunction *cached_function_compile(FunctionCallInfo fcinfo, CachedFunctionDeleteCallback dcallback, Size cacheEntrySize, bool includeResultType, + bool includeInputCollation, bool forValidator); extern void cfunc_resolve_polymorphic_argtypes(int numargs, Oid *argtypes, diff --git a/src/pl/plpgsql/src/pl_comp.c b/src/pl/plpgsql/src/pl_comp.c index 180a20f868a..19e3bb538da 100644 --- a/src/pl/plpgsql/src/pl_comp.c +++ b/src/pl/plpgsql/src/pl_comp.c @@ -122,6 +122,7 @@ plpgsql_compile(FunctionCallInfo fcinfo, bool forValidator) plpgsql_delete_callback, sizeof(PLpgSQL_function), false, + true, forValidator); /* diff --git a/src/pl/plpython/expected/plpython_global.out b/src/pl/plpython/expected/plpython_global.out index a3f33f9cba0..baf51f48869 100644 --- a/src/pl/plpython/expected/plpython_global.out +++ b/src/pl/plpython/expected/plpython_global.out @@ -74,3 +74,17 @@ SELECT static_test_coll('a'); 3 (1 row) +-- Python global variables live in the compiled function, so this checks +-- that we compile only once no matter what the input collation is +CREATE FUNCTION global_var_coll(t text) RETURNS int4 +AS $$ +global n +n = globals().get("n", 0) + 1 +return n +$$ LANGUAGE plpython3u; +SELECT global_var_coll('a'), global_var_coll('a' COLLATE "C"); + global_var_coll | global_var_coll +-----------------+----------------- + 1 | 2 +(1 row) + diff --git a/src/pl/plpython/plpy_procedure.c b/src/pl/plpython/plpy_procedure.c index 76fee7974a2..15681df03a5 100644 --- a/src/pl/plpython/plpy_procedure.c +++ b/src/pl/plpython/plpy_procedure.c @@ -24,7 +24,7 @@ /* * 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. + * one per trigger or per result rowtype, and they must all see the same SD. */ typedef struct PLyStaticsKey { @@ -129,7 +129,9 @@ PLy_procedure_get(FunctionCallInfo fcinfo, bool forValidator) * functions, make the hash key depend on the result of * get_call_result_type() when that's composite, so that we can safely * assume that we'll build a new hash entry if the composite rowtype - * changes. + * changes. Nothing we build depends on the input collation, so leave + * that out of the hash key; otherwise we'd compile the function again for + * each collation it's called with. */ proc = (PLyProcedure *) cached_function_compile(fcinfo, @@ -138,6 +140,7 @@ PLy_procedure_get(FunctionCallInfo fcinfo, bool forValidator) PLy_delete_callback, sizeof(PLyProcedure), true, + false, forValidator); /* diff --git a/src/pl/plpython/sql/plpython_global.sql b/src/pl/plpython/sql/plpython_global.sql index 6f59600b455..47b11defc77 100644 --- a/src/pl/plpython/sql/plpython_global.sql +++ b/src/pl/plpython/sql/plpython_global.sql @@ -54,3 +54,14 @@ return SD["n"] $$ LANGUAGE plpython3u; SELECT static_test_coll('a'); + +-- Python global variables live in the compiled function, so this checks +-- that we compile only once no matter what the input collation is +CREATE FUNCTION global_var_coll(t text) RETURNS int4 +AS $$ +global n +n = globals().get("n", 0) + 1 +return n +$$ LANGUAGE plpython3u; + +SELECT global_var_coll('a'), global_var_coll('a' COLLATE "C"); -- 2.37.1 (Apple Git-137.1)