Re: pull-up subquery if JOIN-ON contains refs to upper-query

Alena Rybakina <a.rybakina@postgrespro.ru>

From: Alena Rybakina <a.rybakina@postgrespro.ru>
To: Ilia Evdokimov <ilya.evdokimov@tantorlabs.com>
Cc: Ranier Vilela <ranier.vf@gmail.com>, PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>, Peter Petrov <p.petrov@postgrespro.ru>, David Rowley <dgrowleyml@gmail.com>
Date: 2025-09-02T21:20:46Z
Lists: pgsql-hackers

Attachments

Hi! Thank you for your attention to this patch!

On 03.09.2025 00:07, Ilia Evdokimov wrote:
> On 04.06.2025 13:40, Alena Rybakina wrote:
>>
>> Do you have any ideas on how to solve this problem? So far, the only 
>> approach I see is to try an alternative plan but I'm still learning this.
>>
> Hi,
>
> I've reviewed this patch, and I have suggestion about the approach.
>
> Currently, the patch extends 'convert_EXISTS_sublick_to_join' with 
> quite complex logic (clause collection, volatile checks, rewriting 
> join quals, etc). While it works, the amount of branching and special 
> cases makes the function harder to follow.
>
> Looking at the logic, it seems that a large part of the complexity 
> comes from trying to directly adapt 'convert_EXISTS_sublink_to_join' 
> instead of factoring out a dedicated path. An alternative would be to 
> introduce a separate function 
> *'convert_EXISTS_sublink_to_lateral_join' *- with a similar API to 
> 'convert_ANY_sublink_to_join'. Such a function can focus only on the 
> EXISTS-to-join case, while keeping the existing function shorter and 
> easier to reason about.
>
> I even made some first rough sketches of this approach (not a finished 
> patch, just an outline). Of course, it would still need proper 
> adaptation, but I think it demonstrates that the overall structure can 
> be kept simpler.
>
> What do you think about refactoring in this direction?
>
> --
> Best regards,
> Ilia Evdokimov,
> Tantor Labs LLC,
> https://tantorlabs.com
>
I'll look at this. I need some time to consider it.

I'm working on this approach right now. I introduced mutator and made 
the transformation if it is possible there but I need to fix some bugs.