Re: BUG #18429: Inconsistent results on similar queries with join lateral
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: "David G. Johnston" <david.g.johnston@gmail.com>
Cc: "b.ryder@ateme.com" <b.ryder@ateme.com>,
"pgsql-bugs@lists.postgresql.org" <pgsql-bugs@lists.postgresql.org>
Date: 2024-04-12T14:50:17Z
Lists: pgsql-bugs
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Friday, April 12, 2024, PG Bug reporting form <noreply@postgresql.org>
> wrote:
>> -- `where` clause should return false: (14 - 6) / 4 = (12 - 6) / 4 → false
>> select 1 from c2 where (14 - c2.t) / c2.d = (12 - c2.t) / c2.
> You are doing integer division here and the right hand side equals, 1.5; I
> suppose something may have used to round that up to the integer 2 which
> would make both sides equals but now (v16) rounds it down (or more
> accurately truncates it) to 1.
I don't think that's relevant. It's clear that the older versions
are returning inconsistent answers. Comparing EXPLAIN output shows
why --- current HEAD produces (for Q1)
Nested Loop Left Join (cost=0.16..3.48 rows=1 width=4)
Filter: (((14 - COALESCE(t.wt, 0)) / arrayd.ad) = ((12 - COALESCE(t.wt, 0)) / arrayd.ad))
-> Function Scan on unnest arrayd (cost=0.00..0.01 rows=1 width=4)
-> Limit (cost=0.15..3.45 rows=1 width=4)
-> Index Only Scan Backward using t_pkey on t (cost=0.15..36.35 rows=11 width=4)
Index Cond: (wd = arrayd.ad)
while v15 produces
Nested Loop Left Join (cost=0.16..3.50 rows=1 width=4)
Filter: (((14 - COALESCE(c.t, 0)) / arrayd.ad) = ((12 - COALESCE(c.t, 0)) / arrayd.ad))
-> Function Scan on unnest arrayd (cost=0.00..0.01 rows=1 width=4)
-> Subquery Scan on c (cost=0.15..3.47 rows=1 width=4)
Filter: (((14 - COALESCE(c.t, 0)) / arrayd.ad) = ((12 - COALESCE(c.t, 0)) / arrayd.ad))
-> Limit (cost=0.15..3.45 rows=1 width=4)
-> Index Only Scan Backward using t_pkey on t (cost=0.15..36.35 rows=11 width=4)
Index Cond: (wd = arrayd.ad)
So there's the problem: a copy of the upper WHERE clause is being
inappropriately applied below the outer join, and that filters out the
only row of the "c" subselect. Then when we re-apply the WHERE at
top level, the COALESCEs produce 0 allowing the condition to evaluate
as true.
(I didn't check Q3-Q5 in detail, but probably they've got variants
of the same issue.)
"git bisect" fingers this commit as the first one producing correct
answers:
commit 2489d76c4906f4461a364ca8ad7e0751ead8aa0d
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Mon Jan 30 13:16:20 2023 -0500
Make Vars be outer-join-aware.
This is kind of exciting for me, as IIRC it's the first field-detected
bug that that work fixes. However, I'm not real sure right now how
we might fix it in the back branches ...
regards, tom lane
Commits
-
Ensure generated join clauses for child rels have correct relids.
- 03107b4eda7f 17.0 landed
- 91800af13710 16.3 landed
-
Fix generation of EC join conditions at the wrong plan level.
- f502849d49a4 12.19 landed
- d9e4ee74f48c 13.15 landed
- ab2402268c04 14.12 landed
- 5aacfa64e5da 15.7 landed
-
Make Vars be outer-join-aware.
- 2489d76c4906 16.0 cited