public inbox for [email protected]  
help / color / mirror / Atom feed
From: Tom Lane <[email protected]>
To: Leendert Gravendeel <[email protected]>
Cc: [email protected]
Subject: Re: BUG: PL/pgSQL FOREACH misparses variable named "slice" with SLICE clause
Date: Fri, 17 Apr 2026 11:16:20 -0400
Message-ID: <[email protected]> (raw)
In-Reply-To: <CA+i9DRE8Uh_rYTcbMvQBWCmjQNgb_4N=7V2vMPOZa1iQV7HOkA@mail.gmail.com>
References: <CA+i9DRE8Uh_rYTcbMvQBWCmjQNgb_4N=7V2vMPOZa1iQV7HOkA@mail.gmail.com>

Leendert Gravendeel <[email protected]> writes:
> When using FOREACH with the SLICE clause, a loop variable named
> `slice` is misinterpreted as the SLICE keyword, causing the statement
> to fail. Renaming the variable to anything else makes the function
> work as expected.

So, um, don't do that.  PL/pgSQL will let you use (many of) its keywords
as variable names, but once you declare such a variable, the word will
be taken as a variable reference not as a keyword for the rest of the
block.  So what the parser is seeing here is

     FOREACH variable variable 1 IN ARRAY variable LOOP

and it naturally doesn't like that.  You could also break this
statement by making a variable named "array", for example.
(But not "foreach", "in", or "loop", as those are reserved words.)

Not sure how well this behavior is documented.  I think there are
a small number of exceptions, in places where the scanner can know
that the next word must be a keyword, but most unreserved keywords
work this way.  The only other thing we could do is make such keywords
fully reserved, which would break legacy code that uses "slice" or
whatever as a variable name without knowledge of the new SLICE syntax.
So we generally try to make newly-added keywords unreserved; this
ambiguity seems the lesser evil.

			regards, tom lane






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 using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: BUG: PL/pgSQL FOREACH misparses variable named "slice" with SLICE clause
  In-Reply-To: <[email protected]>

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

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