PostgreSQL触发器没有触发。你能看到什么问题吗?

时间:2016-09-27 07:20:58

标签: postgresql

在5-6个月前写过这个触发器,它在我部署它的db上运行良好。把它从架子上取下来,几乎把它放在同一个数据库上,它放在一个不同的盒子里。 Sucker不再执行了。

实质上,此触发器只是将“之前”行复制到审计表。审计表位于同一服务器上的不同数据库中,我将其作为FOREIGN表链接回源数据库:

审核db:

CREATE TABLE next_gen_permissions_change
(
  id serial NOT NULL,
  authorizable_type character varying(64),
  authorizable_id integer,
  grantee_id integer,
  grantee_type character varying(255),
  capability_id integer NOT NULL,
  permission integer DEFAULT 0,
  event_time timestamp without time zone,
  event_type character varying(10)
)
GRANT ALL ON TABLE next_gen_permissions_change TO foo;

我在“source”数据库中创建了外表:

CREATE EXTENSION postgres_fdw;
CREATE SERVER myserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost', dbname 'audit', port '5432');
CREATE USER MAPPING FOR foo SERVER myserver OPTIONS (user 'foo', password 'secret');

CREATE foreign TABLE next_gen_permissions_change
(
  id serial NOT NULL,
  authorizable_type character varying(64),
  authorizable_id integer,
  grantee_id integer,
  grantee_type character varying(255),
  capability_id integer NOT NULL,
  permission integer DEFAULT 0,
  event_time timestamp without time zone,
  event_type character varying(10))
SERVER myserver;
GRANT ALL ON TABLE next_gen_permissions_change TO foo;

当我将行插入source.next_gen_permissions_change时,我看到它们出现在audit.next_gen_permissions_change中......所以我得到了这个。

这是功能:

CREATE OR REPLACE FUNCTION audit_permissions()
  RETURNS trigger AS
$BODY$
DECLARE
    trigger_event_time timestamp;
BEGIN
    trigger_event_time := now();

    IF (TG_OP = 'INSERT') THEN
    INSERT INTO next_gen_permissions_change(
            id, 
            authorizable_type, 
            authorizable_id, 
            grantee_id, 
            grantee_type, 
            capability_id, 
            permission, 
            event_time, 
            event_type)
        VALUES (NEW.id,
            NEW.authorizable_type, 
            NEW.authorizable_id, 
            NEW.grantee_id, 
            NEW.grantee_type, 
            NEW.capability_id, 
            NEW.permission, 
            trigger_event_time, 
            'insert');
        RETURN NEW;
    ELSIF (TG_OP = 'DELETE') THEN
            INSERT INTO next_gen_permissions_change(
            id, 
            authorizable_type, 
            authorizable_id, 
            grantee_id, 
            grantee_type, 
            capability_id, 
            permission, 
            event_time, 
            event_type)
        VALUES (OLD.id,
            OLD.authorizable_type, 
            OLD.authorizable_id, 
            OLD.grantee_id, 
            OLD.grantee_type, 
            OLD.capability_id, 
            OLD.permission, 
            trigger_event_time, 
            'delete');
            RETURN OLD;
    END IF;

    RETURN null;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

现在,我将该功能添加为触发器:

CREATE TRIGGER next_gen_permissions_audit
AFTER INSERT OR DELETE
ON next_gen_permissions
FOR EACH ROW
EXECUTE PROCEDURE audit_permissions();

没有错误,一切似乎都很好:

enter image description here

正如你所看到的,我也添加了RAISE NOTICE,所以我希望看到这个sucker在pgadmin中执行。

以下是触发器(父窗口)和函数(嵌入父窗口内的子窗口)的所有属性:

enter image description here

执行类似

的事情
UPDATE next_gen_permissions SET f1=f1

......我在消息中看不到任何内容。

另外,

EXPLAIN ANALYZE UPDATE next_gen_permissions SET f1=f1

不会返回任何有趣的内容:

enter image description here

谁能看到我在这里做错了什么?我没有收到任何错误消息,当我查看日志时,UPDATE会直接通过而不提及触发器被触发。

非常感谢。

0 个答案:

没有答案