Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pOP9A-0003cY-25 for pgsql-bugs@arkaria.postgresql.org; Sat, 04 Feb 2023 20:28:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pOP98-0006A0-0D for pgsql-bugs@arkaria.postgresql.org; Sat, 04 Feb 2023 20:28:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pOP97-000693-Nk for pgsql-bugs@lists.postgresql.org; Sat, 04 Feb 2023 20:28:25 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pOP95-0003yP-Ai for pgsql-bugs@lists.postgresql.org; Sat, 04 Feb 2023 20:28:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 314KSKKU2187723; Sat, 4 Feb 2023 15:28:20 -0500 From: Tom Lane To: Richard Guo cc: tharakan@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17769: Assert triggered in indxpath.c In-reply-to: References: <17769-e4f7a5c9d84a80a7@postgresql.org> Comments: In-reply-to Richard Guo message dated "Fri, 03 Feb 2023 14:10:33 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2187721.1675542500.1@sss.pgh.pa.us> Date: Sat, 04 Feb 2023 15:28:20 -0500 Message-ID: <2187722.1675542500@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Richard Guo writes: > I believe what happens here is that after we pull up the LATERAL > subquery we would have a FromExpr with only one child. Then > remove_useless_result_rtes elides it and merges its qual, which is > 'n.nspname = c.relname', up to its parent. This qual is then handed to > EC machinery. > When it comes to remove_useless_joins, we try to remove the LEFT join. > We are supposed to remove any joinquals referencing rel 'n'. But here > remove_rel_from_query only checks rel->joininfo, and neglects to notice > that there is another joinqual referencing rel 'n' existing in EC. I > think this is where the problem arises. Actually, the problem is that we *shouldn't* try to remove the left join, because the reference to ref_0.schemaname is logically "above" that join. I messed up the test for that in join_is_removable(). Apparently, we had no test cases that would expose such a condition, probably because such a reference would typically cause strength-reducing the left join. > Another finding is that in remove_useless_result_rtes after we elide the > single-child FromExpr and merge its qual up to its parent, the LEFT join > is actually can be reduced to inner join. But we fail to perform that > since all this happens after we've done outer join reduction. Yeah. If we *did* do that then this bug would have escaped detection awhile longer :-(. I'm not terribly concerned about the lack of such an optimization, but maybe we can look at it someday. Fix pushed, thanks for investigating! regards, tom lane