pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
table inheritance and foreign keys
6+ messages / 4 participants
[nested] [flat]

* table inheritance and foreign keys
@ 2003-05-15 00:07  Reece Hart <rkh@gene.COM>
  0 siblings, 1 reply; 6+ messages in thread

From: Reece Hart @ 2003-05-15 00:07 UTC (permalink / raw)
  To: pgsql-general

I'd appreciate some advice regarding the use of foreign keys in
inherited tables.  Specifically, here's the schema I'd like:

A                               S
--------                        --------
id   serial pkey   <---1:1--->  id integer references A(id)
name text                 +-/
                          |
  A1                      |
  --------                |
  (inherits A)     <--1:1-+
  id   serial
  C1   text

This doesn't work because the foreign key constraint can only bind to
one table.  In other words, I'd like S.id to reference the union of id
keys from A and A1.  This is analogous to the union select generated by
'select ... from A'.

Is there a way to achieve the same end by a different design (with
reasonable effort)?  The best I can think of is to make A1 a distinct
table joinable on id, and then create a view with insert rule which
separately inserts into A and A1.

I've used table inheritance extensively and find it quite useful. 
Extending the OO abstraction to a table's indices would enable even more
concise and expressive designs.

Thanks,
Reece

-- 
Reece Hart, Ph.D.                       rkh@gene.com, http://www.gene.com/
Genentech, Inc.                         650/225-6133 (voice), -5389 (fax)
Bioinformatics and Protein Engineering
1 DNA Way, MS-93                        http://www.in-machina.com/~reece/
South San Francisco, CA  94080-4990     reece@in-machina.com, GPG: 0x25EC91A0




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: table inheritance and foreign keys
@ 2003-05-15 06:43  James Gregory <james@anchor.net.au>
  parent: Reece Hart <rkh@gene.COM>
  0 siblings, 1 reply; 6+ messages in thread

From: James Gregory @ 2003-05-15 06:43 UTC (permalink / raw)
  To: Reece Hart <rkh@gene.COM>; +Cc: pgsql-general

On Thu, 2003-05-15 at 10:07, Reece Hart wrote:
> I'd appreciate some advice regarding the use of foreign keys in
> inherited tables.  Specifically, here's the schema I'd like:

Ok, you can't do it with native postgres RI stuff. I'm told that
the subject has been debated extensively, but right now it can't
be done.

I had to do the same thing as you and the way I've gone about it
is to write triggers in plpython that raise an exception if RI
is not maintained. I have a table that lists all the "x
references y" pairs, and it just reads that and does the query
to see if it can find a row in the "y" table with the right ID.

It was working quite well (within my limited testing) until I hit
a but in plpython last night. Said bug is on my todo list to take
a look at, but that won't be happening imediately.

You'll find you can't write this stuff easily in plpgsql, and I
don't recommend plpython right now because I don't trust it at
this point.

You might have some luck writing similar triggers in C. I'm
happy to send you my python triggers if you'd like something to
start from, but be warned: they're a bit of a mess.

HTH

James.





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: table inheritance and foreign keys
@ 2003-05-16 16:23  Reece Hart <rkh@gene.COM>
  parent: James Gregory <james@anchor.net.au>
  0 siblings, 1 reply; 6+ messages in thread

From: Reece Hart @ 2003-05-16 16:23 UTC (permalink / raw)
  To: James Gregory <james@anchor.net.au>; +Cc: pgsql-general

James-

Thanks for your advice.  I think I'll opt for the following approach
instead, but I'm interested to hear of any pitfalls you foresee.

Subtables (for which I wanted to use inherit) will instead contain only
the subtable-specific columns, and in addition contain a foreign key
into the parent table.  A view with insert, update, and delete rules can
emulate the appearance of a full subtable (ie, inherited and
subtable-specific columns).

The upside I see over triggers is that the referential integrity is
still on postgresql's back.  The downside is that I end up with more
tables and views to maintain and an extra join in many cases.

Again, thanks for your advice.  Do you see any reason that the above
sketch wouldn't work to emulate foreign keys into inherited tables?

-Reece

-- 
Reece Hart, Ph.D.                       rkh@gene.com, http://www.gene.com/
Genentech, Inc.                         650/225-6133 (voice), -5389 (fax)
Bioinformatics and Protein Engineering
1 DNA Way, MS-93                        http://www.in-machina.com/~reece/
South San Francisco, CA  94080-4990     reece@in-machina.com, GPG: 0x25EC91A0




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: table inheritance and foreign keys
@ 2003-05-19 02:15  James Gregory <james@anchor.net.au>
  parent: Reece Hart <rkh@gene.COM>
  0 siblings, 0 replies; 6+ messages in thread

