Skip to content

Commit 9bae0cc

Browse files
Shihao-Zhonghackorum
authored andcommitted
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
1 parent 4255812 commit 9bae0cc

7 files changed

Lines changed: 48 additions & 9 deletions

File tree

‎src/backend/executor/functions.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ init_sql_fcache(FunctionCallInfo fcinfo, bool lazyEvalOK)
605605
sql_delete_callback,
606606
sizeof(SQLFunctionHashEntry),
607607
true,
608+
true,
608609
false);
609610

610611
/*

‎src/backend/utils/cache/funccache.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ compute_function_hashkey(FunctionCallInfo fcinfo,
252252
CachedFunctionHashKey *hashkey,
253253
Size cacheEntrySize,
254254
bool includeResultType,
255+
bool includeInputCollation,
255256
bool forValidator)
256257
{
257258
/* Make sure pad bytes within fixed part of the struct are zero */
@@ -286,8 +287,9 @@ compute_function_hashkey(FunctionCallInfo fcinfo,
286287
hashkey->trigOid = trigdata->tg_trigger->tgoid;
287288
}
288289

289-
/* get input collation, if known */
290-
hashkey->inputCollation = fcinfo->fncollation;
290+
/* get input collation, if known and wanted */
291+
if (includeInputCollation)
292+
hashkey->inputCollation = fcinfo->fncollation;
291293

292294
/*
293295
* We include only input arguments in the hash key, since output argument
@@ -487,6 +489,10 @@ delete_function(CachedFunction *func)
487489
* If includeResultType is true and the function returns composite,
488490
* include the actual result descriptor in the cache lookup key.
489491
*
492+
* If includeInputCollation is true, include the call's input collation in
493+
* the cache lookup key. Languages whose compiled form doesn't depend on the
494+
* collation should pass false, to avoid building redundant cache entries.
495+
*
490496
* If forValidator is true, we're only compiling for validation purposes,
491497
* and so some checks are skipped. In this case the passed fcinfo need
492498
* only provide the function OID and indications of whether the function
@@ -507,6 +513,7 @@ cached_function_compile(FunctionCallInfo fcinfo,
507513
CachedFunctionDeleteCallback dcallback,
508514
Size cacheEntrySize,
509515
bool includeResultType,
516+
bool includeInputCollation,
510517
bool forValidator)
511518
{
512519
Oid funcOid = fcinfo->flinfo->fn_oid;
@@ -535,7 +542,7 @@ cached_function_compile(FunctionCallInfo fcinfo,
535542
/* Compute hashkey using function signature and actual arg types */
536543
compute_function_hashkey(fcinfo, procStruct, &hashkey,
537544
cacheEntrySize, includeResultType,
538-
forValidator);
545+
includeInputCollation, forValidator);
539546
hashkey_valid = true;
540547

541548
/* And do the lookup */
@@ -591,7 +598,7 @@ cached_function_compile(FunctionCallInfo fcinfo,
591598
if (!hashkey_valid)
592599
compute_function_hashkey(fcinfo, procStruct, &hashkey,
593600
cacheEntrySize, includeResultType,
594-
forValidator);
601+
includeInputCollation, forValidator);
595602

596603
/*
597604
* Create the new function struct, if not done already. The function

‎src/include/utils/funccache.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ typedef struct CachedFunctionHashKey
7575
Oid trigOid;
7676

7777
/*
78-
* We must include the input collation as part of the hash key too,
79-
* because we have to generate different plans (with different Param
80-
* collations) for different collation settings.
78+
* Usually we must include the input collation as part of the hash key
79+
* too, because we have to generate different plans (with different Param
80+
* collations) for different collation settings. Languages whose compiled
81+
* form doesn't depend on the collation can ask to leave this zero.
8182
*/
8283
Oid inputCollation;
8384

@@ -123,6 +124,7 @@ extern CachedFunction *cached_function_compile(FunctionCallInfo fcinfo,
123124
CachedFunctionDeleteCallback dcallback,
124125
Size cacheEntrySize,
125126
bool includeResultType,
127+
bool includeInputCollation,
126128
bool forValidator);
127129
extern void cfunc_resolve_polymorphic_argtypes(int numargs,
128130
Oid *argtypes,

‎src/pl/plpgsql/src/pl_comp.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ plpgsql_compile(FunctionCallInfo fcinfo, bool forValidator)
122122
plpgsql_delete_callback,
123123
sizeof(PLpgSQL_function),
124124
false,
125+
true,
125126
forValidator);
126127

127128
/*

‎src/pl/plpython/expected/plpython_global.out‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,17 @@ SELECT static_test_coll('a');
7474
3
7575
(1 row)
7676

77+
-- Python global variables live in the compiled function, so this checks
78+
-- that we compile only once no matter what the input collation is
79+
CREATE FUNCTION global_var_coll(t text) RETURNS int4
80+
AS $$
81+
global n
82+
n = globals().get("n", 0) + 1
83+
return n
84+
$$ LANGUAGE plpython3u;
85+
SELECT global_var_coll('a'), global_var_coll('a' COLLATE "C");
86+
global_var_coll | global_var_coll
87+
-----------------+-----------------
88+
1 | 2
89+
(1 row)
90+

‎src/pl/plpython/plpy_procedure.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/*
2525
* SD dictionaries, one per function (and per relation, for DML triggers).
2626
* funccache.c may build several PLyProcedures for one function, for example
27-
* one per input collation, and they must all see the same SD.
27+
* one per trigger or per result rowtype, and they must all see the same SD.
2828
*/
2929
typedef struct PLyStaticsKey
3030
{
@@ -129,7 +129,9 @@ PLy_procedure_get(FunctionCallInfo fcinfo, bool forValidator)
129129
* functions, make the hash key depend on the result of
130130
* get_call_result_type() when that's composite, so that we can safely
131131
* assume that we'll build a new hash entry if the composite rowtype
132-
* changes.
132+
* changes. Nothing we build depends on the input collation, so leave
133+
* that out of the hash key; otherwise we'd compile the function again for
134+
* each collation it's called with.
133135
*/
134136
proc = (PLyProcedure *)
135137
cached_function_compile(fcinfo,
@@ -138,6 +140,7 @@ PLy_procedure_get(FunctionCallInfo fcinfo, bool forValidator)
138140
PLy_delete_callback,
139141
sizeof(PLyProcedure),
140142
true,
143+
false,
141144
forValidator);
142145

143146
/*

‎src/pl/plpython/sql/plpython_global.sql‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,14 @@ return SD["n"]
5454
$$ LANGUAGE plpython3u;
5555

5656
SELECT static_test_coll('a');
57+
58+
-- Python global variables live in the compiled function, so this checks
59+
-- that we compile only once no matter what the input collation is
60+
CREATE FUNCTION global_var_coll(t text) RETURNS int4
61+
AS $$
62+
global n
63+
n = globals().get("n", 0) + 1
64+
return n
65+
$$ LANGUAGE plpython3u;
66+
67+
SELECT global_var_coll('a'), global_var_coll('a' COLLATE "C");

0 commit comments

Comments
 (0)