Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XDEmg-0008PI-0q for pgsql-bugs@arkaria.postgresql.org; Fri, 01 Aug 2014 15:30:18 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XDEmf-0004ms-1b for pgsql-bugs@arkaria.postgresql.org; Fri, 01 Aug 2014 15:30:17 +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 1XDEmd-0004la-3F for pgsql-bugs@postgresql.org; Fri, 01 Aug 2014 15:30:15 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XDEmY-0002sz-Ve for pgsql-bugs@postgresql.org; Fri, 01 Aug 2014 15:30:13 +0000 Received: from pro.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s71FU61Y031943; Fri, 1 Aug 2014 11:30:06 -0400 From: Tom Lane To: jesse.denardo@myfarms.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #11107: UPDATE violates table check constraint In-reply-to: <20140801133013.2696.41347@wrigleys.postgresql.org> References: <20140801133013.2696.41347@wrigleys.postgresql.org> Comments: In-reply-to jesse.denardo@myfarms.com message dated "Fri, 01 Aug 2014 13:30:13 -0000" Date: Fri, 01 Aug 2014 09:30:07 -0600 Message-ID: <1470.1406907007@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) 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 jesse.denardo@myfarms.com writes: > 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. Sorry, but this check constraint has entirely undefined behavior, as does any check constraint that refers to data rows other than the one that is being checked. > -- 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; The "immutable" marking of this function is a lie on its face; not that a proper marking would have helped. The reason the constraint doesn't fail is that Postgres checks it before storing the updated row, not after. But even without that, there would be no way to guarantee proper maintenance of the constraint --- what if somebody changes table a, or if there are two concurrent changes to table b that individually are still consistent but not when taken together? (I've not tried to work out whether the latter is possible given this specific condition, but in general it's a problem if a check constraint on b tries to examine other rows in b.) This is why we don't allow sub-SELECTs in check conditions. You can escape that syntactic constraint by embedding the sub-SELECT in a function as you've done here, but that isn't going to make it work. 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