Query goal: | Find names of triggers that are used within the same schema more than once. Give different triggers different names. |
Notes about the query: | 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. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | High (Few or no false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Give to triggers unique names within a schema. Use table name in the trigger name. Follow a naming convention. |
Data source: | INFORMATION_SCHEMA+system catalog |
SQL query: | Click on query to copy it
WITH triggers AS (SELECT tgname AS trigger_name, nspname AS trigger_schema, c.relname as table_name FROM pg_trigger t INNER JOIN pg_class c ON t.tgrelid=c.oid INNER JOIN pg_namespace n ON c.relnamespace=n.oid WHERE nspname NOT IN (SELECT schema_name FROM INFORMATION_SCHEMA.schemata WHERE schema_name<>'public' AND schema_owner='postgres' AND schema_name IS NOT NULL) AND tgisinternal='f') SELECT trigger_name, trigger_schema, Count(*) AS number_of_occurrences, string_agg(table_name, ';<br>' ORDER BY table_name) AS tables FROM triggers GROUP BY trigger_name, trigger_schema HAVING Count(*)>1 ORDER BY Count(*) DESC, trigger_schema, trigger_name; |
Collection name | Collection 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 . |
Category name | Category 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. |
Triggers and rules | Queries of this category provide information about triggers and rules in a database. |