I am trying to define custom function and I wanted to find how can I calculate estimated cost of that function
https://www.postgresql.org/docs/current/sql-createfunction.html
I tried giving different values of cost function but unable to find to find how to estimate that cost.
2
Answers
The formula postgresql uses for calculate the explain cost plus some examples of explain below,is necessary to sum everything from the table (indexes , foreign keys ,sequence):
references : https://postgrespro.com/blog/pgsql/5969403
You can use EXPLAIN to see the cost of CPU from each query,notice that this value is static and based on the objects used.
If two functions with COST 0.0001 AND 10000 get executed on the same time as the predicate of a SELECT statement the query planner will execute first the function of cost 0.0001 and only later the 10000 cost condition as you can see in this example below.
If I cared enough to bother, I would do it experimentally.
For example, if your function takes double precision, you could compare:
to
And then find the cost setting that makes the ratio of the cost estimates about match the ratio of the actual times.
You could try to subtract the baseline costs of the generate_series and the cast, but if the added time of your function is so small that it warrants such precision, then it is probably small enough to just make the cost 1 and not worry about it.