| Goal | This query identifies reserved or partially reserved SQL keywords in PostgreSQL (categories R, T, and C) that are used as object identifiers, presenting an aggregated summary of their occurrence counts. 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 non-unreserved 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
Use this aggregated summary to prioritize schema naming cleanup:
- Prioritize high-risk keywords: Focus first on fully reserved keywords (category
R) that force double-quoting everywhere, as well as keywords with the highest occurrence counts across the schema. - Locate affected objects: Run the corresponding detailed view query to pinpoint the exact tables, columns, views, or routines using those specific keywords.
- Rename with domain context: Replace reserved or generic words with explicit, unquoted domain identifiers using lowercase letters, digits, and underscores (for example, renaming
"user"toapp_user,"order"tocustomer_order, orintervaltobilling_interval). - Update dependencies: Apply the changes via DDL (such as
ALTER TABLE ... RENAME) and update all dependent views, routines, and 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.
| 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. |
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/ |