3

What query should I implement to get columns that are inherited? Having read this comprehensive post didn't find the solution.

Vadim Samokhin
  • 3,378
  • 4
  • 40
  • 68

1 Answers1

4

If I understand correctly, you want to know the names of the columns that are part of an inheritance between tables.

SELECT nmsp_parent.nspname    AS parent_schema,
       parent.relname         AS parent_table,
       nmsp_child.nspname     AS child_schema,
       child.relname          AS child_table,           
       column_parent.attname  AS column_parent_name
FROM pg_inherits
JOIN pg_class parent            ON pg_inherits.inhparent  = parent.oid
JOIN pg_class child             ON pg_inherits.inhrelid   = child.oid
JOIN pg_namespace nmsp_parent   ON nmsp_parent.oid        = parent.relnamespace
JOIN pg_namespace nmsp_child    ON nmsp_child.oid         = child.relnamespace
JOIN pg_attribute column_parent ON column_parent.attrelid = parent.oid
WHERE column_parent.attnum > 0
AND column_parent.attname NOT ILIKE '%pg.dropped%';

This query displays the column name that is part of a hierarchy. I hope you serve

doctore
  • 3,855
  • 2
  • 29
  • 45