Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1krkaf-0005WC-Ih for pgsql-general@arkaria.postgresql.org; Tue, 22 Dec 2020 16:32:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1krkae-00010Q-AU for pgsql-general@arkaria.postgresql.org; Tue, 22 Dec 2020 16:32:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1krkae-00010J-2Z for pgsql-general@lists.postgresql.org; Tue, 22 Dec 2020 16:32:48 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1krkaX-0008LI-Hg for pgsql-general@postgresql.org; Tue, 22 Dec 2020 16:32:47 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 0BMGWasl613705; Tue, 22 Dec 2020 11:32:36 -0500 From: Tom Lane To: "Joel Jacobson" cc: "Laurenz Albe" , "Philip Semanchuk" , pgsql-general Subject: Re: Avoid excessive inlining? In-reply-to: References: Comments: In-reply-to "Joel Jacobson" message dated "Tue, 22 Dec 2020 17:10:35 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <613703.1608654756.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 22 Dec 2020 11:32:36 -0500 Message-ID: <613704.1608654756@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Joel Jacobson" writes: > I think I was a bit unclear about my problem, and might have used the wr= ong terminology. > In my LATERAL query, there are calculations in a certain order. > For each step, "columns" are computed named e.g. "g", "c", "h", "i", etc= . > However, when looking at the query plan, these steps are gone, and inste= ad there is just one huge fully expanded expression, which doesn't look ve= ry efficient. Yeah, this isn't really about function inlining, it's about subquery flattening (which is similar in some ways, but not the same thing). Unfortunately, subquery flattening happens early enough in the planner that there's no chance of making any useful cost comparisons to decide whether to do it or not. So we just do it unconditionally. I'm not really sure that failing to do it would provide a better outcome in this situation anyway --- sure, you'd save a few scalar calculations, but the overhead of running additional plan nodes could outweigh that. The long and the short of it is that SQL isn't terribly well suited to execute a fundamentally stepwise, imperative algorithm like this one. Rather than hacking up cute tricks with LATERAL, you should just use a language that *is* well suited. That's why we provide PLs. FWIW, another trick for inserting optimization fences is WITH. So you could do something like WITH Q1(g,c) AS MATERIALIZED (SELECT year % 19, year / 100), Q2(h) AS MATERIALIZED (SELECT (c - c/4 - (8*c + 13)/25 + 19*g + 15) % 30 FROM Q1), ... SELECT make_date(year, easter_month, easter_day) FROM Q6; But I'd bet lunch that that won't be faster for this example, because there's a lot of overhead in CTEs. regards, tom lane