Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 9564B1337B99 for ; Wed, 5 Jan 2011 13:51:49 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 65220-10 for ; Wed, 5 Jan 2011 17:51:49 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by mail.postgresql.org (Postfix) with ESMTP id DEE931337B7D for ; Wed, 5 Jan 2011 13:51:48 -0400 (AST) Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by wwwmaster.postgresql.org (8.14.3/8.14.3) with ESMTP id p05HplPc055553 for ; Wed, 5 Jan 2011 17:51:48 GMT (envelope-from www@wwwmaster.postgresql.org) Received: (from www@localhost) by wwwmaster.postgresql.org (8.14.3/8.14.3/Submit) id p05HplaM055549; Wed, 5 Jan 2011 17:51:47 GMT (envelope-from www) Date: Wed, 5 Jan 2011 17:51:47 GMT Message-Id: <201101051751.p05HplaM055549@wwwmaster.postgresql.org> To: pgsql-bugs@postgresql.org Subject: BUG #5816: index not used in function From: "frank" Content-Type: text/plain; charset=utf-8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 201101/43 X-Sequence-Number: 29065 The following bug has been logged online: Bug reference: 5816 Logged by: frank Email address: frank@ros-i.com PostgreSQL version: 8.3.7 Operating system: linux Description: index not used in function Details: Linux: Linux 2.6.24-24-server #1 SMP Tue Jul 7 20:21:17 UTC 2009 i686 GNU/Linux Postgres: "PostgreSQL 8.3.7 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)" Table Description: 1. table (say thisTable) with a column (say thisColumn) of varchar in mixed case data 2. w/o primary key on this column 3. an upper case index on this column with text_pattern_ops as the opclass Query Description: 1. select "thisColumn" from "thisTable" where upper("thisColumn") like 'ABC%' 2. select * from get_this_column('ABC') where get_this_column() is defined as: CREATE OR REPLACE FUNCTION get_this_column(c text) RETURNS SETOF text AS $BODY$ SELECT "thisColumn" FROM "thisTable" WHERE upper("thisColumn") like $1 limit 10; $BODY$ LANGUAGE 'sql' VOLATILE COST 100 ROWS 1000; ALTER FUNCTION get_this_column(text) OWNER TO postgres; Issue Description: The plain query (1) uses the upper case index. The function (2), even though having the same underlying query, does not seem to be able to use index. The wild card can either be in the argument or appended at the query inside the function, the result is the same. Further Question: Why can't force the use of index (even if the plan results in worse performance when user desires)? The index HINT (of ORACLE say) is a bad concept and a horrible design idea. The user should be allowed to force the use of any index regardless of performance. Anyway, the user should know what he/she is doing. Besides, an explain could show which, the system plan or the user's, is better. The combination described here is perhaps not normal. I have not tested it with any other version. Likely the same is true for all versions including 9. Regardless of resolution, I would appreciate a brief response. Thanks