Re: Constraint to ensure value does NOT exist in another table? - Mailing list pgsql-general

From Peter Geoghegan
Subject Re: Constraint to ensure value does NOT exist in another table?
Date
Msg-id BANLkTinSSPzV=Z6ijkRoDKcRBUUrhS-dUQ@mail.gmail.com
Whole thread Raw
In response to Re: Constraint to ensure value does NOT exist in another table?  ("Gauthier, Dave" <dave.gauthier@intel.com>)
Responses Re: Constraint to ensure value does NOT exist in another table?
List pgsql-general
On 16 June 2011 14:41, Gauthier, Dave <dave.gauthier@intel.com> wrote:
> I've dealt with something similar by using a check constraint and a stored procedure.  The check constraint calls a
storedprocedure, passing it (in your case) the key you want to make sure doesn't exist in some other table.  The stored
proceduresqueries that other table for the key and passes back a YES/NO flag that the check constraint detects and acts
on(constraint violated or not). 
>
> I'm not using this to check a prim/foreign key relationship for my app, and the table that the stored procedure is
queryingis a ref table that is very static.  This approach may not be bullet proof for checking key relationships in
dynamictables.  I'll let others speak to that. 

Did you use explicit locking? If not, you likely have a race
condition. The same applies to any sort of enforcement of business
rules inside triggers (or, indeed, check constraints). Check
constraints are generally intended to enforce simple, immutable rules
(i.e. that only reference the tuple that the rule is enforced on). I
would have used a trigger instead.

--
Peter Geoghegan       http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training and Services

pgsql-general by date:

Previous
From: Merlin Moncure
Date:
Subject: Re: Encryption For Specific Column- Where to store the key
Next
From: Tom Lane
Date:
Subject: Re: order by and view def.