Query goal: | Find the total number of columns in the different types of tables as well as average number of columns in the tables as well as minimal and maximal number of columns. |
Query type: | Sofware measure (Numeric values (software measures) about the database) |
Query license: | MIT License |
Data source: | INFORMATION_SCHEMA+system catalog |
SQL query: | Click on query to copy it
CREATE OR REPLACE FUNCTION _final_median(NUMERIC[]) RETURNS NUMERIC AS $$ SELECT AVG(val) FROM ( SELECT val FROM unnest($1) val ORDER BY 1 LIMIT 2 - MOD(array_upper($1, 1), 2) OFFSET CEIL(array_upper($1, 1) / 2.0) - 1 ) sub; $$ LANGUAGE 'sql' IMMUTABLE; CREATE OR REPLACE AGGREGATE median(NUMERIC) ( SFUNC=array_append, STYPE=NUMERIC[], FINALFUNC=_final_median, INITCOND='{}' ); WITH number_of_columns AS (SELECT table_schema, table_name, table_type, Count(*) AS number_of_columns FROM information_schema.columns AS c INNER JOIN INFORMATION_SCHEMA.tables AS t USING (table_schema, table_name) WHERE c.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) GROUP BY table_schema, table_name, table_type UNION SELECT nspname, relname, 'MATERIALIZED VIEW' AS table_type, Count(*) AS number_of_columns FROM pg_class INNER JOIN pg_namespace ON pg_class.relnamespace=pg_namespace.oid INNER JOIN pg_attribute ON pg_class.oid=pg_attribute.attrelid WHERE relkind = 'm' AND attnum>=1 AND nspname NOT IN (SELECT schema_name FROM INFORMATION_SCHEMA.schemata WHERE schema_name<>'public' AND schema_owner='postgres' AND schema_name IS NOT NULL) GROUP BY nspname, relname) SELECT table_type, table_schema, Sum(number_of_columns) total_nr_of_cols, Round(Median(number_of_columns),1) AS median_nr_of_cols, Round(Avg(number_of_columns),1) AS average_nr_of_cols, Max(number_of_columns) AS the_biggest_nr_of_cols, Min(number_of_columns) AS the_smallest_nr_of_cols FROM number_of_columns GROUP BY CUBE (table_type, table_schema); DROP FUNCTION IF EXISTS _final_median(NUMERIC[]) CASCADE; DROP AGGREGATE IF EXISTS median(numeric); |
Collection name | Collection description |
---|---|
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 about integrity constraints | A 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 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 |
Category name | Category description |
---|---|
Comfortability of database evolution | Queries of this category provide information about the means that influence database evolution. |
Derived tables | Queries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer. |
Structure of base tables | Queries of this category provide information about the structuring of base tables at the database conceptual level |