From: Tom Lane <tgl@sss.pgh.pa.us>
To: ldh@laurent-hasson.com <ldh@laurent-hasson.com>
Cc: Marc Mamin <M.Mamin@intershop.de>
Cc: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Re: Inlining of functions (doing LIKE on an array)
Date: Fri, 11 Nov 2016 11:46:24 -0500
Message-ID: <29682.1478882784@sss.pgh.pa.us> (raw)
In-Reply-To: <35BE9BD1DD4DD444B0EDE3721E2AEA860115D550@P3PWEX4MB001.ex4.secureserver.net>
References: <35BE9BD1DD4DD444B0EDE3721E2AEA860115CB08@P3PWEX4MB001.ex4.secureserver.net>
<B6F6FD62F2624C4C9916AC0175D56D88421F197B@jenmbs01.ad.intershop.net>
<35BE9BD1DD4DD444B0EDE3721E2AEA860115D550@P3PWEX4MB001.ex4.secureserver.net>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
"ldh@laurent-hasson.com" <ldh@laurent-hasson.com> writes:
> I tried "exists", but won't work in the Function, i.e.,
> CREATE OR REPLACE FUNCTION ArrayLike(text[], text) RETURNS bigint
> AS 'exists (select * from unnest($1) a where a like $2)'
> LANGUAGE SQL STRICT IMMUTABLE LEAKPROOF
Syntax and semantics problems. This would work:
regression=# CREATE OR REPLACE FUNCTION ArrayLike(text[], text) RETURNS bool
regression-# as 'select exists (select * from unnest($1) a where a like $2)'
regression-# LANGUAGE SQL STRICT IMMUTABLE;
CREATE FUNCTION
regression=# create table tt (f1 text[]);
CREATE TABLE
regression=# explain select * from tt where ArrayLike(f1, 'foo');
QUERY PLAN
-------------------------------------------------------
Seq Scan on tt (cost=0.00..363.60 rows=453 width=32)
Filter: arraylike(f1, 'foo'::text)
(2 rows)
But we don't inline SQL functions containing sub-selects, so you're still
stuck with the rather high overhead of a SQL function. A plpgsql function
might be a bit faster:
CREATE OR REPLACE FUNCTION ArrayLike(text[], text) RETURNS bool
as 'begin return exists (select * from unnest($1) a where a like $2); end'
LANGUAGE plpgSQL STRICT IMMUTABLE;
BTW, I'd be pretty suspicious of marking this function leakproof,
because the underlying LIKE operator isn't leakproof according to
pg_proc.
> It's as expected though. As for the GIN indices, I tried and it didn't make a difference, which I guess is expected as well because of the Like operator. I don't expect regular indices to work on regular columns for Like operations, especially '%xxx' ones, so I didn't expect GIN indices to work either for Array columns with Like. Am I wrong?
Plain GIN index, probably not. A pg_trgm index could help with LIKE
searches, but I don't think we have a variant of that for array columns.
Have you considered renormalizing the data so that you don't have
arrays?
regards, tom lane
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: tgl@sss.pgh.pa.us, ldh@laurent-hasson.com, M.Mamin@intershop.de
Subject: Re: Inlining of functions (doing LIKE on an array)
In-Reply-To: <29682.1478882784@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox