public inbox for [email protected]  
help / color / mirror / Atom feed
From: Leendert Gravendeel <[email protected]>
To: [email protected]
Subject: BUG: PL/pgSQL FOREACH misparses variable named "slice" with SLICE clause
Date: Wed, 15 Apr 2026 20:56:50 +0200
Message-ID: <CA+i9DRE8Uh_rYTcbMvQBWCmjQNgb_4N=7V2vMPOZa1iQV7HOkA@mail.gmail.com> (raw)

Hello,

I believe I have found a parser issue in PL/pgSQL involving the
FOREACH ... SLICE syntax.

Version:
PostgreSQL 18 (reproduced on current release)

Description:
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.

Reproduction:

  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.

Additional notes:
- Renaming the variable (e.g. to `abc`) makes the function work.
- This suggests a keyword/identifier ambiguity where the parser
  prefers the SLICE keyword in this context.

Given that the documentation describes the FOREACH target as a
regular variable, this behavior seems unintended.

Please let me know if additional information is needed.

Best regards,
Leendert Gravendeel






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]
  Subject: Re: BUG: PL/pgSQL FOREACH misparses variable named "slice" with SLICE clause
  In-Reply-To: <CA+i9DRE8Uh_rYTcbMvQBWCmjQNgb_4N=7V2vMPOZa1iQV7HOkA@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