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 1s5eC3-0074fn-TO for pgsql-general@arkaria.postgresql.org; Sat, 11 May 2024 04:18:43 +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 1s5eC1-003MAy-Oe for pgsql-general@arkaria.postgresql.org; Sat, 11 May 2024 04:18: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 1s5eC1-003MAq-EL for pgsql-general@lists.postgresql.org; Sat, 11 May 2024 04:18:41 +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 1s5eBy-000SPw-A6 for pgsql-general@lists.postgresql.org; Sat, 11 May 2024 04:18:41 +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 44B4IabF1715349; Sat, 11 May 2024 00:18:36 -0400 From: Tom Lane To: Ron Johnson cc: "pgsql-generallists.postgresql.org" Subject: Re: Unnecessary buffer usage with multicolumn index, row comparison, and equility constraint In-reply-to: References: Comments: In-reply-to Ron Johnson message dated "Sat, 11 May 2024 00:05:22 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1715347.1715401116.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 11 May 2024 00:18:36 -0400 Message-ID: <1715348.1715401116@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Ron Johnson writes: > On Fri, May 10, 2024 at 11:28=E2=80=AFPM WU Yan <4wuyan@gmail.com> wrote= : >> Simple query that uses the multicolumn index. >> postgres=3D# explain (analyze, buffers) select * from t where row(a, b)= > >> row(123450, 123450) and a =3D 0 order by a, b; > Out of curiosity, why "where row(a, b) > row(123450, 123450)" instead of= "where > a > 123450 and b > 123450"? That row() condition actually means "a > 123450 OR (a =3D 123450 AND b > 123450)", which is not the same. (It'd be a little clearer with two different values in the row constant, perhaps.) It does seem like there's an optimization failure here. I don't expect btree to analyze row comparisons exactly, but it's sad that it seems to be stupider than for the simplified case explain (analyze, buffers) select * from t where a >=3D 123450 and a =3D 0 order by a, b; QUERY PLAN = = --------------------------------------------------------------------------= ------------------------------------- Index Only Scan using my_idx on t (cost=3D0.43..4.45 rows=3D1 width=3D8)= (actual time=3D0.001..0.002 rows=3D0 loops=3D1) Index Cond: ((a >=3D 123450) AND (a =3D 0)) Heap Fetches: 0 Planning: Buffers: shared hit=3D4 Planning Time: 0.081 ms Execution Time: 0.013 ms (7 rows) For that, it's able to see that the index conditions are contradictory, so it fetches no index pages whatever. regards, tom lane