From: Tom Lane <tgl@sss.pgh.pa.us>
To: Łukasz Dąbek <sznurek@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Using b-tree index for >= condition when joining
Date: Sat, 16 May 2020 23:22:18 -0400
Message-ID: <31575.1589685738@sss.pgh.pa.us> (raw)
In-Reply-To: <CAE2Xe9tLfLxE_pthPLFerywyA4RxtGTwMCqKpGk6VqMYOj6Lfw@mail.gmail.com>
References: <CAE2Xe9tLfLxE_pthPLFerywyA4RxtGTwMCqKpGk6VqMYOj6Lfw@mail.gmail.com>
=?UTF-8?B?xYF1a2FzeiBExIViZWs=?= <sznurek@gmail.com> writes:
> I am having a problem with nudging postgres to choose a good plan for
> a query involving a left join and an inequality constraint on a column
> with b-tree index.
> ...
> It looks like the inequality on date isn't pushed down below the left
> join?
Nope. The planner only derives implied conditions from equality clauses.
There've been discussions about that in the past, but it was (and remains)
unclear that trying to account for other clause types would be a net win.
The planner-cycles-expended versus number-of-queries-improved tradeoff
doesn't look promising.
> I can get the plan I'd like to have by putting the same
> constraint on the date column on the second table:
Note that you're not really getting the same plan that way: it's not
a left join anymore, because you put a strict constraint on the join's
inner relation, so the planner realizes it doesn't have to produce any
null-extended rows. You could make it work with the desired semantics
with something along the lines of
SELECT * FROM tbl1 t1
LEFT JOIN (select * from tbl2 where tbl2.date >= '2019-04-21') t2
USING (date)
WHERE t1.date >= '2019-04-21';
but of course that's even less easy :-(
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, sznurek@gmail.com
Subject: Re: Using b-tree index for >= condition when joining
In-Reply-To: <31575.1589685738@sss.pgh.pa.us>
References: <CAE2Xe9tLfLxE_pthPLFerywyA4RxtGTwMCqKpGk6VqMYOj6Lfw@mail.gmail.com> <31575.1589685738@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