Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id A8C409FB1F8 for ; Wed, 29 Nov 2006 13:53:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 49450-04 for ; Wed, 29 Nov 2006 13:53:13 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id B5A1F9FA231 for ; Wed, 29 Nov 2006 13:53:17 -0400 (AST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id C4E96353EC; Wed, 29 Nov 2006 09:53:16 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id C2C70353EB; Wed, 29 Nov 2006 09:53:16 -0800 (PST) Date: Wed, 29 Nov 2006 09:53:16 -0800 (PST) From: Stephan Szabo To: James Robinson Cc: pgsql-sql@postgresql.org Subject: Re: Subselects in CHECK clause ... In-Reply-To: <6881FC4D-8F7C-477B-9AAA-C9B5282D88B2@socialserve.com> Message-ID: <20061129094826.X4910@megazone.bigpanda.com> References: <6881FC4D-8F7C-477B-9AAA-C9B5282D88B2@socialserve.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200611/279 X-Sequence-Number: 26931 On Wed, 29 Nov 2006, James Robinson wrote: > I see that subselects are not directly supported in check clauses, > but one can work around that by writing a stored function which > returns boolean and performs the subselect. Are there any known > gotchas with doing this? To completely get the constraint, you have to also apply constraints on the tables referenced in the function that prevent modifications on those tables from causing the constraint to be violated. For example, if you were to do an exists test on another table for a row that matches up with this row in some fashion (for a specialized referential integrity constraint) modifications on that other table could also cause the constraint to be violated, but that isn't caught by the CHECK function(...) case and you'll probably need triggers or other constraints on that table.