subquery pullup misses lateral refs in join alias Vars

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

subquery pullup misses lateral refs in join alias Vars

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
Вложения:
is_simple_subquery() refuses to pull up a LATERAL subquery whose
targetlist or quals reference rels outside the lowest outer join above
it.  But pull_up_simple_subquery() rechecks this before flattening
join alias Vars in the subquery, so a lateral reference hidden in a
join Var goes unnoticed.  Both queries below hit Assert("sjinfo ==
NULL") in distribute_qual_to_rels():

create table t (a int);

-- hidden in the subquery's targetlist
select 1 from t t1,
  lateral (select (j is null)::int
           from ((select t1.a) s left join (select 1) v on false) j) ss(x)
  left join t t2 on ss.x = t2.a;

-- hidden in the subquery's quals
select 1 from t t1,
  t t2 left join
  lateral (select 1 from ((select t1.a) s left join (select 1) v on false) j
           where length(j::text) > 3) ss on true;

Without asserts, the second fails with "wrong phnullingrels".

I think we should flatten join alias Vars in the subquery's targetlist
and quals before the recheck.  Attached is a WIP patch doing that.

- Richard

Re: subquery pullup misses lateral refs in join alias Vars

От:
Tatsuya Kawata <kawatatatsuya0913@gmail.com>
Дата:
Hi!

> I think we should flatten join alias Vars in the subquery's targetlist
> and quals before the recheck.  Attached is a WIP patch doing that.

I applied v1 and confirmed it works correctly. I also tested the
same master without v1. Without assertions, I found a pattern
where a query silently returns a wrong result rather than hitting
the Assert, so I wanted to share it. I think this warrants a back-patch.

=== Example
  create table t (a int);
  insert into t values (1),(2),(3),(NULL);

  select t1.a, ss.x, t2.a
  from t t1,
    lateral (select (j is null)::int
             from ((select t1.a) s left join (select 1) v on false) j)
      ss(x)
    left join t t2 on ss.x = t2.a;

This returns 1 row, but the correct answer is 4 rows. Unmatched rows
were being discarded. The wrong result seems to show up when both of these hold:
  - a column that the LATERAL subquery itself outputs is used in the
    ON clause of an outer join that has that LATERAL subquery on one side
  - that join has rows with no match
With this patch, these cases seem to be covered too.

Regards,
Tatsuya Kawata

Re: subquery pullup misses lateral refs in join alias Vars

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Tue, Sep 22, 2026 at 3:08 PM Tatsuya Kawata
 wrote:
> I applied v1 and confirmed it works correctly. I also tested the
> same master without v1. Without assertions, I found a pattern
> where a query silently returns a wrong result rather than hitting
> the Assert, so I wanted to share it. I think this warrants a back-patch.

>   select t1.a, ss.x, t2.a
>   from t t1,
>     lateral (select (j is null)::int
>              from ((select t1.a) s left join (select 1) v on false) j)
>       ss(x)
>     left join t t2 on ss.x = t2.a;

Thanks for looking at this.  This query seems to be the same as the
first query in my initial report (with a different targetlist) and has
already been covered in the test cases of v1 patch.

- Richard


Re: subquery pullup misses lateral refs in join alias Vars

От:
Tatsuya Kawata <kawatatatsuya0913@gmail.com>
Дата:
Hi!

> Thanks for looking at this.  This query seems to be the same as the
> first query in my initial report (with a different targetlist) and has
> already been covered in the test cases of v1 patch.

Sorry for the confusion. Yes, it's the same query as yours. I just
wanted to note that without asserts it silently returns wrong results
(1 row instead of 4) rather than raising an error, in case that's
useful for the back-patch decision.

Regards,
Tatsuya Kawata

Re: subquery pullup misses lateral refs in join alias Vars

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Thu, Oct 1, 2026 at 8:43 PM Tatsuya Kawata
 wrote:
> Sorry for the confusion. Yes, it's the same query as yours. I just
> wanted to note that without asserts it silently returns wrong results
> (1 row instead of 4) rather than raising an error, in case that's
> useful for the back-patch decision.

Thanks!  Yeah, this warrants a back-patch.  I've pushed the fix and
back-patched it to all supported branches.

- Richard


FAQ