Sub topic 5
NULL Inputs and STRICT
Assumes you can write basic CREATE FUNCTION statements and understand that SQL NULL means an unknown or missing value. This chapter focuses on how PostgreSQL passes NULL into SQL functions and when STRICT can prevent the function body from running.
`NULL` is an input value with special behavior
NULL is not the same as an empty string, zero, or false. It represents an unknown value. Most operations involving NULL produce NULL:
SELECT 10 + NULL; -- NULL
SELECT 'Ada' || NULL; -- NULL
SELECT NULL = NULL; -- NULL, not trueSQL uses three-valued logic: a condition can be true, false, or unknown. Use IS NULL and IS NOT NULL to test for NULL.
A function normally runs even when one of its arguments is NULL. This is the default behavior, called CALLED ON NULL INPUT. The function is responsible for deciding what NULL means.
CREATE OR REPLACE FUNCTION display_name(p_name text)
RETURNS text
LANGUAGE sql
CALLED ON NULL INPUT
AS $$
SELECT coalesce(p_name, 'Anonymous');
$$;
SELECT display_name('Ada'); -- Anonymous? No, Ada
SELECT display_name(NULL); -- Anonymouscoalesce returns the first value that is not NULL. The function body runs for both calls.
Skipping the body with `STRICT`
A function declared STRICT returns NULL immediately if any input argument is NULL. PostgreSQL does not run the function body.
STRICT is an abbreviation for RETURNS NULL ON NULL INPUT.
CREATE OR REPLACE FUNCTION add_tax_strict(
p_price numeric,
p_rate numeric
)
RETURNS numeric
LANGUAGE sql
IMMUTABLE
STRICT
AS $$
SELECT p_price * (1 + p_rate);
$$;
SELECT add_tax_strict(100, 0.20); -- 120.00
SELECT add_tax_strict(NULL, 0.20); -- NULL
SELECT add_tax_strict(100, NULL); -- NULLThe third call does not evaluate the multiplication. PostgreSQL knows that a NULL input must produce a NULL result.
Use STRICT when NULL makes the result necessarily unknown and the function has no useful fallback. Do not use it when the function must convert, replace, or interpret missing values.
| Declaration | Body runs with a `NULL` input? | Typical use |
|---|---|---|
| `CALLED ON NULL INPUT` | Yes | Apply a fallback such as `coalesce` |
| `STRICT` | No | Propagate `NULL` automatically |
| `RETURNS NULL ON NULL INPUT` | No | Explicit name for `STRICT` behavior |
Defaults and explicit `NULL`
A default value is used only when the caller omits an argument. Passing NULL explicitly does not use the default.
CREATE OR REPLACE FUNCTION add_tax_default(
p_price numeric,
p_rate numeric DEFAULT 0.20
)
RETURNS numeric
LANGUAGE sql
IMMUTABLE
STRICT
AS $$
SELECT p_price * (1 + p_rate);
$$;
SELECT add_tax_default(100); -- 120.00
SELECT add_tax_default(100, NULL); -- NULLThe first call supplies 0.20 through the default. The second call supplies NULL, so STRICT returns NULL.
Choose the behavior from the function's meaning. A calculation such as tax multiplication usually benefits from STRICT. A formatting function that substitutes "Anonymous" should use the default behavior and handle NULL inside its body.
Exercise
What do these calls return: add_tax_default(100), add_tax_default(100, NULL), and add_tax_default(NULL, 0.20)?
Show answer
They return 120.00, NULL, and NULL. The omitted rate uses its default, while either explicit NULL input activates STRICT.