The list of all the queries

Candidate key columns that have a static default value

Query goal: Find base table columns that are covered by a primary key or a unique constraint and that probably have a static default value.
Query type: Problem detection (Each row in the result could represent a flaw in the design)
Query reliability: Low (Many false-positive results)
Query license: MIT License
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

with 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 constraint_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, constraint_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, a_target.attname as key_column, constraint_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),

def_columns AS (SELECT  table_schema,  table_name, c.column_name, coalesce(column_default, domain_default) AS def
FROM INFORMATION_SCHEMA.columns AS c LEFT JOIN INFORMATION_SCHEMA.domains AS d USING (domain_schema, domain_name)
WHERE coalesce(column_default, domain_default) IS NOT NULL)

SELECT d.table_schema, d.table_name, d.column_name, k.constraint_type, def
FROM def_columns as d INNER JOIN keys_with_names as k 
ON d.table_schema=k.target_schema AND d.table_name=k.target_table AND d.column_name=k.key_column
WHERE def~*'^['']' 
OR  def~*'^(true|false)$' 
OR def~*'^([[:digit:]]|[.])+$'
OR def~*'^[(]+([[:digit:]]|[.])+[)]+'
ORDER BY d.table_schema, d.table_name, d.column_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

Categories where the query belongs to

Category nameCategory description
Comfortability of data managementQueries of this category provide information about the means that have been used to make the use or management of database more comfortable and thus, more efficient.
Default valueQueries of this catergory provide information about the use of default values.
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