Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x69WN-0003wE-1T for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Sep 2026 16:27:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x69WM-00EYAz-1c for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Sep 2026 16:27:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x69WM-00EYAq-0b for pgsql-hackers@lists.postgresql.org; Mon, 14 Sep 2026 16:27:06 +0000 Received: from mail-oi1-x236.google.com ([2607:f8b0:4864:20::236]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x69WI-00000000Q2X-0qtt for pgsql-hackers@lists.postgresql.org; Mon, 14 Sep 2026 16:27:05 +0000 Received: by mail-oi1-x236.google.com with SMTP id 5614622812f47-4b28d9537bcso4280b6e.0 for ; Mon, 14 Sep 2026 09:27:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789403221; x=1790008021; darn=lists.postgresql.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=jrjrsf8d1ODNihQdHBrDa4nwNhTKIwRKUNJl2logp6Q=; b=jVnIdVKwIfJFm5MnC3TSEG9/fFCHRR+SE5ivq3o6BWAskcdqSGjwa9yg56vllD+qQ1 h6XLh1G9JEV/fX9sBOburHWrJtHPbjMWdq6AZpORadQTFdU/gUPYw01/wkbfpT6QIjoW rHZZoXpKgaJ/r+zJHTkLmmR9ZmNXXMjWrISTUdsC1nwkjVpYrN3n6LNCG2EIsOTWDSBw 1oWyZCyHydjL56yVKbhRw+fwZDQUbq5vepPjdS9Sf97PcP7hYsmjgEdHmKOrW9HHtKQt SLeW3UZKm46DMBB9JWefuvurn7FJZMKnmtzFX6q0081h9X6k+aR8o3vFQ5GB+GJRqRON MNtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789403221; x=1790008021; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jrjrsf8d1ODNihQdHBrDa4nwNhTKIwRKUNJl2logp6Q=; b=mBptfwhU8AWLyuBgTPUCb8m3qLe3doawE3uCRBugbOIXCBkHuFnpFHJH9LQeVae3SZ Gu7Mt9GJX29TRGsd2Ps0SgHx8a+RkzRd3PdKhsPwvCCZ4dPs678zMcrDRdWiKA/QHDZ7 hKWmwe/Tv9Hgn+aRsw+OuZn3xzoc7rm/AffKi///7BatPXcQGETZPbdrDJpeh8ztbNRo BmyZiC/yrzXLTmaie2bm/1EeTo75wgXk8bxMab7I9AGoNALk843dSbOPMpUVjBJrwYol u3JmcyXXvGUIy3uAvGW/0GA/KxTHitQn3dxB6BXPSvNaZj8PjPJuQyQxksKNznDdjkM5 NVXw== X-Forwarded-Encrypted: i=1; AKwUvBzBabYaPo1P6fzGio8GA7SL6YP5uEYNPH3HvCVSSEAm3bYQAVpVTkC+XAioM7PYJxjepb7XSiSDWRRUDidM@lists.postgresql.org X-Gm-Message-State: AFuF++nmzLRtND3lqfoeOjsdJSmEJGM5EDddf517fC+tsKk1gDEa6/UF Tt/c/iHuVUJ/eTIp+O6n242Tl5IxLTZ72tvblSD9oolxFulq0XIatqA8 X-Gm-Gg: AYBFou1R+eycHDY/a3Wl6iaYPZyLmSEL9ZyrXvsSNjVW4WumZhG4KXXGpJ/aRtyhuYB iz9UTQ02IYO+nyrT8KqbOrZmnd6YSfu+PD/PSiRRPIb5ZMpSB7eC4afeVJxYXTZtqu+hajzenuj ZxUINjfKuGZTLx5BKx8oJlkPTiR0/GVToAs+uUWB1yj/oYVzvfV8tHTj5EhNHHqksWAgVGCeatP CqxFZhC98tKLTa/fnl7mhMhyqwU/i+cT0JScPPz79JgBaybkQu1j7KOuLAZ9uhy22OpsRN7uwdL 6nrcT7JyZrEyrsvPGPv4zkLqLeR+SdY+arvLwZ1WTmMVSqVUqOrsM2dfyzeOGgmycY/q3LtRTEr JkIv+NU10sYMAmPQ4/4I85dT2qhErDdvXaLKPQwy5fli2MdqUaG5neo6o/l7uW1U4DMLHsU5BA9 4slW4n/m6UB7Lp2fK6KygzY2sU7xgATGOjWwQH+U2JPu7zgENAxVpd+ZoEq+/dUBGW2aFq7eN3t NYGagZZf7LNmLeJvH2v1fKz2kOLsXaywQCNUSsEskGbRj/2TMDHiFvVVnhMMvaRPFPyE821ZdPu fujVdL0RBtISFu+LJLs= X-Received: by 2002:a05:6808:13d4:b0:4b8:4703:db8f with SMTP id 5614622812f47-4c8a1a1620amr67094b6e.24.1789403220988; Mon, 14 Sep 2026 09:27:00 -0700 (PDT) Received: from ?IPV6:2603:8082:2b40:1f:ad6f:2150:1283:4f79? ([2603:8082:2b40:1f:ad6f:2150:1283:4f79]) by smtp.gmail.com with ESMTPSA id 5614622812f47-4c331bc9342sm10746036b6e.12.2026.09.14.09.26.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2026 09:26:59 -0700 (PDT) Message-ID: Date: Mon, 14 Sep 2026 11:26:30 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Do not scan index in right table if condition for left join evaluates to false using columns in left table To: Andrei Lepikhov , Tom Lane , Andres Freund Cc: =?UTF-8?B?0JjQu9GM0Y8g0JbQsNGA0LrQvtCy?= , pgsql-hackers@lists.postgresql.org, eng eng References: <3510224.1733609212@sss.pgh.pa.us> <3517444.1733613195@sss.pgh.pa.us> <07d035c5-0e40-45ee-9c78-60a5dc6e9fae@gmail.com> <3d749085-72b6-46d6-a26a-7c95805c1adb@gmail.com> <2c0c380b-fab0-419e-a183-ab08ce2ee1ea@gmail.com> Content-Language: en-US From: Bryan Green In-Reply-To: <2c0c380b-fab0-419e-a183-ab08ce2ee1ea@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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