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 1mDB9c-0001wm-9e for pgsql-general@arkaria.postgresql.org; Mon, 09 Aug 2021 19:41: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 1mDB9b-000788-2D for pgsql-general@arkaria.postgresql.org; Mon, 09 Aug 2021 19:41:43 +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 1mDB9a-000780-Mz for pgsql-general@lists.postgresql.org; Mon, 09 Aug 2021 19:41:42 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mDB9W-0002z7-16 for pgsql-general@lists.postgresql.org; Mon, 09 Aug 2021 19:41:42 +0000 Received: by mail-pl1-x633.google.com with SMTP id a20so17757848plm.0 for ; Mon, 09 Aug 2021 12:41:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:mime-version:subject:message-id:date:to; bh=sYH5ISFMnvCQ8u9i5sEeU31iuCJvxQrUA3ALtTZzrv0=; b=UjWZG80YlZRrq4F1zfqGhcijYrAqgzVY0hIkmGz0Pr2EJmalbGbryDADwCYrKviOeG WSwG4jZDvZvA1MbzvhmFPT8pyNsGJyKGJ478mWzcNmZOc9PwnjHkYEMBoNr8CfFsSxfS sjqmPzmA58ZM/vyrgq0ITw2yk6Rcf3WXI7Ml4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:mime-version:subject:message-id:date:to; bh=sYH5ISFMnvCQ8u9i5sEeU31iuCJvxQrUA3ALtTZzrv0=; b=ffbri8yFcFXRbmwabnSifd8S3gbmkzKnsDd/6EiRrZyhxB/JWXe+Ukok4mVRE+tK9G bGbtfvB9w0tNKzvFozxlbLPifd1mXtJj2Hmx67krjsoqMYws/mhqj9XResuSd8nDQf++ YyY7hODZY9af3wNn0FqmOu82rlbiuwczmsPXUPYLSwN/jTCuZZYrViRdcSvOYTKE7WfK dJ7T/1pMOqYjQ+uQPpiTbYQNbT7Rss3F/Binl3RuocytbtIDktM0j//oSspB5dxLoB95 5TGn+ARfqanvn57YUgViJ6stQufCdr5IhmMNL/CcCzZZpnayO9EktVtSckVaamE4pbLV +ItA== X-Gm-Message-State: AOAM5326/LdvwodOJ0x0pOSXf1/uT+tYV/WHAKEVpKEJEGe0mD+YMTV8 SgzR4AoPoyYp928Rb0qTzu89rIRnRHKk7Q== X-Google-Smtp-Source: ABdhPJzlR1E12XL9AwikNgBvYTbyQRSrmSpD3e4BZje92G6VrVQyQrD7lB2Xhvv49wd5sUJMGB2evA== X-Received: by 2002:a17:90a:4a88:: with SMTP id f8mr756019pjh.226.1628538094925; Mon, 09 Aug 2021 12:41:34 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:750b:8836:cd22:7f4]) by smtp.gmail.com with ESMTPSA id e4sm20644184pgt.22.2021.08.09.12.41.34 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Aug 2021 12:41:34 -0700 (PDT) From: Bryn Llewellyn Content-Type: multipart/alternative; boundary="Apple-Mail=_B5AE5554-B53B-4D1E-AC45-012A885C15F2" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type Message-Id: <4FC8BC35-0048-406D-9BFD-774D7963D31F@yugabyte.com> Date: Mon, 9 Aug 2021 12:41:33 -0700 To: pgsql-general list 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=_B5AE5554-B53B-4D1E-AC45-012A885C15F2 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 The problem that I report here seems to be known and seems, too, to = astonish and annoy users. It's a bare "computer says No". It's hard to = find anything of ultimate use with Google search (either constrained to = the PG doc or unconstrained). Here's an example on stackoverflow:=20 = https://stackoverflow.com/questions/41843016/how-do-i-pass-a-user-defined-= type-variable-to-a-function-as-a-parameter However, it does give the _clue_ to the workaround. Here's an illustration of the issue, starting with what works fine. I = tested in using PG 13.3. create type rect as (h int, w int); create table t1(k int primary key, r1 rect not null); insert into t1(k, r1) values(1, (10, 20)); do $body$ declare r rect; begin r :=3D ( select r1 from t1 where k =3D 1); raise info '%', r::text; end; $body$; The "raise info" shows what you'd expect. This re-write fails. It simply uses the approach that anybody who hasn't = yet been bitten by this would expect to work. do $body$ declare r rect; begin select r1 -- line 5 into r from t1 where k =3D 1; end; $body$; This is the error: 22P02: invalid input syntax for type integer: "(10,20)" ... at line 5 With "VERBOSITY" set to "verbose", there's not hint to tell you what the = problem is and how to work around it. Question 1. ----------- Where, in the PL/pgSQL doc, does it state that "select col into var" = when col is a user-defined type doesn't work=E2=80=94and where is the = viable approach shown? Question 2. ----------- If I can easily re-write a failing approach by hand (once I know that I = must) so that it works, why cannot the PL/pgSQL compiler do this under = the covers? --Apple-Mail=_B5AE5554-B53B-4D1E-AC45-012A885C15F2 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
The problem that I report here seems to = be known and seems, too, to astonish and annoy users. It's a bare = "computer says No". It's hard to find = anything of ultimate use with Google search (either constrained to the = PG doc or unconstrained). Here's an example = on stackoverflow: 


However, it does give the _clue_ to the = workaround.

Here's an illustration of the issue, starting = with what works fine. I tested in using PG = 13.3.

create = type rect as (h int, w int);
create table t1(k int primary = key, r1 rect not null);
insert into t1(k, r1) values(1, = (10, 20));

do $body$
declare  r rect;
begin
  r :=3D (
    select = r1
    from t1
  =   where k =3D 1);
  raise info '%', = r::text;
end;
$body$;

The "raise info" shows = what you'd expect.

This = re-write fails. It simply = uses the approach that anybody who hasn't yet been bitten by this would = expect to work.

do $body$
declare  r rect;
begin
  select r1 -- line 5
  into = r
  from t1
  where k =3D = 1;
end;
$body$;

This is the error:

22P02: invalid input syntax for = type integer: "(10,20)" ... at line 5

With "VERBOSITY" set to "verbose", there's not hint to tell you = what the problem is and how to work around = it.

Question 1.
-----------
Where, in the PL/pgSQL doc, does it = state that "select col into var" when col is a user-defined type = doesn't work=E2=80=94and where is the viable approach shown?


Question 2.
-----------
If I can easily re-write a failing approach by hand (once I = know that I must) so that it works, why cannot the PL/pgSQL compiler do = this under the covers?


= --Apple-Mail=_B5AE5554-B53B-4D1E-AC45-012A885C15F2--