Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Bryn Llewellyn <bryn(at)yugabyte(dot)com>
Cc: Tom Lane PostgreSQL <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-general list <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type
Date: 2021-08-10 20:12:58
Message-ID: CAFj8pRBQ1hidAa8VKv2MrAEyYnbcG454GXBZ1=QzNU4vZxzH=w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>
> r := (select (b, t)::type1 -- it is composite with labels again
>
>
>
postgres=# do $$
declare r record;
begin
r := (select (10,20));
raise notice '%', to_json(r);
end;
$$;
NOTICE: {"f1":10,"f2":20}
DO
postgres=# do $$
declare r record;
begin
r := (select (10,20)::footype);
raise notice '%', to_json(r);
end;
$$;
NOTICE: {"a":10,"b":20}
DO

> Thanks, but I don't understand your "r := (select (b, t)::type1 -- it is
> composite with labels again". I tried this:
>
> create procedure p(i in int)
> language plpgsql
> as $body$
> declare
> r record;
> begin
> case i
> when 1 then
> select (b, t)::type1
> into r
> from tab1
> where k = 1;
>
> when 2 then
> r := (
> select (b, t)::type1
> from tab1
> where k = 1);
>
> else null;
> end case;
> end;
> $body$;
>
> call p(3);
> call p(2);
> call p(1);
>
> My idea with using a procedure and choosing which code path is followed at
> run-time is to distinguish between compile-time errors (there are none
> here) and run-time errors. Of course, "call p(3)" finishes with no error.
>
> But both the other calls cause the same error:
>
> 42846: cannot cast type record to type1
>

the message is maybe not too intuitive, these casts are broken - you try to
cast (boolean, type1) => type1

The cast can ignore some fields from right or can add nulls from right, but
it cannot skip fields from left.

> But you say that this should work!
>
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bryn Llewellyn 2021-08-10 20:30:17 Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type
Previous Message Bryn Llewellyn 2021-08-10 19:55:14 Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type