Sub topic 6
Running SQL Inside a Function
This chapter assumes you know SELECT, INSERT, UPDATE, and DELETE, and that you can create and call PostgreSQL functions. The examples assume a customers table with id, email, and active columns.
PL/pgSQL lets a function run SQL statements and use their results in procedural code. The main tools are:
| Tool | Purpose |
|---|---|
| `SELECT ... INTO` | Store query results in variables |
| `PERFORM` | Run a query when its result is not needed |
| `FOUND` | Check whether the previous statement found or changed rows |
| `GET DIAGNOSTICS` | Read details such as the affected row count |
Store a query result with SELECT INTO
In PL/pgSQL, SELECT ... INTO assigns columns from a query to variables. The target variables appear after INTO.
CREATE FUNCTION customer_email(p_customer_id integer)
RETURNS text
LANGUAGE plpgsql
STABLE
AS $$
DECLARE
v_email text;
BEGIN
SELECT c.email
INTO v_email
FROM customers AS c
WHERE c.id = p_customer_id;
RETURN v_email;
END;
$$;
SELECT customer_email(42);If no row matches, the variable receives NULL unless you use INTO STRICT. If several rows match, PL/pgSQL normally assigns the first row. Use STRICT when exactly one row is required. It raises an error for either zero rows or multiple rows.
Qualify column names with table aliases. A parameter or local variable can have the same name as a column, and qualification makes the intended source clear.
Run a query with PERFORM
Use PERFORM when you need a query's side effect or existence check but do not need its returned columns. PL/pgSQL does not allow an ordinary SELECT whose result is ignored.
CREATE FUNCTION customer_is_active(p_customer_id integer)
RETURNS boolean
LANGUAGE plpgsql
STABLE
AS $$
BEGIN
PERFORM 1
FROM customers AS c
WHERE c.id = p_customer_id
AND c.active;
RETURN FOUND;
END;
$$;PERFORM discards the result. The function returns true when at least one matching row exists and false otherwise.
Check FOUND
FOUND is a special Boolean variable. Statements such as SELECT INTO, PERFORM, UPDATE, DELETE, and INSERT set it according to whether they processed or returned a row.
Read it immediately. Any later statement that changes FOUND can replace its value.
CREATE FUNCTION deactivate_customer(p_customer_id integer)
RETURNS boolean
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE customers
SET active = false
WHERE id = p_customer_id;
RETURN FOUND;
END;
$$;Here, true means an existing row matched the UPDATE. It does not mean that the value changed from true to false; it only reports whether a row was affected.
Read row counts with GET DIAGNOSTICS
FOUND only gives a Boolean result. GET DIAGNOSTICS retrieves the exact number of rows affected by the most recent SQL command.
CREATE FUNCTION deactivate_customers()
RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
v_count integer;
BEGIN
UPDATE customers
SET active = false
WHERE active = true;
GET DIAGNOSTICS v_count = ROW_COUNT;
RETURN v_count;
END;
$$;ROW_COUNT must be read before another statement changes the diagnostics information.
Exercise
Which tool should a function use to store one customer's email, and which tool should it use to test whether any active customer exists?
Show answer
Use SELECT INTO to store the email. Use PERFORM followed immediately by FOUND for the existence test.