Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: multi-row check constraints?


  • From: Jeff Davis <pgsql(at)j-davis(dot)com>
  • To: Angva <angvaw(at)gmail(dot)com>
  • Cc: pgsql-general(at)postgresql(dot)org
  • Subject: Re: multi-row check constraints?
  • Date: Thu, 22 Mar 2007 17:09:26 -0700
  • Message-id: <1174608566(dot)23455(dot)601(dot)camel(at)dogma(dot)v10(dot)wvs>

On Tue, 2007-03-20 at 13:21 -0700, Angva wrote:
> Dear Postgres fans,
> 
> Hi, I was wondering what is the best way to achieve a multi-row check
> constraint. For example, you have a table with two columns: ID and
> percent, no primary key. The goal is to enforce that all values of
> percent, per ID, add up to exactly 100%. I come from an Oracle
> background, and what you would probably do on Oracle is create a
> materialized view with the sum(percent) grouped by ID, then put a
> constraint on the sum column. This problem is also solvable using
> triggers, but it's messy and imposes a lot of serialization. Not to
> mention easy to get wrong.
> 
> So, I've come across this problem in Postgres and was hoping someone
> could steer me in the right direction.
> 

Your Oracle solution is interesting, and can indeed be implemented in
PostgreSQL in exactly the same way. Look at materialized views here:

http://www.varlena.com/varlena/GeneralBits/Tidbits/matviews.html

Another way to do it without using an entire materialized view is to
obtain a row level lock on the ID using SELECT ... WHERE id=123 FOR
UPDATE. To do this you need to have a table that contains all the IDs
and where id has a unique index to prevent race conditions when adding
new IDs.

What are you trying to do exactly? Why does the table have no primary
key?

Regards,
	Jeff Davis





Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group