If I evaluate expression typename('literal'), then type coercion is performed and the function is successfully resolved, i.e.
SELECT regnamespace('"pg_catalog"');
But if I want to prepare this query, I get the error:
postgres=# prepare foo as SELECT regnamespace($1); ERROR: function regnamespace(unknown) does not exist LINE 1: prepare foo as SELECT regnamespace($1);
Certainly, I can explicitly specify parameter type:
prepare foo (text) as SELECT regnamespace($1);
and it will work. But it is not always possible.
There are other similar examples which have even bigger issues, such as now() - interval '6 hours'. now() - interval $1 won't even parse.
Why do I need it? I want to implement autoprepare. My original intention was to let parse_analyze_varparams to infer type of parameters from the context. But it is not always possible and sometime leads to different behavior of query. For example if the query:
select count(*) from test_range_gist where ir @> 10;
is replaced with
select count(*) from test_range_gist where ir @> $1;
then type of parameter will be int4range rather then int, which corresponds to the different operator.
But you know that the type of the literal "10" is int. If you're throwing that information away, surely that's a bug in your code.