Re: BUG #18463: Possible bug in stored procedures with polymorphic OUT parameters

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Bille <andrewbille(at)gmail(dot)com>
Cc: Dmitry Dolgov <9erthalion6(at)gmail(dot)com>, drewk(at)cockroachlabs(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18463: Possible bug in stored procedures with polymorphic OUT parameters
Date: 2024-06-04 21:02:02
Message-ID: 590129.1717534922@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Andrew Bille <andrewbille(at)gmail(dot)com> writes:
> After 70ffb27b in REL_12 following script
> CREATE OR REPLACE PROCEDURE p(inout a anyelement, inout b anyelement)
> LANGUAGE SQL
> AS $$
> SELECT $1, 1;
> $$;
> CALL p(1.1, null);
> crash server with backtrace:

So the problem here is that in v12, check_sql_fn_retval() fails to
resolve the polymorphic output types and then just throws up its hands
and assumes the check will be made at runtime. I think that's true
for ordinary functions returning RECORD, but it doesn't happen in
CALL. What needs to happen is for check_sql_fn_retval to resolve
those types and then notice that the SELECT output doesn't match.

In v13 and later, this was fixed by 913bbd88d ("Improve the handling
of result type coercions in SQL functions"), which not only did the
polymorphism stuff correctly but would also insert a cast from int
to numeric to allow this case to succeed. I thought then, and still
think, that that was too big a behavior change to risk back-patching.
So the best we can hope for in v12 is that this example throws an
error cleanly.

Fortunately that doesn't seem too painful --- with a little bit of
local rejiggering, we can use get_call_result_type instead of
get_func_result_type, and that will resolve the arguments correctly.
So that leads me to the attached.

Even though there's no bug in >= v13, I'm slightly tempted to put
the new test cases into the later branches too. If we'd had a test
like this we'd have noticed the problem ...

regards, tom lane

Attachment Content-Type Size
check-procedure-polymorphic-out-args-v12.patch text/x-diff 6.4 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Etsuro Fujita 2024-06-05 01:08:42 Re: BUG #18467: postgres_fdw (deparser) ignores LimitOption
Previous Message Casey & Gina 2024-06-04 20:56:20 intarray bigint support