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 1oG1yc-0000jZ-JT for pgsql-general@arkaria.postgresql.org; Mon, 25 Jul 2022 17:34:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oG1yb-0003Ki-BG for pgsql-general@arkaria.postgresql.org; Mon, 25 Jul 2022 17:34:41 +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 1oG1ya-0003KZ-TG for pgsql-general@lists.postgresql.org; Mon, 25 Jul 2022 17:34:41 +0000 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oG1yW-0004gc-UU for pgsql-general@lists.postgresql.org; Mon, 25 Jul 2022 17:34:40 +0000 Received: by mail-pj1-x102a.google.com with SMTP id ku18so11158022pjb.2 for ; Mon, 25 Jul 2022 10:34:36 -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=V1nKNzXkR44LC7jc+DmfW6q9eVI5DeOuTuzcar8YVw4=; b=O0jg/JEnMOlYOgxU0mpRIjtoVgEzstI/QZnd/tJjGPtNvb1bt0/huX65dS6snvk76V 3pCttYK1uHUnqDomFmp3FFFMjJlGAJuIZdJFk6aiVgSdO5ie0ITlLHyH2XOMRFUvwss2 p4ExZWxo48GDo/kJdX9mcOC8z75exZEvjzYCw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:mime-version:subject:message-id:date:to; bh=V1nKNzXkR44LC7jc+DmfW6q9eVI5DeOuTuzcar8YVw4=; b=eRyB6EtXBAbzoYzy/0ZXajP6sMvtunQHJemlXGPJZ9dSsAg1X/vWwZ7+erGXiwum8j ipYhBGjonVqEbU6QxsL9RaYB6ikvE6B6DQXCHniy01D+sYtpQgMn3CnrCKFDGLlbV2IO boJSuPTBMnJbzGBBCkz1pZdpW66ncFAbu0gyMo8QFN6LH5UsjFk1hShz/8PCjOwQcSx+ sKDmtqvz+EZrqm3XUFeKQ8qN1HCP5GOY6n3aMRkg9FlnD+4eOKmvuT+RcXCtlf+523Nj zXuyiHfgvr7UnwptomXS0aV7+95YriowaM50sD+BpW6vfuhQW3FOU9sG2Vg7xRzsJN0Y dYqw== X-Gm-Message-State: AJIora9VzoPRtkJnJi8iM0rzcwbmIOq8JDCQrUje5LWI7iqSFrPW6Jzx 2ySndPociUpeT93k0qvFlhbQyyBYyJR1dA== X-Google-Smtp-Source: AGRyM1vPE+xzWIYL1nYzs+X8wym4/SQLM+t/UBZJ+Y/fkiZ3lqhQp5YB8xeq6ZkgoM6KhKqsDTwdOw== X-Received: by 2002:a17:902:bb90:b0:16d:2d15:3a91 with SMTP id m16-20020a170902bb9000b0016d2d153a91mr13230747pls.4.1658770474196; Mon, 25 Jul 2022 10:34:34 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:5daf:d6ca:d93c:6b0a]) by smtp.gmail.com with ESMTPSA id p3-20020a1709028a8300b0016d62ba5665sm4322793plo.254.2022.07.25.10.34.32 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Jul 2022 10:34:32 -0700 (PDT) From: Bryn Llewellyn Content-Type: multipart/alternative; boundary="Apple-Mail=_D295205A-637E-4744-BBCF-A931D8F11A86" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: =?utf-8?Q?PL/pgSQL=3A_=C2=AB_arr=5Bj=5D=2Ea_=3A=3D_v_=C2=BB_works?= =?utf-8?Q?_fine_in_PG_Version_14=2E4=2C_fails_to_compile_in_Version_11=2E?= =?utf-8?Q?2=2E_Which_version_brought_the_fix=3F?= Message-Id: <0ABA97CF-ADA4-43D5-A2DB-38A705561494@yugabyte.com> Date: Mon, 25 Jul 2022 10:34:31 -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=_D295205A-637E-4744-BBCF-A931D8F11A86 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii I copied my testcase at the end. It runs OK and produces the output that = I expect using PG Version 14.4. But using Version 11.9 (and earlier 11 = sub-versions), it fails to compile with this error: syntax error at or near "." lhs[j].a :=3D rhs[j].a; ^ If I comment out the "Ideal approach" loop, then it runs fine in PG 11 = and produces the same output as the "Ideal approach" does in PG 14, I looked at the Release Notes for each of Versions 14, 13, and 12: https://www.postgresql.org/docs/12/release-12-1.html = https://www.postgresql.org/docs/13/release-13-1.html = https://www.postgresql.org/docs/14/release-14-1.html = I searched in the page for "array". But there were no relevant hits on = any of those three pages. Can anybody tell me which PG release brought the improvement? p.s., I'm asking because YugabyteDB still uses the PG 11.2 SQL = processing code. I'm promised that fairly soon a new YugabyteDB version = will use the PG 13 SQL processing code. I'm hoping that, then, I'll be = able to retire my workaround. = --------------------------------------------------------------------------= ------ create type t as (a text, b text); create function f() returns table(z text) language plpgsql as $body$ declare lhs t[] not null :=3D array[('??', '??'), ('??', '??'), = ('??', '??')]; rhs constant t[] not null :=3D array[('a1', 'b1'), ('a2', 'b2'), = ('a3', 'b3')]; r t not null :=3D ('??', '??'); begin for j in 1..3 loop z :=3D rhs[j].a||', '||rhs[j].b; return next; end loop; -- Workaround for j in 1..3 loop r :=3D rhs[j]; r.a :=3D rhs[j].a; r.b :=3D rhs[j].b; lhs[j] :=3D r; end loop; -- Ideal approach. for j in 1..3 loop lhs[j].a :=3D rhs[j].a; lhs[j].b :=3D rhs[j].b; end loop; z :=3D ''; return next; for j in 1..3 loop z :=3D lhs[j].a||', '||lhs[j].b; return next; end loop; end; $body$; select z from f(); --Apple-Mail=_D295205A-637E-4744-BBCF-A931D8F11A86 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
I copied my testcase = at the end. It runs OK and produces the output that I expect = using PG Version 14.4. But using Version 11.9 (and earlier 11 = sub-versions), it fails to compile with this error:

