pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig Ringer <craig@2ndQuadrant.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: David Greco <David_Greco@harte-hanks.com>
Cc: Andrew Dunstan <andrew@dunslane.net>
Cc: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Re: SOLVED - RE: Poor performance using CTE
Date: Thu, 15 Nov 2012 09:17:28 +0800
Message-ID: <50A442A8.10803@2ndQuadrant.com> (raw)
In-Reply-To: <20462.1352910543@sss.pgh.pa.us>
References: <187F6C10D2931A4386EE8E58E13857F61291D897@BY2PRD0811MB415.namprd08.prod.outlook.com>
	<20462.1352910543@sss.pgh.pa.us>

On 11/15/2012 12:29 AM, Tom Lane wrote:
> David Greco <David_Greco@harte-hanks.com> writes:
>> Thanks, that did the trick. Though I'm still not clear as to why. 
> PG treats WITH as an optimization fence --- the WITH query will be
> executed pretty much as-is.  It may be that Oracle flattens the query
> somehow; though if you're using black-box functions in both cases,
> it's not obvious where the optimizer could get any purchase that way.
>

I was looking through the latest spec drafts I have access to and
couldn't find any reference to Pg's optimisation-fence-for-CTEs
behaviour being required by the standard, though I've repeatedly seen it
said that there is such a requirement.

Do you know where it's specified?

All I can see is that the optimised result must have the same effect as
the original. That'd mean that wCTEs and CTE terms that use VOLATILE
functions or functions with side-effects couldn't be optimised into
other queries. Simple CTEs could be, though, and there are times I've
really wished I could use a CTE but I've had to use a set-returning
subquery to get reasonable plans.

-- 
 Craig Ringer                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services





view thread (16+ messages)  latest in thread

Message-ID: <50A442A8.10803@2ndQuadrant.com>
Permalink:  ../50A442A8.10803@2ndQuadrant.com/
Also on:    postgresql.org/message-id/50A442A8.10803@2ndQuadrant.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: craig@2ndQuadrant.com, tgl@sss.pgh.pa.us, David_Greco@harte-hanks.com, andrew@dunslane.net
  Subject: Re: SOLVED - RE: Poor performance using CTE
  In-Reply-To: <50A442A8.10803@2ndQuadrant.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox