Re: BUG #18187: Unexpected error: "variable not found in subplan target lists" triggered by JOIN

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Andrei Lepikhov <a(dot)lepikhov(at)postgrespro(dot)ru>
Cc: zuming(dot)jiang(at)inf(dot)ethz(dot)ch, pgsql-bugs(at)lists(dot)postgresql(dot)org, PG Bug reporting form <noreply(at)postgresql(dot)org>
Subject: Re: BUG #18187: Unexpected error: "variable not found in subplan target lists" triggered by JOIN
Date: 2023-11-09 03:54:08
Message-ID: CAMbWs4_yrqMYXd+zXc4Y6KjnyA8xPZE6aiAW6k2Y10WaNhwRhw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wed, Nov 8, 2023 at 11:42 PM Andrei Lepikhov <a(dot)lepikhov(at)postgrespro(dot)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);

Attached is a hotfix.

Thanks
Richard

Attachment Content-Type Size
v1-0001-Fix-how-SJE-removes-references-from-PHVs.patch application/octet-stream 3.2 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Andrei Lepikhov 2023-11-09 04:51:14 Re: BUG #18187: Unexpected error: "variable not found in subplan target lists" triggered by JOIN
Previous Message Andres Freund 2023-11-09 01:28:09 Re: Segfault when running postgres inside kubernetes with huge pages