[example]Sync data from PostgreSQL database to another PostgreSQL database

利用EDB的database link实现从PostgreSQL到PostgreSQL的数据同步。
源数据是持续插入,无更新操作的记录.将create_time作为同步标记.并且需要定期清除同步来的历史数据.

Source Database : PostgreSQL
Destination Database : PostgreSQL
Sync Database : EnterpriseDB

SD_table : 
                                      Table "public.s_table"
    Column    |            Type             |                             Modifiers                              
--------------+-----------------------------+--------------------------------------------------------------------
 id           | bigint                      | not null default nextval('s_table_id_seq'::regclass)
 create_time  | timestamp without time zone | default now()
 s_id         | integer                     | 
 url          | character varying(1000)     | 
 ip           | character varying(32)       | 
 module       | character varying(64)       | 
 resource_id  | character varying(100)      | 
 source       | character varying(64)       | 
 refer_url    | character varying(1000)     | 
 app_id       | integer                     | 
 last_foot_id | bigint                      | 
Indexes:
    "pk_s_table" PRIMARY KEY, btree (id)
    "create_time_index" btree (create_time)

DD_table : 
            Table "sync.d_table"
   Column   |            Type             |   Modifiers   
------------+-----------------------------+---------------
 id         | bigint                      | not null
 appid      | integer                     | 
 module     | character varying(64)       | 
 createtime | timestamp without time zone | default now()
Indexes:
    "pk_d_table" PRIMARY KEY, btree (id)
    "idx_d_table_1" btree (createtime)

SD_database_link :
                                             List of database links
         Link          | Access |    Owner     | Type  |   User   |              Connection String               
-----------------------+--------+--------------+-------+----------+----------------------------------------------
 sync_s | PUBLIC | enterprisedb | LIBPQ | s_user | host=xxx.xxx.xxx.xxx port=xxxx dbname=s_database_name
 sync_d | PUBLIC | enterprisedb | LIBPQ | d_user | host=xxx.xxx.xxx.xxx port=xxxx dbname=d_database_name

SD_sync_function : 
create or replace function f_sync_d_table () returns void as $BODY$
 declare 
s_max_time timestamp without time zone;
d_max_time timestamp without time zone;
del_time timestamp without time zone;
return_rows int;
begin 
del_time := now() - interval '2 hour';
select max(createtime) into d_max_time from d_table@sync_d;
raise notice 'd_max_time is %.',d_max_time;
select max(create_time) into s_max_time from s_table@sync_s where create_time>d_max_time;
raise notice 'The record time before % and after % will be synced.',s_max_time,d_max_time;
insert into d_table@sync_d (id,appid,module,createtime) select id,app_id,module,create_time from s_table@sync_s where create_time>d_max_time and create_time<s_max_time;
GET DIAGNOSTICS return_rows = ROW_COUNT;
raise notice 'sync row count is %.',return_rows;
raise notice 'The record time before % and before % will be deleted.',d_max_time,del_time;
delete from d_table@sync_d where createtime < d_max_time and createtime < del_time;
GET DIAGNOSTICS return_rows = ROW_COUNT;
raise notice 'del row count is %.',return_rows;
end;
$BODY$ language plpgsql;

TEST:
select * from f_sync_d_table();
NOTICE:  d_max_time is 04-NOV-10 11:06:06.186422.
NOTICE:  The record time before 04-NOV-10 11:08:06.193542 and after 04-NOV-10 11:06:06.186422 will be synced.
NOTICE:  sync row count is 164.
NOTICE:  The record time before 04-NOV-10 11:06:06.186422 and before 04-NOV-10 09:09:01.409094 will be deleted.
NOTICE:  del row count is 0.
 f_sync_tbl_deposit_visit_log 
------------------------------
 
(1 row)

搞个执行计划,定时执行就可以了(前提是至少执行周期比删除范围窄).

时间: 2024-08-02 23:02:42

[example]Sync data from PostgreSQL database to another PostgreSQL database的相关文章

想挑战AlphaGO吗?先和PostgreSQL玩一玩?? PostgreSQL与人工智能(AI)

