These current.* functions always use the "C" collation for their
output, so in this case, they are not related to session variables, in
my view.
Thank you, I did some more testing and see the below.
postgres=# SELECT collation for (user);
pg_collation_for
------------------
"C"
(1 row)
postgres=# SELECT collation for (current_user);
pg_collation_for
------------------
"C"
(1 row)
postgres=# SELECT to_upper_varchar('тест'::varchar);
to_upper_varchar
------------------
ТЕСТ
(1 row)
postgres=# SELECT collation for ('тест');
pg_collation_for
------------------
(1 row)
postgres=# CREATE OR REPLACE FUNCTION debug_func(param1 text, param2 text) RETURNS text AS $$ SELECT 'param1 collation: ' || pg_collation_for(param1) || ', param2 collation: ' || pg_collation_for(param2); $$ LANGUAGE sql;
CREATE FUNCTION
postgres=# SELECT debug_func('тест', user::text);
debug_func
----------------------------------------------
param1 collation: "C", param2 collation: "C"
(1 row)
postgres=# CREATE OR REPLACE FUNCTION debug_func2(param1 text, param2 text) RETURNS text AS $$ SELECT 'param1 collation: ' || pg_collation_for(param1) || ', param2 collation: ' || pg_collation_for(param2); $$ LANGUAGE sql;
CREATE FUNCTION
postgres=# SELECT debug_func2('тест'::text, user::text);
debug_func2
----------------------------------------------
param1 collation: "C", param2 collation: "C"
(1 row)
postgres=#
My observation is the user, current* functions collation is "C" collation and when a "C" collation is passed as a parameter, it converts all the parameters to use the "C" collation.
I was looking at sql_fn_make_param() function and the comments over their say
"/*
* If we have a function input collation, allow it to override the
* type-derived collation for parameter symbols. (XXX perhaps this should
* not happen if the type collation is not default?)
*/
"
From the above code comment it looks like the "C" collation is overriding the type-driven collation, and this might be causing the issue.
I am currently using gdb to debug more and will work on a patch based on my findings.