Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XDCvP-0002Gw-Dj for pgsql-bugs@arkaria.postgresql.org; Fri, 01 Aug 2014 13:31:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XDCvO-0001ue-3e for pgsql-bugs@arkaria.postgresql.org; Fri, 01 Aug 2014 13:31:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XDCvM-0001uV-5V for pgsql-bugs@postgresql.org; Fri, 01 Aug 2014 13:31:08 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XDCvH-0000iC-Ae for pgsql-bugs@postgresql.org; Fri, 01 Aug 2014 13:31:06 +0000 MIME-Version: 1.0 Subject: BUG #11107: UPDATE violates table check constraint To: pgsql-bugs@postgresql.org From: jesse.denardo@myfarms.com Date: Fri, 01 Aug 2014 13:30:13 +0000 Message-ID: <20140801133013.2696.41347@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 The following bug has been logged on the website: Bug reference: 11107 Logged by: Jesse Denardo Email address: jesse.denardo@myfarms.com PostgreSQL version: 9.3.5 Operating system: Arch Linux x64 Description: Version: PostgreSQL 9.3.5 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.9.1, 64-bit Description: After creating two test tables and a table check constraint on one of them, I use an UPDATE statement to update one of the rows. This update causes the row to violate the check constraint, but the update succeeds anyways. Dropping and re-adding the check constraint then fails because the constraint is violated. Test script: DROP SCHEMA IF EXISTS test CASCADE; CREATE SCHEMA test; SET search_path TO test; CREATE TABLE a ( id SERIAL PRIMARY KEY, num integer NOT NULL ); CREATE TABLE b ( id SERIAL PRIMARY KEY, a_id integer, num integer NOT NULL ); ALTER TABLE ONLY b ADD CONSTRAINT b_fk FOREIGN KEY (a_id) REFERENCES a(id); -- Check function to use as constraint CREATE OR REPLACE FUNCTION fn_chk_constraint ( b_id integer, a_id integer ) RETURNS boolean AS $$ SELECT 0 = ( SELECT count(*) FROM a JOIN b ON b.a_id = a.id AND b.num <> a.num WHERE a.id = a_id AND b.id = b_id ) $$ LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT; -- Insert valid test data INSERT INTO a (num) VALUES (10); INSERT INTO b (a_id, num) VALUES (NULL, 11); -- Add constraint to table b ALTER TABLE b ADD CONSTRAINT chk_constraint CHECK(fn_chk_constraint(id, a_id)); -- Make sure no rows violate the constraint...this returns no rows, which is good SELECT * FROM b WHERE fn_chk_constraint(id, a_id) = FALSE; -- Update row in table b...I expect this to fail because it violates the check constraint, but it doesn't UPDATE b SET a_id = 1; -- Check the constraint again...this now returns one row SELECT * FROM b WHERE fn_chk_constraint(id, a_id) = FALSE; -- Check my sanity. Remove the constraint and try to add it again. ALTER TABLE b DROP CONSTRAINT chk_constraint; ALTER TABLE b ADD CONSTRAINT chk_constraint CHECK(fn_chk_constraint(id, a_id)); -- error! Test script output: DROP SCHEMA CREATE SCHEMA SET CREATE TABLE CREATE TABLE ALTER TABLE CREATE FUNCTION INSERT 0 1 INSERT 0 1 ALTER TABLE id | a_id | num ----+------+----- (0 rows) UPDATE 1 # Unexpected success id | a_id | num ----+------+----- 1 | 1 | 11 (1 row) ALTER TABLE psql:constraint_test.sql:50: ERROR: check constraint "chk_constraint" is violated by some row -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs