Re: obtaining ARRAY position for a given match

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Sam Mason <sam(at)samason(dot)me(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: obtaining ARRAY position for a given match
Date: 2009-11-19 19:02:41
Message-ID: 162867790911191102i509e6f3dg174891a5abe71452@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>
> CREATE OR REPLACE FUNCTION idx(anyarray, anyelement)
> RETURNS int AS $$
> SELECT i
>   FROM (SELECT generate_subscripts($1) as i, unnest($1) as v) s
>  WHERE v = $2
>  LIMIT 1;
> $$ LANGUAGE sql;
>

there is bug

correct is

create or replace function idx(anyarray, anyelement)
returns int as $$
select i
from (select generate_subscripts($1,1) i, unnest($1) v) s
where v = $2
limit 1
$$ language sql;

and it is 5% faster than older version

on integer array on my eeepc

Regards
Pavel

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sam Mason 2009-11-19 19:03:01 Re: obtaining ARRAY position for a given match
Previous Message Scott Bailey 2009-11-19 18:59:50 Possible bug with array_agg