Planner does not use btree index for LIKE 'prefix%' on text column, but does for equivalent range query (PostgreSQL 17.4)

From: Abdullah DURSUN <adursuns(at)gmail(dot)com>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Planner does not use btree index for LIKE 'prefix%' on text column, but does for equivalent range query (PostgreSQL 17.4)
Date: 2025-05-03 16:50:00
Message-ID: CADgr+dF2yaJ88e6i=PU+znW3YX7uir2nL56NpoOzuk87-nk4wg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hello PostgreSQL team,

I have encountered what appears to be a planner regression or bug in
PostgreSQL 17.4 regarding the use of btree indexes for prefix searches with
LIKE 'prefix%'.

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.

The same index is used and performs well for an equivalent range query
(WHERE col >= 'prefix' AND col < 'nextprefix').

- This behavior persists even after VACUUM FULL, ANALYZE, and index
re-creation.
- The column and index use the default collation (en_US.UTF-8).
- The index is valid and ready.
- The planner does not use the index for LIKE 'prefix%', but does for the
equivalent range.
- Rewriting the query as a range (col >= 'prefix' AND col < 'nextprefix')
uses the index and is fast, but this should not be necessary.

Thank you for your attention!

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Sergey Koposov 2025-05-03 16:52:21 Re: BUG #18909: Query creates millions of temporary files and stalls
Previous Message Tom Lane 2025-05-03 16:27:06 Re: BUG #18909: Query creates millions of temporary files and stalls