CONTINUE error, even though inside a loop

Started by Michael Fuhrover 20 years ago2 messages
#1Michael Fuhr
mike@fuhr.org

I'm getting "CONTINUE cannot be used outside a loop" errors even
though it's inside a loop. The error appears to be happening when
CONTINUE passes control to the beginning of the loop but there's
no more iterating to be done. I'd expect the loop to end at this
point instead of getting an error. Or did I miss something in the
discussion?

CREATE FUNCTION foo(x integer) RETURNS integer AS $$
DECLARE
i integer;
BEGIN
FOR i IN 1 .. x LOOP
RAISE INFO 'before CONTINUE: i = %', i;
CONTINUE WHEN i <= 2;
RAISE INFO 'after CONTINUE: i = %', i;
END LOOP;

RETURN x;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;

SELECT foo(1);
INFO: before CONTINUE: i = 1
ERROR: CONTINUE cannot be used outside a loop
CONTEXT: PL/pgSQL function "foo"

SELECT foo(2);
INFO: before CONTINUE: i = 1
INFO: before CONTINUE: i = 2
ERROR: CONTINUE cannot be used outside a loop
CONTEXT: PL/pgSQL function "foo"

SELECT foo(3);
INFO: before CONTINUE: i = 1
INFO: before CONTINUE: i = 2
INFO: before CONTINUE: i = 3
INFO: after CONTINUE: i = 3
foo
-----
3
(1 row)

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

#2Neil Conway
neilc@samurai.com
In reply to: Michael Fuhr (#1)
Re: CONTINUE error, even though inside a loop

Michael Fuhr wrote:

I'm getting "CONTINUE cannot be used outside a loop" errors even
though it's inside a loop. The error appears to be happening when
CONTINUE passes control to the beginning of the loop but there's
no more iterating to be done.

Woops, sorry for missing this. This should be fixed in HEAD; thanks for
the report.

-Neil