pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: greenreaper@hotmail.com
To: pgsql-bugs@postgresql.org
Subject: BUG #10051: Documentation should explain use of LOWER(X) text_pattern_ops indexes with LIKE to replace ILIKE
Date: Wed, 16 Apr 2014 17:19:20 +0000
Message-ID: <20140416171920.17202.25766@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      10051
Logged by:          Laurence Parry
Email address:      greenreaper@hotmail.com
PostgreSQL version: 9.3.4
Operating system:   Debain wheezy (Linux 3.2.0)
Description:        

Using ILIKE can be non-performant, e.g.
SELECT user_id FROM users WHERE username ILIKE 'Green%';
would result in a sequential scan on users.

http://www.postgresql.org/docs/current/static/indexes-types.html states that
it is not possible to accelerate a general ILIKE pattern with B-tree
indexes:
"It is also possible to use B-tree indexes for ILIKE and ~*, but only if the
pattern starts with non-alphabetic characters..."

However, it *is* possible to get general prefix-based case-insensitive
searches on B-tree-indexed text, varchar or bpchar columns by using
*_pattern_ops classes:

CREATE INDEX like_lowercase_usernames on users (LOWER(username)
text_pattern_ops);

SELECT user_id FROM users WHERE LOWER(username) LIKE LOWER('Green%');

This was ~200x faster than the ILIKE case for my workload.

I think this possibility should be mentioned in docs section 11.2 above, and
perhaps also at
http://www.postgresql.org/docs/current/static/indexes-opclass.html where
only LIKE is mentioned.

This trick comes up frequently in mailing lists, slides, guides, e.g.:
http://blog.2ndquadrant.com/text-search-strategies-in-postgresql/
http://postgresql.1045698.n5.nabble.com/GENERAL-indexes-for-ILIKE-td1857024.html
http://www.postgresql.org/message-id/16763.1277148967@sss.pgh.pa.us

Ideally ILIKE would not require this workaround - I expected LOWER indexes
to "just work" with ILIKE, as a variation of the support for LIKE. I don't
know how feasible this is, though (collation issues?). If implemented, this
should also be documented.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



Message-ID: <20140416171920.17202.25766@wrigleys.postgresql.org>
Permalink:  ../20140416171920.17202.25766@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20140416171920.17202.25766@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: greenreaper@hotmail.com
  Subject: Re: BUG #10051: Documentation should explain use of LOWER(X) text_pattern_ops indexes with LIKE to replace ILIKE
  In-Reply-To: <20140416171920.17202.25766@wrigleys.postgresql.org>
  References: <20140416171920.17202.25766@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox