Re: Array: comparing first N elements?

From: David Garamond <davidgaramond(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Array: comparing first N elements?
Date: 2009-05-12 08:40:02
Message-ID: 7c33d060905120140p78b4e180rda6101cff30459eb@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, May 12, 2009 at 3:28 PM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>wrote:

> Hello
>
> create or replace function eqn(anyarray, anyarray, int)
> returns boolean as $$
> select not exists(select $1[i] from generate_series(1,$3) g(i)
> except
> select $2[i] from generate_series(1,$3) g(i))
> $$ language sql immutable strict;
>
> postgres=# select eqn(array[1,2,3,4,5], array[1,2,3,5,6], 3);
> eqn
> -----
> t
> (1 row)
>
> Time: 1,590 ms
> postgres=# select eqn(array[1,2,3,4,5], array[1,2,3,5,6], 4);
> eqn
> -----
> f
> (1 row)
>

Hi Pavel,

Thanks for the solution, but that's too slow. I'd rather just do this
instead:

select * from product
where parents[1:(select array_length(parents,1) from product where
name='wanted')+1]=
(select parents from product where name='wanted')||
(select id from product where name='wanted');

but the above query is also unable to use any indices (unlike LIKE 'foo%').

Regards,
Dave

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message David Garamond 2009-05-12 08:42:22 Re: Array: comparing first N elements?
Previous Message Achilleas Mantzios 2009-05-12 08:38:32 Re: Array: comparing first N elements?