Re: [POC] Allow flattening of subquery with a link to upper query - Mailing list pgsql-hackers

From Richard Guo
Subject Re: [POC] Allow flattening of subquery with a link to upper query
Date
Msg-id CAMbWs4_3KyJDMgZLL1xkp0ev+FbuwjJLuVHfQD6LCYPb35rNXw@mail.gmail.com
Whole thread Raw
In response to [POC] Allow flattening of subquery with a link to upper query  (Andrey Lepikhov <a.lepikhov@postgrespro.ru>)
Responses Re: [POC] Allow flattening of subquery with a link to upper query
Re: [POC] Allow flattening of subquery with a link to upper query
List pgsql-hackers

On Wed, Aug 31, 2022 at 2:35 PM Andrey Lepikhov <a.lepikhov@postgrespro.ru> wrote:
Before flattening procedure we just look through the quals of subquery,
pull to the upper level OpExpr's containing variables from the upper
relation and replace their positions in the quals with true expression.
Further, the flattening machinery works as usual.
 
Hmm, I'm not sure this patch works correctly in all cases. It seems to
me this patch pulls up the subquery without checking the constraints
imposed by lateral references. If its quals contain any lateral
references to rels outside a higher outer join, we would need to
postpone quals from below an outer join to above it, which is probably
incorrect. As an example, consider

    select * from a left join b on b.i in
        (select c.i from c where c.j = a.j);

If we pull up the ANY SubLink into parent query and pull up its qual
into upper level, as what the patch does, then its qual 'c.j = a.j'
would have to be postponed past the B/C semi join, which is totally
wrong. Doing this would firstly trigger the assertion failure in
distribute_qual_to_rels

  Assert(root->hasLateralRTEs);   /* shouldn't happen otherwise */
  Assert(jointype == JOIN_INNER); /* mustn't postpone past outer join */

Even if we ignore these assertion checks, in the final plan we would
have to access the RHS of the B/C semi join, i.e. C, to evaluate qual
'c.j = a.j' at the join level of A/BC join, which is wrong.

Thanks
Richard

pgsql-hackers by date:

Previous
From: Daniel Gustafsson
Date:
Subject: Re: TAP output format in pg_regress
Next
From: Tomas Vondra
Date:
Subject: Re: POC: GROUP BY optimization