【问题标题】:How to get the trigger(s) associated with a view or a table in PostgreSQL如何获取与 PostgreSQL 中的视图或表关联的触发器
【发布时间】:2014-10-01 20:28:06
【问题描述】:

我有一个要求,我必须获取与给定表/视图关联的触发器列表。
谁能帮我找到 PostgreSQL 中表的触发器?

【问题讨论】:

  • Pgadmin 在您查看表格时会向您显示很多有关表格的信息。它是否也显示触发器?我知道这不是问题的真正答案,只是好奇。
  • @Bill- pgadmin 工具不在框中。
  • \df 将列出包括触发器在内的所有函数。

标签: sql database postgresql


【解决方案1】:

这将返回你想知道的所有细节

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
        )

【讨论】:

  • 感谢您提供详细信息。 information_schema.triggers 也会有视图条目?
  • @dude- 我们可以得到它的意见。有什么办法可以查看吗?
  • dude - 它没有显示 select * from information_schema.triggers 的任何视图;。由于视图不是物理实体,因此不会在 information_schema.triggers 表中显示它。适用于用于创建视图的表的触发器是什么。我的理解是否正确请纠正我
  • 如何删除从该查询中获得的触发器 • 选择 event_object_schema 作为 table_schema,event_object_table 作为 table_name,trigger_schema,trigger_name,string_agg(event_manipulation, ',') 作为事件,action_timing 作为激活,action_condition作为条件,action_statement 作为 information_schema.triggers 的定义,其中 event_object_table 像 'his_%' 和 trigger_name ='set_timestamp' 按 1、2、3、4、6、7、8 顺序按 table_schema、table_name 分组;
【解决方案2】:

information_schema.triggers 视图的问题(除了慢)是per documentation

视图触发器包含当前定义的所有触发器 当前用户拥有或拥有的表和视图上的数据库 SELECT 以外的特权。

意思是,您只能看到您拥有相应权限的触发器。

要查看表的所有触发器,请查看系统目录pg_trigger

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

适用于表视图。
或者,您可以使用像 pgAdmin 这样的 GUI,在对象浏览器的表节点下显示列表。

【讨论】:

  • 恐怕我看不出这和information_schema.triggers 视图一样有用(假设有适当的特权)。通过视图,我得到trigger_nameevent_manipulation(即I、U、D)、action_timing(例如,BEFORE)、触发器作用的event_object_tableaction_statement。对于SELECT tgname...,我得到的只是触发器名称,它似乎缺少很多上下文,而pg_trigger 中的其他字段似乎并没有那么多信息。我觉得我一定是错过了什么。
  • @Randall:这个问题要求提供触发器列表。也许您想根据自己的要求提出自己的问题?
  • @Randall 这是一个内部系统表,因此需要一些额外的解释才能对人类友好。
【解决方案3】:

在 psql 命令行工具上你也可以使用\dS <table_name>(来自https://serverfault.com/questions/331024/how-can-i-show-the-content-of-a-trigger-with-psql

【讨论】:

  • 这不包括“系统生成的触发器”,例如对于外键约束,以防您(本评论的读者)想要查看这些约束。请参阅 this other answer 了解包含这些的解决方案。
【解决方案4】:
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.

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
【解决方案5】:

我注意到 infoschema 不包含有关触发器的关键关系表信息(至少在 postgres 10 中)。 pg_triggers 确实包含此信息。还注意到当您编写触发器脚本时,datagrip 不会编写关系表脚本,所以我假设它使用 infoschema 来编写它们的脚本(然后您的表会丢失关系表,并且引用它们的触发器函数会失败)。 PG 文档说 action_reference_old_table 的 infoschema 中的列适用于 postgres(10) 中不可用的功能,但我肯定在使用它们,它们肯定会出现在 pg_triggers 中。仅供参考。

【讨论】:

    【解决方案6】:

    \df 将列出包括触发器在内的所有函数。

    \dft 将列出所有触发器。

    【讨论】:

      猜你喜欢
      • 2012-12-20
      • 2021-09-14
      • 2013-08-05
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      相关资源
      最近更新 更多