Re: Partial foreign keys

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Partial foreign keys
Дата
Msg-id 13007.1267387274@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: Partial foreign keys  (rihad <rihad@mail.ru>)
Список pgsql-general
rihad <rihad@mail.ru> writes:
> 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?

No.  To take just the most obvious example, what happens when somebody
modifies or deletes the referenced row?  Your trigger won't handle that
case because it won't be called.

In principle you could get most of the way there if you also hung an
ON UPDATE/DELETE trigger on the referenced table and had it do the
appropriate things.  But even so, there would be race conditions that
are really insoluble in straightforward user triggers.  The built-in
support for FK checking relies on triggers in these places, but the
triggers access some functionality that's not available at the SQL level
in order to handle concurrent updates correctly.

My advice is to rethink your data layout so that you don't need this.
One possibility is to keep the "flag=false" and "flag=true" rows in
two different tables, which could be made to look like one table
(at least for non-updating queries) via a view or inheritance.

            regards, tom lane

В списке pgsql-general по дате отправления:

Предыдущее
От: rihad
Дата:
Сообщение: Re: Partial foreign keys
Следующее
От: Terry
Дата:
Сообщение: continuous copy/update one table to another