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

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:

利用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)

搞个执行计划,定时执行就可以了(前提是至少执行周期比删除范围窄).
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
关系型数据库 数据库 PostgreSQL
postgresql :permission denied to create database
postgresql :permission denied to create database
1011 0
|
1天前
|
人工智能 Oracle 关系型数据库
一篇文章弄懂Oracle和PostgreSQL的Database Link
一篇文章弄懂Oracle和PostgreSQL的Database Link
|
1天前
|
人工智能 关系型数据库 数据库
PostgreSQL 常见问题解决方案 - ERROR: database is being accessed by other users
PostgreSQL 常见问题解决方案 - ERROR: database is being accessed by other users
|
7月前
|
JSON Java 关系型数据库
Spring Boot 学习研究笔记(十三) Spring Data JPA与PostgreSQL的jsonb类型集成
Spring Boot 学习研究笔记(十三) Spring Data JPA与PostgreSQL的jsonb类型集成
101 0
|
12月前
|
SQL 存储 Oracle
|
12月前
|
SQL 关系型数据库 分布式数据库
|
12月前
|
SQL 关系型数据库 分布式数据库
|
存储 Prometheus Kubernetes
云原生 PostgreSQL 集群 - PGO:来自 Crunchy Data 的 Postgres Operator
云原生 PostgreSQL 集群 - PGO:来自 Crunchy Data 的 Postgres Operator
478 0
云原生 PostgreSQL 集群 - PGO:来自 Crunchy Data 的 Postgres Operator
|
存储 Oracle 关系型数据库
德哥解读:微软官宣收购PostgreSQL初创公司Citus Data
昨天在微软的官方博客官宣了微软对PostgreSQL初创公司Citus Data的收购。原文在https://blogs.microsoft.com/blog/2019/01/24/microsoft-acquires-citus-data-re-affirming-its-commitment-to-open-source-and-accelerating-azure-postgresql-performance-and-scale/?from=timeline&isappinstalled=0 背景介绍: Citus Data 2010年成立于加州旧金山。
4369 0