Re: unique amount more than one table

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Perry Smith <pedzsan(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: unique amount more than one table
Date: 2011-04-05 22:28:45
Message-ID: 1302042525.2421.7.camel@jdavis-ux.asterdata.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 2011-04-05 at 17:02 -0500, Perry Smith wrote:
> CREATE OR REPLACE FUNCTION unique_xxx ( ) RETURNS boolean AS $$
> SELECT ( SELECT max(cnt) FROM ( SELECT count(*) AS cnt FROM xxx GROUP BY name ) AS foo ) = 1;
> $$ LANGUAGE SQL;
>
> Next I added a check constraint with:
>
> ALTER TABLE table1 ADD CHECK ( unique_xxx() );

...

> After I insert a row that I want to be rejected, I can do:
>
> select unique_xxx();
> unique_xxx
> ------------
> f
> (1 row)
>
> but the insert was not rejected. I'm guessing because the check constraint runs before the insert?

Yes. But even if it ran afterward, there is still a potential race
condition, because the query in the CHECK constraint doesn't see the
results of concurrent transactions.

To make this work, you should be using LOCK TABLE inside of a trigger
(probably a BEFORE trigger that locks the table, then looks to see if
the value exists in the view already, and if so, throws an exception).
CHECK is not the right place for this kind of thing.

Keep in mind that the performance will not be very good, however. There
is not a good way to make this kind of constraint perform well,
unfortunately. But that may not be a problem in your case -- try it and
see if the performance is acceptable.

Regards,
Jeff Davis

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2011-04-05 22:37:34 Re: unique amount more than one table
Previous Message Perry Smith 2011-04-05 22:02:34 unique amount more than one table