标签 PostgreSQL , AI , 人工智能 , 神经网络 , 机器学习 , MADlib , Greenplum , 物联网 , IoT , pivotalR , R 背景 想必很多朋友这几天被铺天盖地的AI战胜60位围棋高手的新闻,我们不得不承认,人工智能真的越来越强了. http://wallstreetcn.com/node/283152 1月4日晚,随着古力认输,Master对人类顶尖高手的战绩停留在60胜0负1和,而令人尴尬的是这唯一一场和棋还是因为棋手掉线系统自动判和,并不是

如何远程使用PostgreSQL Studio可视化查看PostgreSQL数据库

1.下载 前往官网地址下载最新的PostgreSQL Studio,我下载的是 pgstudio_1.2-bin .zip,因为我的电脑里面没有tomcat. 如果电脑里有配置好tomcat,可以下载pgstudio_1.2.zip,解压之后是一个war包. 下载地址:http://www.postgresqlstudio.org/download/ 2.解压 将压缩文件解压,可以看到如下目录: 3.运行 在bin目录下运行: ./catalina.sh run 4.查看 浏览器中输入:loca

【OH】Creating a Database with the CREATE DATABASE Statement

 来源于online help  http://docs.oracle.com/cd/E11882_01/server.112/e25494/create.htm Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administration Database Administrator's Guide Creating a Database with the CREATE

Error &amp;#39;Can&amp;#39;t drop database &amp;#39;just&amp;#39;; database doesn&amp;#39;t exist&amp;#39; on query.

 Last_SQL_Error: Error 'Can't drop database 'just'; database doesn't exist' on query. Default database: 'just'. Query: 'drop database just'   Relay_Master_Log_File: master-bin.000001              Slave_IO_Running: Yes             Slave_SQL_Running: N

a foolish sync method about sync data from PostgreSQL to Oracle

使用以下脚步从PostgreSQL同步到Oracle的数据不一致.  原因分析在后面 #!/bin/bash . /home/enterprisedb/.bash_profile EMAIL="noc@xxx.com dba@xxx.com" # check running mark test -f /home/enterprisedb/script/run/tbl_charge_xxxx.run if [ $? -eq 0 ]; then SYNC_TIME_CHECK="

Alibaba Cloud RDS vs. IBM Cloud Database vs. Oracle Cloud Database

When moving applications to the cloud, or creating new cloud-native apps, the database that stores your information is vitally important, and this should be a core decision that drives the application design. This article provides a technical review

postgresql php adodb-关于postgreSQL数据库操作的问题

问题描述 关于postgreSQL数据库操作的问题 本人现在要开发一个web程序,用到了postgreSQL数据库,我以前都是Adodb操作数据库的,可是发现不支持postgreSQL最新版本的数据库,现在以前的程序都要修改,比较麻烦,请问有人知道有解决问题的办法吗?

PostgreSQL 并行聚合工具 PostgreSQL Agg

PostgreSQL Agg 详细介绍 Agg 是 PostgreSQL 的并行聚合工具.在很长的时间里 PostgreSQL 只是使用单 CPU 核心来执行昂贵的聚合.Agg 的目的是要改变这种情况,把分析操作扩展到大量的 CPU 核心,使用系统的全部计算能力来执行. 特性 并行执行常规聚合 (sum, count, min, max, etc.) 支持 FILTER-clauses 支持约束限制 (每个分区由一个 CPU 核心处理) 超简单的安装步骤 支持 PostgreSQL 9.5 Ag

exchange 2007数据库无法恢复 Failed to mount database &amp;amp;#x27;Mailbox Database&amp;amp;#x27; (着急,在线等)

问题描述 exchange2007服务器因为磁盘阵列问题导致系统崩溃,数据从磁盘阵列读出来以后,执行装载操作报-1018错误.根据网上搜索,我们执行了eseutil/p操作,由于邮件数据size很大,一直修复过程中.期间当我们要登陆主机再次确认是否修复完成时,无法登陆主机了!非常着急,只能通过网络映射数据文件所在目录检查文件情况,发现文件时间一直没有更新了(执行修复时,文件会随时进行更新),据此我们判断已经修复完成,但由于一直无法登陆主机,所以只能重启.重启后尝试再次进行数据库装载,失败,此时操