如何检查postgres触发器何时被修改?
pg_trigger的哪个列?
答案 0 :(得分:1)
良好的后置触发器不会修改,它会修改您在其中执行特定操作(例如BEFROE UPDATE, BEFORE INSERT, AFTER UPDATE, AFTER INSERT
答案 1 :(得分:1)
如果我很了解您的问题,是您想知道触发器的代码何时更新,并捕获此信息。 (例如在表格中)。对于以前的修改,似乎在postgresql上是不可能知道的。 但是,如果您对将来的修改感兴趣,则可以放置另一种类型的触发器:事件触发器。 (https://www.postgresql.org/docs/current/event-triggers.html)
您可以这样做:
CREATE OR REPLACE FUNCTION [your_schema].ddl_trigger_alter_trigger_fct()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
DECLARE
all_variables character varying;
r RECORD;
schema_name varchar (250);
command_tag varchar (100);
object_type varchar (250);
BEGIN
FOR r IN SELECT * FROM pg_event_trigger_ddl_commands() LOOP
if r.schema_name = '[your_schema]' and r.command_tag = 'ALTER TRIGGER'
then
schema_name := r.schema_name;
command_tag := r.command_tag;
object_type := r.object_type;
RAISE NOTICE 'caught % event on %', r.command_tag, r.object_identity;
all_variables := concat(
'classid: ', r.classid, chr(10)
, 'objid: ', r.objid, chr(10)
, 'objsubid: ', r.objsubid, chr(10)
, 'command_tag: ', r.command_tag, chr(10)
, 'object_type: ', r.object_type, chr(10)
, 'schema_name: ', r.schema_name, chr(10)
, 'object_identity: ', r.object_identity, chr(10)
, 'in_extension: ', r.in_extension);
RAISE NOTICE '%', all_variables;
end if;
END LOOP;
if schema_name = '[your_schema]' and command_tag = 'ALTER TRIGGER'
then
-- Do what you want, for example insert a line in a table of logs
end if;
END;
$$;
CREATE EVENT TRIGGER ddl_trigger_alter_trigger ON ddl_command_end WHEN TAG IN ('ALTER TRIGGER')
EXECUTE FUNCTION [your_schema].ddl_trigger_alter_trigger_fct();
但是如果您感兴趣的是触发器调用的函数代码,那么您只需要在ALTER FUNCTION事件上放置另一个事件触发器即可。
希望这会有所帮助;)