Iterations are implemented by means of loops. Each loop body is always wrapped between the LOOP and END LOOP keywords and can be assigned a label. This can be useful for jumping out of an inner loop to an external loop.
The simplest form of loop is the unconditional loop, which is repeated infinitely until an explicit EXIT is invoked. EXIT forces the current loop to stop immediately and can be subject to a Boolean condition specified by a WHEN predicate. As an example, Listing 16 and Listing 17 show the same loop that prints out a message four times:
testdb=> DO $code$
DECLARE
counter int := 0;
BEGIN
LOOP
counter := counter + 1;
RAISE INFO 'This is the % time I say HELLO!', counter;
EXIT WHEN counter > 3;
END LOOP;
RAISE INFO 'Good bye';
END $code$;
INFO: This is the 1 time I say HELLO!
INFO: This is the 2 time I say...