Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g2OMz-0002vJ-VS for pgsql-performance@arkaria.postgresql.org; Tue, 18 Sep 2018 22:21:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g2OMx-0000TQ-Vz for pgsql-performance@arkaria.postgresql.org; Tue, 18 Sep 2018 22:21:19 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g2OMx-0000TJ-Ko for pgsql-performance@lists.postgresql.org; Tue, 18 Sep 2018 22:21:19 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g2OMu-0006Z2-GC for pgsql-performance@lists.postgresql.org; Tue, 18 Sep 2018 22:21:17 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w8IMLE5X004692; Tue, 18 Sep 2018 18:21:14 -0400 From: Tom Lane To: Nicolas Paris cc: pgsql-performance@lists.postgresql.org Subject: Re: LEFT JOIN LATERAL optimisation at plan time In-reply-to: <20180918070636.tp243ftlwpzrfrsd@riseup.net> References: <20180918070636.tp243ftlwpzrfrsd@riseup.net> Comments: In-reply-to Nicolas Paris message dated "Tue, 18 Sep 2018 09:06:36 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4690.1537309274.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 18 Sep 2018 18:21:14 -0400 Message-ID: <4691.1537309274@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Nicolas Paris writes: > For a traditional LEFT JOIN, in case the SELECT does not mention a field > from a joined table being unique , the planner removes the join. Eg: > SELECT a, b --,c > FROM table1 > LEFT JOIN (select a, c from table2 group by a) joined USING (a) > However this behavior is not the same for LATERAL JOINS > SELECT a, b --,c > FROM table1 > LEFT JOIN LATERAL (select a, c from table2 where table1.a =3D table2.a g= roup by a) joined ON TRUE The way you've set that up, the constraint required to deduce uniqueness (i.e. the table1.a =3D table2.a clause) is hidden inside a non-trivial subquery; and, where it's placed, it isn't actually guaranteeing anything so far as the inner query is concerned, ie the select from table2 could easily return multiple rows. I'm not too surprised that the outer planner level doesn't make this deduction. > In this case, the planner still consider the joined table. My guess is > it could remove it . It looks to me like it would require a substantial amount of additional code and plan-time effort to find cases like this. I'm not convinced that the cost-benefit ratio is attractive. Maybe in some hypothetical future where we're able to flatten sub-selects even though they contain GROUP BY, it would get easier/cheaper to detect this case. But that's just pie in the sky at the moment. regards, tom lane