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
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.
Type
Problem detection (Each row in the result could represent a flaw in the design)
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 statements that help generate fixes for the identified problem.
SQL Query to Generate Fix
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.
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 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.
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.