Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XyT5J-0004QK-Dp for pgsql-sql@arkaria.postgresql.org; Tue, 09 Dec 2014 22:16:45 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XyT5I-0001Ux-Uq for pgsql-sql@arkaria.postgresql.org; Tue, 09 Dec 2014 22:16:44 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XyT5I-0001UY-8V for pgsql-sql@postgresql.org; Tue, 09 Dec 2014 22:16:44 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XyT5F-00011b-F4 for pgsql-sql@postgresql.org; Tue, 09 Dec 2014 22:16:42 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id sB9MGcC8026089; Tue, 9 Dec 2014 17:16:38 -0500 From: Tom Lane To: Scott Rohde cc: pgsql-sql@postgresql.org Subject: Re: Check/unique constraint question In-reply-to: <1418162819422-5829820.post@n5.nabble.com> References: <1418148099196-5829778.post@n5.nabble.com> <7561.1418150595@sss.pgh.pa.us> <1418162819422-5829820.post@n5.nabble.com> Comments: In-reply-to Scott Rohde message dated "Tue, 09 Dec 2014 15:06:59 -0700" Date: Tue, 09 Dec 2014 17:16:38 -0500 Message-ID: <26088.1418163398@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Scott Rohde writes: > Tom Lane-2 wrote >> Indeed, this illustrates perfectly why subqueries in CHECK constraints >> are generally a Bad Idea: the constraint is no longer just about the >> contents of one row but about its relationship to other rows, and that >> makes the timing of checks relevant. Hiding the subquery in a function >> doesn't do anything to resolve that fundamental issue. > I don't think subqueries in CHECK constraints are a bad idea /per se/--to my > mind it would depend on how they actually work. I don't know enough about > the SQL standard or about products that support them to know if they work > the way I /think/ they should work, which is basically this: "Guarantee that > condition X (written as a constraint on table Y) is satisfied by the > database when (1) the constraint is first added, and (2) whenever a change > is made to one or more rows of table Y." They certainly don't work like that in Postgres, and I doubt in other DBMSes either. A CHECK constraint is assumed to involve only the contents of a single row, and it's checked for each row when (actually before) that row is inserted or updated. There is a thing in SQL called an "assertion" which has the sort of unconstrained semantics you imagine. Postgres doesn't implement those, and we're not alone. The cost of enforcing them is nigh prohibitive. regards, tom lane -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql