Find cases where in different schemas there are enumerated or range types with the same name. Types are like words that can be used to construct generalized claims about the real world (table predicates). Better not to duplicate the words in the dictionary. Also make sure that this is not a duplication.
Type
Problem detection (Each row in the result could represent a flaw in the design)
Rename a type. In case of duplication drop the duplicates and refactor the database so that the remaining type will be used everywhere it is needed.
Data Source
INFORMATION_SCHEMA+system catalog
SQL Query
WITH types AS (SELECT nspname AS type_schema, typname AS type_name, typtype
FROM pg_catalog.pg_type t INNER JOIN pg_catalog.pg_namespace n ON t.typnamespace=n.oid
WHERE typtype IN ('e','r') 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))
SELECT type_name, string_agg(type_schema || '(' || CASE WHEN typtype='e' THEN 'ENUMERATED' ELSE 'RANGE' END || ')',';' ORDER BY type_schema) AS schemas, Count(*) AS number_of_types
FROM types
GROUP BY type_name
HAVING Count(*)>1
ORDER BY type_name;
SQL statements that help generate fixes for the identified problem.
SQL Query to Generate Fix
Description
WITH types AS (SELECT nspname AS type_schema, typname AS type_name
FROM pg_catalog.pg_type t INNER JOIN pg_catalog.pg_namespace n ON t.typnamespace=n.oid
WHERE typtype IN ('e','r') 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)),
duplicates AS (SELECT type_name
FROM types
GROUP BY type_name
HAVING Count(*)>1)
SELECT format('DROP TYPE %1$I.%2$I;', type_schema, type_name) AS statements
FROM types
WHERE type_name IN (SELECT type_name
FROM duplicates)
ORDER BY type_schema, type_name;
Drop the duplicates.
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
Comfortability of database evolution
Queries of this category provide information about the means that influence database evolution.
Data types
Queries of this category provide information about the data types and their usage.
Duplication of implementation elements
Queries of this catergory provide information about the duplication of the database objects.
Naming
Queries of this category provide information about the style of naming.