如何判断 PostgreSQL 中是否启用了触发器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/704270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:40:28  来源:igfitidea点击:

How can you tell if a trigger is enabled in PostgreSQL?

postgresqltriggersstatus

提问by Hao

My googling-fu is failing me. How to know if a PostgreSQL trigger is disabled or not?

我的谷歌搜索功能让我失望。如何知道 PostgreSQL 触发器是否被禁用?

采纳答案by Rafael

It's my first day with postresql, but I think you can check the trigger state via pg_trigger system table: http://www.postgresql.org/docs/current/static/catalog-pg-trigger.html

这是我使用 postresql 的第一天,但​​我认为您可以通过 pg_trigger 系统表检查触发器状态:http: //www.postgresql.org/docs/current/static/catalog-pg-trigger.html

The columns you will need are tgrelidand tgenabled.

您需要的列是tgrelidtgenabled

回答by tolgayilmaz

The SQL below will do the work. It displays all triggers in your current database.

下面的 SQL 将完成这项工作。它显示当前数据库中的所有触发器。

SELECT pg_namespace.nspname, pg_class.relname, pg_trigger.*
FROM pg_trigger
JOIN pg_class ON pg_trigger.tgrelid = pg_class.oid
JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace  

If tgenabledis 'D', the trigger is disabled. All other values (documented here) indicate, that it is enabled in some way.

如果tgenabled为“D”,则触发器被禁用。所有其他值(在此处记录)表明它以某种方式启用。

BTW. If you want to check the triggers for a specific table, the query is a bit shorter:

顺便提一句。如果要检查特定表的触发器,查询要短一些:

SELECT * FROM pg_trigger
WHERE tgrelid = 'your_schema.your_table'::regclass

The castto the regclasstype gets you from qualified table nameto OID (object id) the easy way.

regclass的类型让你从限定的表名到OID(对象ID)的简单方法。

回答by Pocketsand

SELECT EXISTS (
    SELECT  tgenabled
    FROM    pg_trigger
    WHERE   tgname='your_unique_trigger_name' AND
            tgenabled != 'D'
);

If you know the trigger name is unique the above will return true (t) if the your_unique_trigger_nametrigger is enabled:

如果您知道触发器名称是唯一的,那么如果启用了your_unique_trigger_name触发器,则上面将返回 true (t) :

 exists
--------
 t
(1 row)

If disabled it would return false (f).

如果禁用,它将返回 false (f)。