Goal Find simple check constraints, i.e., check constraints that cover one column that seem to have multiple tasks. The corresponding code smell in case of cleaning code is "G30: Functions Should Do One Thing". (Robert C. Martin, Clean Code)
Notes The query finds CHECK constraints that are associated with a base table directly as well as CHECK constraints that are associated with domains that are used to define at least one column. The query does not find CHECK constraints of domains that are not associated with any table.
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability Low (Many false-positive results)
License MIT (opens in new tab)
Fixing Suggestion Create multiple CHECK constraints instead of one.
Data Source system catalog only
SQL Query
WITH all_simple_checks AS (SELECT
  (SELECT nspname FROM pg_namespace WHERE oid=c.relnamespace) AS object_schema,
  c.relname || '.' || a.attname AS target,  
  COALESCE(basetype.typname, t.typname) AS data_type,
  o.conname AS constraint_name,  
  pg_get_constraintdef(o.oid) AS check_clause,
  'TABLE CHECK' AS check_type
FROM pg_constraint o
INNER JOIN pg_class c ON c.oid = o.conrelid
INNER JOIN pg_attribute a ON a.attrelid = o.conrelid AND a.attnum = o.conkey[1]
INNER JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_type basetype ON t.typtype = 'd' AND t.typbasetype = basetype.oid
WHERE o.contype = 'c' AND cardinality(o.conkey) = 1
UNION SELECT
  ns.nspname AS object_schema,
  t.typname AS target,  
  basetype.typname AS data_type,
  c.conname AS constraint_name,
  pg_get_constraintdef(c.oid) AS check_clause,
  'DOMAIN CHECK' AS check_type
FROM pg_constraint c
INNER JOIN pg_type t ON c.contypid = t.oid
INNER JOIN pg_namespace ns ON t.typnamespace = ns.oid
INNER JOIN pg_type basetype ON t.typbasetype = basetype.oid
WHERE
  c.contype = 'c'
  AND c.contypid <> 0
  AND pg_get_constraintdef(c.oid) <> 'CHECK ((VALUE IS NOT NULL))')
SELECT object_schema, target, check_clause, check_type, constraint_name
FROM all_simple_checks
WHERE check_clause~*'[[:space:]]AND[[:space:]]'
AND check_clause!~*'[[:space:]]OR[[:space:]]'
AND check_clause~*'[[:space:]](=|<>|~|!~|~\*|!~\*|~~|~~\*|!~~|!~~*)[[:space:]]'
ORDER BY check_type, object_schema, target;

Collections

This query belongs to the following collections:

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 .

NameDescription
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

This query is classified under the following categories:

CHECK constraints

Queries of this category provide information about CHECK constraints.

Comfortability of database evolution

Queries of this category provide information about the means that influence database evolution.

NameDescription
CHECK constraintsQueries of this category provide information about CHECK constraints.
Comfortability of database evolutionQueries of this category provide information about the means that influence database evolution.