How to List All Triggers in PostgreSQL
Summary: in this tutorial, you will learn how to list all triggers in the current database or triggers associated with a specific table in PostgreSQL.
Listing all triggers using SQL statement
To list all triggers along with their associated tables in the current database, you can use the information_schema.triggers
system view.
For example, the following statement retrieves the triggers with their associated tables in the current database from the information_schema.triggers
view:
Sample output:
If you want to list all triggers associated with a specific table, you can filter the event_object_table
column by specifying the table name in the WHERE
clause.
For example, the following query lists all triggers associated with the employees
table in the current database:
Output:
To make it more convenient, you can create a user-defined function that wraps the above query.
For example, the following creates a function named get_triggers()
that returns all triggers with their associated tables in the current database:
The following statement shows how to call the get_triggers()
function:
The following creates a function get_triggers()
that accepts a table name and returns all the triggers of the table:
The following statement uses the get_triggers(text)
function to retrieve all triggers of the employees
table:
Listing all triggers using the pg_trigger view
pg_trigger
is a system view that provides information about triggers defined in the database. Here are some important columns:
Column | Description |
---|---|
tgname | The name of the trigger. |
tgrelid | The object ID of the table or view to which the trigger belongs. |
tgfoid | The object ID of the function is called when the trigger fires. |
tgtype | The type of the trigger, such as BEFORE , AFTER , or INSTEAD OF . |
tgenabled | The status of the trigger, either enabled or disabled. |
tgisinternal | The boolean indicator indicates whether the trigger is a system-generated trigger or a user-defined trigger. |
For example, the following statement retrieves all user-defined triggers of the employees
table from the pg_trigger
view:
Output:
Listing all triggers using psql
First, open the Command Prompt on Windows or Terminal on Unix-like systems and connect to the PostgreSQL server using psql:
Second, use the \dS
command with a table name to list all the triggers associated with the table:
For example, the following command displays the employees
table with its triggers:
Output:
The last part of the output shows the triggers of the employees
table.
Note that psql does not provide a command to list all triggers in the current database.
Summary
- List all triggers in a database or a specific table using the
information_schema.triggers
orpg_trigger
views. - Use psql command
\dS table_name
to display a table along with its associated triggers.