Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 77FC3B5DD64 for ; Fri, 30 Sep 2011 09:23:44 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 97039-07-4 for ; Fri, 30 Sep 2011 12:23:37 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-bw0-f46.google.com (mail-bw0-f46.google.com [209.85.214.46]) by mail.postgresql.org (Postfix) with ESMTP id 5A2E9B5DCE5 for ; Fri, 30 Sep 2011 09:23:14 -0300 (ADT) Received: by bkbzt4 with SMTP id zt4so1678663bkb.19 for ; Fri, 30 Sep 2011 05:23:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=ze6XTGwLqtFex2e+pM4rQ/0xVKCWSzZ4sUrEB+sxR40=; b=vkJVkgE+mqjw41eW6ePwHrSIf4XQaYYEbC1c2cMHCPysP0066qgjZEHQOVAqo+NM9M 0g4TMNo7L1wH4/7oh3TB9OiggdGs+gHTdoHaYXonBfWvuN4o/gPVq8hD0ENN8MClL31v XYW2/1Q23jQkGnPJPbdRB5M4P1jniWOmv8p+k= Received: by 10.204.137.212 with SMTP id x20mr3352027bkt.26.1317385394677; Fri, 30 Sep 2011 05:23:14 -0700 (PDT) Received: from [10.64.1.26] ([94.45.140.16]) by mx.google.com with ESMTPS id e14sm4804232bka.0.2011.09.30.05.23.12 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 30 Sep 2011 05:23:13 -0700 (PDT) Message-ID: <4E85B4AF.2090104@gmail.com> Date: Fri, 30 Sep 2011 15:23:11 +0300 From: Vitalii Tymchyshyn User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.21) Gecko/20110831 Lightning/1.0b2 Thunderbird/3.1.13 MIME-Version: 1.0 To: bricklen CC: Merlin Moncure , "pgsql-performance@postgresql.org" Subject: Re: array_except -- Find elements that are not common to both arrays References: In-Reply-To: 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.799 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_ENVFROM_END_DIGIT=0.1, FREEMAIL_FROM=0.001 X-Spam-Level: X-Archive-Number: 201109/434 X-Sequence-Number: 45210 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