When the top-level code block begins, PL/pgSQL declares a global variable named FOUND, which allows for a quick check of the status of the last SQL statement executed. The FOUND variable is of Boolean type and is set to true if the last statement succeeds, or, in other words, if at least one row was processed by the statement. As an example, Listing 38 shows a very simple check to see if the files table is empty or not. The table is read via PERFORM and, if at least one row is found, the FOUND Boolean variable is implicitly set to true:
testdb=> DO $code$
BEGIN
PERFORM pk FROM files;
IF FOUND THEN
RAISE DEBUG 'The files tables contain some data';
END IF;
END $code$;
DEBUG: The files tables contain some data
Listing 38: A simple use of FOUND
The FOUND variable is defined with a false value and is turned to true only when...