Why type coercion is not performed for parameters?

From: Konstantin Knizhnik <k(dot)knizhnik(at)postgrespro(dot)ru>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Why type coercion is not performed for parameters?
Date: 2017-05-05 08:58:06
Message-ID: ca994443-40ac-5e3f-d3b3-70e9d1590278@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

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.

Actually coerce_type function can normally handle parameters.
But func_get_detail always allows coercion only for constants:

if (sourceType == UNKNOWNOID && IsA(arg1, Const))
{
/* always treat typename('literal') as coercion */
iscoercion = true;
}

If this condition is changed to:

if (sourceType == UNKNOWNOID && (IsA(arg1, Const) ||
IsA(arg1, Param)))

then the example above will normally work.

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.

This is why now I infer parameter type from literal value. But in this
case I get errors in parse_analyze_varparams which is not able to
resolve some functions.
The fix in func_get_detail functions solves the problem and doesn't
cause some new issues: all regression tests are passed.

So my question is whether it is possible to use the same rule for type
coercion of parameters as for constant?

--
Konstantin Knizhnik
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2017-05-05 09:03:09 Re: logical replication and PANIC during shutdown checkpoint in publisher
Previous Message Paresh More 2017-05-05 08:36:55 Re: Patch - Tcl 8.6 version support for PostgreSQL