From: Bryan Green <dbryan.green@gmail.com>
To: Andrei Lepikhov <lepihov@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Andres Freund <andres@anarazel.de>
Cc: Илья Жарков <izharkov1243@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org, eng eng <pspetrov91@gmail.com>
Subject: Re: Do not scan index in right table if condition for left join evaluates to false using columns in left table
Date: Mon, 14 Sep 2026 11:26:30 -0500
Message-ID: <d510e2e0-c3aa-45f7-a8fb-a2684504aba3@gmail.com> (raw)
In-Reply-To: <2c0c380b-fab0-419e-a183-ab08ce2ee1ea@gmail.com>
References: <CAKE=rqQ-LHuh2eVsKC7ihkRJoCBZafSR72o3Xk4Xb=LcQMQfsA@mail.gmail.com>
<tx2q6qi4hn2diocmnwfjo5xw2m56iabz3fyeimk7oeyymy3u4b@4ria2zwtz5mp>
<3510224.1733609212@sss.pgh.pa.us>
<lqgxfkrngt6zh6ldwiw6nkcanrcxn5sravsoum6r6s7xfc3oib@tcknx7ngik3l>
<3517444.1733613195@sss.pgh.pa.us>
<07d035c5-0e40-45ee-9c78-60a5dc6e9fae@gmail.com>
<CAJ-KsAw4EusG+GJCkUnx2SLUvQ0GaydFJzX2EezOYYt3HJpBtA@mail.gmail.com>
<3d749085-72b6-46d6-a26a-7c95805c1adb@gmail.com>
<e419bafa-cfed-4324-89ad-9b2978fff512@gmail.com>
<bb73a0a0-70e6-46a1-900b-55dd75c2669d@gmail.com>
<ad949722-6adb-4c11-bd7c-33ac1db29ed6@gmail.com>
<df139cc4-a9f2-4b85-9528-85c9833cbeef@gmail.com>
<2c0c380b-fab0-419e-a183-ab08ce2ee1ea@gmail.com>
On 9/12/2026 7:02 AM, Andrei Lepikhov wrote:
> On 01/09/2026 11:16, Andrei Lepikhov wrote:
>> On 28/07/2026 10:13, Andrei Lepikhov wrote:
>>
>> Just a rebase onto current master
>>
>
> Another rebase to follow the master branch.
>
> I have also looked at the downside of the opportunistic approach this feature
> takes. The pros are obvious: zero planning overhead, no search-space blowup, and
> simple code. But what does the worst case look like?
>
> By gating the inner side, we add one more node (Result) and force the gating
> qual to be evaluated once per outer row. The vanilla NestLoop sometimes does not
> do that: when the inner side returns no tuple, a LEFT join goes straight to the
> null-fill path and ExecQual(joinqual) is never reached at all. So the gated plan
> pays for the clause on every outer row. I think a parameterised index scan that
> finds no match is the common case for this behaviour.
>
> The worst query therefore looks like this:
>
> CREATE TABLE o (a int, t text);
> INSERT INTO o SELECT g, 'row'||g FROM generate_series(1,1000000) g;
> CREATE TABLE i (a int, b int);
> ANALYZE o, i;
>
> SET enable_hashjoin = off;
> SET enable_mergejoin = off;
> SET max_parallel_workers_per_gather = 0;
> \timing on
>
> SELECT count(*) FROM ( SELECT * FROM o
> LEFT JOIN i ON (md5(md5(md5(md5(o.t)))) <> 'z')) s;
>
> Making the gate progressively more expensive shows what is going on.
> On the current master, I see ~470ms, with the gating clause ~1500ms. This effect
> is linear but isn't limited; it depends on the number of evaluations and the
> outer size.
>
> The basic case, when each row passes the gate (and correspondent join clause)
> doesn't show any degradation, there are no significant difference between gated
> and no-gated approach. To reproduce just add one row into the inner table and
> re-execute the query above:
>
> INSERT INTO i (a,b) VALUES (1, 1);
>
> My conclusion is this. A gated NestLoop usually behaves like a normal NestLoop
> adding benefit of reduced inner scans or nothing. In the degenerate case where
> the inner side returns nothing, it adds an overhead proportional to the outer
> size times the cost of the gating clause, with no ceiling. Separately, the
> Result projection adds a per-joined- tuple cost that grows with the width of the
> inner targetlist.
>
> I do not see this as a fundamental problem, except for SubPlan cases - but
> SubPlan is a different animal, and every new correlated pull-up technique
> attracts complaints of exactly this shape.
>
> So the real question is whether the cost model can account for these two terms
> well enough to choose between a gated and a non-gated NestLoop, assuming we turn
> this into a cost-based optimisation.
>
I've continued doing security audits of commitfest patches, and as part
of that I occasionally turn up differences in output that aren't
security issues but seemed worth passing along. v3 has a few. They all
show up on the gated nestloop plan, so I forced it with:
set enable_hashjoin = off;
set enable_mergejoin = off;
1) A left join that returns a row on HEAD raises an error with the patch:
create table o (k int, x int); insert into o values (1, 0);
create table i (k int); -- empty
select * from o left join i on o.k = i.k and 1/o.x = 1;
-- HEAD: (1, 0, NULL)
-- v3: ERROR: division by zero
2) A volatile condition returns a different number of rows:
create table a (k int, x int); insert into a values (1, 2);
create table b (k int, y int); insert into b values
(1,1),(1,2),(1,3),(1,4);
create sequence s;
select a.k, b.y from a left join b on a.k = b.k and a.x > nextval('s');
-- HEAD: 1 row
-- v3: 4 rows
3) The same when the volatile is inside a sub-select:
alter sequence s restart;
select a.k, b.y from a left join b
on a.k = b.k and a.x > (select nextval('s') where a.k = 1);
-- HEAD: 1 row
-- v3: 4 rows
On HEAD I get the same answers under hash and merge joins as well, so
these aren't just plan-to-plan variation. I haven't worked out whether
any of them matter for what you're doing here. Passing them along in
case they do.
--
Bryan Green
EDB: https://www.enterprisedb.com
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-hackers@postgresql.org
Cc: dbryan.green@gmail.com, lepihov@gmail.com, tgl@sss.pgh.pa.us, andres@anarazel.de, izharkov1243@gmail.com, pspetrov91@gmail.com
Subject: Re: Do not scan index in right table if condition for left join evaluates to false using columns in left table
In-Reply-To: <d510e2e0-c3aa-45f7-a8fb-a2684504aba3@gmail.com>
References: <CAKE=rqQ-LHuh2eVsKC7ihkRJoCBZafSR72o3Xk4Xb=LcQMQfsA@mail.gmail.com> <tx2q6qi4hn2diocmnwfjo5xw2m56iabz3fyeimk7oeyymy3u4b@4ria2zwtz5mp> <3510224.1733609212@sss.pgh.pa.us> <lqgxfkrngt6zh6ldwiw6nkcanrcxn5sravsoum6r6s7xfc3oib@tcknx7ngik3l> <3517444.1733613195@sss.pgh.pa.us> <07d035c5-0e40-45ee-9c78-60a5dc6e9fae@gmail.com> <CAJ-KsAw4EusG+GJCkUnx2SLUvQ0GaydFJzX2EezOYYt3HJpBtA@mail.gmail.com> <3d749085-72b6-46d6-a26a-7c95805c1adb@gmail.com> <e419bafa-cfed-4324-89ad-9b2978fff512@gmail.com> <bb73a0a0-70e6-46a1-900b-55dd75c2669d@gmail.com> <ad949722-6adb-4c11-bd7c-33ac1db29ed6@gmail.com> <df139cc4-a9f2-4b85-9528-85c9833cbeef@gmail.com> <2c0c380b-fab0-419e-a183-ab08ce2ee1ea@gmail.com> <d510e2e0-c3aa-45f7-a8fb-a2684504aba3@gmail.com>
* 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