Goal This query identifies CHECK constraints that contain redundant logic for handling NULLs, a pattern often arising from a misunderstanding of SQL's three-valued logic (TRUE, FALSE, UNKNOWN). A CHECK constraint's condition only fails on FALSE, implicitly permitting NULLs by evaluating comparisons to UNKNOWN. Consequently, an explicit OR column IS NULL clause is tautological and adds no functional value, as demonstrated by the equivalence of CHECK (price IS NULL OR price > 0) and CHECK (price > 0). Removing this superfluous logic improves constraint clarity and eliminates a marginal but unnecessary computational step.
Notes In SQL, CHECK constraints fail only when an expression evaluates to FALSE; any comparison involving a NULL value evaluates to UNKNOWN, which inherently satisfies the constraint and permits the row. Because of this three-valued logic, explicitly combining an IS NULL check with another condition on the exact same column—regardless of whether the null check appears before or after the OR operator—is completely redundant and adds no functional value. This validation rule detects such superfluous logic by verifying that the column tested for nullability is the very same column being evaluated in the alternate branch of the disjunction. Crucially, enforcing that both operands reference the same column prevents false alarms, distinguishing tautological checks from legitimate conditional business rules where a requirement on one column dictates whether a completely different column must be NULL.
Type Problem detection Each row in the result could represent a flaw in the design
Reliability Medium Medium number of false-positive results
License MIT (opens in new tab)
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH expressions AS (
    SELECT 
        n.nspname AS schema,
        c.relname || '.' || o.conname AS name, 
        substring(pg_get_constraintdef(o.oid), 7) AS expression,
        'TABLE CHECK' AS type
    FROM pg_constraint o 
    INNER JOIN pg_class c ON c.oid = o.conrelid
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE o.contype = 'c'   
      AND n.nspname NOT IN (
          SELECT schema_name
          FROM information_schema.schemata
          WHERE schema_name <> 'public' 
            AND schema_owner = 'postgres' 
            AND schema_name IS NOT NULL
      )
    UNION 
    SELECT 
        n.nspname AS schema,
        t.typname || '.' || o.conname AS name, 
        substring(pg_get_constraintdef(o.oid), 7) AS expression,
        'DOMAIN CHECK' AS type
    FROM pg_constraint o 
    INNER JOIN pg_type t ON t.oid = o.contypid
    INNER JOIN pg_namespace n ON n.oid = t.typnamespace
    WHERE o.contype = 'c' 
      AND n.nspname NOT IN (
          SELECT schema_name
          FROM information_schema.schemata
          WHERE schema_name <> 'public' 
            AND schema_owner = 'postgres' 
            AND schema_name IS NOT NULL
      )
)
SELECT 
    schema, 
    name AS object_identifier, 
    type AS object_type, 
    expression AS suspected_expression
FROM expressions
WHERE 
    expression ~* '(\y[[:alnum:]_]+\y)\W+IS\s+NULL\y.*\yOR\y.*\y\1\y'
    OR 
    expression ~* '(\y[[:alnum:]_]+\y)\y.*\yOR\y.*\y\1\W+IS\s+NULL\y'
ORDER BY schema, type, name;

Fixing Suggestion

Remove ( IS NULL) condition from the check.

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.

Missing data

Queries of this category provide information about missing data (NULLs) in a database.

NameDescription
CHECK constraintsQueries of this category provide information about CHECK constraints.
Missing dataQueries of this category provide information about missing data (NULLs) in a database.