Query goal: | Find sequence generators that are not associated with any column through the default value mechanism. Please note, that it is also possible to refer to a sequence generator from a routine or from an application. If these are indeed not used, then these should be dropped, otherwise these are dead code. |
Notes about the query: | The query takes into account both external and internal (are created as the result of declaring a column as the identity column) sequence generators. The query takes into account a possibility that a column may be associated with a sequence generator through a domain. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | Medium (Medium number of false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Start to use the sequence generator or drop it. |
Data source: | INFORMATION_SCHEMA only |
SQL query: | Click on query to copy it
WITH sequences AS (SELECT sequence_schema, sequence_name FROM information_schema.sequences WHERE sequence_schema NOT IN (SELECT schema_name FROM information_schema.schemata WHERE schema_name<>'public' AND schema_owner='postgres' AND schema_name IS NOT NULL)), columns_with_sequences AS (SELECT A.table_schema, A.table_name , A.column_name, A.data_type, A.column_default FROM information_schema.columns A LEFT JOIN information_schema.domains d USING (domain_schema, domain_name) INNER JOIN information_schema.tables T ON A.table_schema = T.table_schema AND A.table_name = T.table_name INNER JOIN information_schema.schemata S ON A.table_schema=S.schema_name WHERE coalesce (a.column_default, d.domain_default) ILIKE '%nextval%' AND T.table_type='BASE TABLE' AND (A.table_schema = 'public' OR S.schema_owner<>'postgres')) SELECT sequence_schema, sequence_name FROM sequences AS s WHERE NOT EXISTS (SELECT 1 FROM columns_with_sequences AS cs WHERE cs.column_default ILIKE '%' || replace(s.sequence_name,'_','\_') || '%') ORDER BY sequence_schema, sequence_name; |
SQL query | Description |
---|---|
WITH sequences AS (SELECT sequence_schema, sequence_name FROM information_schema.sequences WHERE sequence_schema NOT IN (SELECT schema_name FROM information_schema.schemata WHERE schema_name<>'public' AND schema_owner='postgres' AND schema_name IS NOT NULL)), columns_with_sequences AS (SELECT A.table_schema, A.table_name , A.column_name, A.data_type, A.column_default FROM information_schema.columns A LEFT JOIN information_schema.domains d USING (domain_schema, domain_name) INNER JOIN information_schema.tables T ON A.table_schema = T.table_schema AND A.table_name = T.table_name INNER JOIN information_schema.schemata S ON A.table_schema=S.schema_name WHERE coalesce (a.column_default, d.domain_default) LIKE '%nextval%' AND T.table_type='BASE TABLE' AND (A.table_schema = 'public' OR S.schema_owner<>'postgres')) SELECT format('DROP SEQUENCE %1$I.%2$I;', sequence_schema, sequence_name) AS statements FROM sequences AS s WHERE NOT EXISTS (SELECT 1 FROM columns_with_sequences AS cs WHERE cs.column_default ILIKE '%' || replace(s.sequence_name,'_','\_') || '%') ORDER BY sequence_schema, sequence_name; | Drop the sequence. |
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 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 . |
Category name | Category description |
---|---|
Comfortability of database evolution | Queries of this category provide information about the means that influence database evolution. |
Sequence generators | Queries of this category provide information about sequence generators and their usage. |
Unused implementation elements | Queries of this catergory provide information about the database objects that are not used. |
Reference |
---|
https://en.wikipedia.org/wiki/Dead_code |
The corresponding code smells in case of cleaning code are "F4: Dead Function" and "G9: Dead Code". (Robert C. Martin, Clean Code) |