Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ua5No-0002gR-37 for pgsql-bugs@arkaria.postgresql.org; Wed, 08 May 2013 14:30:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Ua5Nn-0000iC-J0 for pgsql-bugs@arkaria.postgresql.org; Wed, 08 May 2013 14:30:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ua5NY-0000YK-6h for pgsql-bugs@postgresql.org; Wed, 08 May 2013 14:30:00 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ua5NU-0002vq-Ti for pgsql-bugs@postgresql.org; Wed, 08 May 2013 14:29:59 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id r48ETtSE024417; Wed, 8 May 2013 10:29:56 -0400 (EDT) From: Tom Lane To: andras.vaczi@zalando.de cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8141: multi-column check expression evaluating to NULL In-reply-to: References: Comments: In-reply-to andras.vaczi@zalando.de message dated "Wed, 08 May 2013 13:07:00 -0000" Date: Wed, 08 May 2013 10:29:55 -0400 Message-ID: <24416.1368023395@sss.pgh.pa.us> X-Pg-Spam-Score: -3.3 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org andras.vaczi@zalando.de writes: > Consider the following table with a CHECK constraint: > CREATE TABLE check_test > ( > id integer NOT NULL, > col integer, > CONSTRAINT unique_with_null_check1 CHECK (col >= 1 AND id < 20) > ); > This INSERT statement succeeds: > INSERT INTO check_test (id, col) VALUES (1, NULL); > While, col being NULL, the whole CHECK condition evaluates to NULL - this is > covered in the documentation. > But this is refused: > INSERT INTO check_test (id, col) VALUES (21, NULL); > ERROR: new row for relation "check_test" violates check constraint > "unique_with_null_check1" > I think this behaviour should be either also mentioned in the docs or > cosidered a bug. I see no bug here. In the first case, the "col >= 1" condition yields NULL while "id < 20" yields TRUE, so you have NULL AND TRUE which is NULL, which is considered a "pass" for a CHECK condition per spec. In the second case, "col >= 1" is still NULL, but "id < 20" is FALSE, so you have NULL AND FALSE which is FALSE (*not* NULL), and so failure is per spec. Yes, the behavior of AND/OR with NULLs is documented. http://www.postgresql.org/docs/9.1/static/functions-logical.html regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs