Re: expression index not used within function - Mailing list pgsql-general

From Albe Laurenz
Subject Re: expression index not used within function
Date
Msg-id A737B7A37273E048B164557ADEF4A58B17C5C8F7@ntex2010i.host.magwien.gv.at
Whole thread Raw
In response to Re: expression index not used within function  (LPlateAndy <andy@centremaps.co.uk>)
List pgsql-general
LPlateAndy wrote:
> Just wondering what kind of execute statement (within a function) i should
> use to force the planner to use the index for the following?:

You cannot force anything.
The best you can do is to provide an index that *can* be used
and keep your statistics accurate.

> SELECT pcode searchmatch, geometry  FROM postcode
> WHERE (replace(lower(pcode), ' '::text, ''::text)) LIKE
> (replace((lower($1)::text),' '::text,''::text)||'%'::text)

I assume that pcode is of type text.

In that case you could create an index like

CREATE INDEX my_index ON table_name ((replace(lower(pcode), ' '::text, ''::text)) text_pattern_ops);
ANALYZE table_name;

Such an index can be used for queries with a LIKE, if you
have a constant on the right hand side that does not
start with a wildcard.

If you have PostgreSQL 9.2 or later, that might work out of
the box in a PL/pgSQL function.

In doubt, or if you have an older version, first compute
the right hand side and run the query with EXECUTE.

Yours,
Laurenz Albe

pgsql-general by date:

Previous
From: Karsten Hilbert
Date:
Subject: pg_upgrade ?deficiency
Next
From: Pavel Stehule
Date:
Subject: Re: ERROR: out of memory DETAIL: Failed on request of size ???