In PostgreSQL, the COST clause is used inside CREATE FUNCTION, ALTER FUNCTION, CREATE OPERATOR and ALTER OPERATOR statements. It lets the author tell the query planner how expensive a call to the function (or operator) is relative to a simple operator such as an integer addition, which has a default cost of 1.0. The planner multiplies the cost by the estimated number of calls when comparing execution plans, so a higher COST discourages use of the function in performance-critical sections like WHERE filters or JOIN conditions. The value is a floating-point number measured in arbitrary cost units; it should grow with CPU time, memory usage, and possible I/O done by the function. PostgreSQL assigns defaults of 100 for functions written in languages that can access the database (plpgsql, SQL) and 1000 for functions marked as VOLATILE and written in C, but explicit COST lets you override these heuristics. Caveats:- COST affects planning only, not execution. A wrong estimate may cause poor plans.- Only superusers or the function owner can set or change COST.- The clause has no effect in other SQL dialects.
cost_value
(numeric) - Positive floating point number representing relative expense; higher means more expensive.CREATE FUNCTION, ALTER FUNCTION, PARALLEL, ROWS, VOLATILE, IMMUTABLE, EXPLAIN
PostgreSQL 7.4
COST tells the PostgreSQL planner how expensive a function or operator call is relative to a simple operation. The planner uses it when comparing execution plans.
No. PostgreSQL supplies default costs (100 or 1000) based on language and volatility. Explicit COST is optional but useful for fine-tuning.
Benchmark the function with EXPLAIN ANALYZE, then scale the cost so the planner's estimates align with real runtime. Start small and iterate.
Yes. An inaccurate COST may mislead the planner into choosing suboptimal plans, increasing total query time.