PostgreSQL 13: 逻辑复制支持分区表
发表于 2020-06-22 | 分类于
Postgres基础 |
0 | 阅读次数: 847
PostgreSQL 10 版本开始支持逻辑复制,在12版本之前逻辑复制仅支持普通表,不支持分区表,如果需要对分区表进行逻辑复制,需单独对所有分区进行逻辑复制。
PostgreSQL 13 版本的逻辑复制新增了对分区表的支持,如下:
- 可以显式地发布分区表,自动发布所有分区。
- 从分区表中添加/删除分区将自动从发布中添加/删除。
发行说明的解释如下:
发行说明
Allow partitioned tables to be logically replicated via publications (Amit Langote)
Previously, partitions had to be replicated individually. Now partitioned tables can be published explicitly causing all partitions to be automatically published. Addition/removal of partitions from partitioned tables are automatically added/removed from publications. The CREATE PUBLICATION option publish_via_partition_root controls whether changes to partitions are published as their own or their ancestors.
Allow logical replication into partitioned tables on subscribers (Amit Langote)
Previously, subscribers could only receive rows into non-partitioned tables.
关于逻辑复制之前博客有介绍,详见PostgreSQL10:逻辑复制(Logical Replication)之一,本文仅做简单演示。
环境规划
环境规划,如下:
节点 |
数据库版本 |
IP |
端口 |
源库 |
PostgreSQL 13beta1 |
192.168.2.11 |
1922 |
目标库 |
PostgreSQL 13beta1 |
192.168.2.13 |
1924 |
环境准备
在源库、目标库安装 PostgreSQL 13beta1软件并初始化数据库,本文略。
部署mydb数据库
在源库和目标库上均部署 mydb 数据库,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
--建用户 CREATE ROLE pguser LOGIN ENCRYPTED PASSWORD 'pguser' nosuperuser noinherit nocreatedb nocreaterole ;
--创建表空间(如果有 Standby ,也需要创建目录) mkdir -p /pgdata/pg13/pg_tbs/tbs_mydb
--创建数据库 CREATE DATABASE mydb WITH OWNER = postgres TEMPLATE = template0 ENCODING = 'UTF8' TABLESPACE = tbs_mydb;
--赋权 grant all on database mydb to pguser with grant option; grant all on tablespace tbs_mydb to pguser;
\c mydb pguser create schema pguser;
|
创建分区表
在源库和目标库上创建分区表,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
--创建父表 CREATE TABLE tbl_log ( id serial, user_id int4, create_time timestamp(0) without time zone ) PARTITION BY RANGE(create_time);
--创建子表 CREATE TABLE tbl_log_his PARTITION OF tbl_log FOR VALUES FROM (minvalue) TO ('2020-01-01'); CREATE TABLE tbl_log_202001 PARTITION OF tbl_log FOR VALUES FROM ('2020-01-01') TO ('2020-02-01'); CREATE TABLE tbl_log_202002 PARTITION OF tbl_log FOR VALUES FROM ('2020-02-01') TO ('2020-03-01'); CREATE TABLE tbl_log_202003 PARTITION OF tbl_log FOR VALUES FROM ('2020-03-01') TO ('2020-04-01'); CREATE TABLE tbl_log_202004 PARTITION OF tbl_log FOR VALUES FROM ('2020-04-01') TO ('2020-05-01'); CREATE TABLE tbl_log_202005 PARTITION OF tbl_log FOR VALUES FROM ('2020-05-01') TO ('2020-06-01'); CREATE TABLE tbl_log_202006 PARTITION OF tbl_log FOR VALUES FROM ('2020-06-01') TO ('2020-07-01'); CREATE TABLE tbl_log_202007 PARTITION OF tbl_log FOR VALUES FROM ('2020-07-01') TO ('2020-08-01');
--创建索引 CREATE INDEX idx_tbl_log_ctime ON tbl_log USING BTREE (create_time);
|
部署逻辑复制
源库执行以下操作,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
--创建复制用户 CREATE USER repuser REPLICATION LOGIN CONNECTION LIMIT 10 ENCRYPTED PASSWORD 'rep123us345er';
--创建发布 mydb=> CREATE PUBLICATION pub1 FOR TABLE tbl_log; CREATE PUBLICATION
--给repuser用户赋权 mydb=> GRANT CONNECT ON DATABASE mydb TO repuser; GRANT mydb=> GRANT USAGE ON SCHEMA pguser TO repuser; GRANT mydb=> GRANT SELECT ON ALL TABLES IN SCHEMA pguser TO repuser; GRANT
|
以上有个步骤是给源库上的repuser用户赋相关权限,如果不给repuser用户赋权,创建订阅后目标库无法初始化同步源库数据。
目标库创建订阅,如下:
1 2 3
|
mydb=# CREATE SUBSCRIPTION sub1 CONNECTION 'host=192.168.2.11 port=1922 dbname=mydb user=repuser' PUBLICATION pub1; NOTICE: created replication slot "sub1" on publisher CREATE SUBSCRIPTION
|
注意配置好源库的pg_hba.conf
和.pgpass
文件,否则创建订阅会报相关的连接不上错误。
数据验证
源库批量插入数据,如下:
1 2
|
INSERT INTO tbl_log(user_id,create_time) SELECT round(100000000*random()),generate_series('2019-10-01'::date, '2020-06-20'::date, '1 day');
|
源库查看数据,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
[pg13@ydtf01 ~]$ psql mydb pguser -p 1922 psql (13beta1) Type "help" for help.
mydb=> SELECT count(*) FROM tbl_log; count ------- 264 (1 row)
mydb=> SELECT count(*) FROM tbl_log_202001; count ------- 31 (1 row)
mydb=> SELECT count(*) FROM tbl_log_his; count ------- 92 (1 row)
|
目标库验证数据,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
[pg13@ydtf03 ~]$ psql mydb pguser -p 1924 psql (13beta1) Type "help" for help.
mydb=> SELECT count(*) FROM tbl_log; count ------- 264 (1 row)
mydb=> SELECT count(*) FROM tbl_log_202001; count ------- 31 (1 row)
mydb=> SELECT count(*) FROM tbl_log_his; count ------- 92 (1 row)
|
可见分区表的数据已从源库同步到目标库。
参考