This query identifies potential schema discrepancies related to column sizing. It flags text columns in base tables where the static default value is unusually short—specifically, less than half the column's maximum allowed capacity.
Type
Problem detection (Each row in the result could represent a flaw in the design)
SELECT c.table_schema, c.table_name, c.column_name, c.data_type, c.character_maximum_length AS suspected_field_size,
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,
char_length(regexp_replace(coalesce(c.column_default, domain_default), '::[^:]*$', ''))-2 AS default_length
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)
AND c.data_type ~ '(character|text)'
AND coalesce(c.column_default, domain_default)~*'\''.*\'''
AND (coalesce(c.character_maximum_length,1000000)/2)>char_length(regexp_replace(coalesce(c.column_default, domain_default), '::[^:]*$', ''))-2
ORDER BY c.table_schema, c.table_name, c.column_name;
Collections
This query belongs to the following collections:
Name
Description
Find problems automatically
Queries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .
Categories
This query is classified under the following categories:
Name
Description
Default value
Queries of this catergory provide information about the use of default values.
Field size
Queries of this category provide information about the maximum size of values that can be recorded in column fields