| Goal | This query identifies columns of base tables and foreign tables that have a single-column table or domain CHECK constraint restricting the column to only one fixed value (for example, col = 5, col = 'test', or VALUE = false).Restricting a column to a single constant value is frequently a schema design flaw or a typo in the constraint definition (for instance, mistakenly using the equality operator = instead of LIKE, ~, IN (...), or a range operator, as in a = 'a%'). Storing an identical constant in every row of a multi-row table also wastes storage and obscures schema intent.However, such a constraint may be intentional and valid in two specific cases:
|
|---|---|
| Notes | Queries PostgreSQL system catalogs (pg_constraint, pg_class, pg_attribute, pg_type, pg_namespace, pg_inherits) and information_schema views to collect single-column table CHECK constraints (contype = 'c', cardinality(conkey) = 1) and domain CHECK constraints on base and foreign tables, along with inheritance metadata (supertable_schema, supertable_name).The WHERE clause looks for equality (=) checks while excluding:
To prevent short column names (such as a) appearing inside string literals (such as 'a%' or 'a\s') from being mistaken for self-comparisons or function arguments, single-quoted string literal contents are stripped with regexp_replace(check_clause, '''(?:''''|[^''])*''', '''''', 'g') prior to evaluating the function-call and self-comparison exclusions. |
| Type | General Overview of some aspect of the database. |
| License | MIT (opens in new tab) |
| Data Source | INFORMATION_SCHEMA+system catalog |
| SQL Query |
|
Fixing Suggestion
- If the constraint was written with the wrong operator (for example,
a = 'a%'instead ofa LIKE 'a%'or a missingIN (...)list), fix theCHECKexpression. - If the column genuinely stores only one fixed constant in a regular multi-row table, consider removing the redundant column from the table.
- If the constraint intentionally fixes a subtype value on an inherited subtable or enforces a single-row configuration table, no change is required.
Collections
This query belongs to the following collections:
Find problems about integrity constraints
A 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
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 about integrity constraints | A 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 |
| 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.
Validity and completeness
Queries 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).
| Name | Description |
|---|---|
| CHECK constraints | Queries of this category provide information about CHECK constraints. |
| Validity and completeness | Queries 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). |