The list of all the queries

Potentially missing PRIMARY KEY or UNIQUE constraints (based on foreign keys)

Query goal: All the keys must be enforced. Find base tables that implement M:N relationship types and that allow multiple relationships of the same type between the same entities.
Notes about the query: Find tables that have at least two foreign key constraints, do not have any other columns except for primary key/unique/foreign key columns, and where the primary key/unique and foreign key columns do not overlap.
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: Declare missing keys by using UNIQUE + NOT NULL constraints.
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

WITH f AS (select (select nspname from pg_namespace where oid=c.relnamespace) as target_ns,
c.relname as target_table
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
inner join pg_class f on f.oid = o.confrelid
where o.contype = 'f'),
non_key_columns AS (SELECT table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.columns c
WHERE (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE') AND 
table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND schema_owner='postgres')
EXCEPT SELECT table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.key_column_usage kcu),
overlapping_key_columns AS (
SELECT table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.key_column_usage kcu
WHERE position_in_unique_constraint IS NULL
INTERSECT SELECT table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.key_column_usage kcu
WHERE position_in_unique_constraint IS NOT NULL)
SELECT t.table_schema, t.table_name
FROM INFORMATION_SCHEMA.tables t
INNER JOIN INFORMATION_SCHEMA.schemata s
ON t.table_schema=s.schema_name 
WHERE t.table_type='BASE TABLE' AND 
(t.table_schema = 'public' OR s.schema_owner<>'postgres')  AND
(SELECT Count(*) AS cnt FROM f WHERE t.table_schema=f.target_ns AND t.table_name=f.target_table)>=2 AND
NOT EXISTS (SELECT 1 FROM non_key_columns nkc WHERE nkc.table_schema=t.table_schema AND nkc.table_name=t.table_name) AND
NOT EXISTS (SELECT 1 FROM overlapping_key_columns okc WHERE okc.table_schema=t.table_schema AND okc.table_name=t.table_name)
ORDER BY t.table_schema, t.table_name;

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
Relationships between tablesQueries of this category provide information about how database tables are connected to each other and whether such connections have been explicitly defined and whether it has been done correctly.
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