pglogic中的replication set是逻辑复制中表的集合,默认自带了三个,如下:
logic=# select * from pglogical.replication_set;
set_id | set_nodeid | set_name | replicate_insert | replicate_update | replicate_delete | replicate_truncate
------------+------------+---------------------+------------------+------------------+------------------+--------------------
2551820615 | 2230476291 | default | t | t | t | t
968332880 | 2230476291 | default_insert_only | t | f | f | t
3714372109 | 2230476291 | ddl_sql | t | f | f | f
在有多个订阅端的时候,可以利用replication set来标识表复制到那些订阅端。
发布端:
创建测试表
\c logic logic
logic=> create table tbl_test01 (a int primary key,b text);
logic=> insert into tbl_test01 select t,t||'hank' from generate_series(1,100) as t;
创建replication set,并给set添加需要复制的表
\c logic postgres
logic=# select pglogical.create_replication_set('replica01');
create_replication_set
------------------------
1500904000
logic=# select * from pglogical.replication_set;
set_id | set_nodeid | set_name | replicate_insert | replicate_update | replicate_delete | replicate_truncate
------------+------------+---------------------+------------------+------------------+------------------+--------------------
2551820615 | 2230476291 | default | t | t | t | t
968332880 | 2230476291 | default_insert_only | t | f | f | t
3714372109 | 2230476291 | ddl_sql | t | f | f | f
1500904000 | 2230476291 | replica01 | t | t | t | t
logic=# select pglogical.replication_set_add_table('replica01','logic.tbl_test01');
replication_set_add_table
---------------------------
t
(1 row)
logic=# select * from pglogical.tables ;
relid | nspname | relname | set_name
-------+---------+------------+-----------
16653 | logic | tbl_logic | default
16905 | logic | tbl_test01 | replica01
订阅端:
创建subscription,加上replica01
logic=# select pglogical.create_subscription('sub_replica01', 'host=10.4.9.166 port=1921 user=postgres dbname=logic', ARRAY['replica01']);
create_subscription
---------------------
1714075028
logic=# select count(*) from logic.tbl_test01 ;
count
-------
100
(1 row)
给原有的subscription添加或删除replication set
#添加
logic=# select pglogical.alter_subscription_add_replication_set('logic_subscription','replica01' );
-[ RECORD 1 ]--------------------------+--
alter_subscription_add_replication_set | t
logic=# select * from pglogical.subscription;
-[ RECORD 1 ]-----------+------------------------------------------------
sub_id | 1714075028
sub_name | sub_replica01
sub_origin | 2230476291
sub_target | 774206297
sub_origin_if | 2529271354
sub_target_if | 1665314130
sub_enabled | t
sub_slot_name | pgl_logic_logic_pub_sub_replica01
sub_replication_sets | {replica01}
sub_forward_origins | {all}
sub_apply_delay | 00:00:00
sub_force_text_transfer | f
-[ RECORD 2 ]-----------+------------------------------------------------
sub_id | 218173469
sub_name | logic_subscription
sub_origin | 2230476291
sub_target | 774206297
sub_origin_if | 2529271354
sub_target_if | 1665314130
sub_enabled | t
sub_slot_name | pgl_logic_logic_pub_logic_su0d01101
sub_replication_sets | {default,default_insert_only,ddl_sql,replica01}
sub_forward_origins | {all}
sub_apply_delay | 00:00:00
sub_force_text_transfer | f
#删除
logic=# select pglogical.alter_subscription_remove_replication_set('logic_subscription','replica01' );
-[ RECORD 1 ]-----------------------------+--
alter_subscription_remove_replication_set | t
logic=# select * from pglogical.subscription;
-[ RECORD 1 ]-----------+--------------------------------------
sub_id | 1714075028
sub_name | sub_replica01
sub_origin | 2230476291
sub_target | 774206297
sub_origin_if | 2529271354
sub_target_if | 1665314130
sub_enabled | t
sub_slot_name | pgl_logic_logic_pub_sub_replica01
sub_replication_sets | {replica01}
sub_forward_origins | {all}
sub_apply_delay | 00:00:00
sub_force_text_transfer | f
-[ RECORD 2 ]-----------+--------------------------------------
sub_id | 218173469
sub_name | logic_subscription
sub_origin | 2230476291
sub_target | 774206297
sub_origin_if | 2529271354
sub_target_if | 1665314130
sub_enabled | t
sub_slot_name | pgl_logic_logic_pub_logic_su0d01101
sub_replication_sets | {default,default_insert_only,ddl_sql}
sub_forward_origins | {all}
sub_apply_delay | 00:00:00
sub_force_text_transfer | f
注意
如果上面添加replication set后,在订阅端又新创建了subscription,那么就会多出一个复制槽
logic=# select * from pg_replication_slots ;
pgl_logic_logic_pub_logic_su0d01101 | pglogical_output | logical | 16651 | logic | f | t | 16094 | | 689 | 0/B7244600 | 0/B
7244638
pgl_logic_logic_pub_sub_replica01 | pglogical_output | logical | 16651 | logic | f | t | 22278 | | 689 | 0/B7244600 | 0/B
7244638
如果是把replication set加入到原来的subscription,就还是用原来的复制槽
logic=# select * from pg_replication_slots ;
pgl_logic_logic_pub_logic_su0d01101 | pglogical_output | logical | 16651 | logic | f | t | 24520 | | 701 | 0/B7265108 | 0/B
7265140
无论是新建subscription,还是添加到原来的subscription,复制表的效果是 一样的。