Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TavLL-0003CH-V2 for pgsql-performance@postgresql.org; Tue, 20 Nov 2012 21:26:56 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TavLK-0003E2-SI for pgsql-performance@postgresql.org; Tue, 20 Nov 2012 21:26:55 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id qAKLQnHN015382; Tue, 20 Nov 2012 16:26:49 -0500 (EST) From: Tom Lane To: Jon Nelson cc: Merlin Moncure , Peter Geoghegan , Andrew Dunstan , Craig Ringer , David Greco , "pgsql-performance@postgresql.org" Subject: Re: SOLVED - RE: Poor performance using CTE In-reply-to: References: <187F6C10D2931A4386EE8E58E13857F61291D897@BY2PRD0811MB415.namprd08.prod.outlook.com> <20462.1352910543@sss.pgh.pa.us> <50A442A8.10803@2ndQuadrant.com> <50A4497D.80902@dunslane.net> Comments: In-reply-to Jon Nelson message dated "Tue, 20 Nov 2012 13:53:30 -0600" Date: Tue, 20 Nov 2012 16:26:49 -0500 Message-ID: <15381.1353446809@sss.pgh.pa.us> X-Pg-Spam-Score: -2.3 (--) X-Archive-Number: 201211/214 X-Sequence-Number: 48580 Jon Nelson writes: > ... Perhaps even including a > small blurb about what an optimization barrier even means (my > understanding is that it merely forces materialization of that part of > the query). FWIW, it has nothing to do with materialization; it means that we don't push conditions down into that subquery, nor pull subexpressions up out of it, nor rearrange join order across the subquery boundary. In short the subquery is planned separately from the outer query. But it could then be run by the executor in the usual tuple-at-a-time fashion, without materializing the whole subquery result. It is true that CTEScan nodes materialize the subquery output (ie copy it into a tuplestore), but that's to support multiple CTEScans reading the same CTE. One of the optimizations we *should* put in place sometime is skipping the tuplestore if there's only one CTEScan on the CTE. regards, tom lane