
Throwing away a query result set
Any SQL statement that returns values must put its result into one or more variables by using INTO, for example. This means that invoking other functions that return values, or executing statements that return a result set (such as INSERT, UPDATE or DELETE with RETURNING or SELECT) is not possible until the returned values are stored in variables. This means that even a simple block code such as Listing 35 will fail because the value returned by the now() function is implicitly discarded:
testdb=> DO $code$ BEGIN
SELECT now();
END $code$;
ERROR: query has no destination for result data
If we have the need to execute a query and throw away its results, the PERFORM statement must be used. This statement can be thought of an alias for SELECT. It is in fact possible to write the query text in the same way, including variable interpolation, just by substituting the SELECT keyword with PERFORM. As an example, Listing 36 shows a couple of queries that throw away the result without leading to the abortion of the execution:
testdb=> DO $code$
DECLARE
file_type text := 'png';
BEGIN
-- ok
PERFORM now();
PERFORM -- SELECT
f_size, f_name
FROM files
WHERE f_type = file_type -- interpolation
ORDER BY f_size DESC;
RAISE INFO 'I've survived!';
END $code$;
INFO: I've survived!
If the query that must throw away results is a CTE, the whole query must be passed in parentheses as argument to PERFORM, without substituting the SELECT keyword in the top-level statement, as shown in Listing 37. However, in this case, the CTE must return a single row from the top-level statement or the execution will be aborted:
testdb=> DO $code$
DECLARE
file_type text := 'png';
BEGIN
PERFORM ( WITH biggest_file_by_type AS (
SELECT f_name FROM files
WHERE f_type = file_type
ORDER BY f_size DESC
LIMIT 1 ) SELECT f_name
FROM biggest_file_by_type );
RAISE INFO 'I've survived!';
END $code$;
INFO: I've survived!