Goal Find all the base tables that serve as supertables in the inheritance hierarchies
Notes The query finds base tables that participate in an inheritance hierarchy and do not have any parent table.
Type Sofware measure (Numeric values (software measures) about the database)
License MIT (opens in new tab)
Data Source system catalog only
SQL Query
WITH tables_in_inheritance_hierarchy AS (SELECT pn.nspname AS parent_schema, p.oid AS parent_oid, p.relname AS parent_table, pi.inhrelid AS child_oid
FROM pg_inherits pi INNER JOIN pg_class p ON pi.inhparent=p.oid
INNER JOIN pg_namespace pn ON p.relnamespace=pn.oid
INNER JOIN pg_authid AS ap ON pn.nspowner=ap.oid
WHERE (pn.nspname='public' OR ap.rolname<>'postgres'))
SELECT parent_schema, parent_table, Count(*) AS number_of_direct_children
FROM tables_in_inheritance_hierarchy AS tih
WHERE NOT EXISTS (SELECT 1
FROM tables_in_inheritance_hierarchy AS tih_check 
WHERE tih.parent_oid=tih_check.child_oid)
GROUP BY parent_schema, parent_table
ORDER BY Count(*) DESC, parent_schema, parent_table;

Collections

This query belongs to the following collections:

Find problems about base tables

A selection of queries that return information about the data types, field sizes, default values as well as general structure of base tables. Contains all the types of queries - problem detection, software measure, and general overview

Find problems by overview

Queries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .

Find quick numeric overview of the database

Queries that return numeric values showing mostly the number of different types of database objects in the database

NameDescription
Find problems about base tablesA selection of queries that return information about the data types, field sizes, default values as well as general structure of base tables. Contains all the types of queries - problem detection, software measure, and general overview
Find problems by overviewQueries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .
Find quick numeric overview of the databaseQueries that return numeric values showing mostly the number of different types of database objects in the database

Categories

This query is classified under the following categories:

Table inheritance

Queries of this category provide information about the inheritance between base tables.

NameDescription
Table inheritanceQueries of this category provide information about the inheritance between base tables.