Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id CE453B5DBF5 for ; Tue, 4 Oct 2011 04:17:00 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 73338-09 for ; Tue, 4 Oct 2011 07:16:54 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mbx.knossos.net.nz (mbx.knossos.net.nz [202.160.48.10]) by mail.postgresql.org (Postfix) with ESMTP id 81436B5DBEE for ; Tue, 4 Oct 2011 04:16:53 -0300 (ADT) Received: from [10.1.1.3] (60-234-150-59.bitstream.orcon.net.nz [60.234.150.59]) (authenticated bits=0) by mbx.knossos.net.nz (8.14.4/8.14.4) with ESMTP id p947GdUl014079 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 4 Oct 2011 20:16:47 +1300 Message-ID: <4E8AB2D7.2010901@archidevsys.co.nz> Date: Tue, 04 Oct 2011 20:16:39 +1300 From: Gavin Flower Organization: ArchiDevSys User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 MIME-Version: 1.0 To: Vitalii Tymchyshyn CC: bricklen , Merlin Moncure , "pgsql-performance@postgresql.org" Subject: Re: array_except -- Find elements that are not common to both arrays References: <4E85B4AF.2090104@gmail.com> In-Reply-To: <4E85B4AF.2090104@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201110/39 X-Sequence-Number: 45261 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?