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 archives
  Advanced Search

Re: Partial foreign keys


  • From: rihad <rihad(at)mail(dot)ru>
  • To: pgsql-general(at)postgresql(dot)org
  • Subject: Re: Partial foreign keys
  • Date: Sun, 28 Feb 2010 23:26:54 +0400
  • Message-id: <4B8AC37E.5000406@mail.ru> <text/plain>

rihad wrote:
Due to lack of support for partial (conditional) multi-column foreign keys in 8.3, can before-triggers be used to implement them in terms of data consistency and speed?

Let me clarify the question in semi-pseudo-SQL:

table foo {
  bar_id int not null;
  baz_id int not null;
  flag bool;
  key (bar_id, baz_id) references (bar.id, bar.baz_id);
}

table bar {
  id int primary key;
  baz_id int not null;
}


I want the effects of the above foo.key in every sense, but only for entries having foo.flag=true. So I think I'll write before-statement triggers to do just that instead of the key. But is data consistency still guaranteed as the foreign key in foo would otherwise do? What if, after the first query trigger checked to see that (foo.bar_id, foo.baz_id) multikey exists in bar, another query modifies bar in between, and the first query ends up with the wrong assumption? Similar problem goes for the table bar's trigger checking that nothing in foo still refers to the old column tuple.



Home | Main Index | Thread Index

Privacy Policy | About PostgreSQL
Copyright © 1996 – 2012 PostgreSQL Global Development Group