Re: Why type coercion is not performed for parameters?

From: Marko Tiikkaja <marko(at)joh(dot)to>
To: Konstantin Knizhnik <k(dot)knizhnik(at)postgrespro(dot)ru>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Why type coercion is not performed for parameters?
Date: 2017-05-05 10:29:08
Message-ID: CAL9smLAKJ+KP-JGhxLu9kbiiXgd2A7Yr1m4S5ak8m0JTwyd0Mw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, May 5, 2017 at 10:58 AM, Konstantin Knizhnik <
k(dot)knizhnik(at)postgrespro(dot)ru> wrote:

> 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.

.m

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Konstantin Knizhnik 2017-05-05 10:58:32 Re: Why type coercion is not performed for parameters?
Previous Message Magnus Hagander 2017-05-05 10:10:06 Re: [patch] Build pgoutput with MSVC