Sub topic 3
Parameters, Defaults and OUT Columns
Assume you already know basic SELECT statements, PostgreSQL data types, schemas, and how to run SQL in psql or another PostgreSQL client. This chapter focuses on how values enter a SQL function and how results leave it.
Naming function parameters
A function parameter is a named value supplied by the caller. PostgreSQL also gives every parameter a positional name such as $1, $2, and so on.
Use descriptive parameter names in the function definition. Prefixing them with p_ helps distinguish parameters from table columns.
CREATE FUNCTION add_tax(
p_price numeric,
p_rate numeric DEFAULT 0.20
)
RETURNS numeric
LANGUAGE sql
IMMUTABLE
AS $$
SELECT p_price * (1 + p_rate);
$$;
SELECT add_tax(100);
SELECT add_tax(100, 0.05);The first call uses the default rate of 0.20. The second call supplies 0.05, so it calculates a five-percent tax.
You can also use named notation at the call site:
SELECT add_tax(p_price => 100, p_rate => 0.05);
SELECT add_tax(p_rate => 0.05, p_price => 100);Named notation makes calls easier to read and allows arguments to appear in a different order. Positional arguments must come before named arguments when you mix the two styles.
Defaults and positional references
Input parameters with defaults can be omitted when they are at the end of the input parameter list. PostgreSQL uses the default only when the caller omits the argument. Passing NULL is different: the function receives NULL.
Inside a SQL function, positional references are useful when parameter names would be awkward or when writing a generic function.
CREATE FUNCTION make_pair(anyelement, anyelement)
RETURNS anyarray
LANGUAGE sql
IMMUTABLE
AS $$
SELECT ARRAY[$1, $2];
$$;
SELECT make_pair(10, 20);
SELECT make_pair('left'::text, 'right'::text);Here $1 and $2 refer to the first and second input parameters. The polymorphic type anyelement lets PostgreSQL infer a compatible type from the call.
Returning multiple values with OUT parameters
An OUT parameter is an output column declared in the function signature. The caller does not provide an OUT argument. PostgreSQL builds a composite result from the output columns.
CREATE FUNCTION split_name(
p_full_name text,
OUT first_name text,
OUT last_name text
)
LANGUAGE sql
IMMUTABLE
AS $$
SELECT
split_part(p_full_name, ' ', 1),
split_part(p_full_name, ' ', 2);
$$;
SELECT * FROM split_name('Ada Lovelace');The result has two columns, first_name and last_name. When a function has multiple OUT parameters, its effective return type is a record with those named columns. You normally omit a separate RETURNS clause because the OUT declarations define the result.
INOUT parameters combine both roles. The caller supplies a value, and the function returns the updated value. Use them sparingly because a separate return type or a RETURNS TABLE declaration is often clearer.
| Parameter form | Caller supplies it | Function returns it |
|---|---|---|
| `IN` | Yes | No |
| `OUT` | No | Yes |
| `INOUT` | Yes | Yes |
Common errors
- Giving a default to a parameter and then placing a required input parameter after it.
- Calling a named parameter with a misspelled name.
- Adding
OUTparameters to the call. - Confusing an omitted argument with an explicit
NULL. - Using an unqualified parameter name that is also a column name. Prefix parameters, or qualify columns with a table alias.
Exercise
What does SELECT add_tax(100, NULL); pass to the function, and does PostgreSQL use the default rate?
Show answer
It passes NULL as p_rate. PostgreSQL does not use the default because the argument was supplied explicitly.