Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TYo5T-0008Nr-B8 for pgsql-performance@postgresql.org; Thu, 15 Nov 2012 01:17:47 +0000 Received: from outmail148094.authsmtp.co.uk ([62.13.148.94]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TYo5R-0008HH-1m for pgsql-performance@postgresql.org; Thu, 15 Nov 2012 01:17:46 +0000 Received: from mail-c232.authsmtp.com (mail-c232.authsmtp.com [62.13.128.232]) by punt7.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id qAF1HfTV067680; Thu, 15 Nov 2012 01:17:41 GMT Received: from ayaki.localdomain (124-148-90-153.dyn.iinet.net.au [124.148.90.153]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/) with ESMTP id qAF1HUir056865 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Thu, 15 Nov 2012 01:17:33 GMT Message-ID: <50A442A8.10803@2ndQuadrant.com> Date: Thu, 15 Nov 2012 09:17:28 +0800 From: Craig Ringer Organization: 2nd Quadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121016 Thunderbird/16.0.1 MIME-Version: 1.0 To: Tom Lane CC: David Greco , Andrew Dunstan , "pgsql-performance@postgresql.org" Subject: Re: SOLVED - RE: Poor performance using CTE References: <187F6C10D2931A4386EE8E58E13857F61291D897@BY2PRD0811MB415.namprd08.prod.outlook.com> <20462.1352910543@sss.pgh.pa.us> In-Reply-To: <20462.1352910543@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Server-Quench: 3cc9f922-2ec2-11e2-b10b-0025903375e2 X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdxZQATClZeVg0b BQteCiN5VAwpPBRK HVkIKg5MJUcNSQVJ NksachtFaQBbY1ZH HGQLWlZEUFx7WGR/ aA8fbAdZfEtKVxtj UFZIR1FNFgBqBAMA SF9vJBgKElwEeHt2 bUBnEHlSVEE0dhUu SxhdF28bZzIzan0e URVaalFVcFFXfh9H b1l/V3AKZDAFKBg1 TUcIGBQPEBkXbWxv b1NFHmo/e2gsJBsJ DygFGi9nBUAfSSIp KhUgIVNUAV0QNEp6 GmcMMQAA X-Authentic-SMTP: 61633235383639.1019:706 X-AuthFastPath: 0 (Was 255) X-AuthSMTP-Origin: 124.148.90.153/2525 X-AuthVirus-Status: No virus detected - but ensure you scan with your own anti-virus system. X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201211/174 X-Sequence-Number: 48540 On 11/15/2012 12:29 AM, Tom Lane wrote: > David Greco 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