pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bryn Llewellyn <bryn@yugabyte.com>
To: Pavel Stehule <pavel.stehule@gmail.com>
Cc: Tom Lane PostgreSQL <tgl@sss.pgh.pa.us>
Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type
Date: Tue, 10 Aug 2021 12:55:14 -0700
Message-ID: <2100F674-AFEC-463B-8763-C4F9640CCAE7@yugabyte.com> (raw)
In-Reply-To: <CAFj8pRBTx46Q9Y7xKeWY5X_MQxwcv1B2QrXkFbRZDnoSBZ0Q0w@mail.gmail.com>
References: <4FC8BC35-0048-406D-9BFD-774D7963D31F@yugabyte.com>
	<CAFj8pRC0j4hSK=j-NHSRkS7LYSzc=A746pvoc+U6ss7QRJJo0A@mail.gmail.com>
	<3575571.1628543627@sss.pgh.pa.us>
	<CAFj8pRBn6yEb1EZY3N=-KLfXWVpx9FBCvL_YKMBK91wWKjjjYA@mail.gmail.com>
	<AAFA5570-FEE8-42CF-8B83-1CD6F00FE595@yugabyte.com>
	<CAFj8pRBTx46Q9Y7xKeWY5X_MQxwcv1B2QrXkFbRZDnoSBZ0Q0w@mail.gmail.com>

> pavel.stehule@gmail.com wrote:
> 
> bryn@yugabyte.com <mailto:bryn@yugabyte.com> wrote:
> 
>> pavel.stehule@gmail.com <mailto:pavel.stehule@gmail.com> wrote:
>> 
>>> tgl@sss.pgh.pa.us <mailto:tgl@sss.pgh.pa.us> wrote:
>>> 
>>>> pavel.stehule@gmail.com <mailto:pavel.stehule@gmail.com> wrote:
>>>> 
>>>> Some errors like this, but not this can be detected by plpgsql_check
>>>> https://github.com/okbob/plpgsql_check <https://www.google.com/url?q=https://github.com/okbob/plpgsql_check&source=gmail-imap&ust=16...;
>>>> probably the heuristic for type check is not complete.
>>> 
>>> STRICTMULTIASSIGNMENT would detect most cases of this, except that the condition is checked too late.  We'd need to count the fields *before* trying to assign values, not after.
>>> 
>>> In the meantime, it does seem like the docs could be more explicit about this, and perhaps give an example showing the (x).* solution.
>> 
>> Yes, a more detailed explanation of this behavior can be nice.  There can be an example of value unnesting, but I think so for this case, there should be mainly an example of ANSI assign syntax.
>> 
>> var := (SELECT x FROM ..)
>> 
>> This syntax has advantages so is not amigonuous for this case, and explicit unnesting is not necessary (and it is not possible). Moreover, this is ANSI SQL syntax.
> 
> Consider this example:
> 
> create type type1 as (a1 int, a2 int);
> create table tab1(k int primary key, b boolean not null, t type1 not null);
> insert into tab1(k, b, t) values(1, true, (10, 20));
> select b::text, t::text from tab1 where k = 1;
> 
> It seems to be perfectly plausible—and so it seems equally plausible that you'd want to do it using PL/pgSQL. Each of these two alternatives, inspired by the advice that I got in this thread, works and produces the expected output:
> 
> Yes, this works. This syntax is not ambiguous.
> 
> do $body$
> declare
>   r record;
> begin
>   select ((b, t)).*
>   into r
>   from tab1
>   where k = 1;
>   raise info 'Alt 1: % | %', r.f1::text, r.f2::text;
> 
>   r := (
>     select (b, t)
>     from tab1
>     where k = 1);
>   raise info 'Alt 2: % | %', r.f1::text, r.f2::text;
> end;
> $body$;
> 
> It feels a smidge uncomfortable because I'm forced to use reference by field position (f1, f2) rather than by field name (b, t). But reference by position is commonplace in PostgreSQL (for example, in the PREPARE statement). So I'spose that I have to learn to like it.
> 
> postgres=# do $$
> declare r record;
> begin
>   select 10 as a, 20 as b into r;
>   raise notice '% %', r.a, r.b;
> end;
> $$;
> NOTICE:  10 20
> DO
> 
> The composite value always has structure, and types, but sometimes it can lose labels. You can push labels by casting
> 
> r := (select (b, t) -- this is dynamic composity value, but without labels - the scalar value doesn't hold label
> 
> or
> 
> r := (select (b, t)::type1 -- it is composite with labels again

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

But you say that this should work!

view thread (15+ messages)  latest in thread

Message-ID: <2100F674-AFEC-463B-8763-C4F9640CCAE7@yugabyte.com>
Permalink:  ../2100F674-AFEC-463B-8763-C4F9640CCAE7@yugabyte.com/
Also on:    postgresql.org/message-id/2100F674-AFEC-463B-8763-C4F9640CCAE7@yugabyte.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: bryn@yugabyte.com, pavel.stehule@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type
  In-Reply-To: <2100F674-AFEC-463B-8763-C4F9640CCAE7@yugabyte.com>
  References: <4FC8BC35-0048-406D-9BFD-774D7963D31F@yugabyte.com> <CAFj8pRC0j4hSK=j-NHSRkS7LYSzc=A746pvoc+U6ss7QRJJo0A@mail.gmail.com> <3575571.1628543627@sss.pgh.pa.us> <CAFj8pRBn6yEb1EZY3N=-KLfXWVpx9FBCvL_YKMBK91wWKjjjYA@mail.gmail.com> <AAFA5570-FEE8-42CF-8B83-1CD6F00FE595@yugabyte.com> <CAFj8pRBTx46Q9Y7xKeWY5X_MQxwcv1B2QrXkFbRZDnoSBZ0Q0w@mail.gmail.com> <2100F674-AFEC-463B-8763-C4F9640CCAE7@yugabyte.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox