Goal This query identifies numeric columns (including columns typed via a domain over numeric) that use a single-column table or domain CHECK constraint to restrict the number of decimal places (digits after the decimal point), such as CHECK (price = round(price, 2)), CHECK (trunc(price, 2) = price), CHECK (scale(price) <= 2), CHECK (mod(price, 0.01) = 0), or CHECK (price::text ~ '^[0-9]+(\.[0-9]{1,2})?$').

In PostgreSQL, decimal precision and scale are normally declared directly on the data type itself (e.g., NUMERIC(10, 2)). Enforcing scale via a CHECK constraint instead of the type definition hides the scale from database metadata catalogs (such as information_schema.columns.numeric_scale) and ORM/UI tools, while adding unnecessary per-row function evaluation overhead.

Moreover, if the column already specifies a matching scale (e.g., NUMERIC(10, 2), where numeric_scale IS NOT NULL), the CHECK constraint is completely redundant because PostgreSQL automatically rounds input values to the column's declared scale before evaluating CHECK constraints. Note that for an unconstrained NUMERIC column (numeric_scale IS NULL), a CHECK constraint strictly rejects excess decimal digits (e.g., 1.234) with an error, whereas NUMERIC(10, 2) silently rounds them to 1.23; reviewers should verify which behavior is required.
Notes Queries PostgreSQL system catalogs (pg_constraint, pg_class, pg_namespace, pg_attribute, pg_type) and information_schema views (columns, tables, column_domain_usage, domain_constraints, check_constraints) to collect single-column table CHECK constraints (contype = 'c', cardinality(conkey) = 1) and domain CHECK constraints on base-table numeric columns. Joining pg_type via typbasetype ensures that table-level CHECK constraints placed on domain-typed numeric columns are also captured.

The numeric_chk CTE uses case-insensitive regular expressions (~*) to detect four common decimal-scale restriction patterns (supporting both table column names and the domain keyword VALUE):

  • Self-comparisons using round, trunc, floor, ceil, or ceiling in either operand order (e.g., col = round(col, 2), round(col, 2) = col, or (col * 100) = trunc(col * 100)).

  • Usage of PostgreSQL's scale(...) function (e.g., scale(col) <= 2 or scale(trim_scale(col)) <= 2).

  • Modulo checks against 1 or fractional decimals (e.g., mod(col, 0.01) = 0, mod(col * 100, 1) = 0, or (col % 0.01) = 0).

  • Casting the numeric column to text or varchar and matching it with a regular expression operator (~, ~*, !~, !~*).



The output includes numeric_precision, numeric_scale, and the aggregated CHECK constraint definitions separated by ,<br> for better readability in a web browser.
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 columns_with_domains_chk AS (
    SELECT 
        cdu.table_schema, 
        cdu.table_name, 
        cdu.column_name, 
        (SELECT data_type 
         FROM INFORMATION_SCHEMA.domains AS d
         WHERE d.domain_schema = dc.domain_schema 
           AND d.domain_name = dc.domain_name) AS data_type, 
        cc.check_clause
    FROM INFORMATION_SCHEMA.column_domain_usage AS cdu 
    INNER JOIN INFORMATION_SCHEMA.tables AS t USING (table_schema, table_name)
    INNER JOIN INFORMATION_SCHEMA.domain_constraints AS dc USING (domain_schema, domain_name)
    INNER JOIN INFORMATION_SCHEMA.check_constraints AS cc USING (constraint_schema, constraint_name)
    WHERE t.table_type IN ('BASE TABLE') 
      AND cc.check_clause NOT LIKE '%IS NOT NULL'    
      AND cdu.table_schema NOT IN (
          SELECT schema_name
          FROM INFORMATION_SCHEMA.schemata
          WHERE schema_name <> 'public' 
            AND schema_owner = 'postgres' 
            AND schema_name IS NOT NULL
      )
),

numeric_columns_with_domains_chk AS (
    SELECT table_schema, table_name, column_name, data_type, check_clause
    FROM columns_with_domains_chk
    WHERE data_type = 'numeric'
),

numeric_simple_chk AS (
    SELECT 
        n.nspname AS table_schema,
        c.relname AS table_name, 
        a.attname AS column_name,
        COALESCE(bt.typname, t.typname) AS type_name,
        pg_get_constraintdef(o.oid) AS check_clause
    FROM pg_constraint o 
    INNER JOIN pg_class c ON c.oid = o.conrelid
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    INNER JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = o.conkey[1] AND a.attisdropped = FALSE
    INNER JOIN pg_type t ON a.atttypid = t.oid
    -- Tugi ka domeenipõhistele numeric-veergudele, mille CHECK on tabeli tasemel:
    LEFT JOIN pg_type bt ON t.typbasetype = bt.oid
    WHERE cardinality(o.conkey) = 1 
      AND o.contype = 'c' 
      AND c.relkind IN ('r', 'p')
      AND (t.typname = 'numeric' OR bt.typname = 'numeric')
      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 table_schema, table_name, column_name, data_type, check_clause
    FROM numeric_columns_with_domains_chk
),

numeric_chk AS (
    SELECT table_schema, table_name, column_name, type_name
    FROM numeric_simple_chk
    WHERE 
        check_clause ~* ('\y(' || column_name || '|value)\y[^=]*=[[:space:]]*\(*[[:space:]]*\y(round|trunc|floor|ceil|ceiling)[[:space:]]*\([^)]*\y(' || column_name || '|value)\y')
        OR check_clause ~* ('\y(round|trunc|floor|ceil|ceiling)[[:space:]]*\([^)]*\y(' || column_name || '|value)\y[^=]*=[[:space:]]*\(*[[:space:]]*\y(' || column_name || '|value)\y')
        OR check_clause ~* '\yscale[[:space:]]*\('
        OR check_clause ~* ('\ymod[[:space:]]*\([^)]*\y(' || column_name || '|value)\y[^)]*,[[:space:]]*\(?(1|0\.[0-9]+)\)?[[:space:]]*\)[[:space:]]*=[[:space:]]*\(?0\y')
        OR check_clause ~* ('\y(' || column_name || '|value)\y[^;%]*%[[:space:]]*\(?(1|0\.[0-9]+)\)?[[:space:]]*\)?[[:space:]]*=[[:space:]]*\(?0\y')
        OR check_clause ~* ('\y(' || column_name || '|value)\y\)?[[:space:]]*::[[:space:]]*(text|varchar|character[[:space:]]+varying)[[:space:]]*!?~\*?')
),

numeric_columns AS (
    SELECT 
        table_schema, 
        table_name, 
        table_type, 
        column_name, 
        data_type, 
        numeric_precision, 
        numeric_scale, 
        is_nullable, 
        ordinal_position
    FROM INFORMATION_SCHEMA.columns 
    INNER JOIN INFORMATION_SCHEMA.tables USING (table_schema, table_name)
    WHERE data_type = 'numeric'
      AND table_type IN ('BASE TABLE') 
      AND table_schema NOT IN (
          SELECT schema_name
          FROM INFORMATION_SCHEMA.schemata
          WHERE schema_name <> 'public' 
            AND schema_owner = 'postgres' 
            AND schema_name IS NOT NULL
      )
)

SELECT 
    tc.table_schema, 
    tc.table_name, 
    tc.column_name, 
    tc.data_type, 
    tc.numeric_precision, 
    tc.numeric_scale, 
    tc.is_nullable, 
    (SELECT string_agg(check_clause, ',<br>' ORDER BY check_clause)
     FROM numeric_simple_chk AS tsc 
     WHERE tc.table_schema = tsc.table_schema 
       AND tc.table_name = tsc.table_name 
       AND tc.column_name = tsc.column_name) AS checks
FROM numeric_columns AS tc
WHERE EXISTS (
    SELECT 1 
    FROM numeric_chk AS nec 
    WHERE tc.table_schema = nec.table_schema 
      AND tc.table_name = nec.table_name
      AND tc.column_name = nec.column_name
)
ORDER BY tc.table_schema, tc.table_name, tc.ordinal_position;

Fixing Suggestion

Replace the single-column scale-checking CHECK constraint with an explicit NUMERIC(precision, scale) column definition and drop the constraint.

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.

NameDescription
CHECK constraintsQueries of this category provide information about CHECK constraints.