使用Postgres触发器记录仅修改字段的JSON
有没有办法获得唯一修改的字段的JSON?
现在,我使用以下触发器,但整个行都印在ChangElog中。
示例表:
TABLE tbl_changelog (
tbl TEXT,
op TEXT,
new JSON,
old JSON
);
TABLE tbl_items (
f1 TEXT,
f2 TEXT
);
触发器:
CREATE OR REPLACE FUNCTION changelog_procedure() RETURNS trigger AS $$
BEGIN
INSERT INTO tbl_changelog(tbl, op, new, old)
VALUES (TG_TABLE_NAME, TG_OP, row_to_json(NEW), row_to_json(OLD));
RETURN NULL;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER changelog_items
AFTER INSERT OR UPDATE OR DELETE ON tbl_items
FOR EACH ROW EXECUTE PROCEDURE changelog_procedure();
插入和上传的F2和F1后,ChangElog如下:
tbl_changelog
------------------------------------------------------------------
tbl | op | new | old
------------------------------------------------------------------
tbl_items | INSERT | {f1: "aa", f2: "bb"} |
------------------------------------------------------------------
tbl_items | UPDATE | {f1: "aa", f2: "cc"} | {f1: "aa", f2: "bb"}
------------------------------------------------------------------
tbl_items | UPDATE | {f1: "dd", f2: "cc"} | {f1: "aa", f2: "cc"}
------------------------------------------------------------------
我只想记录更改,即:
tbl_changelog
------------------------------------------------------------------
tbl | op | new | old
------------------------------------------------------------------
tbl_items | INSERT | {f1: "aa", f2: "bb"} |
------------------------------------------------------------------
tbl_items | UPDATE | {f2: "cc"} | {f2: "bb"}
------------------------------------------------------------------
tbl_items | UPDATE | {f1: "dd"} | {f1: "aa"}
------------------------------------------------------------------
Is there a way to get the JSON of the only modified fields?
Now I use the following trigger but the entire line is printed in the changelog.
Example tables:
TABLE tbl_changelog (
tbl TEXT,
op TEXT,
new JSON,
old JSON
);
TABLE tbl_items (
f1 TEXT,
f2 TEXT
);
Trigger:
CREATE OR REPLACE FUNCTION changelog_procedure() RETURNS trigger AS $
BEGIN
INSERT INTO tbl_changelog(tbl, op, new, old)
VALUES (TG_TABLE_NAME, TG_OP, row_to_json(NEW), row_to_json(OLD));
RETURN NULL;
END;
$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER changelog_items
AFTER INSERT OR UPDATE OR DELETE ON tbl_items
FOR EACH ROW EXECUTE PROCEDURE changelog_procedure();
After inserting and uploaded f2 and f1 the changelog is the following:
tbl_changelog
------------------------------------------------------------------
tbl | op | new | old
------------------------------------------------------------------
tbl_items | INSERT | {f1: "aa", f2: "bb"} |
------------------------------------------------------------------
tbl_items | UPDATE | {f1: "aa", f2: "cc"} | {f1: "aa", f2: "bb"}
------------------------------------------------------------------
tbl_items | UPDATE | {f1: "dd", f2: "cc"} | {f1: "aa", f2: "cc"}
------------------------------------------------------------------
I would like to record only the changes, that is:
tbl_changelog
------------------------------------------------------------------
tbl | op | new | old
------------------------------------------------------------------
tbl_items | INSERT | {f1: "aa", f2: "bb"} |
------------------------------------------------------------------
tbl_items | UPDATE | {f2: "cc"} | {f2: "bb"}
------------------------------------------------------------------
tbl_items | UPDATE | {f1: "dd"} | {f1: "aa"}
------------------------------------------------------------------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的触发功能无法正常工作,它在插入一行时会产生错误:
您应该分别处理
insert
的三种情况 和delete
单独处理:Your trigger function cannot work well, it produces the error while inserting a row:
You should treat the three cases of
INSERT
,UPDATE
andDELETE
separately: