pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christophe Pettus <xof@thebuild.com>
To: Bryn Llewellyn <bryn@yugabyte.com>
Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: « The PL/pgSQL interpreter parses the function's source text and produces an internal binary instruction tree... »
Date: Thu, 28 Jul 2022 19:09:36 -0700
Message-ID: <445B2142-D857-4BD4-9DA9-3988D6CF5658@thebuild.com> (raw)
In-Reply-To: <C4FB1CF4-AF47-4443-B7D2-94CB817D4666@yugabyte.com>
References: <DD03DCB0-1946-4CEB-8D72-D94EE7A85A7A@yugabyte.com>
	<14BAC5B9-225E-49A8-A225-60C2F179E334@thebuild.com>
	<C4FB1CF4-AF47-4443-B7D2-94CB817D4666@yugabyte.com>



> On Jul 28, 2022, at 18:49, Bryn Llewellyn <bryn@yugabyte.com> 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 := 0;
BEGIN
   x := y;
END;
$$ language plpgsql;

But gets an error on execution:

xof=# SELECT f();
ERROR:  column "y" does not exist
LINE 1: x := y
             ^
QUERY:  x := 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 := 0;
BEGIN
   y := x;
END;
$$ language plpgsql;

ERROR:  "y" is not a known variable
LINE 5:    y := 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.




view thread (8+ messages)  latest in thread

Message-ID: <445B2142-D857-4BD4-9DA9-3988D6CF5658@thebuild.com>
Permalink:  ../445B2142-D857-4BD4-9DA9-3988D6CF5658@thebuild.com/
Also on:    postgresql.org/message-id/445B2142-D857-4BD4-9DA9-3988D6CF5658@thebuild.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: xof@thebuild.com, bryn@yugabyte.com
  Subject: Re: « The PL/pgSQL interpreter parses the function's source text and produces an internal binary instruction tree... »
  In-Reply-To: <445B2142-D857-4BD4-9DA9-3988D6CF5658@thebuild.com>
  References: <DD03DCB0-1946-4CEB-8D72-D94EE7A85A7A@yugabyte.com> <14BAC5B9-225E-49A8-A225-60C2F179E334@thebuild.com> <C4FB1CF4-AF47-4443-B7D2-94CB817D4666@yugabyte.com> <445B2142-D857-4BD4-9DA9-3988D6CF5658@thebuild.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox