我有一个要求,我必须得到与给定表/视图关联的触发器列表 任何人都可以帮我找到PostgreSQL中表的触发器吗?
答案 0 :(得分:39)
这将返回您想要了解的所有详细信息
select * from information_schema.triggers
或者如果您想对特定表格的结果进行排序,那么您可以尝试
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
以下将返回具有触发器的表名
select relname as table_with_trigger
from pg_class
where pg_class.oid in (
select tgrelid
from pg_trigger
)
答案 1 :(得分:28)
视图information_schema.triggers
的问题(除了缓慢)是per documentation:
视图触发器包含当前定义的所有触发器 当前用户拥有或拥有的表和视图的数据库 除
SELECT
之外的其他权限。
意思是,你只能看到你拥有适当特权的触发器。
要查看表的所有触发器,请查看系统目录pg_trigger
SELECT tgname
FROM pg_trigger
WHERE tgrelid = 'myschema.mytbl'::regclass; -- optionally schema-qualified
适用于表和视图 或者您可以使用像pgAdmin这样的GUI来显示对象浏览器中表节点下的列表。
答案 2 :(得分:5)
在psql命令行工具上,您还可以使用\dS <table_name>
(来自https://serverfault.com/questions/331024/how-can-i-show-the-content-of-a-trigger-with-psql)
答案 3 :(得分:0)
select tgname
,relname
,tgenabled
,nspname from pg_trigger
join pg_class on (pg_class.oid=pg_trigger.tgrelid)
join pg_namespace on (nspowner=relowner);
tgenabled (To check if its disabled)
O = trigger fires in "origin" and "local" modes,
D = trigger is disabled,
R = trigger fires in "replica" mode,
A = trigger fires always.
答案 4 :(得分:0)
我注意到infoschema不包含有关触发器的键关系表信息(至少在postgres 10中)。 pg_triggers确实包含此信息。还注意到,当您编写触发器脚本时,datagrip不会编写关系表脚本,因此我假设它使用infoschema编写脚本(然后您的表将丢失关系表,并且引用它们的触发器函数将失败)。 PG文档说,infoschema中的action_reference_old_table列适用于postgres(10)中不可用的功能,但是我肯定使用它们,并且它们肯定会出现在pg_triggers中。仅供参考。