Sub topic 14
COST, ROWS and parallel safety
Prerequisites and the planner's estimates
This chapter assumes that you can create SQL functions and understand basic query planning. You should know SELECT, function volatility, and the difference between a scalar result and a set of rows.
When PostgreSQL plans a query, it estimates the cost of each operation. It uses these estimates to choose between options such as calling a function once, using an index, or running work in parallel. Function declarations can provide three important hints:
COSTestimates how expensive one function call is.ROWSestimates how many rows a set-returning function produces.PARALLELdeclares whether PostgreSQL may run the function in a parallel worker.
These values guide the planner. They do not impose a runtime limit or guarantee a particular plan.
COST and ROWS
COST is expressed in PostgreSQL's internal cost units. For a scalar function, it estimates the cost of one invocation. For a set-returning function, it describes the function's execution cost, while ROWS estimates the number of rows returned.
The default COST is usually 100 for functions written in SQL or PL/pgSQL. The default ROWS value is 1,000 for set-returning functions. These defaults can be poor estimates for a small or very large function.
This function usually returns at most 100 rows, so its declaration gives the planner more useful information:
CREATE FUNCTION numbers_up_to(p_limit integer)
RETURNS SETOF integer
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
COST 1
ROWS 100
AS $$
SELECT n
FROM generate_series(1, p_limit) AS series(n);
$$;
SELECT * FROM numbers_up_to(5);A low COST does not make a function faster. It tells the planner that calling the function is relatively cheap. Set ROWS to an estimate that matches typical calls, not an extreme case.
Parallel safety
The PARALLEL clause has three values:
| Setting | Meaning |
|---|---|
| `PARALLEL SAFE` | The function can run in a parallel worker. |
| `PARALLEL RESTRICTED` | The function can run during a parallel query, but only in the leader process. |
| `PARALLEL UNSAFE` | The function prevents the query from using parallel execution. |
Functions are PARALLEL UNSAFE by default. Mark a function PARALLEL SAFE only when its operations are safe in workers and do not depend on worker-local state, temporary tables, changing settings, or unsafe side effects.
A function that only calculates a value from its arguments is often safe:
CREATE FUNCTION discounted_price(price numeric, discount numeric)
RETURNS numeric
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
COST 1
AS $$
SELECT price * (1 - discount);
$$;Do not claim that a function is safe merely because it is read-only. A function can read data whose behavior is unsuitable for parallel workers. Check the operations inside the function and its volatility before choosing the setting.
Choosing values
Use EXPLAIN to see how PostgreSQL uses estimates:
EXPLAIN
SELECT *
FROM numbers_up_to(1000);Adjust COST and ROWS only when you have observed the function's normal behavior. Test representative inputs with EXPLAIN ANALYZE, and compare the estimated rows with the actual rows.
Exercise
Which setting should you use for a function that returns about 20 rows and performs a small calculation without side effects?
Show answer
Use a set-returning declaration with a ROWS estimate near 20, a low COST, and PARALLEL SAFE if every operation is safe in a parallel worker.