Re: [HACKERS] Why type coercion is not performed for parameters? - Mailing list pgsql-hackers

From Marko Tiikkaja
Subject Re: [HACKERS] Why type coercion is not performed for parameters?
Date
Msg-id CAL9smLAKJ+KP-JGhxLu9kbiiXgd2A7Yr1m4S5ak8m0JTwyd0Mw@mail.gmail.com
Whole thread Raw
In response to [HACKERS] Why type coercion is not performed for parameters?  (Konstantin Knizhnik <k.knizhnik@postgrespro.ru>)
Responses Re: [HACKERS] Why type coercion is not performed for parameters?  (Konstantin Knizhnik <k.knizhnik@postgrespro.ru>)
List pgsql-hackers
On Fri, May 5, 2017 at 10:58 AM, Konstantin Knizhnik <k.knizhnik@postgrespro.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

pgsql-hackers by date:

Previous
From: Magnus Hagander
Date:
Subject: Re: [HACKERS] [patch] Build pgoutput with MSVC
Next
From: Konstantin Knizhnik
Date:
Subject: Re: [HACKERS] Why type coercion is not performed for parameters?