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 1ja9sn-0008O5-PE for pgsql-general@arkaria.postgresql.org; Sun, 17 May 2020 03:22:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ja9sl-0008Uy-AL for pgsql-general@arkaria.postgresql.org; Sun, 17 May 2020 03:22:31 +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 1ja9sl-0008Ur-2N for pgsql-general@lists.postgresql.org; Sun, 17 May 2020 03:22:31 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ja9sc-0000RH-Kh for pgsql-general@lists.postgresql.org; Sun, 17 May 2020 03:22:29 +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 04H3MITu031576; Sat, 16 May 2020 23:22:18 -0400 From: Tom Lane To: =?UTF-8?B?xYF1a2FzeiBExIViZWs=?= cc: pgsql-general@lists.postgresql.org Subject: Re: Using b-tree index for >= condition when joining In-reply-to: References: Comments: In-reply-to =?UTF-8?B?xYF1a2FzeiBExIViZWs=?= message dated "Sat, 16 May 2020 18:56:20 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <31574.1589685738.1@sss.pgh.pa.us> Date: Sat, 16 May 2020 23:22:18 -0400 Message-ID: <31575.1589685738@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =?UTF-8?B?xYF1a2FzeiBExIViZWs=?= writes: > I am having a problem with nudging postgres to choose a good plan for > a query involving a left join and an inequality constraint on a column > with b-tree index. > ... > It looks like the inequality on date isn't pushed down below the left > join? Nope. The planner only derives implied conditions from equality clauses. There've been discussions about that in the past, but it was (and remains) unclear that trying to account for other clause types would be a net win. The planner-cycles-expended versus number-of-queries-improved tradeoff doesn't look promising. > I can get the plan I'd like to have by putting the same > constraint on the date column on the second table: Note that you're not really getting the same plan that way: it's not a left join anymore, because you put a strict constraint on the join's inner relation, so the planner realizes it doesn't have to produce any null-extended rows. You could make it work with the desired semantics with something along the lines of SELECT * FROM tbl1 t1 LEFT JOIN (select * from tbl2 where tbl2.date >= '2019-04-21') t2 USING (date) WHERE t1.date >= '2019-04-21'; but of course that's even less easy :-( regards, tom lane