Sub topic 1
Your first SQL function
A SQL function is a named database object that accepts input values, runs one or more SQL statements, and returns a result. PostgreSQL stores the function definition, so you can call it from queries without repeating its SQL.
Prerequisites
You should be able to run SELECT statements and understand basic PostgreSQL types such as numeric and text. You should also know how schemas and database roles work. This chapter assumes PostgreSQL 14 or later for the BEGIN ATOMIC example.
The basic structure
Use CREATE FUNCTION to define a function. A simple SQL function has these parts:
- A name and parameter list.
- A return type after
RETURNS. - The language, usually
LANGUAGE sql. - A body containing SQL statements.
This function calculates a price after tax:
CREATE FUNCTION add_tax(price numeric, rate numeric DEFAULT 0.20)
RETURNS numeric
LANGUAGE sql
IMMUTABLE
AS $$
SELECT price * (1 + rate);
$$;
SELECT add_tax(100);
SELECT add_tax(100, 0.05);The first call uses the default rate of 0.20, so it returns 120.00. The second call supplies 0.05, so it returns 105.00.
numeric is used for both parameters and the result because it supports exact decimal arithmetic. The parameter names, price and rate, can be used inside the SQL body. DEFAULT supplies a value when the caller omits that argument.
IMMUTABLE tells PostgreSQL that the result depends only on the input values and does not change because of database contents, time, or other session state. This declaration helps the query planner optimize calls. Use it only when the function really has that behavior.
The $$ markers are dollar quotes. They enclose the function body without requiring single quotes around every string inside it. You can use a tag, such as $function$, instead when nested dollar quotes would make the body unclear.
Choosing the body form
For a one-statement function, a dollar-quoted body is concise. PostgreSQL also supports an SQL-standard body with BEGIN ATOMIC:
CREATE FUNCTION full_name(first_name text, last_name text)
RETURNS text
LANGUAGE sql
IMMUTABLE STRICT
BEGIN ATOMIC
SELECT first_name || ' ' || last_name;
END;The || operator concatenates text. STRICT means PostgreSQL returns NULL immediately if either argument is NULL; it does not execute the body.
BEGIN ATOMIC is parsed when the function is created. PostgreSQL can therefore detect syntax and some object dependencies at definition time. Dollar-quoted bodies are parsed when the function runs, which can be more convenient for some generated or complex definitions.
Calling and inspecting functions
Call a scalar function anywhere an expression is allowed, including a SELECT list, a filter, or an ORDER BY expression:
SELECT add_tax(total, 0.08)
FROM invoices
WHERE status = 'open';PostgreSQL identifies a function by its name and input argument types. The return type is not part of that identity, so two functions cannot have the same name and input types but different return types.
Exercise
What does SELECT add_tax(50, 0.10) return, and which function declaration feature lets you omit the rate in a different call?
Show answer
It returns 55. The rate parameter has a DEFAULT value, so SELECT add_tax(50) uses 0.20.