How to find inherited tables programatically in PostgreSQL?

前端 未结 4 838
借酒劲吻你
借酒劲吻你 2021-01-05 13:41

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 ta

4条回答
  •  無奈伤痛
    2021-01-05 14:08

    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);
    

提交回复
热议问题