Re: ERROR: wrong varnullingrels (b 3) (expected (b)) for Var 2/1

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Markus Winand <markus(dot)winand(at)winand(dot)at>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: ERROR: wrong varnullingrels (b 3) (expected (b)) for Var 2/1
Date: 2023-05-31 06:36:45
Message-ID: CAMbWs49Eq8x9aR0M1fumb53c1soBozqwy0-sZtRJkm2tmOpWBQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, May 31, 2023 at 10:47 AM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:

> On Tue, May 30, 2023 at 10:48 PM Markus Winand <markus(dot)winand(at)winand(dot)at>
> wrote:
>
>> I found an error similar to others before ([1]) that is still persists as
>> of head right now (0bcb3ca3b9).
>>
>> CREATE TABLE t (
>> n INTEGER
>> );
>>
>> SELECT *
>> FROM (VALUES (1)) t(c)
>> LEFT JOIN t ljl1 ON true
>> LEFT JOIN LATERAL (WITH cte AS (SELECT * FROM t WHERE t.n = ljl1.n)
>> SELECT * FROM cte) ljl2 ON ljl1.n = 1;
>>
>> ERROR: wrong varnullingrels (b 3) (expected (b)) for Var 2/1
>>
>> Note that the error does **not** occur if the CTE is unwrapped like this:
>>
>> SELECT *
>> FROM (VALUES (1)) t(c)
>> LEFT JOIN t ljl1 ON true
>> LEFT JOIN LATERAL (SELECT * FROM t WHERE t.n = ljl1.n) ljl2 ON ljl1.n =
>> 1;
>
>
> Thanks for the report! Reproduced here. Also it can be reproduced with
> subquery, as long as the subquery is not pulled up.
>
> SELECT *
> FROM (VALUES (1)) t(c)
> LEFT JOIN t ljl1 ON true
> LEFT JOIN LATERAL (SELECT * FROM t WHERE t.n = ljl1.n offset 0) ljl2 ON
> ljl1.n = 1;
> ERROR: wrong varnullingrels (b 3) (expected (b)) for Var 2/1
>
> When we transform the first form of identity 3 to the second form, we've
> converted Pb*c to Pbc in deconstruct_distribute_oj_quals. But we
> neglect to consider that rel C might be a RTE_SUBQUERY and contains
> quals that have lateral references to B. So the B vars in such quals
> have wrong nulling bitmaps and we'd finally notice that when we do
> fix_upper_expr for the NestLoopParam expressions.
>

We can identify in which form of identity 3 the plan is built up by
checking the relids of the B/C join's outer rel. If it's in the first
form, the outer rel's relids must contain the A/B join. Otherwise it
should only contain B's relid. So I'm considering that maybe we can
adjust the nulling bitmap for nestloop parameters according to that.

Attached is a patch for that. Does this make sense?

Thanks
Richard

Attachment Content-Type Size
v1-0001-Fix-nulling-bitmap-for-nestloop-parameters.patch application/octet-stream 3.8 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Markus Winand 2023-05-31 07:03:56 Re: ERROR: wrong varnullingrels (b 3) (expected (b)) for Var 2/1
Previous Message David Rowley 2023-05-31 06:13:57 Re: [16Beta1][doc] pgstat: Track time of the last scan of a relation