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 1t9Bpt-008Iu3-N9 for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Nov 2024 23:22:45 +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 1t9Bpq-003lQ8-C1 for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Nov 2024 23:22:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t9Bpq-003lPs-1n for pgsql-bugs@lists.postgresql.org; Thu, 07 Nov 2024 23:22:42 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t9Bpm-000kna-OB for pgsql-bugs@lists.postgresql.org; Thu, 07 Nov 2024 23:22:42 +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 4A7NMbBr3515828; Thu, 7 Nov 2024 18:22:37 -0500 From: Tom Lane To: David Rowley cc: Mineharu Takahara , pgsql-bugs@lists.postgresql.org, "mtakahar@gmail.com" Subject: Re: Suboptimal query plans for BETWEEN SYMMETRIC operations In-reply-to: References: Comments: In-reply-to David Rowley message dated "Fri, 08 Nov 2024 11:58:07 +1300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3515826.1731021757.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 07 Nov 2024 18:22:37 -0500 Message-ID: <3515827.1731021757@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > On Fri, 8 Nov 2024 at 08:36, Mineharu Takahara = wrote: >> A condition: "col BETWEEN SYMMETRIC val1 AND val2" is currently rewritt= en to "(((col >=3D val1) AND (col <=3D val2)) OR ((col >=3D val2) AND (col= <=3D val1)))" that would lead to suboptimal plans using an extra Bitmap I= ndex Scan or Index Scan/Index Only Scan with the entire predicate placed i= n the "Filter" instead of "Index Cond". > This isn't a bug, it's just something that could perhaps be made more op= timal. Indeed. The trouble with the LEAST/GREATEST formulation is that it may result in different semantics in situations where val1 and val2 aren't the same type. Also, LEAST/GREATEST rely on the default btree opclass for the common type, which might not match the semantics of the comparison operators that the current coding chooses. There are ways around that --- one could be to transform to LEAST/GREATEST only when the arguments do resolve as the same type. And perhaps you could convince people that BETWEEN ought to depend on the default btree opclass not on operator names. But it's all a lot messier than you might think. > If you're interested in making improvements in this area for core > PostgreSQL, then pgsql-hackers is the place to discuss that. Yup. regards, tom lane