Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you tell if a trigger is enabled in PostgreSQL?

My googling-fu is failing me. How to know if a PostgreSQL trigger is disabled or not?

like image 788
Hao Avatar asked Apr 01 '09 06:04

Hao


People also ask

How do I enable triggers in PostgreSQL?

To enable a trigger or all triggers associated with a table, you use the ALTER TABLE ENABLE TRIGGER statement: ALTER TABLE table_name ENABLE TRIGGER trigger_name | ALL; In this syntax: First, specify the name of the table of the trigger that you want to enable.

Do we have triggers in PostgreSQL?

PostgreSQL Triggers are database callback functions, which are automatically performed/invoked when a specified database event occurs. A trigger that is marked FOR EACH ROW is called once for every row that the operation modifies.

Which three events can activate a trigger PostgreSQL?

A constraint trigger can only be specified as AFTER . One of INSERT , UPDATE , DELETE , or TRUNCATE ; this specifies the event that will fire the trigger. Multiple events can be specified using OR , except when transition relations are requested.


2 Answers

The SQL below will do the work. It displays all triggers in your current database.

SELECT pg_namespace.nspname, pg_class.relname, pg_trigger.*
FROM pg_trigger
JOIN pg_class ON pg_trigger.tgrelid = pg_class.oid
JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace  

If tgenabled is 'D', the trigger is disabled. All other values (documented here) indicate, that it is enabled in some way.

BTW. If you want to check the triggers for a specific table, the query is a bit shorter:

SELECT * FROM pg_trigger
WHERE tgrelid = 'your_schema.your_table'::regclass

The cast to the regclass type gets you from qualified table name to OID (object id) the easy way.

like image 157
tolgayilmaz Avatar answered Oct 11 '22 10:10

tolgayilmaz


It's my first day with postresql, but I think you can check the trigger state via pg_trigger system table: http://www.postgresql.org/docs/current/static/catalog-pg-trigger.html

The columns you will need are tgrelid and tgenabled.

like image 36
Rafael Avatar answered Oct 11 '22 09:10

Rafael