| 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):
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 |
|
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 .
| Name | Description |
|---|---|
| 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 . |
Categories
This query is classified under the following categories:
CHECK constraints
Queries of this category provide information about CHECK constraints.
| Name | Description |
|---|---|
| CHECK constraints | Queries of this category provide information about CHECK constraints. |