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

Partitioning/inherited tables vs FKs

From: Boszormenyi Zoltan <zb(at)cybertec(dot)at>
To: pgsql-hackers(at)postgreSQL(dot)org
Cc: Sándor Miglécz <sandor(at)cybertec(dot)at>, Hans-Juergen Schoenig <hs(at)cybertec(dot)at>
Subject: Partitioning/inherited tables vs FKs
Date: 2010-05-06 08:52:42
Message-ID: 4BE2835A.5020601@cybertec.at (view raw or flat)
Thread:
Lists: pgsql-hackers
Hi,

we came across an interesting problem.

=# create table parent (id serial primary key, t text);
NOTICE:  CREATE TABLE will create implicit sequence "parent_id_seq" for
serial column "parent.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index
"parent_pkey" for table "parent"
CREATE TABLE
=# create table child () inherits (parent);
CREATE TABLE
=# create table refer (id serial primary key, parent_id integer
references parent (id));
NOTICE:  CREATE TABLE will create implicit sequence "refer_id_seq" for
serial column "refer.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index
"refer_pkey" for table "refer"
CREATE TABLE
=# begin;
BEGIN
=# insert into child (t) values ('a') returning id;
 id
----
  1
(1 sor)

INSERT 0 1
=# 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.

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?

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.

Best regards,
Zoltán Böszörményi

-- 
Bible has answers for everything. Proof:
"But let your communication be, Yea, yea; Nay, nay: for whatsoever is more
than these cometh of evil." (Matthew 5:37) - basics of digital technology.
"May your kingdom come" - superficial description of plate tectonics

----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
http://www.postgresql.at/


Responses

pgsql-hackers by date

Next:From: Dimitri FontaineDate: 2010-05-06 09:26:30
Subject: Re: max_standby_delay considered harmful
Previous:From: Simon RiggsDate: 2010-05-06 08:12:51
Subject: Re: max_standby_delay considered harmful

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group