Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 51873B5DBEE for ; Tue, 16 Aug 2011 10:41:15 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 22300-08 for ; Tue, 16 Aug 2011 13:41:02 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 6A622B5DBC4 for ; Tue, 16 Aug 2011 10:41:01 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p7GDewho025602; Tue, 16 Aug 2011 09:40:58 -0400 (EDT) To: Eyal Wilde cc: pgsql-performance@postgresql.org, erans@impactsoft.co.il Subject: Re: index not being used when variable is sent In-reply-to: References: Comments: In-reply-to Eyal Wilde message dated "Tue, 16 Aug 2011 07:30:18 +0300" Date: Tue, 16 Aug 2011 09:40:58 -0400 Message-ID: <25601.1313502058@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.888 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.988 X-Spam-Level: X-Archive-Number: 201108/169 X-Sequence-Number: 44616 Eyal Wilde writes: > CREATE OR REPLACE FUNCTION test_func(STR text) > ... > perform t1.val FROM t1 WHERE > (COALESCE(rpad(t1.val, 100),'') ) like COALESCE(STR || '%','') > order by COALESCE(rpad(t1.val, 100), '') using ~<~ LIMIT 5; [ doesn't use index ] No, it doesn't. The LIKE index optimization requires the LIKE pattern to be a constant at plan time, so that the planner can extract the pattern's fixed prefix. An expression depending on a function parameter is certainly not constant. If you really need this to work, you could use EXECUTE USING so that the query is re-planned for each execution. regards, tom lane