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

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Sergey Soloviev <sergey(dot)soloviev(at)tantorlabs(dot)ru>, Alexander Korotkov <aekorotkov(at)gmail(dot)com>, 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-28 06:18:53
Message-ID: CAMbWs49113e8NsU79kyxhnOeWBe5cWmkJo377-LpU8yX_JxndQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Aug 28, 2025 at 6:42 AM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> This leaves us with some pretty unappetizing choices about what
> to do in v18:
>
> 1. Try to emulate the proposed HEAD fix.
>
> 2. Try to fix up the SJE patch so that it calculates relid changes
> honestly, or at least no less honestly than what happened before
> a3179ab69.
>
> 3. Revert SJE as well as a3179ab69 in v18.
>
> I will have a look at #1. If there's somebody who wants to look
> at #2, feel free, but it won't be me because I don't understand
> the SJE patch well enough. Either way, it's not great to be
> doing stuff like this just days before rc1 ...

I took a look at #2. I don't have a good understanding of the SJE
patch either, so I might be missing something.

The core dump you mentioned can be reproduced with this query.

create table sj (a int unique, b int);

explain (verbose, costs off)
select t3.a from sj t1
join sj t2 on t1.a = t2.a
join lateral (select t1.a offset 0) t3 on true;

The reason is that remove_rel_from_query() does not update baserels'
lateral_vars lists and thus there are still references to the removed
relid there.

After fixing that issue, another error occurs during the regression
tests.

explain (costs off) select 1 from
(sk k1 join sk k2 on k1.a = k2.a)
join (sj j1 join sj j2 on j1.a = j2.a) on j1.b = k1.b;
ERROR: variable not found in subplan target lists

The reason is that remove_rel_from_query() removes the old relid from
the attr_needed arrays but fails to add the substitute relid to them.

Hence, attached is the fix for SJE after reverting a3179ab69. With
it, all regression tests pass.

Thanks
Richard

Attachment Content-Type Size
fix-SJE-after-revert-a3179ab69.patch application/octet-stream 1.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Hayato Kuroda (Fujitsu) 2025-08-28 06:49:13 doc patch: missing tags in protocol.sgml
Previous Message Zhijie Hou (Fujitsu) 2025-08-28 06:17:23 RE: Fix replica identity checks for MERGE command on published table.