Re: index not being used when variable is sent - Mailing list pgsql-performance

From Tom Lane
Subject Re: index not being used when variable is sent
Date
Msg-id 25601.1313502058@sss.pgh.pa.us
Whole thread Raw
In response to index not being used when variable is sent  (Eyal Wilde <eyal@impactsoft.co.il>)
Responses Re: index not being used when variable is sent
List pgsql-performance
Eyal Wilde <eyal@impactsoft.co.il> 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

pgsql-performance by date:

Previous
From: Eyal Wilde
Date:
Subject: index not being used when variable is sent
Next
From: Pin007
Date:
Subject: Re: Streaming replication performance