X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 893F59DC810 for ; Sat, 28 Jan 2006 01:25:11 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 88132-03 for ; Sat, 28 Jan 2006 01:25:09 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id AEEDC9DC80C for ; Sat, 28 Jan 2006 01:25:08 -0400 (AST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id D6BFC354C0; Fri, 27 Jan 2006 21:24:49 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id D3953354BF; Fri, 27 Jan 2006 21:24:49 -0800 (PST) Date: Fri, 27 Jan 2006 21:24:49 -0800 (PST) From: Stephan Szabo To: "Kashmira Patel (kupatel)" Cc: Michael Fuhr , pgsql-sql@postgresql.org Subject: Re: Question about check constraints In-Reply-To: <18AE59788A3FC640A367E5652E664D800173133E@xmb-sjc-237.amer.cisco.com> Message-ID: <20060127210947.K43706@megazone.bigpanda.com> References: <18AE59788A3FC640A367E5652E664D800173133E@xmb-sjc-237.amer.cisco.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.145 required=5 tests=[AWL=0.145] X-Spam-Score: 0.145 X-Spam-Level: X-Archive-Number: 200601/203 X-Sequence-Number: 23992 On Fri, 27 Jan 2006, Kashmira Patel (kupatel) wrote: > Both concerns. > 1) There are actually more than two columns with such checks, and each > one calls a few functions which execute some more queries. So I would > like to invoke these checks only when necessary. > 2) The bigger concern is the side effect: Here's my schema: > > CREATE TABLE vm_device > ( > device_id INTEGER UNIQUE NOT NULL > REFERENCES device_table(device_id) > ON UPDATE CASCADE > ON DELETE CASCADE, > preference VARCHAR(1) NOT NULL DEFAULT 'U' > CHECK (CASE WHEN preference = 'U' > THEN true > ELSE validate_preference() > END), > enabled BOOLEAN NOT NULL DEFAULT false > CHECK (CASE WHEN enabled = false > THEN true > ELSE > validate_system_enabled() AND > > validate_enabled(device_id) > END), > attach_vm BOOLEAN NOT NULL DEFAULT false > CHECK (CASE WHEN attach_vm = false > THEN true > ELSE validate_attach_vm() > END), > > PRIMARY KEY (device_id) > ) WITHOUT OIDS; > > This table contains some information about a device in my system. The > issue is with the enabled column. It basically enables/disables the > device. The device can be enabled only when the two check conditions > pass. But once it is enabled, the conditions of the system might change > such that if executed again, these conditions might not pass. We want to > allow such situations. The problem arises when we want to change the > value of some other column, say attach_vm. Although the check > constraints for the attach_vm column pass, those for enabled column > fail, and I cannot complete my updates. In that case check constraints are probably not the correct tool. IIRC the theoretical model from the spec implies that all check constraints in the system are supposed to be satisfied at their check time (statement end in most cases) and that it doesn't matter what the change was, and as such, the conditions of the system shouldn't have been allowed to change such that a row with enabled=true existed when its constraint would be violated at this moment. I don't think that's practically reasonable to enforce in general, but we do the best we can which is fail the later update. > Any suggestions on the best way to overcome this? I'd second Michael's suggestion of a trigger. > -----Original Message----- > From: Michael Fuhr [mailto:mike@fuhr.org] > Sent: Friday, January 27, 2006 4:40 PM > To: Kashmira Patel (kupatel) > Cc: pgsql-sql@postgresql.org > Subject: Re: [SQL] Question about check constraints > > On Fri, Jan 27, 2006 at 03:06:26PM -0800, Kashmira Patel (kupatel) > wrote: > > I have a table where two columns have two different check > > constraints associated with them. When I update one column, the check > > constraint on the other column is also executed. Is there a way to > > avoid this? I want to check only for the condition defined for the > column being updated. > > I don't think you can change this behavior: each CHECK constraint is > evaluated for the new row regardless of whether a particular column > changed or not. However, you could enforce the constraints with a > trigger and skip checks where NEW.column is the same as OLD.column. > > Why the concern? Are the checks expensive? Do they have side effects? > What do they do?