Goal Find names of triggers that are used within the same schema more than once. Give different triggers different names.
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 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
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, ';
' 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;
Collections

This query belongs to the following collections:

NameDescription
Find problems about namesA 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 automaticallyQueries, 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:

NameDescription
Comfortability of database evolutionQueries of this category provide information about the means that influence database evolution.
NamingQueries of this category provide information about the style of naming.
Triggers and rulesQueries of this category provide information about triggers and rules in a database.