Re: [BUG] Remove self joins causes 'variable not found in subplan target lists' error

From: Andrei Lepikhov <lepihov(at)gmail(dot)com>
To: Richard Guo <guofenglinux(at)gmail(dot)com>, Sergey Soloviev <sergey(dot)soloviev(at)tantorlabs(dot)ru>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [BUG] Remove self joins causes 'variable not found in subplan target lists' error
Date: 2025-08-25 13:59:49
Message-ID: 21bed061-9907-4c40-9a76-378dcb6304c3@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 25/8/2025 15:28, Richard Guo wrote:
> On Sat, Aug 23, 2025 at 12:27 AM Sergey Soloviev
> <sergey(dot)soloviev(at)tantorlabs(dot)ru> wrote:
>> I would like write a test in 'join.sql', but for now it requires patches
>> to easily reproduce the bug. I appreciate it if someone could find
>> an easier way to reproduce the bug and write a simple test.
>
> Nice catch! Here's a query that reproduces the error without needing
> to hack the code.
>
> create table t (a int, b int);
> create unique index on t (a);
>
> select t1.a from t t1
> left join t t2 on t1.a = t2.a
> join t t3 on true
> where exists (select 1 from t t4
> join t t5 on t4.b = t5.b
> join t t6 on t5.b = t6.b
> where t1.a = t4.a and t3.a = t5.a and t4.a = 2);
> ERROR: variable not found in subplan target lists
Thanks for your reproduction.
Unfortunately, it works only in the absence of an ANALYZE, like the
original example.
Also, I would say it is not a self-join-related issue. This example
employs the removal of the 'unnecessary left join'. Currently, I'm
unsure why this example causes the issue: the removing t2 table
shouldn't have any references in ECs within the EXISTS part.

--
regards, Andrei Lepikhov

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Mihail Nikalayeu 2025-08-25 14:15:16 Re: Adding REPACK [concurrently]
Previous Message Greg Sabino Mullane 2025-08-25 13:38:16 Re: Making WAL archiving faster — multi-file support and async ideas