The following bug has been logged on the website:
Bug reference: 17610
Logged by: Martin Jurča
Email address: mjurca@centrum.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
(https://www.postgresql.org/docs/current/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING)
correctly, this should work. I noticed that the record type is not listed
among polymorphic types
(https://www.postgresql.org/docs/current/extend-type-system.html#EXTEND-TYPES-POLYMORPHIC),
but PL/pgSQL documentation
(https://www.postgresql.org/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)
);