A gap in preventing bogus parameterizations in a798660ebe3

Поиск
Список
Период
Сортировка
Искать

A gap in preventing bogus parameterizations in a798660ebe3

От:
Tomas Vondra <tomas@vondra.me>
Дата:
Вложения:
Hi,

The second bug / assert failure I ran into when running the randomized
join generator seems to be hitting a gap in a798660ebe3:

    Defend against bogus parameterization of join input paths.

A trivial reproducer for this is:

  CREATE TABLE t (a integer, b integer);
  CREATE INDEX ON t (a, b);

  EXPLAIN
  SELECT 1 FROM t t1
  WHERE t1.a IN (SELECT t2.a
                 FROM t t2
                   LEFT JOIN t t3 ON (t2.a = t3.a)
                   LEFT JOIN (t t4 LEFT JOIN t t5 ON (t4.a = t5.a))
                             ON (t3.a = t4.a)
                   WHERE coalesce(t5.b, 0) = t2.b);

Which trips over this asssert in get_baserel_parampathinfo():

 Assert(!bms_overlap(joinrel->relids, required_outer));

This is not an area I know very well, but it seems the defense added by
a798660ebe3 considers only sjinfo->ojrelid. But that does not seem to be
enough for joins pushed down through identity 3. IIUC those are added to
ojrelid only for the join that completes it, and so the sjinfo->ojrelid
may not be entirely "up to date", and the parameterized paths have the
same issue as those rejected by a798660ebe3.

The attached patch fixes that by adding a new JoinPathExtraData field
ojrelids, and calculating it from joinrel/inner/outer like this:

  if (sjinfo->ojrelid != 0)
  {
    extra.ojrelids = bms_difference(joinrel->relids, outerrel->relids);
    extra.ojrelids = bms_del_members(extra.ojrelids, innerrel->relids);
    extra.ojrelids = bms_add_member(extra.ojrelids, sjinfo->ojrelid);
  }

There's a comment in add_paths_to_joinrel explaining why I think this is
correct, but is it? Perhaps I'm forgetting something.

Also, the new field would be a problem for backpatching (the problem
exists since 16), and a new field might break ABI. Perhaps it'd be
better to just calculate it in the handful of places that need it.


regards

-- 
Tomas Vondra

Re: A gap in preventing bogus parameterizations in a798660ebe3

От:
Tender Wang <tndrwang@gmail.com>
Дата:
Hi Tomas,

Tomas Vondra  于2026年11月2日周一 04:39写道:
>
> Hi,
>
> The second bug / assert failure I ran into when running the randomized
> join generator seems to be hitting a gap in a798660ebe3:
>
>     Defend against bogus parameterization of join input paths.
>
> A trivial reproducer for this is:
>
>   CREATE TABLE t (a integer, b integer);
>   CREATE INDEX ON t (a, b);
>
>   EXPLAIN
>   SELECT 1 FROM t t1
>   WHERE t1.a IN (SELECT t2.a
>                  FROM t t2
>                    LEFT JOIN t t3 ON (t2.a = t3.a)
>                    LEFT JOIN (t t4 LEFT JOIN t t5 ON (t4.a = t5.a))
>                              ON (t3.a = t4.a)
>                    WHERE coalesce(t5.b, 0) = t2.b);
>
> Which trips over this asssert in get_baserel_parampathinfo():
>
>  Assert(!bms_overlap(joinrel->relids, required_outer));
>
> Also, the new field would be a problem for backpatching (the problem
> exists since 16), and a new field might break ABI. Perhaps it'd be
> better to just calculate it in the handful of places that need it.
>

The existing checks only consider sjinfo->ojrelid, but outer-join
identity 3 can add additional OJ relids to the joinrel.
An input path parameterized by one of those OJs can therefore pass the
checks, leaving the resulting path dependent on the joinrel itself.

As you noted, adding a field to JoinPathExtraData raises an ABI
concern, so that version of the fix might need to be master-only.
For backpatching, perhaps a minimal fix would be to check
required_outer immediately after calculating it in
try_nestloop_path(), try_mergejoin_path(), and try_hashjoin_path():

    if (bms_overlap(required_outer, joinrel->relids))
    {
        bms_free(required_outer);
        return;
    }

