Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the trigger(s) associated with a view or a table in PostgreSQL

I have one requirement that I have to get the list of triggers associated to the given table/view.
Can anyone help me to find the triggers for a table in PostgreSQL?

like image 865
Saravanan Avatar asked Aug 08 '14 11:08

Saravanan


People also ask

How do you find the trigger of a specific table?

Just go to your table name and expand the Triggers node to view a list of triggers associated with that table. Right click to modify your trigger. Show activity on this post. This way you can list out all the triggers associated with the given table.

Can you table have a trigger associated with it?

A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.

Where are triggers in Pgadmin?

The Trigger dialog organizes the development of a trigger through the following dialog tabs: General, Definition, Events, and Code. The SQL tab displays the SQL code generated by dialog selections. Use the fields in the General tab to identify the trigger: Use the Name field to add a descriptive name for the trigger.


2 Answers

This will return all the details you want to know

select * from information_schema.triggers 

or if you want to sort the results of a specific table then you can try

SELECT event_object_table       ,trigger_name       ,event_manipulation       ,action_statement       ,action_timing FROM  information_schema.triggers WHERE event_object_table = 'tableName' -- Your table name comes here ORDER BY event_object_table      ,event_manipulation 

the following will return table name that has trigger

select relname as table_with_trigger from pg_class where pg_class.oid in (         select tgrelid         from pg_trigger         ) 
like image 170
Vivek S. Avatar answered Sep 21 '22 07:09

Vivek S.


The problem with the view information_schema.triggers (besides being slow) is, per documentation:

The view triggers contains all triggers defined in the current database on tables and views that the current user owns or has some privilege other than SELECT on.

Meaning, you only get to see triggers you have appropriate privileges on.

To see all triggers for a table, look in the system catalog pg_trigger

SELECT tgname FROM   pg_trigger WHERE  tgrelid = 'myschema.mytbl'::regclass; -- optionally schema-qualified 

Works for tables and views.
Or you could use a GUI like pgAdmin that displays the list under the table node in the object browser.

like image 27
Erwin Brandstetter Avatar answered Sep 20 '22 07:09

Erwin Brandstetter