版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/ctypyb2002/article/details/84030093
os: ubuntu 16.04
postgresql: 9.6.8
citus: postgresql-9.6-citus 8.0.0
ip规划如下:
192.168.0.92 pgsql1 --coordinator 节点
192.168.0.90 pgsql2 --worker 节点
192.168.0.88 pgsql3 --worker 节点
192.168.0.86 pgsql4 --worker 节点
现在需要移除pgsql4节点。
一定要将pgsql4节点上的数据移动到其余的worker节点上,才能执行master_remove_node。
如若先执行master_remove_node,就会提示 WARNING: relation " " does not exist。
使用pg_dump 迁移数据
要使用逻辑复制就必须是 postgresql 10 以上,10版本以下没有逻辑复制功能。也可以使用dblink或者 postgresql_fdw。
使用逻辑复制功能能够处理期间变化的数据,否则就需要停机维护,确保期间数据库数据不会变化。
复制分片
–获取worker节点
citusdb=# select * from pg_dist_node;
nodeid | groupid | nodename | nodeport | noderack | hasmetadata | isactive | noderole | nodecluster
--------+---------+--------------+----------+----------+-------------+----------+----------+-------------
1 | 1 | 192.168.0.90 | 5432 | default | f | t | primary | default
2 | 2 | 192.168.0.88 | 5432 | default | f | t | primary | default
4 | 4 | 192.168.0.86 | 5432 | default | f | t | primary | default
(3 rows)
–分布信息
citusdb=# select * from pg_dist_placement
where shardid in (select shardid from pg_dist_shard where logicalrelid='tmp_t0'::regclass)
and groupid=4;
placementid | shardid | shardstate | shardlength | groupid
-------------+---------+------------+-------------+---------
71 | 102074 | 1 | 0 | 2
72 | 102075 | 1 | 0 | 1
citusdb=# select * from pg_dist_placement
where shardid in (select shardid from pg_dist_shard where logicalrelid='tmp_t3'::regclass)
and groupid=4;
placementid | shardid | shardstate | shardlength | groupid
-------------+---------+------------+-------------+---------
297 | 102234 | 1 | 0 | 4
(1 row)
或者一次性查询出来
citusdb=# select t2.logicalrelid,t2.shardid,t2.shardstorage,t0.shardstate,t1.nodeid,t1.groupid,t1.nodename
from pg_dist_placement t0,
pg_dist_node t1,
pg_dist_shard t2
where 1=1
and t0.groupid=t1.groupid
and t1.groupid=4
and t0.shardid=t2.shardid
;
logicalrelid | shardid | shardstorage | shardstate | nodeid | groupid | nodename
--------------+---------+--------------+------------+--------+---------+--------------
tmp_t0 | 102074 | t | 1 | 4 | 4 | 192.168.0.86
tmp_t0 | 102075 | t | 1 | 4 | 4 | 192.168.0.86
ref_t0 | 102072 | t | 1 | 4 | 4 | 192.168.0.86
ref_t1 | 102073 | t | 1 | 4 | 4 | 192.168.0.86
tmp_t3 | 102234 | t | 1 | 4 | 4 | 192.168.0.86
(5 rows)
pgsql4节点上导出指定分片表
$ pg_dump -U cituser -h 192.168.0.86 -t tmp_t0_102075 -b -v -f /tmp/tmp_t0_102075.sql citusdb
$ pg_dump -U cituser -h 192.168.0.86 -t tmp_t0_102074 -b -v -f /tmp/tmp_t0_102074.sql citusdb
$ pg_dump -U cituser -h 192.168.0.86 -t tmp_t3_102234 -b -v -f /tmp/tmp_t3_102234.sql citusdb
pgsql2节点上导入指定表
$ psql -h 192.168.0.90 -U cituser citusdb < /tmp/tmp_t0_102074.sql
$ psql -h 192.168.0.90 -U cituser citusdb < /tmp/tmp_t0_102075.sql
$ psql -h 192.168.0.90 -U cituser citusdb < /tmp/tmp_t3_102234.sql
元数据修改
pgsql1节点上锁定元数据
citusdb=# begin;
citusdb=#
update pg_dist_placement set groupid=1 where shardid=102075 and groupid=4;
update pg_dist_placement set groupid=1 where shardid=102074 and groupid=4;
update pg_dist_placement set groupid=1 where shardid=102234 and groupid=4;
coordinator 节点上移除 worker 节点
citusdb=# select * from pg_dist_node;
nodeid | groupid | nodename | nodeport | noderack | hasmetadata | isactive | noderole | nodecluster
--------+---------+--------------+----------+----------+-------------+----------+----------+-------------
1 | 1 | 192.168.0.90 | 5432 | default | f | t | primary | default
2 | 2 | 192.168.0.88 | 5432 | default | f | t | primary | default
4 | 4 | 192.168.0.86 | 5432 | default | f | t | primary | default
(3 rows)
citusdb=# select * from master_remove_node('192.168.0.86',5432);
citusdb=#
验证
pgsql1 节点上查询
citusdb=# select count(1) from tmp_t0;
count
---------
2000000
(1 row)
citusdb=# select count(1) from tmp_t3;
count
-------
10000
(1 row)
参考:
https://www.citusdata.com/
https://docs.citusdata.com/en/v8.0/
https://docs.citusdata.com/en/stable/index.html