From: Andrei Lepikhov <a.lepikhov@postgrespro.ru>
To: Richard Guo <guofenglinux@gmail.com>
Cc: zuming.jiang@inf.ethz.ch, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>
Subject: Re: BUG #18187: Unexpected error: "variable not found in subplan target lists" triggered by JOIN
Date: Thu, 9 Nov 2023 11:51:14 +0700
Message-ID: <e7f740cd-fcc8-4022-bb12-bb0f5f736ba6@postgrespro.ru> (raw)
In-Reply-To: <CAMbWs4_yrqMYXd+zXc4Y6KjnyA8xPZE6aiAW6k2Y10WaNhwRhw@mail.gmail.com>
References: <18187-831da249cbd2ff8e@postgresql.org>
<491881ef-f1c7-4a86-b5dd-dcad55a88ab4@postgrespro.ru>
<CAMbWs4_yrqMYXd+zXc4Y6KjnyA8xPZE6aiAW6k2Y10WaNhwRhw@mail.gmail.com>
On 9/11/2023 10:54, Richard Guo wrote:
>
> On Wed, Nov 8, 2023 at 11:42 PM Andrei Lepikhov
> <a.lepikhov@postgrespro.ru <mailto:a.lepikhov@postgrespro.ru>> wrote:
>
> The issue looks like previous one, related to new SJE feature.
>
> Reduced case:
> CREATE TABLE t2 (vkey int4, c9 text, primary key(vkey));
> SELECT * FROM (
> SELECT CASE WHEN '1' = ref_0.c9 THEN 1 ELSE 1 END AS c_3
> FROM t2 as ref_0
> JOIN t2 AS ref_1
> ON ref_0.vkey = ref_1.vkey
> RIGHT OUTER JOIN t2 AS ref_2
> ON ref_1.vkey = ref_2.vkey) AS t5
> RIGHT OUTER JOIN (SELECT 1 AS c_2) AS t4
> ON t4.c_2 IS NOT NULL;
>
> The key problem lies in the 'CASE' statement.
>
>
> I've looked into this a little bit. I think it's caused by the SJE
> logic not properly removing references from PHVs. Specifically, it
> fails to replace the ref_0's Vars within phv->phexpr, leading them to be
> added in ref_2/ref_1 join's targetlist.
>
> Also, I noticed that in remove_rel_from_query() we perform replace_relid
> for phv->phrels twice at line 475 and 478, which seems not right to me.
>
> 475 phv->phrels = replace_relid(phv->phrels, relid, subst);
> 476 phv->phrels = replace_relid(phv->phrels, ojrelid, subst);
> 477 phinfo->ph_lateral = replace_relid(phinfo->ph_lateral, relid,
> subst);
> 478 phinfo->ph_var->phrels = replace_relid(phinfo->ph_var->phrels,
> relid, subst);
Thanks a lot!
Your patch looks correct. We really have been missing replacing the
PlaceHolderVar expression references for all the development time.
The test in join.sql works correctly. Should we add a reference to the
bug that triggered the issue as a comment to the test? Also, to be sure,
maybe add column t4.code into the list of the coalesce parameters?
--
regards,
Andrei Lepikhov
Postgres Professional
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: a.lepikhov@postgrespro.ru, guofenglinux@gmail.com, noreply@postgresql.org
Subject: Re: BUG #18187: Unexpected error: "variable not found in subplan target lists" triggered by JOIN
In-Reply-To: <e7f740cd-fcc8-4022-bb12-bb0f5f736ba6@postgrespro.ru>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox