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)
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:
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:
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.
Triggers and rules
Queries of this category provide information about triggers and rules in a database.