The list of all the queries

Definition of a non-minimal superkey instead of a candidate key (based on sequence generators)

Query goal: Candidate key is a minimal superkey, meaning that it is not possible to remove columns from the candidate key without losing its uniqueness property. One should define primary key's and unique constraints based on candidate keys. Find primary keys and unique constraints where a proper subset of the key is a column with an associated sequence generator. Most certainly such subset contains unique values. Thus, this subset should be the key, not its superset.
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: Drop the primary key/unique constraint. Declare primary key/unique constraint to the subset of columns.
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

with def_columns AS (SELECT  table_schema,  table_name, c.column_name, c.is_identity, coalesce(column_default, domain_default) AS def
FROM INFORMATION_SCHEMA.columns AS c LEFT JOIN INFORMATION_SCHEMA.domains AS d ON c.domain_schema=d.domain_schema AND c.domain_name=d.domain_name),
def_columns_with_names AS (
SELECT table_schema, table_name, array_agg(column_name ORDER BY column_name)::text[] AS def_columns
FROM def_columns
WHERE def ILIKE '%nextval%' OR is_identity='YES'
GROUP BY table_schema, table_name),
keys as (select 
o.conname, 
(select nspname from pg_namespace where oid=c.relnamespace) as target_schema,
c.relname as target_table, 
c.oid as target_table_oid,
o.conkey AS target_col,
case when o.contype='p' then 'PRIMARY KEY' ELSE 'UNIQUE' END as key_type
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
where o.contype in ('u', 'p') ),
keys_unnest as (select conname, target_schema, target_table, target_table_oid, target_col, target_col_num, ordin, key_type
from keys, unnest(keys. Target_col) with ordinality as f(target_col_num, ordin)),
keys_with_names as (select conname, target_schema, target_table, array_agg(a_target.attname order by a_target.attname)::text[] as key_columns, key_type
from keys_unnest k inner join  pg_attribute a_target on k.target_col_num = a_target.attnum and k.target_table_oid = a_target.attrelid and a_target.attisdropped = false
group by conname, target_schema, target_table, key_type)
SELECT K.target_schema, K.target_table, K.key_type, key_columns AS perhaps_superkey, def_columns AS perhaps_candidate_key 
FROM def_columns_with_names AS Dc, keys_with_names AS K
WHERE Dc.table_schema=K.target_schema AND Dc.table_name=K.target_table AND cardinality(key_columns)>cardinality(def_columns) AND key_columns@>def_columns
ORDER BY K.target_schema, K.target_table;

Collections where the query belongs to

Collection nameCollection description
Find problems about integrity constraintsA selection of queries that return information about the state of integrity constraints in the datadabase. Contains all the types of queries - problem detection, software measure, and general overview
Find problems automaticallyQueries, 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 where the query belongs to

Category nameCategory description
Sequence generatorsQueries of this category provide information about sequence generators and their usage.
UniquenessQueries of this category provide information about uniqueness constraints (PRIMARY KEY, UNIQUE, EXCLUDE) as well as unique indexes.
Validity and completenessQueries of this category provide information about whether database design represents the world (domain) correctly (validity) and whether database design captures all the information about the world (domain) that is correct and relevant (completeness).

The list of all the queries