Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rB4Z0-009KdO-FL for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Dec 2023 02:56:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rB4Xw-00ERSS-U3 for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Dec 2023 02:55:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rB4Xw-00ERSJ-HG for pgsql-bugs@lists.postgresql.org; Thu, 07 Dec 2023 02:55:28 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rB4Xm-009BAT-2P for pgsql-bugs@lists.postgresql.org; Thu, 07 Dec 2023 02:55:24 +0000 Received: from [10.10.20.150] (node-15h0.pool-118-172.dynamic.totinternet.net [118.172.209.244]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: a.lepikhov@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id 44081E20FD5; Thu, 7 Dec 2023 05:55:12 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1701917713; bh=btNcQYst7nFcCnur+FBLrSJFw5YM3PTsCXI+HY3Ow0U=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=VwdqPiQ8hL3yp+Tm2sxl//nDQfFUCFMGAyObJTERuJqIbY/EYZnFIb+GpNW2mcHVH fUhTjaEU0y1QzuC8bLsXVbUb5SszO2flgoHh7/e0Ez0cC/bul61yk1H54aFGdyu516 8v3CLGROomF+t8bHI23zxaREJozkIiJPOSQsglTgCV5dQkDv+0UhS+9F63YaZ4tFgy wZ8tV4cCdRgMM8ygzI1SOmX0J7XF4k7AymRw6m2KQb2LqTBPFZe0UTg13f6jwQzjVj 10sFs+itYNrQe16E36zWIMru+zL+pqwecnEy00ndFjpmP7BpiPyBZfc4PFlbU3OSNM dDPR5Cmj1RiMw== Message-ID: <543cdbff-3116-46a5-86e0-269c1087c602@postgrespro.ru> Date: Thu, 7 Dec 2023 09:55:08 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BUG #18187: Unexpected error: "variable not found in subplan target lists" triggered by JOIN To: Richard Guo , Alexander Korotkov , Tom Lane Cc: zuming.jiang@inf.ethz.ch, pgsql-bugs@lists.postgresql.org, PG Bug reporting form References: <18187-831da249cbd2ff8e@postgresql.org> <491881ef-f1c7-4a86-b5dd-dcad55a88ab4@postgrespro.ru> <3883031.1701106936@sss.pgh.pa.us> Content-Language: en-US From: Andrei Lepikhov Organization: Postgres Professional In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Nov 28, 2023 at 1:42 AM Tom Lane wrote: > :phnullingrels (b 5) > It's correct to move both phrels locations to rel 7 (the surviving > self-joined rel) but what became of the reference to nullingrel 5? > That seems clearly wrong, since we have not removed the left join. I must confess I couldn't reproduce this issue. I watched only the value ':phnullingrels (b)' everywhere. On 5/12/2023 09:35, Richard Guo wrote: > > On Tue, Nov 28, 2023 at 3:03 PM Richard Guo > wrote: > > On Tue, Nov 28, 2023 at 1:42 AM Tom Lane > wrote: > > Much worse, if you look around elsewhere in the data structure, > particularly at the processed_tlist, you find instances of the > PlaceHolderVars that have not been updated and still have the > old values such as ":phrels (b 6)". > > > Good catch!  We fail to handle PlaceHolderVar case in sje_walker and > replace_varno_walker, which is wrong.  Fixed in v3 patch. Yes, it was my blunder. I didn't recognize that phinfo->ph_var points to a copy of the PlaceHolderVar and thought it was enough to pass through the root->placeholder_list only. Adding replacement code into the walker is the proper correction. > I noticed that this issue exists on master even without the > Don't-constrain-SJE-due-to-PHVs patch, and it can be illustrated with a > query from regression test. > > -- Test that placeholders are updated correctly after join removal > explain (costs off) > select * from (values (1)) x > left join (select coalesce(y.q1, 1) from int8_tbl y >     right join sj j1 inner join sj j2 on j1.a = j2.a >     on true) z > on true; > Also, it seems to me that sje_walker is redundant.  Currently it is used > to walk the query tree for varno replacement, a task that can be > fulfilled with replace_varno_walker. Agree. >> It's not apparent to me why the cross-checks we have in setrefs.c >> and elsewhere don't detect a problem with this. But it seems >> clear that remove_useless_self_joins is still several bricks >> shy of a load in terms of fully updating the data structure for a >> join removal. Probably with some more work to add complication >> to this test case, we could demonstrate an observable failure. > Fair point. I have the same concern that we still have other loose > ends > in SJE updating necessary data structures in self join removal. And I > noticed that Andres expressed the same concern in [1]. I think we > should come up with some solution to detect/avoid such problems, but I > imagine that that should be a separate patch. It is an excellent task to ponder. At least, we should answer later why the setrefs.c didn't detect that, and could we do something here. Maybe all we could do - is add comments to the PlannerInfo structure. According to the patches: 0001 - looks good for me. 0002 - I don't understand why to use 'explain' in VERBOSE mode in tests. What do you try to detect here? 0003 - ok, but too short. Maybe squash all these patches into one? I think these corrections are good enough for commit. -- regards, Andrei Lepikhov Postgres Professional