Postgres利用触发器实现增量订阅

在进行数据库迁移时涉及增量,对于增量数据,可以利用触发器对要迁移的表的dml操作进行跟踪记录。该方式同样可应用于dml审计。操作如下:
1、创建测试表

CREATE TABLE test (
   id int primary key,
   info varchar(255)
)

2、安装hstore extension

CREATE EXTENSION hstore

3、创建用于存储dml记录的表

CREATE TABLE IF NOT EXISTS dml_record (
   id serial8 primary key,
   -- 操作的表所归属的schema
   schema_name text,
   -- 操作的表
   table_name text,
   level text,
   -- 操作类型:INSERT、UPDATE、DETELE
   operation text,
   -- 原记录值
   old_record hstore,
   -- 新记录值
   new_record hstore,
   -- dml操作时间
   dml_time timestamp without time zone DEFAULT now()
);

4、创建通用触发器

CREATE OR REPLACE FUNCTION dml_trace()
RETURNS trigger LANGUAGE plpgsql 
AS $BODY$ 
  DECLARE
    v_new_rec hstore;
    v_old_rec hstore;
  BEGIN
    case TG_OP
      when 'DELETE' then 
        v_old_rec := hstore(OLD.*); 
        insert into dml_record (schema_name, table_name, level, operation, old_record) values (tg_table_schema, tg_table_name, tg_level, tg_op, v_old_rec);
      when 'INSERT' then 
        v_new_rec := hstore(NEW.*);
        insert into dml_record (schema_name, table_name, level, operation, new_record) values (tg_table_schema, tg_table_name, tg_level, tg_op, v_new_rec);
      when 'UPDATE' then
        v_old_rec := hstore(OLD.*); 
        v_new_rec := hstore(NEW.*);
        insert into dml_record (schema_name, table_name, level, operation, old_record, new_record) values (tg_table_schema, tg_table_name, tg_level, tg_op, v_old_rec, v_new_rec);
      else return null; 
    end case; 
    RETURN null; 
  END; 
$BODY$ strict;

5、在测试表创建触发器

CREATE TRIGGER tg AFTER DELETE or INSERT or UPDATE 
ON test FOR EACH ROW EXECUTE PROCEDURE dml_trace()

6、通过JAVA代码读取dml_record表中记录,生成增量数据的SQL语句导入到目标数据库中,完成增量迁移。
7、完成增量迁移后,删除函数、触发器和记录表即可。

猜你喜欢

转载自blog.csdn.net/Loiterer_Y/article/details/84951556
今日推荐