Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WaUWd-0004MS-FD for pgsql-bugs@arkaria.postgresql.org; Wed, 16 Apr 2014 18:25:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WaUWc-0005ax-2M for pgsql-bugs@arkaria.postgresql.org; Wed, 16 Apr 2014 18:25:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WaTVH-0004MX-4s for pgsql-bugs@postgresql.org; Wed, 16 Apr 2014 17:20:07 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WaTVD-0008Nb-7o for pgsql-bugs@postgresql.org; Wed, 16 Apr 2014 17:20:06 +0000 MIME-Version: 1.0 Subject: BUG #10051: Documentation should explain use of LOWER(X) text_pattern_ops indexes with LIKE to replace ILIKE To: pgsql-bugs@postgresql.org From: greenreaper@hotmail.com Date: Wed, 16 Apr 2014 17:19:20 +0000 Message-ID: <20140416171920.17202.25766@wrigleys.postgresql.org> X-Pg-Spam-Score: -0.1 (/) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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