How to find inherited tables programatically in PostgreSQL?

前端 未结 4 817
借酒劲吻你
借酒劲吻你 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:07

    The following statement retrieves all child tables of the table public.base_table_name:

    select bt.relname as table_name, bns.nspname as table_schema 
    from pg_class ct 
        join pg_namespace cns on ct.relnamespace = cns.oid and cns.nspname = 'public' 
        join pg_inherits i on i.inhparent = ct.oid and ct.relname = 'base_table_name' 
        join pg_class bt on i.inhrelid = bt.oid 
        join pg_namespace bns on bt.relnamespace = bns.oid
    

    It should work with 8.3 although I'm not 100% sure.

提交回复
热议问题