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 1uBJ5w-0099lN-9Q for pgsql-bugs@arkaria.postgresql.org; Sat, 03 May 2025 20:04:20 +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 1uBJ5t-00G6pN-RR for pgsql-bugs@arkaria.postgresql.org; Sat, 03 May 2025 20:04:18 +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 1uBJ5t-00G6pF-JR for pgsql-bugs@lists.postgresql.org; Sat, 03 May 2025 20:04:18 +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.96) (envelope-from ) id 1uBJ5r-0000K1-1v for pgsql-bugs@lists.postgresql.org; Sat, 03 May 2025 20:04:18 +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 543K4EPp2202694; Sat, 3 May 2025 16:04:14 -0400 From: Tom Lane To: Abdullah DURSUN cc: pgsql-bugs@lists.postgresql.org Subject: Re: Planner does not use btree index for LIKE 'prefix%' on text column, but does for equivalent range query (PostgreSQL 17.4) In-reply-to: References: Comments: In-reply-to Abdullah DURSUN message dated "Sat, 03 May 2025 19:50:00 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2202692.1746302653.1@sss.pgh.pa.us> Date: Sat, 03 May 2025 16:04:14 -0400 Message-ID: <2202693.1746302654@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Abdullah DURSUN writes: > A btree index on a text column is not used for a query of the form WHERE > col LIKE 'prefix%', even though the index is healthy, statistics are > correct, and the query is highly selective. LIKE with a prefix condition can only use an index if the index's collation is "C", or if it uses the "pattern" opclass. This isn't new in v17, it's been true for a very long time. d1=# create table foo (t text); CREATE TABLE d1=# create index on foo (t); CREATE INDEX d1=# explain select * from foo where t like 'prefix%'; QUERY PLAN ----------------------------------------------------- Seq Scan on foo (cost=0.00..27.00 rows=7 width=32) Filter: (t ~~ 'prefix%'::text) (2 rows) d1=# create index on foo (t collate "C"); CREATE INDEX d1=# explain select * from foo where t like 'prefix%'; QUERY PLAN ------------------------------------------------------------------------- Bitmap Heap Scan on foo (cost=4.22..14.37 rows=7 width=32) Filter: (t ~~ 'prefix%'::text) -> Bitmap Index Scan on foo_t_idx1 (cost=0.00..4.22 rows=7 width=0) Index Cond: ((t >= 'prefix'::text) AND (t < 'prefiy'::text)) (4 rows) regards, tom lane