Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 2D900B5DCDF for ; Wed, 2 Nov 2011 14:13:17 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 72645-01-3 for ; Wed, 2 Nov 2011 17:13:10 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail.anarazel.de (mail.anarazel.de [217.115.131.40]) by mail.postgresql.org (Postfix) with ESMTP id 0D958B5DD29 for ; Wed, 2 Nov 2011 14:13:09 -0300 (ADT) Received: from intern.anarazel.de (p5B3D563E.dip.t-dialin.net [91.61.86.62]) (Authenticated sender: andres@anarazel.de) by mail.anarazel.de (Postfix) with ESMTPA id 0B202448006; Wed, 2 Nov 2011 18:13:08 +0100 (CET) From: Andres Freund To: pgsql-performance@postgresql.org Subject: Re: Guide to PG's capabilities for inlining, predicate hoisting, flattening, etc? Date: Wed, 2 Nov 2011 18:13:06 +0100 User-Agent: KMail/1.13.7 (Linux/3.0.0-2-amd64; KDE/4.6.5; x86_64; ; ) Cc: Robert Haas , Tom Lane , Jay Levitt References: <29918.1320244719@sss.pgh.pa.us> In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201111021813.06401.andres@anarazel.de> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.101 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.201 X-Spam-Level: X-Archive-Number: 201111/22 X-Sequence-Number: 45533 On Wednesday 02 Nov 2011 16:13:09 Robert Haas wrote: > On Wed, Nov 2, 2011 at 10:38 AM, Tom Lane wrote: > > Jay Levitt writes: > >> So you can see where I'm going. I know if I break everything into > >> elegant, composable functions, it'll continue to perform poorly. If I > >> write one big hairy, it'll perform great but it will be difficult to > >> maintain, and it will be inelegant and a kitten will die. My tools > >> are CTEs, subqueries, aliases, SQL functions, PL/pgSQL functions, and > >> views (and other tools?) What optimizations do each of those prevent? > > > > plpgsql functions are black boxes to the optimizer. If you can express > > your functions as single SQL commands, using SQL-language functions is > > usually a better bet than plpgsql. > > > > CTEs are also treated as optimization fences; this is not so much an > > optimizer limitation as to keep the semantics sane when the CTE contains > > a writable query. > > I wonder if we need to rethink, though. We've gotten a number of > reports of problems that were caused by single-use CTEs not being > equivalent - in terms of performance - to a non-CTE formulation of the > same idea. It seems necessary for CTEs to behave this way when the > subquery modifies data, and there are certainly situations where it > could be desirable otherwise, but I'm starting to think that we > shouldn't do it that way by default. Perhaps we could let people say > something like WITH x AS FENCE (...) when they want the fencing > behavior, and otherwise assume they don't (but give it to them anyway > if there's a data-modifying operation in there). +1. I avoid writing CTEs in many cases where they would be very useful just for that reasons. I don't even think some future inlining necessarily has to be restricted to one-use cases only... +1 for making fencing behaviour as well. Currently there is no real explicit method to specify this which is necessarily future proof (WITH, OFFSET 0)... Andres