| Goal | Find multiple rules with the same definition (event, condition, action) on the same table. Do remember that the same task can be solved in SQL usually in multiple different ways. Thus, the exact copies are not the only possible duplication. |
|---|---|
| Notes | In case of the string_agg function, the line break (br) tag is used as a part of the separator for the better readability in case the query result is displayed in a web browser. |
| 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) |
| Fixing Suggestion | All but one are redundant. Drop the redundant rules. |
| Data Source | system catalog only |
| SQL Query |
|
SQL statements that help generate fixes for the identified problem.
Fix Action #1
WITH duplicate_rules AS (
SELECT schemaname, tablename, array_agg(rulename) AS rules_array
FROM pg_rules
GROUP BY schemaname, tablename, replace(definition, rulename, 'rulename')
HAVING Count(*)>1)
SELECT format('DROP RULE %1$I ON TABLE %2$I.%3$I;', unnest(rules_array), schemaname, tablename) AS statements
FROM duplicate_rules
ORDER BY schemaname, tablename, unnest(rules_array);Drop the rule. One of the rules must stay in place.
| SQL Query to Generate Fix | Description |
|---|---|
| Drop the rule. One of the rules must stay in place. |
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:
Duplication of implementation elements
Queries of this catergory provide information about the duplication of the database objects.
Triggers and rules
Queries of this category provide information about triggers and rules in a database.
| Name | Description |
|---|---|
| Duplication of implementation elements | Queries of this catergory provide information about the duplication of the database objects. |
| Triggers and rules | Queries of this category provide information about triggers and rules in a database. |
Further reading and related materials:
The corresponding code smell in case of cleaning code is "G5: Duplication". (Robert C. Martin, Clean Code)
| Reference |
|---|
| https://refactoring.guru/smells/alternative-classes-with-different-interfaces |
| https://refactoring.guru/smells/duplicate-code |
| The corresponding code smell in case of cleaning code is "G5: Duplication". (Robert C. Martin, Clean Code) |