Re: obtaining ARRAY position for a given match - Mailing list pgsql-general

From Pavel Stehule
Subject Re: obtaining ARRAY position for a given match
Date
Msg-id 162867790911191102i509e6f3dg174891a5abe71452@mail.gmail.com
Whole thread Raw
In response to Re: obtaining ARRAY position for a given match  (Pavel Stehule <pavel.stehule@gmail.com>)
List 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

pgsql-general by date:

Previous
From: Sam Mason
Date:
Subject: Re: obtaining ARRAY position for a given match
Next
From: Pavel Stehule
Date:
Subject: Re: Possible bug with array_agg