Re: Partitioning/inherited tables vs FKs - Mailing list pgsql-hackers

From Jaime Casanova
Subject Re: Partitioning/inherited tables vs FKs
Date
Msg-id v2i3073cc9b1005060337x4883e16ak8e6bdf1996a29ffe@mail.gmail.com
Whole thread Raw
In response to Partitioning/inherited tables vs FKs  (Boszormenyi Zoltan <zb@cybertec.at>)
Responses Re: Partitioning/inherited tables vs FKs
List pgsql-hackers
2010/5/6 Boszormenyi Zoltan <zb@cybertec.at>:
>
> =# 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.
>
> No matter what are the semantics, the parent table in the
> inheritance chain cannot be used as and endpoint for FKs.
>
> Is it a bug, or intentional?

i would call it a bug, but this is a known issue

>
> The only solution currently is that the referring table has to be
> partitioned the same way as the referred table in the FK, and
> its parent table has to be queried.
>

no, you can install a trigger on the child table that verifies the
existence of the id on your partitioned parent table, the SELECT
you'll use inside that trigger will look at the entire set of tables
(as long as you don't use FROM ONLY)

also could be useful to put an index (even a PK) on every child to
ensure uniqueness and make the SELECT more efficient, and of course a
check constraint in every child emulating a partition key

--
Jaime Casanova         www.2ndQuadrant.com
Soporte y capacitación de PostgreSQL


pgsql-hackers by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: LD_LIBRARY_PATH versus rpath
Next
From: Simon Riggs
Date:
Subject: Re: max_standby_delay considered harmful