Re: array_except -- Find elements that are not common to both arrays

From: Gavin Flower <GavinFlower(at)archidevsys(dot)co(dot)nz>
To: Vitalii Tymchyshyn <tivv00(at)gmail(dot)com>
Cc: bricklen <bricklen(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>, "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: array_except -- Find elements that are not common to both arrays
Date: 2011-10-04 07:16:39
Message-ID: 4E8AB2D7.2010901@archidevsys.co.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On 01/10/11 01:23, Vitalii Tymchyshyn wrote:
> Since you are using except and not except all, you are not looking at
> arrays with duplicates.
> For this case next function what the fastest for me:
>
> create or replace function array_except2(anyarray,anyarray) returns
> anyarray as $$
> select ARRAY(
> (
> select r.elements
> from (
> (select 1,unnest($1))
> union all
> (select 2,unnest($2))
> ) as r (arr, elements)
> group by 1
> having min(arr)=max(arr)
> ))
> $$ language sql strict immutable;
>
> Best regards, Vitalii Tymchyshyn
>
Very neat!

I could see that this function could trivially be modified to handle 3
arrays.

QUESTION: Could this be modified to take an arbitrary number of arrays?

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Gregg Jaskiewicz 2011-10-04 08:08:13 Re: Query with order by and limit is very slow - wrong index used
Previous Message Nowak Michał 2011-10-04 07:10:27 Re: Query with order by and limit is very slow - wrong index used