| Goal | This query identifies user-defined database objects whose names (identifiers) match SQL keywords that are reserved or partially reserved in PostgreSQL (categories R, T, and C). As Robert C. Martin notes in Clean Code: "Names in software are 90 percent of what make software readable. You need to take the time to choose them wisely and keep them relevant. Names are too important to treat carelessly. Names should not cause confusion." In PostgreSQL, "there are several different classes of tokens ranging from those that can never be used as an identifier to those that have absolutely no special status in the parser as compared to an ordinary identifier" (PostgreSQL Documentation). Although PostgreSQL permits these keywords to be used as identifiers—either by enclosing them in double quotes or, for partially reserved categories, using them unquoted only for certain types of database objects—doing so creates several issues:
|
|---|---|
| Notes | The query uses pg_get_keywords() system catalog information function to find all the keywords that are not unreserved. The subquery where this function is invoked returns reserved keywords and unreserved keywords that have restrictions, i.e., these keywords cannot be used as regular identifiers in case of certain types of database objects. In case of each suspected identifier it also presents the status of the keyword in PostgreSQL and information about its usage as column label. The query does not consider objects that are a part of an extension. |
| Type | Problem detection Each row in the result could represent a flaw in the design |
| Reliability | High Few or no false-positive results |
| License | MIT (opens in new tab) |
| Data Source | INFORMATION_SCHEMA+system catalog |
| SQL Query |
|
Fixing Suggestion
Review each flagged identifier and rename it to a descriptive, domain-specific name that is not an SQL keyword:
- Eliminate reserved words: Replace reserved or context-restricted keywords with explicit domain terms (for example, rename "user" to app_user, "order" to customer_order, or interval to billing_interval).
- Avoid double-quoting: Ensure the new name consists solely of lowercase letters, digits, and underscores so it can be referenced reliably without double quotes in every context.
- Refactor safely: Rename the objects using the appropriate DDL commands (e.g., ALTER TABLE ... RENAME, ALTER TABLE ... RENAME COLUMN), and update all dependent views, routines, and client application queries accordingly."
Collections
This query belongs to the following collections:
Find problems about names
A selection of queries that return information about the names of database objects. 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 names | A selection of queries that return information about the names of database objects. 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:
Comfortability of database evolution
Queries of this category provide information about the means that influence database evolution.
Naming
Queries of this category provide information about the style of naming.
| Name | Description |
|---|---|
| Comfortability of database evolution | Queries of this category provide information about the means that influence database evolution. |
| Naming | Queries of this category provide information about the style of naming. |
Further reading and related materials:
Perhaps the names are too general. For instance, table name "Table" or column name "column" are at the wrong level of abstraction. The corresponding code smells in case of cleaning code are "N1: Choose Descriptive Names", "N2: Choose Names at the Appropriate Level of Abstraction", "N4: Unambiguous Names", "N7: Names Should Describe Side-Effects". (Robert C. Martin, Clean Code)
Smell "Meaningless name": Sharma, T., Fragkoulis, M., Rizou, S., Bruntink, M. and Spinellis, D.: Smelly relations: measuring and understanding database schema quality. In: Proceedings of the 40th International Conference on Software Engineering: Software Engineering in Practice, pp. 55-64. ACM, (2018).
| Reference |
|---|
| https://www.postgresql.org/docs/current/sql-keywords-appendix.html |
| Perhaps the names are too general. For instance, table name "Table" or column name "column" are at the wrong level of abstraction. The corresponding code smells in case of cleaning code are "N1: Choose Descriptive Names", "N2: Choose Names at the Appropriate Level of Abstraction", "N4: Unambiguous Names", "N7: Names Should Describe Side-Effects". (Robert C. Martin, Clean Code) |
| Smell "Meaningless name": Sharma, T., Fragkoulis, M., Rizou, S., Bruntink, M. and Spinellis, D.: Smelly relations: measuring and understanding database schema quality. In: Proceedings of the 40th International Conference on Software Engineering: Software Engineering in Practice, pp. 55-64. ACM, (2018). |
| https://www.sqlstyle.guide/ |