| Goal | Identify disabled triggers. These should be enabled or dropped, otherwise these are dead code. |
|---|---|
| Notes | The query finds triggers that are associated with a table as well as event triggers, which react to schema modifications. |
| 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 | Enable the disabled trigger or drop it. |
| Data Source | system catalog only |
| SQL Query |
|
SQL statements that help generate fixes for the identified problem.
Fix Action #1
SELECT format('ALTER TABLE %1$I.%2$I ENABLE TRIGGER %3$I;', n.nspname, c.relname, t.tgname) AS statements
FROM pg_catalog.pg_namespace n,
pg_catalog.pg_class c,
pg_catalog.pg_trigger t
WHERE n.oid = c.relnamespace AND c.oid = t.tgrelid AND t.tgenabled='D' AND t.tgisinternal=FALSE
ORDER BY n.nspname, c.relname, t.tgname;Enable the trigger.
Fix Action #2
SELECT format('DROP TRIGGER %1$I ON %2$I.%3$I RESTRICT;', t.tgname, n.nspname, c.relname) AS statements
FROM pg_catalog.pg_namespace n,
pg_catalog.pg_class c,
pg_catalog.pg_trigger t
WHERE n.oid = c.relnamespace AND c.oid = t.tgrelid AND t.tgenabled='D' AND t.tgisinternal=FALSE
ORDER BY n.nspname, c.relname, t.tgname;Drop the trigger.
Fix Action #3
SELECT format('ALTER EVENT TRIGGER %1$I ENABLE;', evtname) AS statements
FROM pg_catalog.pg_event_trigger
WHERE evtenabled='D'
ORDER BY evtname;Enable the event trigger.
Fix Action #4
SELECT format('DROP EVENT TRIGGER %1$I RESTRICT;', evtname) AS statements
FROM pg_catalog.pg_event_trigger
WHERE evtenabled='D'
ORDER BY evtname;Drop the event trigger.
| SQL Query to Generate Fix | Description |
|---|---|
| Enable the trigger. |
| Drop the trigger. |
| Enable the event trigger. |
| Drop the event trigger. |
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:
Triggers and rules
Queries of this category provide information about triggers and rules in a database.
Unused implementation elements
Queries of this catergory provide information about the database objects that are not used.
| Name | Description |
|---|---|
| Triggers and rules | Queries of this category provide information about triggers and rules in a database. |
| Unused implementation elements | Queries of this catergory provide information about the database objects that are not used. |
Further reading and related materials: