Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TaxKV-0005Km-8U for pgsql-performance@postgresql.org; Tue, 20 Nov 2012 23:34:11 +0000 Received: from outmail148108.authsmtp.net ([62.13.148.108]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TaxKS-00052j-NI for pgsql-performance@postgresql.org; Tue, 20 Nov 2012 23:34:10 +0000 Received: from mail-c232.authsmtp.com (mail-c232.authsmtp.com [62.13.128.232]) by punt5.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id qAKNY3xE034572; Tue, 20 Nov 2012 23:34:03 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 qAKNXqoT042809 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Tue, 20 Nov 2012 23:33:56 GMT Message-ID: <50AC135F.8030508@2ndQuadrant.com> Date: Wed, 21 Nov 2012 07:33:51 +0800 From: Craig Ringer Organization: 2nd Quadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121029 Thunderbird/16.0.2 MIME-Version: 1.0 To: Jon Nelson CC: Merlin Moncure , Peter Geoghegan , Andrew Dunstan , Tom Lane , David Greco , "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> <50A442A8.10803@2ndQuadrant.com> <50A4497D.80902@dunslane.net> In-Reply-To: X-Enigmail-Version: 1.4.5 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Server-Quench: c1d22725-336a-11e2-b10b-0025903375e2 X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdxZQATClZeVg0b BQteCiN5VAwpPBRK HVkIKg5MJUcNSQVJ NksacRtFagVbY1ZH HGQLWlZEUl57WmB/ bgIfbAdZfEtKVxtj UFZIR1FNFgBqBAMA SF9vKWcaNloieHpy bUZrEHJaWEx6OxV8 FkxWEm8DeGRjbjEC UUENdR4GcQMYYx9F aFV2VHAOZmBSZDQC El17BBcVAxhzYGEd eCxfZWEqbHwlOQQZ clg6GjI1VVUIXSs0 NRkvLFURVFsKM0M9 eXAddDoA 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/215 X-Sequence-Number: 48581 On 11/21/2012 03:53 AM, Jon Nelson wrote: > My perspective on this is that CTEs *should* be just like creating a > temporary table and then joining to it, but without the > materialization costs. In that respect, they seem like they should be > like nifty VIEWs. If I wanted the behavior of materialization and then > join, I'd do that explicitly with temporary tables, but using CTEs as > an explicit optimization barrier feels like the explaining away > surprising behavior. I agree, especially since that barrier isn't specified as standard, so we're using a standard feature with a subtle quirk as a database-specific optimisation trick. A hint, as it were, like OFFSET 0. *(Dons asbestos underwear an dives for cover)* My big problem with the status quo is that it breaks queries from other databases, like MS SQL server, where CTEs are optimised. I see this periodically on Stack Overflow, with people asking variants of "Why does PostgreSQL take 10,000 times longer to execute this query"? (not a literal quote). I really want to see this formalized and made explicit with `WITH tablename AS MATERIALIZE (SELECT)` or similar. Right now I often can't use CTEs to clean up hard-to-read queries because of the optimisation barrier, so I have to create a temporary view, temporary table, or use nested subqueries in FROM instead. Ugly. -- Craig Ringer http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services