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 1gye5M-0001tA-UY for pgsql-hackers@arkaria.postgresql.org; Tue, 26 Feb 2019 14:51:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gye5K-0006aU-EF for pgsql-hackers@arkaria.postgresql.org; Tue, 26 Feb 2019 14:51:54 +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 1gye5K-0006Vi-37 for pgsql-hackers@lists.postgresql.org; Tue, 26 Feb 2019 14:51:54 +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 1gye5H-0006co-8u for pgsql-hackers@postgresql.org; Tue, 26 Feb 2019 14:51:52 +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 x1QEpcG5006697; Tue, 26 Feb 2019 09:51:40 -0500 From: Tom Lane To: Andrew Gierth cc: Andreas Karlsson , Tomas Vondra , Merlin Moncure , Alvaro Herrera , Peter Eisentraut , Michael Paquier , Robert Haas , Andres Freund , Thomas Munro , David Fetter , Pg Hackers Subject: Re: Early WIP/PoC for inlining CTEs In-reply-to: <87wolmg60q.fsf@news-spur.riddles.org.uk> References: <20190214160209.GA13250@alvherre.pgsql> <31353.1550351689@sss.pgh.pa.us> <62080f95-3934-4700-00ca-7c35630ab7a0@proxel.se> <87wolmg60q.fsf@news-spur.riddles.org.uk> Comments: In-reply-to Andrew Gierth message dated "Tue, 26 Feb 2019 11:41:51 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <6695.1551192698.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 26 Feb 2019 09:51:38 -0500 Message-ID: <6696.1551192698@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Andrew Gierth writes: > Here, uncommenting that NOT actually changes the result, from 22 rows to > 4 rows, because we end up generating multiple worktable scans and the > recursion logic is not set up to handle that. Ugh. > So what I think we need to do here is to forbid inlining if (a) the > refcount is greater than 1 and (b) the CTE in question contains, > recursively anywhere inside its rtable or the rtables of any of its > nested CTEs, a "self_reference" RTE. That's kind of "ugh" too: it sounds expensive, and doing it in a way that doesn't produce false positives would be even more complicated. Idle uncaffeinated speculation: is it practical to fix the restriction about multiple worktable scans? Also, I thought of a somewhat-related scenario that the code isn't accounting for: you can break the restrictions about single evaluation with nested WITHs, like with x as not materialized (with y as materialized (select random() r) sel= ect * from y) select * from x, x x1; In this particular example, we're saved from computing random() twice by the checks for volatile functions. But without that, y is inlined and computed twice, e.g. explain verbose with x as not materialized (with y as (select now() r) sel= ect * from y) select * from x, x x1; QUERY PLAN = ------------------------------------------------ Nested Loop (cost=3D0.00..0.06 rows=3D1 width=3D16) Output: (now()), (now()) -> Result (cost=3D0.00..0.01 rows=3D1 width=3D8) Output: now() -> Result (cost=3D0.00..0.01 rows=3D1 width=3D8) Output: now() (6 rows) As a user I think I'd find that surprising, and bad if y were expensive. Is it practical to inline the outer "x" level and still compute "y" only once? If not, I think we need to disallow inlining anything that contains a "with". regards, tom lane