Sub topic 12
How the Planner Inlines SQL Functions
Assume you know basic SELECT statements, PostgreSQL data types, and how to read a simple EXPLAIN plan. This chapter focuses on scalar SQL functions. Set-returning functions have additional inlining rules.
What inlining means
Inlining means that PostgreSQL replaces a function call with the function's SQL expression before it builds the query plan. The planner can then optimize the expanded expression together with the surrounding query.
For example, this function adds tax:
CREATE TEMP TABLE products (
id integer,
price numeric
);
INSERT INTO products (id, price)
VALUES (1, 80), (2, 120), (3, 200);
CREATE FUNCTION price_with_tax(price numeric, rate numeric)
RETURNS numeric
LANGUAGE sql
IMMUTABLE
AS $$
SELECT price * (1 + rate);
$$;
EXPLAIN (COSTS OFF)
SELECT id
FROM products
WHERE price_with_tax(price, 0.20) > 100;If PostgreSQL inlines the function, the filter can become equivalent to:
(price * 1.20) > 100The plan will usually show the expression in the scan filter rather than a separate function operation. The exact formatting depends on the PostgreSQL version and plan choices.
Inlining can expose constants, simplify expressions, and allow predicates to move through joins or other query operations. It does not guarantee an index scan. An expression applied to a column may still require an expression index or a different query shape.
When PostgreSQL can inline a function
A scalar SQL function is a good candidate when it has these properties:
- Its body contains one simple
SELECTorVALUESstatement. - It returns one scalar value rather than a set of rows.
- It is declared
IMMUTABLEorSTABLE, notVOLATILE. - It is not
SECURITY DEFINER. - It does not require a special
SETconfiguration. - Its arguments and result types allow the planner to substitute the expression safely.
IMMUTABLE means the result depends only on the arguments. STABLE allows the function to read database state that remains consistent during one statement. These declarations describe behavior; they are not performance hints. Declaring a function IMMUTABLE when it reads changing data can produce incorrect results.
Why a function might not inline
A multi-statement SQL function, a function with procedural control flow, or a function that performs data modification generally cannot be treated as one replaceable expression. A VOLATILE function also cannot be freely moved or evaluated fewer times because doing so could change how often its side effects occur.
A plan containing Function Scan often indicates that PostgreSQL is executing a set-returning function as a separate plan node. For scalar calls, inspect the filter or output expression instead of relying on one plan node name. Always use EXPLAIN on the actual query and PostgreSQL version you run.
SQL functions and PL/pgSQL
PL/pgSQL is useful when you need variables, loops, exception handling, or several conditional steps. A simple SQL function is often easier for PostgreSQL to inline because its body directly describes a relational expression.
Choose SQL for a small expression or query that can stand alone. Choose PL/pgSQL when the procedure needs control flow. Do not rewrite a clear function only to chase inlining. Measure with EXPLAIN (ANALYZE, BUFFERS) when performance matters.
Exercise
Why might changing a simple SQL function from IMMUTABLE to VOLATILE prevent useful planner optimization?
Show answer
VOLATILE tells PostgreSQL that the result or side effects may change on each call, so the planner must avoid substitutions, reordering, or constant folding that could change call behavior.