syntax error at or near = "."
lhs[j].a :=3D = rhs[j].a;
      = ^
If I comment out the "Ideal = approach" loop, then it runs fine in PG 11 and produces the same output = as the "Ideal approach" does in PG 14,

I looked at the Release Notes for each = of Versions 14, 13, and 12:

https://www.postgresql.org/docs/12/release-12-1.html
<= div class=3D"">https://www.postgresql.org/docs/13/release-13-1.html
<= div class=3D"">https://www.postgresql.org/docs/14/release-14-1.html<= div class=3D"">
I searched in the = page for "array". But there were no relevant hits on any of those three = pages.

Can = anybody tell me which PG release brought the improvement?

p.s., I'm asking because = YugabyteDB still uses the PG 11.2 SQL processing code. I'm promised that = fairly soon a new YugabyteDB version will use the PG 13 SQL = processing code. I'm hoping that, then, I'll be able to retire my = workaround.

---------------------------------------------------------------= -----------------

create type t as (a text, b text);

create function f()
  returns table(z text)
  language plpgsql
as $body$
declare
  lhs      =     t[] not null :=3D array[('??', '??'), ('??', '??'), = ('??', '??')];
  rhs constant t[] not null :=3D = array[('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')];

  r          =   t   not null :=3D     =   ('??', '??');
begin
  for j in 1..3 loop
  =   z :=3D rhs[j].a||', '||rhs[j].b;    return = next;
  end loop;

  -- Workaround
  for j in = 1..3 loop
    r :=3D rhs[j];
    r.a :=3D rhs[j].a;
  =   r.b :=3D rhs[j].b;
    lhs[j] = :=3D r;
  end loop;

  -- Ideal approach.
  for j in 1..3 loop
  =   lhs[j].a :=3D rhs[j].a;
  =   lhs[j].b :=3D rhs[j].b;
  end = loop;

  z :=3D '';    =                     =     return next;
  for j in 1..3 = loop
    z :=3D lhs[j].a||', = '||lhs[j].b;    return next;
  end = loop;
end;
$body$;

select z from f();

= --Apple-Mail=_D295205A-637E-4744-BBCF-A931D8F11A86--