Re: Some problems regarding the self-join elimination code

From: Andrei Lepikhov <lepihov(at)gmail(dot)com>
To: Richard Guo <guofenglinux(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Some problems regarding the self-join elimination code
Date: 2025-04-08 09:44:32
Message-ID: 3a1103d9-626b-4149-a7d6-d0bf144938ee@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 4/2/25 15:26, Richard Guo wrote:
> While working on another patch, I looked at ChangeVarNodes() and found
> some issues introduced by the self-join elimination commit. I think
> it'd better to fix or improve them.
>
> * ChangeVarNodes_walker includes special handling for RestrictInfo
> nodes. And it adjusts RestrictInfo.orclause only if the rt_index of
> the relation to be removed is contained in RestrictInfo.clause_relids.
>
> I don't think this is correct. Even if the to-be-removed relation is
> not present in clause_relids, it can still be found somewhere within
> the orclause. As an example, consider:
Yeah, discovering how we tolerated such a gaffe I found that the comment
on the clause_relids field is quite vague here:

"The relids (varnos+varnullingrels) actually referenced in the clause:"

and only in the RestrictInfo description you can find some additional
information. I think we should modify the check, uniting clause_relids
and required_relids before searching for the removing relid.

> + rinfo->num_base_rels = bms_num_members(rinfo->clause_relids);
>
> I don't think this is correct either. num_base_rels is the number of
> base rels in clause_relids and should not count outer-join relids.
> And we have no guarantee that clause_relids does not include any
> outer-join relids.
It is a clear mistake, no apologies to me.
>
> To fix, I think we should exclude all outer-join relids. Perhaps we
> can leverage root->outer_join_rels to achieve this.
I think we may use a difference in size of rinfo->clause_relids before
and after adjustion. If something were removed, just decrease num_base_rels.

> * Speaking of comments, I’ve noticed that some changes are lacking
> comments. For example, there are almost no comments regarding the
> special handling of RestrictInfo nodes in ChangeVarNodes_walker,
> except for an explanation about adding the NullTest.
Ok, it seems that comments were removed too hastily. Not addressed it yet.

I also added little code refactoring to make it more readable. See
fix.diff in attachment as my proposal for future discussion.

--
regards, Andrei Lepikhov

Attachment Content-Type Size
fix.diff text/x-patch 3.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2025-04-08 09:46:59 Re: Enhancing Memory Context Statistics Reporting
Previous Message Thomas Munro 2025-04-08 09:42:23 Re: CREATE DATABASE with filesystem cloning