Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6381811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:26:32+00:00 2026-05-25T02:26:32+00:00

I am trying to build my first Postgres database schema involving inheritance. I am

  • 0

I am trying to build my first Postgres database schema involving inheritance. I am aware of the foreign key problem as discussed in PostgreSQL foreign key not existing, issue of inheritance?. However, the answers on this question did not really give an example for the solution so I came up with my own (inspired by http://people.planetpostgresql.org/dfetter/index.php?/archives/51-Partitioning-Is-Such-Sweet-Sorrow.html):

CREATE TABLE a (
    id  SERIAL PRIMARY KEY INITIALLY DEFERRED,
    foo TEXT
);

CREATE TABLE b (
    PRIMARY KEY(id),
    a_number REAL
) inherits(a);

CREATE TABLE c (
    PRIMARY KEY(id),
    a_number INTEGER
) inherits(a);

-- SELECT * FROM ONLY a; will always return an empty record.

CREATE TABLE  x (
    a_id INTEGER NOT NULL,
    bar  TEXT
);

CREATE TABLE  xb (
    FOREIGN KEY( a_id ) REFERENCES b INITIALLY DEFERRED
) inherits(x);

CREATE TABLE  xc (
    FOREIGN KEY( a_id ) REFERENCES c INITIALLY DEFERRED
) inherits(x);

In order to perform a working INSERT INTO x I defined the following trigger:

CREATE FUNCTION marshal_x()
    RETURNS TRIGGER
    LANGUAGE plpgsql
    AS $$
        DECLARE
            ref_table varchar;
        BEGIN
            SELECT INTO ref_table p.relname FROM a, pg_class p WHERE a.id = NEW.a_id AND a.tableoid = p.oid;

            IF ref_table = 'b'
                THEN INSERT INTO xb ( a_id, bar ) VALUES ( NEW.a_id, NEW.bar );
            ELSIF ref_table = 'c'
                THEN INSERT INTO xc ( a_id, bar ) VALUES ( NEW.a_id, NEW.bar );
            END IF;
            RETURN NULL;
        END;
    $$;

CREATE TRIGGER insert_x_trg
    BEFORE INSERT ON x
    FOR EACH ROW
        EXECUTE PROCEDURE marshal_x();

EDIT: Does anyone see a problem with this definition, which are not obvious to my untrained eye?
Assume table c to be empty. Would the performance of

SELECT * FROM a JOIN x ON a.id= x.a_id;

be the same as

SELECT * FROM b JOIN x ON b.id= x.a_id;

? Many thanks in advance.

Isam

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:26:33+00:00Added an answer on May 25, 2026 at 2:26 am

    What’s the purpose of using inheritance anyway? If you just want different columns depending on row type, like in OOP, you’re better off using a single table and putting NULL in the columns that aren’t applicable. Or split off additional columns to another table, which you join to the original.

    The primary use of inheritance in PostgreSQL is table partitioning and there are a number of caveats with inheritance. PostgreSQL 9.1 introduces a “Merge Append” optimization that helps somewhat, but it’s still suboptimal.

    SELECT * FROM a JOIN x ON a.id= x.a_id;

    Joining two large-ish inherited tables together sounds like it won’t scale very well, particularly as you increase the number of child tables. PostgreSQL is not smart enough to follow the xb and xc foreign key constraints here, it would try to join the whole table a to the whole of x.

    But then again, it could turn out not to be a problem, it all depends on your queries and performance expectations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build my first chrome extension, I have a problem where
I am trying to build my first XML schema validator as a reusable component
I'm trying to build my first generic list and have run into some problems.
I'm trying to build my first cocoa application. done some iPhone developing before. I
I am trying to build my first CRUD application and cant get the update
I'm trying to build my first fractal (Pythagoras Tree): alt text http://img13.imageshack.us/img13/926/lab6e.jpg in Java
I've been trying to build my first rails app and have gotten stuck on
I am (trying) to build a web app, first one :) I need to
I'm trying to build an FAQ page. The page will have 2 columns: first
This is my first post on Stack Overflow. I am trying to build a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.