Find the total number of columns with a default value as well as the number of columns with different kinds of default values (the number of columns where the default is used to implement surrogate key, the number of columns where the default is not used to implement surrogate key, the number of columns with a static default value, the number of columns with a dynamic default value).
Type
Sofware measure (Numeric values (software measures) about the database)
WITH defs AS (SELECT c.table_schema, t.table_type, c.table_name, c.column_name, c.data_type, c.domain_schema, c.domain_name, coalesce(c.column_default, domain_default) AS default_value,
CASE WHEN c.column_default IS NOT NULL THEN 'Column default' ELSE 'Domain default' END AS default_type
FROM information_schema.columns AS c LEFT JOIN information_schema.domains d USING (domain_schema, domain_name)
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)
AND (column_default IS NOT NULL OR domain_default IS NOT NULL))
SELECT table_schema,
Count(DISTINCT (table_schema || '.' || table_name)) AS nr_of_tables,
Count(DISTINCT (table_schema || '.' || table_name)) FILTER (WHERE default_value~*'nextval[(]') AS nr_of_tables_for_surrogate_key,
Count(DISTINCT (table_schema || '.' || table_name)) FILTER (WHERE default_value!~*'nextval[(]') AS nr_of_tables_for_non_surrogate_key,
Count(*) AS nr_of_default_values,
Count(*) FILTER (WHERE default_value~*'nextval[(]') AS nr_for_surrogate_key,
Count(*) FILTER (WHERE default_value!~*'nextval[(]') AS nr_for_non_surrogate_key,
Count(*) FILTER (WHERE NOT (default_value~*'^['']' OR default_value~*'^(true|false)$' OR default_value~*'^([[:digit:]]|[.])+$' OR default_value~*'^[(]+([[:digit:]]|[.])+[)]+')) AS nr_of_dynamic,
Count(*) FILTER (WHERE (NOT (default_value~*'^['']' OR default_value~*'^(true|false)$' OR default_value~*'^([[:digit:]]|[.])+$' OR default_value~*'^[(]+([[:digit:]]|[.])+[)]+')) AND default_value!~*'nextval[(]') AS nr_of_dynamic_non_surrogate,
Count(*) FILTER (WHERE default_value~*'^['']' OR default_value~*'^(true|false)$' OR default_value~*'^([[:digit:]]|[.])+$' OR default_value~*'^[(]+([[:digit:]]|[.])+[)]+') AS nr_of_static
FROM defs
GROUP BY ROLLUP (table_schema);
Collections
This query belongs to the following collections:
Name
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 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
Categories
This query is classified under the following categories:
Name
Description
Comfortability of data management
Queries of this category provide information about the means that have been used to make the use or management of database more comfortable and thus, more efficient.
Default value
Queries of this catergory provide information about the use of default values.