Skip to content

EXIT WHEN from a nested FOR .. IN loop may produce an infinite loop #148989

Open
@lukaseder

Description

@lukaseder

Describe the problem

EXIT WHEN from a nested FOR .. IN loop may produce an infinite loop.

To Reproduce

When playing around with potential workarounds for #148988, I stumbled upon this:

create or replace procedure "proc_ForIndexedLoopNested"()
language plpgsql
as
$$
begin
  for i in 0 .. 1 loop
    for j in 1 .. 5 loop
      raise notice '%', (("i" * 5) + "j");
      exit when j = 4;
    end loop;
  end loop;
end;
$$

call "proc_ForIndexedLoopNested"();

This just loops forever, printing weird stuff:

1
2
3
4
26
27
28
29
26
27
28
29
26
27
28
29
26
27
28

It may be the same issue as #148988, I can't tell for sure. In case it isn't, I just wanted to report this separately, because it's much worse than #148988, which doesn't produce infinite loops.

Expected behavior

It should print:

1
2
3
4
6
7
8
9

Environment:

  • CockroachDB version: CockroachDB CCL v25.2.1 (x86_64-pc-linux-gnu, built 2025/06/02 23:17:23, go1.23.7 X:nocoverageredesign)
  • Server OS: Linux on Docker on Windows
  • Client app: JDBC

Jira issue: CRDB-51915

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-sql-plpgsqlPL/pgSQL languageC-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.O-communityOriginated from the communityT-sql-queriesSQL Queries Team

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions