瀚高数据库
本文主要用于介绍pglogical的安装配置
一、简介
pglogical 2插件(后边简称pglogical)使用发布/订阅的模式为PostgreSQL提供了逻辑流复制的实现方式。pglogicla是基于BDR项目的一部分技术发展而来。我们一般使用以下名称来描述节点间的数据流,复用了之前的Slony技术解析:
Nodes(节点) - Postgresql数据库实例
Providers and Subscribers(发布者和订阅者) - Nodes使用的角色
Replication Set(复制集) - 表的集合
pglogical使用pg最新的内核特性,因为使用上有一下版本限制:
发布者和订阅者节点必要运行在PostgreSQL 9.4版本及以上
复制源的筛选和冲突检测只在PostgreSQL9.5版本及以上
订阅者端可以是Postgres-XL 9.5版本及以上
支持的使用场景包括
可用于大版本升级。
完整数据库复制。
使用复制集选择性地复制表集。
在发布者服务器端或订阅者服务器端选择性复制表行(row_filter)。
在发布者端选择性复制表列
从多个上游服务器收集/合并数据
架构细节:
pglogical工作在单库级别而不是像流复制一样的实例级别
一个订阅者端可以在不引起额外的磁盘写负载的情况下同时为多个订阅端传输数据
一个发布者端可以合并来自多个源的更改并自动检测与处理冲突。(多主机需求的某些方面)
二、要求
为了使用pglogical插件,发布者端和订阅者端必须跑在Postgresql9.4及以上的版本中。pglogical插件必须在发布者端跟订阅者端同时安装,必须在两端执行CREATE EXTENSION pglogical需要同步的表在发布者端和订阅者端必须有相同的模式名称及表名称需要同步的表在发布者和订阅者端必须有相同的列,每列的数据类型必须相同。检查约束、非空约束等也要相同,或者订阅者端的限制比发布者端要弱一些。表必须有相同的主键。不建议额外添加除主键之外的其他唯一约束。
三、安装示例
pglogical提供rpm包安装及源码安装两种方式,rpm包安装较为简答,本文主要介绍源码包的安装方法。pglogical源码包的安装同其他任何PostgreSQL的扩展安装一样使用PGXS。确保包含PostgreSQL发行版中pg_config的目录配置在PATH的环境变量中。如果你的环境中没有安装pg_config这个命令,你需要用你的包管理器来安装对应版本的开发包。下面以pg14.6为数据库版本为大家演示pglogical插件安装及单向数据同步搭建过程。OS:redhat7.7 CPU: X86_64database:pg14.6extension version:2.4.2Providers:192.168.164.51 Subscribers:192.168.164.52 1、安装扩展发布端、订阅端解压缩安装pglogical,下面为发布端的日志
[root@host1 opt]# tar -zxvf pglogical-REL2_4_2.tar.gz [root@host1 opt]# chown -R postgres:postgres pglogical-REL2_4_2 [root@host1 opt]# su - postgres [postgres@host1 ~]$ cd /opt/pglogical-REL2_4_2 [postgres@host1 pglogical-REL2_4_2]$ make clean [postgres@host1 pglogical-REL2_4_2]$ make [postgres@host1 pglogical-REL2_4_2]$ make install
2、修改数据库参数及pg_hba文件,发布端订阅端同时修改postgresql.conf文件修改内容
wal_level = 'logical' max_worker_processes = 10 # one per database needed on provider node # one per node needed on subscriber node max_logical_replication_workers = 10 max_replication_slots = 10 # one per node needed on provider node max_wal_senders = 10 # one per node needed on provider node shared_preload_libraries = 'pglogical' pg_hba.conf文件修改内容,增加本文段免密登录。 # IPv4 local connections: host all all 127.0.0.1/32 trust host all all 192.168.164.0/24 trust 修改完成后重启发布端及订阅端数据库生效 [postgres@host1 data]$ pg_ctl restart [postgres@host2 data]$ pg_ctl restart
3、创建扩展
发布端 [postgres@host1 data]$ psql test1 psql (14.6) Type "help" for help. test1=# create extension pglogical; CREATE EXTENSION 订阅端: [postgres@host2 data]$ psql test2 postgres psql (14.6) Type "help" for help. test2=# create extension pglogical; CREATE EXTENSION
4、创建测试表及测试数据发布端创建测试表,模拟测试数据
test1=# \c test1 test1 You are now connected to database "test1" as user "test1". test1=> test1=> create table t01(id int primary key,name text); CREATE TABLE test1=> insert into t01 select n,'aaaaaa' from generate_series(1,1000) n; INSERT 0 1000 test1=> create table t02(id int primary key,name text); CREATE TABLE test1=> insert into t02 select n,'aaaaaa' from generate_series(1,1000) n; INSERT 0 1000 test1=> create table t03(id int primary key,name text); CREATE TABLE test1=> insert into t03 select n,'aaaaaa' from generate_series(1,1000) n; INSERT 0 1000 订阅端只创建测试表 [postgres@host2 ~]$ psql test2 test2 test2=> create table t01(id int primary key,name text); CREATE TABLE test2=> create table t02(id int primary key,name text); CREATE TABLE test2=> create table t03(id int primary key,name text); CREATE TABLE
5、发布端创建节点,创建复制集,将要同步的表t01加入到复制集中
##使用超级用户创建发布节点
test1=> \c test1 postgres
You are now connected to database "test1" as user "postgres".
test1=# select pglogical.create_node(node_name :='provider1',dsn :='host=192.168.164.51 port=5432 dbname=test1');
create_node
-------------
2976894835
(1 row)
##创建复制集
test1=# select pglogical.create_replication_set('defalut');
create_replication_set
------------------------
692406752
(1 row)
##增加表
test1=# select pglogical.replication_set_add_table('default','public.t01');
replication_set_add_table
---------------------------
t
(1 row)
##确定复制集表信息
test1=# select * from pglogical.replication_set_table;
set_id | set_reloid | set_att_list | set_row_filter
-----------+------------+--------------+----------------
290045701 | t01 | |
(1 row)
6、订阅端创建节点 ##使用超级用户创建订阅节点
test2=# \c test2 postgres You are now connected to database "test2" as user "postgres". test2=# test2=# select pglogical.create_node(node_name :='subscriber1',dsn :='host=192.168.164.52 port=5432 dbname=test2'); create_node ------------- 330520249 (1 row) test2=# 2022-12-09 16:53:06.188 CST [12715] LOG: manager worker [12715] at slot 0 generation 4 detaching cleanly 2022-12-09 16:53:06.191 CST [12716] LOG: starting pglogical database manager for database test2 2022-12-09 16:53:07.195 CST [12717] LOG: manager worker [12717] at slot 1 generation 1 detaching cleanly ##创建订阅 test2=# SELECT pglogical.create_subscription(subscription_name := 'subscription1',provider_dsn := 'host=192.168.164.51 port=5432 dbname=test1'); create_subscription --------------------- 1763399739 (1 row) test2=# 2022-12-09 16:56:18.755 CST [12744] LOG: manager worker [12744] at slot 2 generation 1 detaching cleanly 2022-12-09 16:56:18.756 CST [12745] LOG: starting apply for subscription subscription1 2022-12-09 16:56:18.759 CST [12746] LOG: manager worker [12746] at slot 2 generation 2 detaching cleanly
7、核对订阅端数据 ##查询三张表信息,可以看到只有加入到复制集的表的数据同步了过来
test2=# select count(*) from t01; count ------- 1000 (1 row) test2=# select count(*) from t02; count ------- 0 (1 row) test2=# select count(*) from t03; count ------- 0 (1 row) ##发布端再插入1000条数据,查阅订阅端数据 test1=# \c test1 test1 You are now connected to database "test1" as user "test1". test1=> insert into t01 select n,'aaaaaa' from generate_series(1001,2000) n; INSERT 0 1000 test1=> select count(*) from t01; count ------- 2000 (1 row) test2=# select count(*) from t01; count ------- 2000 (1 row) 单向逻辑复制同步完成,增量数据及基量数据均成功。
编辑推荐:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- pglogical的安装配置
pglogical的安装配置
26-03-14 - PostgreSQL的 \watch命令
PostgreSQL的 \watch命令
26-03-14 - 培训动态 | 第3期PGCA-浪潮K1 Power培训认证圆满结束
培训动态 | 第3期PGCA-浪潮K1 Power培训认证圆满结束
26-03-14 - RockyLinux RPM包查询全攻略(手把手教你使用rpm -q命令)
- PostgreSQL共享内存里的内容(initCommunication)
- PostgreSQL:Redhat 8.5 + PostgreSQL 14.5 安装
- PostgreSQL数据目录下的postmaster.pid详解
PostgreSQL数据目录下的postmaster.pid详解
26-03-14 - PG 数据库 从阿里云pg rds 同步数据。
PG 数据库 从阿里云pg rds 同步数据。
26-03-14 - PostgreSQL/GreenPlum Merge Inner Join解密
- 从Oracle的SQL_ID到PG14引入内核的QUERY_ID
从Oracle的SQL_ID到PG14引入内核的QUERY_ID
26-03-14
