From: Tom Lane <tgl@sss.pgh.pa.us>
To: Joel Jacobson <joel@compiler.org>
Cc: Laurenz Albe <laurenz.albe@cybertec.at>
Cc: Philip Semanchuk <philip@americanefficient.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Avoid excessive inlining?
Date: Tue, 22 Dec 2020 11:32:36 -0500
Message-ID: <613704.1608654756@sss.pgh.pa.us> (raw)
In-Reply-To: <b86f2afd-84a7-47a8-8d82-e12d0aa50599@www.fastmail.com>
References: <e70cde40-a7ef-4894-9f1d-453ba13a0ced@www.fastmail.com>
<F3C88129-A382-48E9-AB24-BECDADB8DBB5@americanefficient.com>
<bfe8c0f43c5211c71645071c6190dae259f08236.camel@cybertec.at>
<b86f2afd-84a7-47a8-8d82-e12d0aa50599@www.fastmail.com>
"Joel Jacobson" <joel@compiler.org> writes:
> I think I was a bit unclear about my problem, and might have used the wrong 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 instead there is just one huge fully expanded expression, which doesn't look very 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, joel@compiler.org, laurenz.albe@cybertec.at, philip@americanefficient.com
Subject: Re: Avoid excessive inlining?
In-Reply-To: <613704.1608654756@sss.pgh.pa.us>
References: <e70cde40-a7ef-4894-9f1d-453ba13a0ced@www.fastmail.com> <F3C88129-A382-48E9-AB24-BECDADB8DBB5@americanefficient.com> <bfe8c0f43c5211c71645071c6190dae259f08236.camel@cybertec.at> <b86f2afd-84a7-47a8-8d82-e12d0aa50599@www.fastmail.com> <613704.1608654756@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox