public inbox for [email protected]
help / color / mirror / Atom feedFrom: David G. Johnston <[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 08:17:52 -0700
Message-ID: <CAKFQuwbjcqPV-xf6FUq4ECrwmt0woWVB78gs2sszJWVzJH5dYA@mail.gmail.com> (raw)
In-Reply-To: <CA+i9DRE8Uh_rYTcbMvQBWCmjQNgb_4N=7V2vMPOZa1iQV7HOkA@mail.gmail.com>
References: <CA+i9DRE8Uh_rYTcbMvQBWCmjQNgb_4N=7V2vMPOZa1iQV7HOkA@mail.gmail.com>
On Fri, Apr 17, 2026 at 7:33 AM Leendert Gravendeel <[email protected]>
wrote:
> I believe I have found a parser issue in PL/pgSQL involving the
> FOREACH ... SLICE syntax.
>
Thanks for the report!
>
> CREATE FUNCTION test_slice_conflict() RETURNS text
> LANGUAGE plpgsql AS $$
> DECLARE
> slice integer[];
> arr integer[] := ARRAY[[1,2],[3,4]];
> BEGIN
> FOREACH slice SLICE 1 IN ARRAY arr LOOP
> END LOOP;
> RETURN 'ok';
> END;
> $$;
>
> Observed behavior:
> The function fails to compile due to incorrect parsing of `slice`
> as the SLICE keyword.
>
> Expected behavior:
> `slice` should be treated as a normal identifier (loop variable),
> and the function should compile and run successfully.
>
>
Confirmed on master.
Chat provided much more context for how/why this happened and why this
seems like a good fix; prior art is my main argument though.
If a PL/pgSQL variable is named "slice", using it in a FOREACH ... SLICE
loop produces a spurious syntax error:
DO $$ DECLARE
slice integer[];
arr integer[] := ARRAY[[1,2],[3,4]];
BEGIN
FOREACH slice SLICE 1 IN ARRAY arr LOOP
END LOOP;
END; $$;
ERROR: syntax error at or near "SLICE"
The one-token lookahead in the for_variable grammar action runs under
normal identifier lookup, so when "slice" is in scope the following SLICE
keyword is consumed as a T_DATUM reference rather than K_SLICE, and
foreach_slice fails.
The fix is to suppress variable lookup for that lookahead, using the same
save/restore pattern already used in pl_gram.y::read_cursor_args():
/* Read the argument name, ignoring any matching variable */
save_IdentifierLookup = plpgsql_IdentifierLookup;
plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
yylex(yylvalp, yyllocp, yyscanner);
argname = yylvalp->str;
plpgsql_IdentifierLookup = save_IdentifierLookup;
Therefore we need:
diff --git a/src/pl/plpgsql/src/pl_gram.y b/src/pl/plpgsql/src/pl_gram.y
index 5009e59a78f..681fd3d5cff 100644
--- a/src/pl/plpgsql/src/pl_gram.y
+++ b/src/pl/plpgsql/src/pl_gram.y
@@ -1635,11 +1635,15 @@ for_variable : T_DATUM
else
{
int tok;
+ IdentifierLookup save_IdentifierLookup;
$$.scalar = $1.datum;
$$.row = NULL;
/* check for comma-separated list */
+ save_IdentifierLookup = plpgsql_IdentifierLookup;
+ plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
tok = yylex(&yylval, &yylloc, yyscanner);
+ plpgsql_IdentifierLookup = save_IdentifierLookup;
plpgsql_push_back_token(tok, &yylval, &yylloc,
yyscanner);
if (tok == ',')
$$.row = (PLpgSQL_datum *)
David J.
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: <CAKFQuwbjcqPV-xf6FUq4ECrwmt0woWVB78gs2sszJWVzJH5dYA@mail.gmail.com>
* 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