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 8443259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:05:26+00:00 2026-06-10T09:05:26+00:00

I have a PostgreSQL 8.3 database where table inheritance is being used. I would

  • 0

I have a PostgreSQL 8.3 database where table inheritance is being used. I would like to get a list of all tables along with its schema name which is inherited from a base table using query. Is there any way we can get this using PGSQL?

  • 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-06-10T09:05:28+00:00Added an answer on June 10, 2026 at 9:05 am

    Since you’re on such an old version of PostgreSQL you’ll probably have to use a PL/PgSQL function to handle inheritance depths of > 1. On modern PostgreSQL (or even 8.4) you’d use a recursive common table expression (WITH RECURSIVE).

    The pg_catalog.pg_inherits table is the key. Given:

    create table pp( );     -- The parent we'll search for
    CREATE TABLE notpp(); -- Another root for multiple inheritance
    create table cc( ) inherits (pp); -- a 1st level child of pp
    create table dd( ) inherits (cc,notpp); -- a 2nd level child of pp that also inherits aa
    create table notshown( ) inherits (notpp); -- Table that inherits only notpp
    create table ccdd () inherits (cc,dd) -- Inheritance is a graph not a tree; join node
    

    A correct result will find cc, dd, and ccdd, but not find notpp or notshown.

    A single-depth query is:

    SELECT pg_namespace.nspname, pg_class.relname 
    FROM pg_catalog.pg_inherits 
      INNER JOIN pg_catalog.pg_class ON (pg_inherits.inhrelid = pg_class.oid) 
      INNER JOIN pg_catalog.pg_namespace ON (pg_class.relnamespace = pg_namespace.oid) 
    WHERE inhparent = 'pp'::regclass;
    

    … but this will only find cc.

    For multi-depth inheritance (ie tableC inherits tableB inherits tableA) you have to extend that via a recursive CTE or a loop in PL/PgSQL, using the children of the last loop as parents in the next.

    Update: Here’s an 8.3 compatible version that should recursively find all tables that inherit directly or indirectly from a given parent. If multiple inheritance is used, it should find any table that has the target table as one of its parents at any point along the tree.

    CREATE OR REPLACE FUNCTION find_children(oid) RETURNS SETOF oid as $$
    SELECT i.inhrelid FROM pg_catalog.pg_inherits i WHERE i.inhparent = $1
    UNION
    SELECT find_children(i.inhrelid) FROM pg_catalog.pg_inherits i WHERE i.inhparent = $1;
    $$ LANGUAGE 'sql' STABLE;
    
    CREATE OR REPLACE FUNCTION find_children_of(parentoid IN regclass, schemaname OUT name, tablename OUT name) RETURNS SETOF record AS $$
    SELECT pg_namespace.nspname, pg_class.relname 
            FROM find_children($1) inh(inhrelid) 
              INNER JOIN pg_catalog.pg_class ON (inh.inhrelid = pg_class.oid) 
              INNER JOIN pg_catalog.pg_namespace ON (pg_class.relnamespace = pg_namespace.oid);
    $$ LANGUAGE 'sql' STABLE;
    

    Usage:

    regress=# SELECT * FROM find_children_of('pp'::regclass);
     schemaname | tablename 
    ------------+-----------
     public     | cc
     public     | dd
     public     | ccdd
    (3 rows)
    

    Here’s the recursive CTE version, which will work if you update Pg, but won’t work on your current version. It’s much cleaner IMO.

    WITH RECURSIVE inh AS (
            SELECT i.inhrelid FROM pg_catalog.pg_inherits i WHERE inhparent = 'pp'::regclass
            UNION
            SELECT i.inhrelid FROM inh INNER JOIN pg_catalog.pg_inherits i ON (inh.inhrelid = i.inhparent)
    )
    SELECT pg_namespace.nspname, pg_class.relname 
        FROM inh 
          INNER JOIN pg_catalog.pg_class ON (inh.inhrelid = pg_class.oid) 
          INNER JOIN pg_catalog.pg_namespace ON (pg_class.relnamespace = pg_namespace.oid);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a postgresql database I have 2 tables like these ones: Table items: item_id
I have a table in my postgresql 8.4 database like this: id(serial), event_type_id(id, foreign
Suppose we have a PostgreSQL database with two tables A, B. table A columns:
I have a PostgreSQL database with PostGIS functions loaded into it. I would like
I have the following table and sequence in my postgresql-8.4 database: CREATE TABLE complexobjectpy
i have latitude and longitude columns in location table in PostgreSQL database, and I
I have a table in a postgresql-9.1.x database which is defined as follows: #
I have the following table in my postgreSQL 8.3.14 database timestamp status 2012-03-12 19:15:01
Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships.
I have a large readonly Wordnet PostgreSQL database that I'd like to use from

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.