Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gsZce-00048f-1h for pgsql-hackers@arkaria.postgresql.org; Sat, 09 Feb 2019 20:53:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gsZcc-0000FF-Hb for pgsql-hackers@arkaria.postgresql.org; Sat, 09 Feb 2019 20:53:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gsZcc-0000AP-0k for pgsql-hackers@lists.postgresql.org; Sat, 09 Feb 2019 20:53:10 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gsZcY-0007l6-SF for pgsql-hackers@postgresql.org; Sat, 09 Feb 2019 20:53:08 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x19Kqqxh001446; Sat, 9 Feb 2019 15:52:52 -0500 From: Tom Lane To: Andrew Gierth cc: Michael Paquier , Peter Eisentraut , Robert Haas , Andreas Karlsson , Andres Freund , Thomas Munro , David Fetter , Pg Hackers Subject: Re: Early WIP/PoC for inlining CTEs In-reply-to: <87d0o970th.fsf@news-spur.riddles.org.uk> References: <20190111190403.77ye7m5mxypxsf4l@alap3.anarazel.de> <00818be9-2f63-f584-efcd-d97ee0662905@proxel.se> <5715d93e-6ebc-be17-c2c3-56a873100a38@proxel.se> <29576.1548084476@sss.pgh.pa.us> <12408.1548707732@sss.pgh.pa.us> <32184.1548713132@sss.pgh.pa.us> <20190129045158.GD3121@paquier.xyz> <17787.1548791564@sss.pgh.pa.us> <4591.1549134061@sss.pgh.pa.us> <87d0o970th.fsf@news-spur.riddles.org.uk> Comments: In-reply-to Andrew Gierth message dated "Sun, 03 Feb 2019 08:14:44 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1444.1549745572.1@sss.pgh.pa.us> Date: Sat, 09 Feb 2019 15:52:52 -0500 Message-ID: <1445.1549745572@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Andrew Gierth writes: > "Tom" == Tom Lane writes: > Tom> After further reflection I really don't like Andrew's suggestion > Tom> that we not document the rule that multiply-referenced CTEs won't > Tom> be inlined by default. That would be giving up the principle that > Tom> WITH calculations are not done multiple times by default, and I > Tom> draw the line at that. It's an often-useful behavior as well as > Tom> one that's been documented from day one, so I do not accept the > Tom> argument that we might someday override it on the basis of nothing > Tom> but planner cost estimates. > The case that springs to mind is when a CTE with grouping is then joined > multiple times in the main query with different conditions. If the > planner is able to deduce (e.g. via ECs) that restrictions on grouped > columns can be pushed into the CTE, then inlining the CTE multiple times > might be a significant win. But if that isn't possible, then inlining > multiple times might be a significant loss. Sure, but this is exactly the sort of situation where we should offer a way for the user to force either decision to be made. I think it's very unlikely that we'll ever be in a position to make a realistic cost-based decision for that. Actually planning it out both ways would be horrendously expensive (and probably none too reliable anyway, given how shaky ndistinct estimates tend to be); and we certainly don't have enough info to make a smart choice without doing that. regards, tom lane