BUG: PL/pgSQL FOREACH misparses variable named "slice" with SLICE clause

From: Leendert Gravendeel <leenderthenk(at)gmail(dot)com>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG: PL/pgSQL FOREACH misparses variable named "slice" with SLICE clause
Date: 2026-04-15 18:56:50
Message-ID: CA+i9DRE8Uh_rYTcbMvQBWCmjQNgb_4N=7V2vMPOZa1iQV7HOkA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Masahiko Sawada 2026-04-15 20:50:01 Re: TRAP: failed Assert("offsets[i] > offsets[i - 1]"), File: "tidstore.c"
Previous Message Daniel Schreiber 2026-04-15 15:55:01 PostgreSQL 17: Bug in libpq when libpq is dlopened/closed multiple times