This would reject the invalid combination before creating the join
path, without changing JoinPathExtraData.

The existing sjinfo->ojrelid checks would remain necessary, since the
current OJ's relid might not yet be included in joinrel->relids when
it has been pushed down.
All other existing checks would remain unchanged.

Would this be a reasonable minimal fix for the back branches?


-- 
Thanks,
Tender Wang


Re: A gap in preventing bogus parameterizations in a798660ebe3

От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
On 2026-Nov-02, Tender Wang wrote:

> As you noted, adding a field to JoinPathExtraData raises an ABI
> concern, so that version of the fix might need to be master-only.

But nothing allocates JoinPathExtraData anywhere except
add_paths_to_joinrel in its stack; everybody else just receives a
pointer to that.  Even the hooks for it receive it as a pointer.  I
think the ABI concern is overblown in this case; just make sure to add
the new field at the end.

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
"Find a bug in a program, and fix it, and the program will work today.
Show the program how to find and fix a bug, and the program
will work forever" (Oliver Silfridge)


Re: A gap in preventing bogus parameterizations in a798660ebe3

От:
Tomas Vondra <tomas@vondra.me>
Дата:
On 11/2/26 12:01, Álvaro Herrera wrote:
> On 2026-Nov-02, Tender Wang wrote:
> 
>> As you noted, adding a field to JoinPathExtraData raises an ABI
>> concern, so that version of the fix might need to be master-only.
> 
> But nothing allocates JoinPathExtraData anywhere except
> add_paths_to_joinrel in its stack; everybody else just receives a
> pointer to that.  Even the hooks for it receive it as a pointer.  I
> think the ABI concern is overblown in this case; just make sure to add
> the new field at the end.
> 

