Postgresql触发器来检查更改了哪些列的值?

时间:2018-07-30 07:56:26

标签: python postgresql triggers

在使用postgresql(v.9.5)触发器进行更新之前,需要查找行中哪些列的值已更改,基于列名,必须返回Null或NEW。当表的列数很多时如何实现?

1 个答案:

答案 0 :(得分:2)

在触发器json中使用FOR EACH ROW。 注意:如果旧值/新值可能是IS DISTINCT FROM,请使用NULL

CREATE OR REPLACE FUNCTION public.test_tr_old_new()
  RETURNS trigger AS $$
DECLARE
  _rec record;
BEGIN
  FOR _rec IN
    SELECT
      o.key
      , o.value AS old_value
      , n.value AS new_value
    FROM json_each(to_json(new)) n
      INNER JOIN json_each(to_json(old)) o ON o.key = n.key
  LOOP
    IF (_rec.old_value::text IS DISTINCT FROM _rec.new_value::text) THEN
      RAISE NOTICE 'test_tr_old_new(): Column % changed from ''%'' to ''%''', _rec.key, _rec.old_value, _rec.new_value;
    END IF;
  END LOOP;

  RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';

CREATE TRIGGER old_new
  BEFORE UPDATE
  ON public._tr_table
  FOR EACH ROW
EXECUTE PROCEDURE public.test_tr_old_new();