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 1oHFSd-0007Lw-Rq for pgsql-general@arkaria.postgresql.org; Fri, 29 Jul 2022 02:10:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oHFSc-0000K7-Dj for pgsql-general@arkaria.postgresql.org; Fri, 29 Jul 2022 02:10:42 +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 1oHFSc-0000Jy-2O for pgsql-general@lists.postgresql.org; Fri, 29 Jul 2022 02:10:42 +0000 Received: from smtp91.ord1d.emailsrvr.com ([184.106.54.91]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oHFSZ-0001ge-76 for pgsql-general@lists.postgresql.org; Fri, 29 Jul 2022 02:10:40 +0000 X-Auth-ID: xof@thebuild.com Received: by smtp4.relay.ord1d.emailsrvr.com (Authenticated sender: xof-AT-thebuild.com) with ESMTPSA id D5CB1400A1; Thu, 28 Jul 2022 22:10:37 -0400 (EDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Subject: =?utf-8?Q?Re=3A_=C2=AB_The_PL/pgSQL_interpreter_parses_the_functi?= =?utf-8?Q?on=27s_source_text_and_produces_an_internal_binary_instruction_?= =?utf-8?Q?tree=2E=2E=2E_=C2=BB?= From: Christophe Pettus In-Reply-To: Date: Thu, 28 Jul 2022 19:09:36 -0700 Cc: pgsql-general list X-Mailbutler-Message-Id: F010A9F1-ACBC-4F9E-A935-B370A4717DE3 Content-Transfer-Encoding: quoted-printable Message-Id: <445B2142-D857-4BD4-9DA9-3988D6CF5658@thebuild.com> References: <14BAC5B9-225E-49A8-A225-60C2F179E334@thebuild.com> To: Bryn Llewellyn X-Mailer: Apple Mail (2.3608.120.23.2.7) X-Classification-ID: 1b2df4c2-6b78-4f47-ba3f-5bb55859f8d3-1-1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Jul 28, 2022, at 18:49, Bryn Llewellyn wrote: > It's this that surprises me. And it's this, and only this, that I'm = asking about: might _just_ this be a fixable bug? It might be surprising, but it's not a bug. You can demonstrate it with = a very small test case: CREATE FUNCTION f() RETURNS VOID AS $$ DECLARE x int not null :=3D 0; BEGIN x :=3D y; END; $$ language plpgsql; But gets an error on execution: xof=3D# SELECT f(); ERROR: column "y" does not exist LINE 1: x :=3D y ^ QUERY: x :=3D y CONTEXT: PL/pgSQL function f() line 5 at assignment The clue is that it is complaining about a missing "column." Assignment = in PL/pgSQL is essentially syntactic sugar around a SELECT ... INTO. = The assignment there is processed pretty much as if it were written: SELECT y INTO x; Note, however, that this does *not* compile: CREATE OR REPLACE FUNCTION f() RETURNS VOID AS $$ DECLARE x int not null :=3D 0; BEGIN y :=3D x; END; $$ language plpgsql; ERROR: "y" is not a known variable LINE 5: y :=3D x; Unquestionably, this is surprising! The reasons, such as they are, are = based in how PL/pgSQL processes SQL statements. (For example, if you = look at the grammar, it literally takes "SELECT x INTO y;" turns it into = "SELECT x ;", and passes that to the SPI. This has the virtue = that it doesn't have to have a complete PostgreSQL SQL grammar = replicated in it (what a nightmare), but it does result in some of the = implementation poking through.=