Re: BUG #19765: Wrong result from a chain of LEFT JOINs since v16

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Nitin Motiani <nitinmotiani(at)google(dot)com>
Cc: Tender Wang <tndrwang(at)gmail(dot)com>, leis(at)in(dot)tum(dot)de, pgsql-bugs(at)lists(dot)postgresql(dot)org, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: BUG #19765: Wrong result from a chain of LEFT JOINs since v16
Date: 2026-10-26 07:01:52
Message-ID: CAMbWs4-0ASKv8oDD_PA6skL=UE71LBGYff6h5PhdPULZEHw3Yw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, Oct 23, 2026 at 9:09 PM Nitin Motiani <nitinmotiani(at)google(dot)com> wrote:
> Attached is v4 based on your suggestions, which keeps all cloned
> outer-join clauses and sets norm_selec = 1.0 and outer_selec = 1.0 on
> them.

Setting norm_selec and outer_selec to 1.0 is not enough. Those are
only cached values, and clause_selectivity_ext() skips the cache when
varRelid is nonzero and the clause references more than one base rel.
That is the case when estimating a parameterized scan, so the kept
clause gets its full selectivity there and is double-counted with the
derived INNERVAR = CONSTANT clause.

I think a simple way to fix this is to restore the redundancy marker
that v15 and earlier used and b448f1c8d removed: the kept clause gets
norm_selec = 2.0, and clause_selectivity_ext() returns 1.0 for any
clause with norm_selec > 1, before the cache check.

Also I think we should put the new tests in the session of "regression
test for improper pushing of constants across outer-join clauses", and
reuse the existing zt1/zt2/zt3 tables.

Hence, here is the v5 patch.

- Richard

Attachment Content-Type Size
v5-0001-Don-t-replace-clone-clauses-with-constant-TRUE.patch application/octet-stream 10.3 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Henson Choi 2026-10-26 07:00:14 Re: BUG #19767: pg_get_viewdef prints ORDER BY (- '3'::int4) as (- 3), which reparses as column position -3