Aquí hay tres opciones para enumerar los disparadores para una tabla dada en PostgreSQL.
El information_schema.triggers
Ver
Esta vista contiene todas las funciones y procedimientos en la base de datos actual que el usuario actual posee o tiene algún privilegio que no sea SELECT
sobre.
Una de las columnas en esta vista es event_object_table
, que contiene el nombre de la tabla en la que está definido el disparador.
Por lo tanto, podemos usar esta vista para obtener los activadores de una tabla específica como esta:
SELECT
trigger_schema,
trigger_name,
event_manipulation,
action_statement
FROM information_schema.triggers
WHERE event_object_table = 'film';
Resultado de ejemplo:
+----------------+-----------------------+--------------------+----------------------------------------------------------------------------------------------------+ | trigger_schema | trigger_name | event_manipulation | action_statement | +----------------+-----------------------+--------------------+----------------------------------------------------------------------------------------------------+ | public | film_fulltext_trigger | INSERT | EXECUTE FUNCTION tsvector_update_trigger('fulltext', 'pg_catalog.english', 'title', 'description') | | public | film_fulltext_trigger | UPDATE | EXECUTE FUNCTION tsvector_update_trigger('fulltext', 'pg_catalog.english', 'title', 'description') | | public | last_updated | UPDATE | EXECUTE FUNCTION last_updated() | +----------------+-----------------------+--------------------+----------------------------------------------------------------------------------------------------+
Al ver que la vista devuelve solo aquellos disparadores que el usuario actual posee o tiene algún privilegio que no sea SELECT
encendido, la lista resultante podría representar solo un subconjunto de los disparadores reales que están definidos en la tabla.
El pg_trigger
Catálogo
El pg_catalog.pg_trigger
catalog almacena activadores en tablas y vistas, y no se limita solo a aquellos activadores que el usuario actual posee o tiene algún privilegio que no sea SELECT
en:
SELECT
tgname AS trigger_name
FROM
pg_trigger
WHERE
tgrelid = 'public.film'::regclass
ORDER BY
trigger_name;
Resultado de ejemplo:
+------------------------------+ | trigger_name | +------------------------------+ | RI_ConstraintTrigger_a_24890 | | RI_ConstraintTrigger_a_24891 | | RI_ConstraintTrigger_a_24900 | | RI_ConstraintTrigger_a_24901 | | RI_ConstraintTrigger_a_24915 | | RI_ConstraintTrigger_a_24916 | | RI_ConstraintTrigger_c_24907 | | RI_ConstraintTrigger_c_24908 | | RI_ConstraintTrigger_c_24912 | | RI_ConstraintTrigger_c_24913 | | film_fulltext_trigger | | last_updated | +------------------------------+
Para obtener la fuente, podemos incluir el pg_proc
ver en nuestra consulta:
SELECT
t.tgname,
p.prosrc
FROM
pg_trigger t JOIN pg_proc p ON p.oid = t.tgfoid
WHERE
t.tgrelid = 'public.film'::regclass
ORDER BY
t.tgname;
Resultado de ejemplo:
+------------------------------+------------------------------------------+ | tgname | prosrc | +------------------------------+------------------------------------------+ | RI_ConstraintTrigger_a_24890 | RI_FKey_restrict_del | | RI_ConstraintTrigger_a_24891 | RI_FKey_cascade_upd | | RI_ConstraintTrigger_a_24900 | RI_FKey_restrict_del | | RI_ConstraintTrigger_a_24901 | RI_FKey_cascade_upd | | RI_ConstraintTrigger_a_24915 | RI_FKey_restrict_del | | RI_ConstraintTrigger_a_24916 | RI_FKey_cascade_upd | | RI_ConstraintTrigger_c_24907 | RI_FKey_check_ins | | RI_ConstraintTrigger_c_24908 | RI_FKey_check_upd | | RI_ConstraintTrigger_c_24912 | RI_FKey_check_ins | | RI_ConstraintTrigger_c_24913 | RI_FKey_check_upd | | film_fulltext_trigger | tsvector_update_trigger_byid | | last_updated | +| | | BEGIN +| | | NEW.last_update = CURRENT_TIMESTAMP;+| | | RETURN NEW; +| | | END | +------------------------------+------------------------------------------+
Tenga en cuenta que el contenido del pg_proc.prosrc
la columna depende del lenguaje de implementación/convención de llamadas. Podría contener el código fuente real de la función para idiomas interpretados, un símbolo de enlace, un nombre de archivo o casi cualquier otra cosa.
El \dS
Comando
Al usar psql, el \dS
El comando es una forma rápida de obtener información sobre tablas, vistas, etc., incluidos los activadores que se definen contra ellos.
Podemos agregar el nombre de la tabla a este comando para devolver información solo sobre esa tabla:
\dS film
Resultado de ejemplo:
+----------------------+-----------------------------+-----------+----------+---------------------------------------+ | Column | Type | Collation | Nullable | Default | +----------------------+-----------------------------+-----------+----------+---------------------------------------+ | film_id | integer | | not null | nextval('film_film_id_seq'::regclass) | | title | character varying(255) | | not null | | | description | text | | | | | release_year | year | | | | | language_id | smallint | | not null | | | original_language_id | smallint | | | | | rental_duration | smallint | | not null | 3 | | rental_rate | numeric(4,2) | | not null | 4.99 | | length | smallint | | | | | replacement_cost | numeric(5,2) | | not null | 19.99 | | rating | mpaa_rating | | | 'G'::mpaa_rating | | last_update | timestamp without time zone | | not null | now() | | special_features | text[] | | | | | fulltext | tsvector | | not null | | +----------------------+-----------------------------+-----------+----------+---------------------------------------+ Indexes: "film_pkey" PRIMARY KEY, btree (film_id) "film_fulltext_idx" gist (fulltext) "idx_fk_language_id" btree (language_id) "idx_fk_original_language_id" btree (original_language_id) "idx_title" btree (title) Foreign-key constraints: "film_language_id_fkey" FOREIGN KEY (language_id) REFERENCES language(language_id) ON UPDATE CASCADE ON DELETE RESTRICT "film_original_language_id_fkey" FOREIGN KEY (original_language_id) REFERENCES language(language_id) ON UPDATE CASCADE ON DELETE RESTRICT Referenced by: TABLE "film_actor" CONSTRAINT "film_actor_film_id_fkey" FOREIGN KEY (film_id) REFERENCES film(film_id) ON UPDATE CASCADE ON DELETE RESTRICT TABLE "film_category" CONSTRAINT "film_category_film_id_fkey" FOREIGN KEY (film_id) REFERENCES film(film_id) ON UPDATE CASCADE ON DELETE RESTRICT TABLE "inventory" CONSTRAINT "inventory_film_id_fkey" FOREIGN KEY (film_id) REFERENCES film(film_id) ON UPDATE CASCADE ON DELETE RESTRICT Triggers: film_fulltext_trigger BEFORE INSERT OR UPDATE ON film FOR EACH ROW EXECUTE FUNCTION tsvector_update_trigger('fulltext', 'pg_catalog.english', 'title', 'description') last_updated BEFORE UPDATE ON film FOR EACH ROW EXECUTE FUNCTION last_updated()