X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 327DD9FA0EC for ; Sat, 1 Jul 2006 02:10:46 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 32586-01 for ; Sat, 1 Jul 2006 02:10:17 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web31813.mail.mud.yahoo.com (web31813.mail.mud.yahoo.com [68.142.207.76]) by postgresql.org (Postfix) with SMTP id 89FF09FA319 for ; Sat, 1 Jul 2006 02:10:17 -0300 (ADT) Received: (qmail 94525 invoked by uid 60001); 1 Jul 2006 05:10:16 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=ddeCySHttaWbVXCCIDQKq/IPyYvFYGZkEUCwe0RvM2jZ8+S+P03ZYGfb0eZFH/IIz6zczgmZO/CPwDH6yo9Ye+u+DIbnNw3zLl8/dCBiGrTUnOC7BeHYRx+NZOTJ8SOPzDBPb5QnbQf4x7mc6+hUjoLulj0FSp5BvAeaHu5qY9E= ; Message-ID: <20060701051016.94523.qmail@web31813.mail.mud.yahoo.com> Received: from [24.54.196.198] by web31813.mail.mud.yahoo.com via HTTP; Fri, 30 Jun 2006 22:10:16 PDT Date: Fri, 30 Jun 2006 22:10:16 -0700 (PDT) From: Richard Broersma Jr Subject: Re: Alternative to Select in table check constraint To: Tom Lane Cc: General Postgresql List In-Reply-To: <18959.1151727082@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/2 X-Sequence-Number: 25576 > > The following codes doesn't work on PostgreSQL 8.1.4 but according to > > the book does conform to SQL-92. > > > CHECK ( 1 = ALL ( SELECT COUNT(STATUS) > > FROM BADGES > > WHERE STATUS = 'A' > > GROUP BY EMPNO)) > > Yeah, the spec does allow sub-SELECTs in CHECK constraints but PG > doesn't implement that. The problem with it is that there's no clear > way to make it perform reasonably, because the CHECK doesn't simply > implicate the row you're currently inserting/updating --- every other > row is potentially referenced by the sub-SELECT, and so changing row > X might make the CHECK condition fail at row Y. A brute-force > implementation would be that every update of any sort to BADGES causes > us to re-evaluate the CHECK constraint (hence, re-scan BADGES entirely > to compute the sub-SELECT) for every row of BADGES (hence, O(N^2) work > if there are N rows in the table). That is certainly unworkable :-(. > A bright person can think of ways to optimize particular cases but > it's not easy to see how the machine might figure it out for arbitrary > SELECTs. > > The unique-index hack that Michael suggested amounts to hand-optimizing > the sub-SELECT constraint into something that's efficiently checkable. > > regards, tom lane Ah. Thanks for the clarification. Regards, Richard Broersma Jr.