Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 5BE5E633708 for ; Mon, 11 Jan 2010 16:15:31 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 39644-10 for ; Mon, 11 Jan 2010 20:15:20 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id C422D6332A3 for ; Mon, 11 Jan 2010 16:15:20 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o0BKFIZs018739; Mon, 11 Jan 2010 15:15:18 -0500 (EST) To: Andy Shellam cc: pgsql-sql@postgresql.org Subject: Re: CHECK constraint removing brackets In-reply-to: References: Comments: In-reply-to Andy Shellam message dated "Mon, 11 Jan 2010 19:49:50 +0000" Date: Mon, 11 Jan 2010 15:15:18 -0500 Message-ID: <18738.1263240918@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.589 tagged_above=-10 required=5 tests=BAYES_00=-2.599, FH_DATE_PAST_20XX=3.188 X-Spam-Level: X-Archive-Number: 201001/20 X-Sequence-Number: 33556 Andy Shellam writes: > With the above in mind, I decided on the following check to enforce this: > (state = 'Unconfirmed'::client.order_state AND invoice_id = NULL) OR (state != 'Unconfirmed'::client.order_state AND invoice_id != NULL) > However PostgreSQL (8.4.2) converts this to the following: > state = 'Unconfirmed'::client.order_state AND invoice_id = NULL::integer OR state <> 'Unconfirmed'::client.order_state AND invoice_id <> NULL::integer There is no "conversion" there, that means exactly the same thing. (AND binds tighter than OR.) I think your real problem is that you're trying to use "= NULL" and "!= NULL" where you should say IS NULL or IS NOT NULL. regards, tom lane