You're probably right. I was concerned some extension might be me
allocating the struct (as it's in a public header), but I did check a
couple extensions that might be doing that (like pg_hint_plan) and I
haven't found such cases.

So putting the new field at the end should be fine.


regards

-- 
Tomas Vondra



Re: A gap in preventing bogus parameterizations in a798660ebe3

От:
Tomas Vondra <tomas@vondra.me>
Дата:
Вложения:
Hi,

Here's a cleaned up version of the fix, with patches for each backbranch
(it's mostly the same as master, except that 18 changed one of the plans
in regression tests, and 19 introduced pg_mask field in the struct).

I've considered fixing it per Tender's suggestion, but I didn't like it.
It feels more like after-the-fact protection - it constructs the paths
and then discards them later. I think we don't want to construct these
nonsensical parameterized paths at all. First, it adds overhead (I have
not bothered to measure how much, though). Second, I'm afraid it would
silently mask a bug/thinko elsewhere.

I'm now fairly confident the fix is correct. I'd still welcome someone
else doing a quick review, of course. But similarly to [1] I'm planning
to probably push this in a couple days, once the minor releases are out
the door.


regards

-- 
Tomas Vondra

Re: A gap in preventing bogus parameterizations in a798660ebe3

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Mon, Nov 9, 2026 at 4:47 AM Tomas Vondra  wrote:
> I'm now fairly confident the fix is correct. I'd still welcome someone
> else doing a quick review, of course.

I looked at the master patch, and it LGTM.

One nit on the commit message: "we still reject the path, just a bit
later" isn't quite accurate.  AFAICS nothing rejects such a path
later.  It'd be added to the joinrel's pathlist, just that it can
never be used, because its parameterization includes a relid within
the joinrel, which the other input of an upper join can never supply.
Maybe something like "In non-assert builds this is mostly harmless,
since such paths can never appear in a complete plan."

This check only covers parameters that need an outer join completed at
the current join, though.  It misses the case where a parameter can
only be supplied above some outer join that itself has to be formed
above the current join.  If such a parameter comes from a lateral
reference, every path of the joinrel is dead, and the join has to be
rejected in join_is_legal() instead.  I'm working on that in [1].  So
if your join generator hits "failed to build any N-way joins" with
LATERAL, it's likely that issue rather than another hole in this
patch.

[1] https://postgr.es/m/CAMbWs48YGwbxxz-3_g0snstuVNWdkMfJwM7iwqf8NY5_uLQ23Q@mail.gmail.com

- Richard


Re: A gap in preventing bogus parameterizations in a798660ebe3

От:
Tomas Vondra <tomas@vondra.me>
Дата:


On 11/9/26 09:10, Richard Guo wrote:
> On Mon, Nov 9, 2026 at 4:47 AM Tomas Vondra  wrote:
>> I'm now fairly confident the fix is correct. I'd still welcome someone
>> else doing a quick review, of course.
> 
> I looked at the master patch, and it LGTM.
> 
> One nit on the commit message: "we still reject the path, just a bit
> later" isn't quite accurate.  AFAICS nothing rejects such a path
> later.  It'd be added to the joinrel's pathlist, just that it can
> never be used, because its parameterization includes a relid within
> the joinrel, which the other input of an upper join can never supply.
> Maybe something like "In non-assert builds this is mostly harmless,
> since such paths can never appear in a complete plan."
> 

Right. That's what I mean by "rejected" by I agree it's a bit unclear.

> This check only covers parameters that need an outer join completed at
> the current join, though.  It misses the case where a parameter can
> only be supplied above some outer join that itself has to be formed
> above the current join.  If such a parameter comes from a lateral
> reference, every path of the joinrel is dead, and the join has to be
> rejected in join_is_legal() instead.  I'm working on that in [1].  So
> if your join generator hits "failed to build any N-way joins" with
> LATERAL, it's likely that issue rather than another hole in this
> patch.
> 
> [1] https://postgr.es/m/CAMbWs48YGwbxxz-3_g0snstuVNWdkMfJwM7iwqf8NY5_uLQ23Q@mail.gmail.com
Ah, OK. I don't think I've seen such failures yes, but good to know.


Thanks!

-- 
Tomas Vondra



Re: A gap in preventing bogus parameterizations in a798660ebe3

От:
Tomas Vondra <tomas@vondra.me>
Дата:
On 11/9/26 12:33, Tomas Vondra wrote:
> 
> 
> On 11/9/26 09:10, Richard Guo wrote:
>> On Mon, Nov 9, 2026 at 4:47 AM Tomas Vondra  wrote:
>>> I'm now fairly confident the fix is correct. I'd still welcome someone
>>> else doing a quick review, of course.
>>
>> I looked at the master patch, and it LGTM.
>>
>> One nit on the commit message: "we still reject the path, just a bit
>> later" isn't quite accurate.  AFAICS nothing rejects such a path
>> later.  It'd be added to the joinrel's pathlist, just that it can
>> never be used, because its parameterization includes a relid within
>> the joinrel, which the other input of an upper join can never supply.
>> Maybe something like "In non-assert builds this is mostly harmless,
>> since such paths can never appear in a complete plan."
>>
> 
> Right. That's what I mean by "rejected" by I agree it's a bit unclear.
Pushed, after rewording the commit message a little bit to make it clear
we were not rejecting the paths, but a final plan would not have used
them because of the bogus parameterization.

Thanks for the review!

-- 
Tomas Vondra



Re: A gap in preventing bogus parameterizations in a798660ebe3

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Tomas Vondra  writes:
> Pushed, after rewording the commit message a little bit to make it clear
> we were not rejecting the paths, but a final plan would not have used
> them because of the bogus parameterization.

The buildfarm is pointing out that this change requires updating
.abi-compliance-history in the back branches.

			regards, tom lane


Re: A gap in preventing bogus parameterizations in a798660ebe3

От:
Tomas Vondra <tomas@vondra.me>
Дата:
On 11/12/26 18:39, Tom Lane wrote:
> Tomas Vondra  writes:
>> Pushed, after rewording the commit message a little bit to make it clear
>> we were not rejecting the paths, but a final plan would not have used
>> them because of the bogus parameterization.
> 
> The buildfarm is pointing out that this change requires updating
> .abi-compliance-history in the back branches.
> 

Thanks for reminding me, will fix.


-- 
Tomas Vondra



FAQ