Goal Find the number of tables (base, foreign, and derived) in different schemas.
Notes The query (due to the use of CUBE) also finds the number of tables in different schemas, the number of different types of tables, and the total number of tables. The query only returns data about schemas that have at least one table.
Type Sofware measure (Numeric values (software measures) about the database)
License MIT License
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH tables_union AS (SELECT tables.table_schema, tables.table_type
FROM information_schema.tables
WHERE table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
UNION ALL SELECT schemaname AS table_schema,  'MATERIALIZED VIEW' AS table_type
FROM pg_catalog.pg_matviews)
SELECT table_schema, table_type, count(*) AS number_of_tables
FROM tables_union
GROUP BY CUBE (table_schema, table_type)
ORDER BY table_schema, table_type;

Collections

This query belongs to the following collections:

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 about integrity constraintsA selection of queries that return information about the state of integrity constraints in the datadabase. 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 .
Categories

This query is classified under the following categories:

NameDescription
Derived tablesQueries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer.
Structure of base tablesQueries of this category provide information about the structuring of base tables at the database conceptual level