Re: Index on VARCHAR with text_pattern_ops inside PL/PGSQL procedure. - Mailing list pgsql-performance

From Richard Huxton
Subject Re: Index on VARCHAR with text_pattern_ops inside PL/PGSQL procedure.
Date
Msg-id 475D3947.7000107@archonet.com
Whole thread Raw
In response to Index on VARCHAR with text_pattern_ops inside PL/PGSQL procedure.  (Piotr Gasidło <quaker@barbara.eu.org>)
List pgsql-performance
Piotr Gasidło wrote:
> Some performance loss, but OK. Now I've changed "=" into "LIKE" to use
> users_user_name_unique_text_pattern_ops index and rerun query:
>
> explain analyze select user_login('quaker');
>
>                                       QUERY PLAN
> --------------------------------------------------------------------------------------
>
>  Result  (cost=0.00..0.01 rows=1 width=0) (actual time=41.606..41.608
> rows=1 loops=1)
>  Total runtime: 41.629 ms
> (2 rows)
>
> Second run give 61.061 ms. So no improvements.
>
> Why PL/PGSQL is unable to proper utilize
> users_user_name_unique_text_pattern_ops?

It plans the query just once for the pl/pgsql function. That means it
doesn't know whether you are passing in a name '%foo' which can't use
the index. Since only one plan can be used it has to use a scan of the
table.

You can use EXECUTE to get plpgsql to plan the query each time it is
called. That should let it recognise that it can use the index (if it
can, of course).

--
   Richard Huxton
   Archonet Ltd

pgsql-performance by date:

Previous
From: Piotr Gasidło
Date:
Subject: Index on VARCHAR with text_pattern_ops inside PL/PGSQL procedure.
Next
From: "Pavel Stehule"
Date:
Subject: Re: Index on VARCHAR with text_pattern_ops inside PL/PGSQL procedure.