Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mDXqO-0004hN-33 for pgsql-general@arkaria.postgresql.org; Tue, 10 Aug 2021 19:55:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mDXqM-00080J-BB for pgsql-general@arkaria.postgresql.org; Tue, 10 Aug 2021 19:55:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mDXqL-00080A-VU for pgsql-general@lists.postgresql.org; Tue, 10 Aug 2021 19:55:22 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mDXqJ-0006Qs-04 for pgsql-general@lists.postgresql.org; Tue, 10 Aug 2021 19:55:21 +0000 Received: by mail-pj1-x1030.google.com with SMTP id g12-20020a17090a7d0cb0290178f80de3d8so653824pjl.2 for ; Tue, 10 Aug 2021 12:55:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=ufrxKzbgG8mjyvqYX339yNlusFOcldrsP/L1QyVJhoA=; b=RYinYdA2jJNwSAmbb2kMVPFnAGEFt3UCHEOV1P5qhhhDQnHOVZgBNKnCxHDyMxi+Pr IAhJFZh3aBkNJdOJu7R5PFlQ7rUcHYHzdB8ERp2DSHHE7PBYCfV4y5pdEujBFcZ0FBu5 M9udEwFG5ns3IjVEds78P8y/dsibKmiCcj+Ik= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=ufrxKzbgG8mjyvqYX339yNlusFOcldrsP/L1QyVJhoA=; b=RR11Lld3r/Nm98dN4JvwLICLI8N8qvHyiYggpoXigYNBAgfBPqrnrSyxEWw0mh9hPP gSzi2yn0HZ3GA97Et5vq9OJnYwC674ZS2XrMfzYQ+2qTI+t9Z21ep15/btCdcaoFhvep AEw5NTFJB38ZbGsgVXXcFw7AO28657+ig9WkgqM46qXyl3SBejnRkZAO+T/T7LSaO6+m Ovpe/lQk7qdREZvzUfmqPArH0sPuofFuodFbC1UGREeIM/uAIfLeNcuoL8SkGcmskIje 4SDI8lwxpXSWuklR8QC0Bj/QTDrjFzW3TYQ5XwVEJBGmRRI4yDt7KnSskJXTU7Bea3/X Qgow== X-Gm-Message-State: AOAM532ehDEdig/ILqdSTCAz0vMLFGkQqW2qqP++rc6aQWIUHVr0gnR7 QhkIfuak/Wr64lrOzOC9p2M6FQ== X-Google-Smtp-Source: ABdhPJynxh4JAiWqJ6awctiNGRpiY7BMpVFjTx7LRvjCZZGQgItWmHvkcj/2rjOalUNX6o6IeUVU8Q== X-Received: by 2002:aa7:870e:0:b029:3c2:f326:468b with SMTP id b14-20020aa7870e0000b02903c2f326468bmr30998221pfo.53.1628625316162; Tue, 10 Aug 2021 12:55:16 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:7d7a:f60b:1fea:27d0]) by smtp.gmail.com with ESMTPSA id c14sm28592199pgv.86.2021.08.10.12.55.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Aug 2021 12:55:15 -0700 (PDT) From: Bryn Llewellyn Message-Id: <2100F674-AFEC-463B-8763-C4F9640CCAE7@yugabyte.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_DC188E88-B28D-41A8-BB64-FB71A678B06E" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) 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 In-Reply-To: Cc: Tom Lane PostgreSQL , pgsql-general list To: Pavel Stehule References: <4FC8BC35-0048-406D-9BFD-774D7963D31F@yugabyte.com> <3575571.1628543627@sss.pgh.pa.us> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_DC188E88-B28D-41A8-BB64-FB71A678B06E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > pavel.stehule@gmail.com wrote: >=20 > bryn@yugabyte.com wrote: >=20 >> pavel.stehule@gmail.com wrote: >>=20 >>> tgl@sss.pgh.pa.us wrote: >>>=20 >>>> pavel.stehule@gmail.com wrote: >>>>=20 >>>> Some errors like this, but not this can be detected by = plpgsql_check >>>> https://github.com/okbob/plpgsql_check = >>>> probably the heuristic for type check is not complete. >>>=20 >>> 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. >>>=20 >>> In the meantime, it does seem like the docs could be more explicit = about this, and perhaps give an example showing the (x).* solution. >>=20 >> 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. >>=20 >> var :=3D (SELECT x FROM ..) >>=20 >> 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. >=20 > Consider this example: >=20 > 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 =3D 1; >=20 > It seems to be perfectly plausible=E2=80=94and 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: >=20 > Yes, this works. This syntax is not ambiguous. >=20 > do $body$ > declare > r record; > begin > select ((b, t)).* > into r > from tab1 > where k =3D 1; > raise info 'Alt 1: % | %', r.f1::text, r.f2::text; >=20 > r :=3D ( > select (b, t) > from tab1 > where k =3D 1); > raise info 'Alt 2: % | %', r.f1::text, r.f2::text; > end; > $body$; >=20 > 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. >=20 > postgres=3D# 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 >=20 > The composite value always has structure, and types, but sometimes it = can lose labels. You can push labels by casting >=20 > r :=3D (select (b, t) -- this is dynamic composity value, but without = labels - the scalar value doesn't hold label >=20 > or >=20 > r :=3D (select (b, t)::type1 -- it is composite with labels again Thanks, but I don't understand your "r :=3D (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 =3D 1; when 2 then r :=3D ( select (b, t)::type1 from tab1 where k =3D 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! --Apple-Mail=_DC188E88-B28D-41A8-BB64-FB71A678B06E Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
pavel.stehule@gmail.com wrote:


pavel.stehule@gmail.com wrote:

tgl@sss.pgh.pa.us wrote:

pavel.stehule@gmail.com wrote:

Some errors like this, = but not this can be detected by plpgsql_check
https://github.com/okbob/plpgsql_check
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 :=3D (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 =3D 1;


It = seems to be perfectly plausible=E2=80=94and 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 =3D 1;
  raise info 'Alt 1: % | %', r.f1::text, = r.f2::text;

  r :=3D (
    select (b, t)
  =   from tab1
    where k =3D = 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=3D# 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 :=3D (select (b, t) -- this is = dynamic composity value, but without labels - the scalar value doesn't = hold label

or

r = :=3D (select (b, t)::type1 -- it is composite with labels = again

Thanks, = but I don't understand your "r :=3D (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 =3D = 1;

    when 2 then
      r :=3D (
  =       select (b, t)::type1
  =       from tab1
      =   where k =3D 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!


= --Apple-Mail=_DC188E88-B28D-41A8-BB64-FB71A678B06E--