Re: [HACKERS] CTE inlining - Mailing list pgsql-hackers

From Tom Lane
Subject Re: [HACKERS] CTE inlining
Date
Msg-id 24165.1493923954@sss.pgh.pa.us
Whole thread Raw
In response to Re: [HACKERS] CTE inlining  (Tomas Vondra <tomas.vondra@2ndquadrant.com>)
Responses Re: [HACKERS] CTE inlining  (Serge Rielau <serge@rielau.com>)
Re: [HACKERS] CTE inlining  (Craig Ringer <craig.ringer@2ndquadrant.com>)
List pgsql-hackers
Tomas Vondra <tomas.vondra@2ndquadrant.com> writes:
> On 5/4/17 8:03 PM, Joe Conway wrote:
>>> I haven't been able to follow this incredibly long thread, so please
>>> excuse me if way off base, but are we talking about that a CTE would be
>>> silently be rewritten as an inline expression potentially unless it is
>>> decorated with some new syntax?

> I agree with this, but there's a difference between "executed exactly 
> once" and "producing the same result as if executed exactly once".

> I may be misunderstanding what other people proposed in this thread, but 
> I think the plan was to only inline CTEs where we know it won't change 
> the results, etc. So e.g. CTEs with volatile functions would not get 
> inlined, which includes nextval() for example.

I haven't been keeping close tabs either, but surely we still have to have
the optimization fence in (at least) all these cases:

* CTE contains INSERT/UPDATE/DELETE
* CTE contains SELECT FOR UPDATE/SHARE (else the set of rows that get locked might change)
* CTE contains volatile functions

I'm willing to write off cases where, eg, a function should have been
marked volatile and was not.  That's user error and there are plenty
of hazards of that kind already.  But if the optimizer has reason
to know that discarding the fence might change any query side-effects,
it mustn't.
        regards, tom lane



pgsql-hackers by date:

Previous
From: Jordan Deitch
Date:
Subject: Re: [HACKERS] json_agg produces nonstandard json
Next
From: Serge Rielau
Date:
Subject: Re: [HACKERS] CTE inlining