Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id B0B2C632D2A for ; Mon, 19 Apr 2010 11:21:14 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 54351-08 for ; Mon, 19 Apr 2010 14:20:54 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id C958F632C56 for ; Mon, 19 Apr 2010 11:21:03 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o3JEKxCi013341; Mon, 19 Apr 2010 10:20:59 -0400 (EDT) To: Jasen Betts cc: Mario Splivalo , pgsql-sql@postgresql.org Subject: Re: Re: CHECK constraints with plpgsql functions - check 'fires' BEFORE data modification? In-reply-to: References: <4BCC272C.3020505@megafon.hr> Comments: In-reply-to Jasen Betts message dated "19 Apr 2010 10:49:57 +0000" Date: Mon, 19 Apr 2010 10:20:59 -0400 Message-ID: <13340.1271686859@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.415 tagged_above=-10 required=5 tests=AWL=-0.905, BAYES_05=-0.5, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201004/61 X-Sequence-Number: 33988 Jasen Betts writes: > On 2010-04-19, Mario Splivalo wrote: >> The 'proper' way to do this (as suggested by earlier posts on this >> mailing list) is to use partial UNIQUE indexes, but I have problem with >> that too: indexes are not part of DDL (no matter that primary key >> constraints and/or unique constraints use indexes to employ those >> constraints), and as far as I know there is no 'partial unique >> constraint' in SQL? > huh? I think what Mario is actually complaining about is that partial unique indexes are not part of the SQL standard, and he wants a solution that at least gives the illusion that it might be portable to some other RDBMS in the future. Unfortunately, an illusion is all it would be. Even presuming that the other DBMS lets you run plpgsql-equivalent functions in CHECK constraints, the whole approach is broken by concurrency considerations. If you have two transactions simultaneously inserting rows that would be valid given the prior state of the table, but it's *not* valid for them both to be present, then a CHECK or trigger-based constraint is going to fail, because neither transaction will see the other's uncommitted row. At least that's how it works in Postgres. In some other DBMS it might work differently, but you're right back up against the fact that your solution is not portable. Unique constraints (partial or otherwise) deal with the race-condition problem by doing low-level things that aren't exposed at the SQL level. So there's simply no way to get the equivalent behavior in pure standard SQL. >> And, wouldn't it be better to have CHECK constraints check the data >> AFTER data-modification? > no. Indeed. The race condition is still there. CHECK is meant to handle constraints on a row's value *in isolation*. If you try to use it to enforce cross-row conditions, the project will certainly end badly. regards, tom lane