如何从information.schema.tables postgresql 9.3中多次运行触发器

时间:2018-07-31 19:45:36

标签: sql postgresql postgresql-9.3

您好,我正在执行审核,我需要在架构上为每个表运行触发器,我不确定如何执行此操作,我尝试了此操作,但它不起作用:

CREATE TRIGGER triggerAuditoria AFTER INSERT OR UPDATE OR DELETE
ON * FROM information_schema.tables where table_schema ='auditoria' FOR EACH ROW EXECUTE PROCEDURE auditoriaTablas();

这是功能:

CREATE OR REPLACE FUNCTION auditoriaTablas() RETURNS trigger AS
$$
DECLARE consulta varchar;
DECLARE iphost varchar;
DECLARE app varchar;
BEGIN
SELECT query into consulta FROM pg_stat_activity where usename= USER;
SELECT client_port into iphost FROM pg_stat_activity where usename= USER;
SELECT application_name into app FROM pg_stat_activity where usename= USER;

  IF (TG_OP = 'DELETE') THEN
    INSERT INTO auditoria ("fechahora", "usuario", "nombretabla", "operacion", "consulta", "iphost", "app")
           VALUES (now(), USER, TG_TABLE_NAME, 'E', consulta, iphost, app);
    RETURN OLD;
  ELSIF (TG_OP = 'UPDATE') THEN
    INSERT INTO auditoria ("fechahora", "usuario", "nombretabla", "operacion", "consulta", "iphost", "app")
           VALUES (now(), USER, TG_TABLE_NAME, 'A', consulta, iphost, app);
    RETURN NEW;
  ELSIF (TG_OP = 'INSERT') THEN
    INSERT INTO auditoria ("fechahora", "usuario", "nombretabla", "operacion", "consulta", "iphost", "app")
           VALUES (now(), USER, TG_TABLE_NAME, 'I', consulta, iphost, app);
    RETURN NEW;
  END IF;
  RETURN NULL;
END;
$$
LANGUAGE 'plpgsql' VOLATILE COST 100;
ALTER FUNCTION auditoriaTablas() OWNER TO postgres;

有什么想法吗?谢谢。

0 个答案:

没有答案