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 1mDVro-0001Pz-Og for pgsql-general@arkaria.postgresql.org; Tue, 10 Aug 2021 17:48:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mDVrn-0001D6-Mh for pgsql-general@arkaria.postgresql.org; Tue, 10 Aug 2021 17:48:43 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mDVrn-0001Cy-8o for pgsql-general@lists.postgresql.org; Tue, 10 Aug 2021 17:48:43 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mDVrj-00051c-E1 for pgsql-general@lists.postgresql.org; Tue, 10 Aug 2021 17:48:42 +0000 Received: by mail-pl1-x62b.google.com with SMTP id l11so10603067plk.6 for ; Tue, 10 Aug 2021 10:48:39 -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=X9BGp2jwOd5pkNzpDlScFqUpvb83bFovXGpSRZ57Bio=; b=c6sJQHtBNvKQefT+BF5lXI8jXsfspb1chkywtIrwOUoi1TnN+FLf7/TmQsrJPcTb8P T+vIL3v2ihVLfNQ883+hveeGgmZ6JvW/0jU7PPlvjPZCmatpukS9qRnF9MKluzVPA9mW as4xO2zLQWY07H5iEqA6fdu5dHiMTDIWZ+16I= 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=X9BGp2jwOd5pkNzpDlScFqUpvb83bFovXGpSRZ57Bio=; b=Lrwo362KOIJOsLJrrhsacx1e0U1psBcVF+vt6ZDdTr7x9P+M6Gt4fb7SERfDqdGY/t LKRcptwFfL8js6zP3V04+rS31SINAcUfWvir2XG9Iwb8+3dKosmzocobYhCHZAkxY2wT jIWKkTI/S+5yB+J7JRTxmaDWGLam4OofgxdQjClFmvc+U94FuPls8ttfM3mVfc0bvTPP OE2JR+9I9mYgb3uJ6LlNU8VoK1F6kNHHQOAc0P8CIVml0L8FzdTYIcS8q+m7eo1SydtR sHsQ8T+V1+GX9QRPimfHLGTdfacxtWZakRx+Ez+H2HA0ORMimyLdJnkuHhWMU9x3ch9J rKag== X-Gm-Message-State: AOAM532KmHUKanJy9UXmHmIcgq60QJr06f0IJypYr97tmw4KghXUVRpG DMtItYsC0OuHGRcWTA9mBQu7aA== X-Google-Smtp-Source: ABdhPJyxFrvDSlYk6uSL5PT+RQfvqMKno09HUBJhJBvuJPVJOn2aQ/5O/E2lh3Eugsr9O37JMX40OQ== X-Received: by 2002:a65:5a8a:: with SMTP id c10mr557645pgt.290.1628617718526; Tue, 10 Aug 2021 10:48:38 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:7d7a:f60b:1fea:27d0]) by smtp.gmail.com with ESMTPSA id g22sm15567854pfo.164.2021.08.10.10.48.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Aug 2021 10:48:38 -0700 (PDT) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_AA049F71-D22E-43EF-AAAF-A0092CB96FB0" 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 10:48:37 -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=_AA049F71-D22E-43EF-AAAF-A0092CB96FB0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > 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. 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: 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. Do you (all) think that the "Alt 1" and "Alt 2" that I show are the best = way to implement the requirement? I prefer "Alt 2" because it doesn't = have the clutter (and the burden for understanding and readability) of = the extra parentheses and the ".*". I can get the reference by field name that I prefer with a schema-level = type: create type tmp as (b text, t type1); and by declaring "r" with this data type. But this is a greater = discomfort than using the dynamically shaped "record" because it needs = you to create a dedicated schema-level type for every new SELCT list = that you come need. --Apple-Mail=_AA049F71-D22E-43EF-AAAF-A0092CB96FB0 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
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:

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.

Do you (all) think that = the "Alt 1" and "Alt 2" that I show are the best way to implement the = requirement? I prefer "Alt 2" because it doesn't have the clutter (and = the burden for understanding and readability) of the extra parentheses = and the ".*".

I = can get the reference by field name that I prefer with a = schema-level type:

create type = tmp as (b text, t type1);

and by declaring "r" with this data = type. But this is a greater discomfort than using the dynamically shaped = "record" because it needs you to create a dedicated schema-level type = for every new SELCT list that you come need.




= --Apple-Mail=_AA049F71-D22E-43EF-AAAF-A0092CB96FB0--