(Postgresql) make table 触发器函数将更改行的 ID 值插入日志 table,用于自定义复制
(Postgresql) make table trigger function insert ID value of changed row into log table, for custom replication
对于 Postgresql---最终是 Sybase ADS---我有 tables 将使用自制触发器进行复制。触发器有一个函数,它应该记录 INSERT UPDATE DELETE 更改的信息,其中包括 I/U/D 操作和更改的行的 ID。我正在使用带有名字列和自动递增 ID 的测试 tables 来测试双向复制。
如果我对 NAME table 进行插入,例如添加自动 ID 为“1”的 'JOHN',我是否能够插入该 ID 值甚至可能 'JOHN'进入日志 table?我正在查看 OLD.ID 或 NEW.ID 选项,但我不知道这是否可行。我的 trigger/function 代码是:
CREATE OR REPLACE FUNCTION Insert() RETURNS TRIGGER AS $ReplInsert$
BEGIN
IF (TG_OP = 'DELETE') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('DELETE', OLD.ID, OLD.NAME);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('UPDATE', NEW.ID ,NEW.NAME);
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('UPDATE', NEW.ID , NEW.NAME);
RETURN NEW;
END IF;
RETURN NULL;
END;
$ReplInsert$ LANGUAGE plpgsql;
CREATE TRIGGER logTrg
AFTER INSERT OR UPDATE OR DELETE ON "FIRST_NAME"
FOR EACH ROW EXECUTE PROCEDURE Insert();
这最终是为了使用触发器进行双向复制。感谢您的任何考虑或指导。我会根据需要进行编辑。
我能够通过在触发器中将 AFTER 更改为 BEFORE 来解决这个问题,我认为一开始这是行不通的,或者:
CREATE OR REPLACE FUNCTION Insert() RETURNS TRIGGER AS $ReplInsert$
BEGIN
IF (TG_OP = 'DELETE') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('DELETE', OLD.ID, OLD.NAME);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('UPDATE', NEW.ID ,NEW.NAME);
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('UPDATE', NEW.ID , NEW.NAME);
RETURN NEW;
END IF;
RETURN NULL;
END;
$ReplInsert$ LANGUAGE plpgsql;
CREATE TRIGGER logTrg
BEFORE INSERT OR UPDATE OR DELETE ON "FIRST_NAME"
FOR EACH ROW EXECUTE PROCEDURE Insert();
对于 Postgresql---最终是 Sybase ADS---我有 tables 将使用自制触发器进行复制。触发器有一个函数,它应该记录 INSERT UPDATE DELETE 更改的信息,其中包括 I/U/D 操作和更改的行的 ID。我正在使用带有名字列和自动递增 ID 的测试 tables 来测试双向复制。
如果我对 NAME table 进行插入,例如添加自动 ID 为“1”的 'JOHN',我是否能够插入该 ID 值甚至可能 'JOHN'进入日志 table?我正在查看 OLD.ID 或 NEW.ID 选项,但我不知道这是否可行。我的 trigger/function 代码是:
CREATE OR REPLACE FUNCTION Insert() RETURNS TRIGGER AS $ReplInsert$
BEGIN
IF (TG_OP = 'DELETE') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('DELETE', OLD.ID, OLD.NAME);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('UPDATE', NEW.ID ,NEW.NAME);
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('UPDATE', NEW.ID , NEW.NAME);
RETURN NEW;
END IF;
RETURN NULL;
END;
$ReplInsert$ LANGUAGE plpgsql;
CREATE TRIGGER logTrg
AFTER INSERT OR UPDATE OR DELETE ON "FIRST_NAME"
FOR EACH ROW EXECUTE PROCEDURE Insert();
这最终是为了使用触发器进行双向复制。感谢您的任何考虑或指导。我会根据需要进行编辑。
我能够通过在触发器中将 AFTER 更改为 BEFORE 来解决这个问题,我认为一开始这是行不通的,或者:
CREATE OR REPLACE FUNCTION Insert() RETURNS TRIGGER AS $ReplInsert$
BEGIN
IF (TG_OP = 'DELETE') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('DELETE', OLD.ID, OLD.NAME);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('UPDATE', NEW.ID ,NEW.NAME);
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO "log" ("Operation","ID","NAME")
VALUES ('UPDATE', NEW.ID , NEW.NAME);
RETURN NEW;
END IF;
RETURN NULL;
END;
$ReplInsert$ LANGUAGE plpgsql;
CREATE TRIGGER logTrg
BEFORE INSERT OR UPDATE OR DELETE ON "FIRST_NAME"
FOR EACH ROW EXECUTE PROCEDURE Insert();