Sub topic 7
Declaring Volatility Correctly
This chapter assumes that you know basic SQL statements, PostgreSQL data types, and how to run commands in a PostgreSQL client. You will learn how a function’s volatility declaration describes when its result may change.
Volatility is a promise
A function’s volatility category tells PostgreSQL how its result behaves. PostgreSQL uses this information when planning and executing queries. The declaration must match the function’s actual behavior.
PostgreSQL has three categories:
| Category | Result may change during one statement? | Typical use |
|---|---|---|
| `IMMUTABLE` | No | Calculations based only on arguments |
| `STABLE` | No, but it may change between statements | Reading tables or statement-time settings |
| `VOLATILE` | Yes | Random values, current time, or data changes |
A function is VOLATILE by default. Choose a less volatile category only when you can keep its promise.
`IMMUTABLE`: the same inputs always produce the same result
An immutable function depends only on its arguments and does not inspect database state, system time, locale settings, or configuration values. PostgreSQL may calculate calls with constant arguments while it plans the query.
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);Do not mark a function immutable merely because it usually returns the same value. A function that reads a table, calls random(), or depends on the current time is not immutable.
Immutable functions can be used in expression indexes because their results remain valid after the index is built.
`STABLE`: consistent within one statement
A stable function can read database data, but it must return the same result for the same arguments during one statement. It cannot modify the database. PostgreSQL may call it once and reuse the result during that statement.
Functions such as current_date and statement_timestamp() have statement-level behavior. A lookup function that reads a table can also be stable if the statement cannot observe changes to that table during its execution.
`VOLATILE`: may change or cause side effects
A volatile function can return a different result each time it runs. It can also modify database state. random() and clock_timestamp() are examples.
CREATE FUNCTION random_discount()
RETURNS numeric
LANGUAGE sql
VOLATILE
AS $$
SELECT random() * 0.25;
$$;
SELECT random_discount(), random_discount();A data-changing function should normally remain volatile. Marking it stable or immutable could let the planner remove calls or reuse results, which would skip required side effects.
How the planner uses volatility
When in doubt, use VOLATILE. Then choose STABLE or IMMUTABLE after checking every value and operation the function uses.
Exercise
What volatility category should a SQL function use if it calculates a result only from two numeric arguments and never reads or changes database state?
Show answer
IMMUTABLE, because the same arguments always produce the same result.