Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gVLCO-00088C-T5 for pgsql-general@arkaria.postgresql.org; Fri, 07 Dec 2018 18:50:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gVLCN-000608-N4 for pgsql-general@arkaria.postgresql.org; Fri, 07 Dec 2018 18:50:03 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gVLCN-000601-Bi for pgsql-general@lists.postgresql.org; Fri, 07 Dec 2018 18:50:03 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gVLCJ-0007mK-Vr for pgsql-general@postgresql.org; Fri, 07 Dec 2018 18:50:02 +0000 Received: from [192.168.127.1] (port=10118 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gVLCF-000FEC-Ln; Fri, 07 Dec 2018 18:49:55 +0000 Received: from [127.0.0.1] (port=13398 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gVLCE-000C6x-Ro; Fri, 07 Dec 2018 18:49:54 +0000 From: Andrew Gierth To: Chris Wilson Cc: "pgsql-general\@postgresql.org" Subject: Re: != ANY(array) does not behave as expected In-Reply-To: (Chris Wilson's message of "Fri, 7 Dec 2018 12:39:50 +0000") Message-ID: <87o99x2ne0.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Fri, 07 Dec 2018 18:49:53 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Chris" == Chris Wilson writes: Chris> However, if we try to invert it by using the != operator, then Chris> we get unexpected results: Mr. De Morgan would like a word. https://en.wikipedia.org/wiki/De_Morgan%27s_laws In short, if you have a condition of the form (a OR b) and you want to negate it, then you find that: NOT (a OR b) is equivalent to (NOT a) AND (NOT b) Since x = ANY (array[1,2]) is equivalent to (x = 1) OR (x = 2), then the negation would be (x != 1) AND (x != 2), not OR. Which can be conveniently expressed as x != ALL (array[1,2]). So just as you interchange AND and OR when inverting the sense of a condition, you also interchange ALL and ANY for exactly the same reasons. Chris> expression NOT IN (subquery) https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_NOT_IN Chris> And is it a bug that one can't use unnest in a NOT IN expression Chris> in the WHERE clause? No. -- Andrew (irc:RhodiumToad)