Lists: | pgsql-bugs |
---|
From: | PG Bug reporting form <noreply(at)postgresql(dot)org> |
---|---|
To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
Cc: | mjurca(at)centrum(dot)cz |
Subject: | BUG #17610: Use of multiple composite types incompatible with record-typed function parameter |
Date: | 2022-09-08 10:19:30 |
Message-ID: | 17610-fb1eef75bf6c2364@postgresql.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-bugs |
The following bug has been logged on the website:
Bug reference: 17610
Logged by: Martin Jurča
Email address: mjurca(at)centrum(dot)cz
PostgreSQL version: 14.5
Operating system: Linux, Debian 10.2.1-6, x86
Description:
The SQL code at the end of this bug report ends with the following error:
2022-09-08 10:09:02.173 GMT [174] ERROR: type of parameter 1
(composite_type_2) does not match that when preparing the plan
(composite_type_1)
2022-09-08 10:09:02.173 GMT [174] CONTEXT: PL/pgSQL function
polymorphic_fuction(record) line 6 at EXECUTE
2022-09-08 10:09:02.173 GMT [174] STATEMENT: SELECT * FROM
polymorphic_fuction(
CAST(ROW(2) AS composite_type_2)
);
ERROR: 42804: type of parameter 1 (composite_type_2) does not match that
when preparing the plan (composite_type_1)
CONTEXT: PL/pgSQL function polymorphic_fuction(record) line 6 at EXECUTE
LOCATION: plpgsql_param_eval_generic_ro, pl_exec.c:6648
The expected output is:
polymorphic_fuction
---------------------
2
(1 row)
If I understand the documentation
(/docs/current/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING)
correctly, this should work. I noticed that the record type is not listed
among polymorphic types
(/docs/current/extend-type-system.html#EXTEND-TYPES-POLYMORPHIC)
but PL/pgSQL documentation
(/docs/current/plpgsql-overview.html#PLPGSQL-ARGS-RESULTS)
states that any composite type can be passed to a record parameter.
Full version info:
PostgreSQL 14.5 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6)
10.2.1 20210110, 64-bit
I am not sure if this is an error on my part, at the PostgreSQL server, or
in the documentation, but any help or clarification would be appreciated.
SQL code producing the error:
CREATE TYPE composite_type_1 AS (
num int
);
CREATE TYPE composite_type_2 AS (
num int
);
CREATE FUNCTION polymorphic_fuction(
arg record
)
RETURNS int
LANGUAGE plpgsql
AS $$
DECLARE
result_num int;
BEGIN
-- Using either SELECT or EXECUTE has the same outcome (this I
understand).
EXECUTE 'SELECT ($1).num'
INTO STRICT result_num
USING arg;
RETURN result_num;
END;
$$;
SELECT * FROM polymorphic_fuction(
CAST(ROW(1) AS composite_type_1)
);
SELECT * FROM polymorphic_fuction(
CAST(ROW(2) AS composite_type_2)
);
From: | Japin Li <japinli(at)hotmail(dot)com> |
---|---|
To: | mjurca(at)centrum(dot)cz, pgsql-bugs(at)lists(dot)postgresql(dot)org |
Subject: | Re: BUG #17610: Use of multiple composite types incompatible with record-typed function parameter |
Date: | 2022-09-08 15:21:51 |
Message-ID: | MEYP282MB16693A87D77FF13C23E45EA4B6409@MEYP282MB1669.AUSP282.PROD.OUTLOOK.COM |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-bugs |
On Thu, 08 Sep 2022 at 18:19, PG Bug reporting form <noreply(at)postgresql(dot)org> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 17610
> Logged by: Martin Jurča
> Email address: mjurca(at)centrum(dot)cz
> PostgreSQL version: 14.5
> Operating system: Linux, Debian 10.2.1-6, x86
> Description:
>
> The SQL code at the end of this bug report ends with the following error:
>
> 2022-09-08 10:09:02.173 GMT [174] ERROR: type of parameter 1
> (composite_type_2) does not match that when preparing the plan
> (composite_type_1)
> 2022-09-08 10:09:02.173 GMT [174] CONTEXT: PL/pgSQL function
> polymorphic_fuction(record) line 6 at EXECUTE
> 2022-09-08 10:09:02.173 GMT [174] STATEMENT: SELECT * FROM
> polymorphic_fuction(
> CAST(ROW(2) AS composite_type_2)
> );
> ERROR: 42804: type of parameter 1 (composite_type_2) does not match that
> when preparing the plan (composite_type_1)
> CONTEXT: PL/pgSQL function polymorphic_fuction(record) line 6 at EXECUTE
> LOCATION: plpgsql_param_eval_generic_ro, pl_exec.c:6648
>
> The expected output is:
>
> polymorphic_fuction
> ---------------------
> 2
> (1 row)
>
The exec_eval_using_params [1] function will handle the parameters, however,
it creates a plan for the expression only the first time [2], for the other
time, it uses the cached plan, which also caches the parameters' type
information, so it causes the problem that you mentation.
> If I understand the documentation
> (/docs/current/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING)
> correctly, this should work. I noticed that the record type is not listed
The documentation says:
The mutable nature of record variables presents another problem in this
connection. When fields of a record variable are used in expressions or
statements, **the data types of the fields must not change from one call
of the function to the next**, since each expression will be analyzed
using the data type that is present when the expression is first reached.
EXECUTE can be used to get around this problem when necessary.
However, I'am not sure how to use EXECUTE to avoid this problem.
[1] https://github.com/postgres/postgres/blob/ccd10a9bfa54c1aad3561232bf24222f1b455e1c/src/pl/plpgsql/src/pl_exec.c#L8573
[2] https://github.com/postgres/postgres/blob/ccd10a9bfa54c1aad3561232bf24222f1b455e1c/src/pl/plpgsql/src/pl_exec.c#L5671
--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.
From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Japin Li <japinli(at)hotmail(dot)com> |
Cc: | mjurca(at)centrum(dot)cz, pgsql-bugs(at)lists(dot)postgresql(dot)org |
Subject: | Re: BUG #17610: Use of multiple composite types incompatible with record-typed function parameter |
Date: | 2022-09-08 16:17:14 |
Message-ID: | 4048043.1662653834@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-bugs |
Japin Li <japinli(at)hotmail(dot)com> writes:
> On Thu, 08 Sep 2022 at 18:19, PG Bug reporting form <noreply(at)postgresql(dot)org> wrote:
>> The SQL code at the end of this bug report ends with the following error:
>> 2022-09-08 10:09:02.173 GMT [174] ERROR: type of parameter 1
>> (composite_type_2) does not match that when preparing the plan
>> (composite_type_1)
> The documentation says:
> The mutable nature of record variables presents another problem in this
> connection. When fields of a record variable are used in expressions or
> statements, **the data types of the fields must not change from one call
> of the function to the next**, since each expression will be analyzed
> using the data type that is present when the expression is first reached.
I think that is specifically referring to function internal variables of
type RECORD, which are handled specially. For function input arguments,
we could get around this by treating RECORD like a polymorphic type, as
attached. (For some reason I thought we already did that, but nope.)
regards, tom lane
Attachment | Content-Type | Size |
---|---|---|
treat-record-inputs-as-polymorphism.patch | text/x-diff | 2.3 KB |
From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Japin Li <japinli(at)hotmail(dot)com> |
Cc: | mjurca(at)centrum(dot)cz, pgsql-bugs(at)lists(dot)postgresql(dot)org |
Subject: | Re: BUG #17610: Use of multiple composite types incompatible with record-typed function parameter |
Date: | 2022-09-08 18:36:47 |
Message-ID: | 4102534.1662662207@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-bugs |
I wrote:
> I think that is specifically referring to function internal variables of
> type RECORD, which are handled specially. For function input arguments,
> we could get around this by treating RECORD like a polymorphic type, as
> attached. (For some reason I thought we already did that, but nope.)
Oh ... I see why we hadn't noticed this before. The case works fine
if you refer to the RECORD parameter by name, like this example from
our regression tests:
create function getf1(x record) returns int language plpgsql as
$$ begin return x.f1; end $$;
It does not work fine when you do this:
create function getf1(record) returns int language plpgsql as
$$ begin return $1.f1; end $$;
The reason is that the core parser's callback APIs allow plpgsql
to deal with the "x.f1" construct as a unit, and it can handle
the varying actual type of "x" internally. However, the callback
APIs are not equivalently intelligent about "$N" references.
Those get resolved as Params of type RECORD (with a separate
FieldSelect on top, in this case), and then we don't have enough
context to figure out which record type is involved, or indeed
that different record types could be involved.
My proposed patch fixes things for the case where the caller
passes a named composite type, but not for passing an anonymous
record, as you can see in the test cases in the attached.
(If you don't apply the code patch, the last getf2() call also
fails, matching the OP's complaint. But the getf1() calls
still work.)
So really the way we ought to fix this is to upgrade the parser
APIs so that plpgsql could deal with "$1.f1" as a unit. But
that seems like a lot of work, and it would certainly not be
back-patchable.
In the meantime, I'm uncertain whether we want this change or not.
Duplicating the function cache entry for each composite type
that gets passed during a session is a pretty expensive solution,
especially if it only fixes cases that are being written in a
semi-deprecated fashion.
regards, tom lane
Attachment | Content-Type | Size |
---|---|---|
treat-record-inputs-as-polymorphism-2.patch | text/x-diff | 5.3 KB |
From: | Japin Li <japinli(at)hotmail(dot)com> |
---|---|
To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Cc: | mjurca(at)centrum(dot)cz, pgsql-bugs(at)lists(dot)postgresql(dot)org |
Subject: | Re: BUG #17610: Use of multiple composite types incompatible with record-typed function parameter |
Date: | 2022-09-09 01:07:04 |
Message-ID: | MEYP282MB16698A279FEBE7967E5B299DB6439@MEYP282MB1669.AUSP282.PROD.OUTLOOK.COM |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-bugs |
On Fri, 09 Sep 2022 at 02:36, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> The reason is that the core parser's callback APIs allow plpgsql
> to deal with the "x.f1" construct as a unit, and it can handle
> the varying actual type of "x" internally. However, the callback
> APIs are not equivalently intelligent about "$N" references.
> Those get resolved as Params of type RECORD (with a separate
> FieldSelect on top, in this case), and then we don't have enough
> context to figure out which record type is involved, or indeed
> that different record types could be involved.
>
> My proposed patch fixes things for the case where the caller
> passes a named composite type, but not for passing an anonymous
> record, as you can see in the test cases in the attached.
> (If you don't apply the code patch, the last getf2() call also
> fails, matching the OP's complaint. But the getf1() calls
> still work.)
>
> So really the way we ought to fix this is to upgrade the parser
> APIs so that plpgsql could deal with "$1.f1" as a unit. But
> that seems like a lot of work, and it would certainly not be
> back-patchable.
>
Tested and looks good!
> In the meantime, I'm uncertain whether we want this change or not.
> Duplicating the function cache entry for each composite type
> that gets passed during a session is a pretty expensive solution,
> especially if it only fixes cases that are being written in a
> semi-deprecated fashion.
>
Yeah, maybe we can document it. Or is there any way for the user to
discard the cache entry?
--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.
From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Japin Li <japinli(at)hotmail(dot)com> |
Cc: | mjurca(at)centrum(dot)cz, pgsql-bugs(at)lists(dot)postgresql(dot)org |
Subject: | Re: BUG #17610: Use of multiple composite types incompatible with record-typed function parameter |
Date: | 2022-09-16 17:28:02 |
Message-ID: | 3841530.1663349282@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-bugs |
Japin Li <japinli(at)hotmail(dot)com> writes:
> On Fri, 09 Sep 2022 at 02:36, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> So really the way we ought to fix this is to upgrade the parser
>> APIs so that plpgsql could deal with "$1.f1" as a unit. But
>> that seems like a lot of work, and it would certainly not be
>> back-patchable.
> Tested and looks good!
After letting this bake awhile, I still haven't thought of a better
idea. Also, even if we wanted to invest the effort to make "$1.f1"
an integrated construct, I think you could still easily break it,
say with syntax like "($1).f1". The variant "(arg).f1" would be
problematic too without this change.
Hence, pushed as-is.
regards, tom lane