Check if table inherits from other table in PostgreSQL

后端 未结 3 1771
南方客
南方客 2021-01-17 17:05

In PostgreSQL for these tables

CREATE TABLE cities (
    name            text,
    population      float,
    altitude        int     -- in feet
);

CREATE T         


        
3条回答
  •  Happy的楠姐
    2021-01-17 17:28

    There is a catalog table for that: pg_inherits.

    The catalog pg_inherits records information about table inheritance hierarchies. There is one entry for each direct child table in the database. (Indirect inheritance can be determined by following chains of entries.)

    Here's a query that fits your question:

    SELECT EXISTS (
       SELECT 1
       FROM   pg_catalog.pg_inherits
       WHERE  inhrelid = 'public.cities_capitals'::regclass
       );
    

    TRUE if table cities_capitals inherits from somewhere, else FALSE.
    Schema-qualify the name to be sure.

提交回复
热议问题