Re: functions: VOLATILE performs better than STABLE

2018-03-24 Thread Laurenz Albe
Peter wrote:
> Given an arbitrary function fn(x) returning numeric.
> 
> Question: how often is the function executed?
> [...]
> C.
> select v.v,v.v from (select fn('const') as v) as v;
> 
> Answer:
> Once if declared VOLATILE.
> Twice if declared STABLE.
> 
> Now this IS a surprize. It is clear that the system is not allowed to
> execute the function twice when declared VOLATILE. It IS ALLOWED to
> execute it twice when STABLE - but to what point, except prolonging
> execution time?
> 
> Over all, VOLATILE performs better than STABLE.

The reason is that the subquery with the VOLATILE function can be
flattened; see the EXPLAIN (VERBOSE) output.

There is not guarantee that less volatility means better performance.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com



Re: Should from_collapse be switched off? (queries 10 times faster)

2018-03-24 Thread Laurenz Albe
Peter wrote:
> On Fri, Mar 23, 2018 at 10:14:19AM -0400, Tom Lane wrote:
> 
> ! It's conceivable that the OP's problem is actually planning time
> ! (if the query joins sufficiently many tables) and that restricting
> ! the cost of the join plan search is really what he needs to do.
> 
> Negative. Plnning time 10 to 27 ms. Execution time 600 to 6300 ms.
> 
> ! Lacking any further information about the problem, we can't say.
> ! We can, however, point to
> ! https://wiki.postgresql.org/wiki/Slow_Query_Questions
> ! concerning how to ask this type of question effectively.
> 
> I strongly hope the data that I sent as followup will now 
> suffice Your expectations.

Your reported execution times don't match the time reported in the
EXPLAIN output...

The cause of the long execution time is clear:

The row count of the join between "places" (WHERE platz = 'WAEHR'),
"wpnames" and "places AS places_1" is underestimated by a factor of 10
(1 row instead of 10).

The nested loop join that is chosen as a consequence is now executed
10 times instead of the estimated 1 time, which is where almost all the
execution time is spent.

The question how to fix that is more complicated, and I cannot solve
it off-hand with a complicated query like that.

Setting "enable_nestloop = off" is as coarse as forcing "from_collapse = 1"
and will negatively impact other queries - if it helps at all.

You'll probably have to rewrite the query.
Sorry that I cannot be of more help.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com