Re: Partitioning/inherited tables vs FKs
От
Florian Pflug
Тема
Re: Partitioning/inherited tables vs FKs
Дата
Msg-id
FAA7C1DF-63CC-4E6D-940B-83EBC5A50D0B@phlo.org
Ответ на
Partitioning/inherited tables vs FKs (Boszormenyi Zoltan)
Список
Дерево обсуждения
Partitioning/inherited tables vs FKs Boszormenyi Zoltan <zb@cybertec.at>
Re: Partitioning/inherited tables vs FKs Florian Pflug <fgp@phlo.org>
Re: Partitioning/inherited tables vs FKs Tom Lane <tgl@sss.pgh.pa.us>
Re: Partitioning/inherited tables vs FKs Florian Pflug <fgp@phlo.org>
Re: Partitioning/inherited tables vs FKs Tom Lane <tgl@sss.pgh.pa.us>
Re: Partitioning/inherited tables vs FKs Greg Stark <gsstark@mit.edu>
Re: Partitioning/inherited tables vs FKs Jim Nasby <decibel@decibel.org>
Re: Partitioning/inherited tables vs FKs Jaime Casanova <jaime@2ndquadrant.com>
Re: Partitioning/inherited tables vs FKs Robert Haas <robertmhaas@gmail.com>
Re: Partitioning/inherited tables vs FKs Dmitry Fefelov <fozzy@ac-sw.com>
Re: Partitioning/inherited tables vs FKs Robert Haas <robertmhaas@gmail.com>
Re: Partitioning/inherited tables vs FKs Marko Tiikkaja <marko.tiikkaja@cs.helsinki.fi>
Re: Partitioning/inherited tables vs FKs Nicolas Barbier <nicolas.barbier@gmail.com>
Re: Partitioning/inherited tables vs FKs "Kevin Grittner" <Kevin.Grittner@wicourts.gov>
Re: Partitioning/inherited tables vs FKs Marko Tiikkaja <marko.tiikkaja@cs.helsinki.fi>
Re: Partitioning/inherited tables vs FKs Nicolas Barbier <nicolas.barbier@gmail.com>
Re: Partitioning/inherited tables vs FKs Marko Tiikkaja <marko.tiikkaja@cs.helsinki.fi>
Re: Partitioning/inherited tables vs FKs Marko Tiikkaja <marko.tiikkaja@cs.helsinki.fi>
Re: Partitioning/inherited tables vs FKs Tom Lane <tgl@sss.pgh.pa.us>
SHARE locks vs. DELETE in SERIALIZABLE mode (Was: Partitioning/inherited tables vs FKs) Florian Pflug <fgp@phlo.org>
Re: SHARE locks vs. DELETE in SERIALIZABLE mode
(Was: Partitioning/inherited tables vs FKs) "Kevin Grittner" <Kevin.Grittner@wicourts.gov>
Re: SHARE locks vs. DELETE in SERIALIZABLE mode (Was:
Partitioning/inherited tables vs FKs) Robert Haas <robertmhaas@gmail.com>
Re: SHARE locks vs. DELETE in SERIALIZABLE mode (Was: Partitioning/inherited tables vs FKs) Florian Pflug <fgp@phlo.org>
Re: SHARE locks vs. DELETE in SERIALIZABLE mode (Was:
Partitioning/inherited tables vs FKs) Jan Wieck <JanWieck@Yahoo.com>
Re: SHARE locks vs. DELETE in SERIALIZABLE mode (Was: Partitioning/inherited tables vs FKs) Florian Pflug <fgp@phlo.org>
On May 6, 2010, at 10:52 , Boszormenyi Zoltan wrote:
> =# create table parent (id serial primary key, t text);
> ...
> =# create table child () inherits (parent);
> ...
> =# create table refer (id serial primary key, parent_id integer
> ...
> =# insert into child (t) values ('a') returning id;
> ...
> =# select * from parent;
> id | t
> ----+---
> 1 | a
> (1 sor)
>
> =# insert into refer (parent_id) values (1);
> ERROR: insert or update on table "refer" violates foreign key
> constraint "refer_parent_id_fkey"
> DETAIL: Key (parent_id)=(1) is not present in table "parent".
>
> The use case for this was there were different news items,
> and there were another table for summaries, that could point
> to any of the news items table. Another use case could be
> a large partitioned table with an FK to the main table where
> the referring table might only contain very few "interesting" data.
Yeah, this is a long-standing issue with inheritance. Table inheritance in postgres isn't much more than an implicit UNION done on selects plus some logic in ALTER TABLE to keep propagate structural changes. Indices and constraints basically always behave as if ONLY had been specified. I'm not even sure if the ids are globally unique in your example - it might be that each child's "id serial" column gets its very own sequence.
One possible workaround is no create a table, say referred_ids, that contains all the ids from parent and all of its children, kept up-to-date via triggers, and point the FK constraint to that table. That also allows for a global unique constraint on the ids by definition a suitable unique or primary key constraint on referred_ids.
What lies at the heart of this problem is the lack of multi-table indices and hence multi-table unique constraints in postgres. AFAIK with those in place the rest amounts to the removal of ONLY from the constraint check queries plus some code to propagate constraint triggers to child tables.
best regards,
Florian Pflug
В списке pgsql-hackers по дате отправления