From: James Gregory @ 2003-05-19 02:15 UTC (permalink / raw)
  To: Reece Hart <rkh@gene.COM>; +Cc: pgsql-general

On Sat, 2003-05-17 at 02:23, Reece Hart wrote:
> James-
> 
> Thanks for your advice.  I think I'll opt for the following approach
> instead, but I'm interested to hear of any pitfalls you foresee.
> 
> Subtables (for which I wanted to use inherit) will instead contain only
> the subtable-specific columns, and in addition contain a foreign key
> into the parent table.  A view with insert, update, and delete rules can
> emulate the appearance of a full subtable (ie, inherited and
> subtable-specific columns).
> 
> The upside I see over triggers is that the referential integrity is
> still on postgresql's back.  The downside is that I end up with more
> tables and views to maintain and an extra join in many cases.

Heh, that's quite clever. I must admit I didn't even consider that.
You're right about it requiring lots of views, but my solution requires
lots of triggers. There's not much difference there, but it's more
visible when you forget to make a view - if I forget a trigger I
probably won't see it until the next audit of the database. I decided to
chase the "real inheritance" angle rather than looking for alternatives
such as this because I figured it was likely to be more future
compatible - I was hoping that either the postgres developers would add
inherited RI functionality or I would get time to implement it myself
(which I started doing). Of course, the RI in place atm is done with
triggers, so a C module to do it, along with some clever code to
actually add the triggers would allow it to live in contribs and not
sacrifice too much convenience.

> 
> Again, thanks for your advice.  Do you see any reason that the above
> sketch wouldn't work to emulate foreign keys into inherited tables?

I think it's a good way to proceed with the current version of
postgresql.

James.





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Table Inheritance and Foreign Keys
@ 2019-02-17 22:11  Riaan Stander <rstander@exa.co.za>
  0 siblings, 1 reply; 6+ messages in thread

From: Riaan Stander @ 2019-02-17 22:11 UTC (permalink / raw)
  To: pgsql-general

Good day

 

I've got a scenario where I'm building a table structure that allows for
building a tree. In order to properly separate the different column
requirements I've looked at using table inheritance.

Example structure:

 

CREATE TABLE base_table

(

    id    bigint primary key,

    parent_id    bigint,

    some_common_columns ..

)

 

CREATE TABLE folder

(

    id    bigint primary key,

    some_folder_specific_columns .

) INHERITS(base_table)

 

CREATE TABLE node_type_x

(

    id    bigint primary key,

    some_node_x_specific_columns..

) INHERITS (base_table)

 

Now obviously when inserting folders and node type x records they would go
to their respective tables, but with the table inheritance I can query
base_table to get them all.

The rules state that folders can only be inserted under folders, but node
type x can go under folders or other node type x's.

 

Now if I want to add a foreign key to the base table from node type x I get
an error due to Postgres not supported this scenario. The documentation
talks about this, but also state as follows

"These deficiencies will probably be fixed in some future release, but in
the meantime considerable care is needed in deciding whether inheritance is
useful for your application"

 

My question is if anybody knows more as to if and when this actually will be
allowed/solved.

 

Regards

Riaan

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Table Inheritance and Foreign Keys
@ 2019-02-17 22:58  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Riaan Stander <rstander@exa.co.za>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2019-02-17 22:58 UTC (permalink / raw)
  To: Riaan Stander <rstander@exa.co.za>; +Cc: pgsql-general

"Riaan Stander" <rstander@exa.co.za> writes:
> I've got a scenario where I'm building a table structure that allows for
> building a tree. In order to properly separate the different column
> requirements I've looked at using table inheritance.
> [ but can't point a foreign key at an inheritance tree ]
> My question is if anybody knows more as to if and when this actually will be
> allowed/solved.

Don't hold your breath :-(.  It's been like that for decades.

We do, as of v11, support foreign keys referencing partitioned tables,
but that relies on the ability to declare a unique constraint across
a partitioned table, which in turn relies on there being a known
partitioning key (which has to be part of the unique column set).

Generic inheritance doesn't have that much semantic knowledge about
how the data is divvied up, so it's not really practical to support
these things in a generic inheritance tree.

Probably we should revise that bit of documentation to point people
at the partitioning features; I doubt anything's ever going to be
done about this for generic inheritance.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2019-02-17 22:58 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-05-15 00:07 table inheritance and foreign keys Reece Hart <rkh@gene.COM>
2003-05-15 06:43 ` James Gregory <james@anchor.net.au>
2003-05-16 16:23   ` Reece Hart <rkh@gene.COM>
2003-05-19 02:15     ` James Gregory <james@anchor.net.au>
2019-02-17 22:11 Table Inheritance and Foreign Keys Riaan Stander <rstander@exa.co.za>
2019-02-17 22:58 ` Re: Table Inheritance and Foreign Keys Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox