本专栏全部文章 ​​​​​​​https://blog.csdn.net/tonghu_note/category_11713514.html
总目录 https://blog.csdn.net/tonghu_note/article/details/124333034

来我的dou音 aa10246666, 看配套视频


一、实战环境

节点 cn_node1(协调节点,不存储数据) postgresql 14.2 10.211.55.9
节点 worker1_node2(工作节点,存储数据) postgresql 14.2 10.211.55.4
节点 worker2_node3(工作节点,存储数据) postgresql 14.2 10.211.55.6
citus-10.2 / debian 9

二、SQL命令参考

1、分布表转为普通表

在cn节点,查看系统中现有的Citus表

d1=# select * from citus_tables;
 table_name | citus_table_type | distribution_column | colocation_id | table_size | shard_count | table_owner | access_method 
------------+------------------+---------------------+---------------+------------+-------------+-------------+---------------
 t1         | distributed      | id                  |             2 | 8512 kB    |          32 | postgres    | heap
 t11        | distributed      | id                  |             2 | 8512 kB    |          32 | postgres    | heap
 t2         | reference        | <none>              |             1 | 13 MB      |           1 | postgres    | heap
 t22        | reference        | <none>              |             1 | 13 MB      |           1 | postgres    | heap
(4 rows)

在cn节点,我们将以上的分布表t1在线改为普通表

d1=# SELECT undistribute_table('t1');
NOTICE:  creating a new table for public.t1
NOTICE:  moving the data of public.t1
NOTICE:  dropping the old public.t1
NOTICE:  renaming the new table to public.t1
 undistribute_table 
--------------------
 
(1 row)

在cn节点,再查看系统中现有的Citus表,可以看到表t1已经转为了普通表

d1=# select * from citus_tables;
 table_name | citus_table_type | distribution_column | colocation_id | table_size | shard_count | table_owner | access_method 
------------+------------------+---------------------+---------------+------------+-------------+-------------+---------------
 t11        | distributed      | id                  |             2 | 8512 kB    |          32 | postgres    | heap
 t2         | reference        | <none>              |             1 | 13 MB      |           1 | postgres    | heap
 t22        | reference        | <none>              |             1 | 13 MB      |           1 | postgres    | heap
(3 rows)

在worker节点, 查看表情况

worker1节点

d1=# \d
            List of relations
 Schema |     Name     | Type  |  Owner   
--------+--------------+-------+----------
 public | citus_tables | view  | postgres
 public | t11_102040   | table | postgres
 public | t11_102042   | table | postgres
 public | t11_102044   | table | postgres
 public | t11_102046   | table | postgres
 public | t11_102048   | table | postgres
 public | t11_102050   | table | postgres
 public | t11_102052   | table | postgres
 public | t11_102054   | table | postgres
 public | t11_102056   | table | postgres
 public | t11_102058   | table | postgres
 public | t11_102060   | table | postgres
 public | t11_102062   | table | postgres
 public | t11_102064   | table | postgres
 public | t11_102066   | table | postgres
 public | t11_102068   | table | postgres
 public | t11_102070   | table | postgres
 public | t22_102073   | table | postgres
 public | t2_102072    | table | postgres
(19 rows)

worker2节点

d1=# \d
            List of relations
 Schema |     Name     | Type  |  Owner   
--------+--------------+-------+----------
 public | citus_tables | view  | postgres
 public | t11_102041   | table | postgres
 public | t11_102043   | table | postgres
 public | t11_102045   | table | postgres
 public | t11_102047   | table | postgres
 public | t11_102049   | table | postgres
 public | t11_102051   | table | postgres
 public | t11_102053   | table | postgres
 public | t11_102055   | table | postgres
 public | t11_102057   | table | postgres
 public | t11_102059   | table | postgres
 public | t11_102061   | table | postgres
 public | t11_102063   | table | postgres
 public | t11_102065   | table | postgres
 public | t11_102067   | table | postgres
 public | t11_102069   | table | postgres
 public | t11_102071   | table | postgres
 public | t22_102073   | table | postgres
 public | t2_102072    | table | postgres
(19 rows)

可以看到在worker1和worker2节点,t1表已经消失了,说明已经转成了普通表。

2、指定分布表之间的亲和性

新建分布表

SELECT create_distributed_table('stores', 'store_id');

指定分布表之间的亲和性

SELECT create_distributed_table('orders', 'store_id', colocate_with => 'stores');
SELECT create_distributed_table('products', 'store_id', colocate_with => 'stores');

通过指定colocate_with => 'stores'可以指定分布表之间的关联亲和性,对于关联查询非常有帮助

3、在所有worker节点执行命令

查询所有worker节点的端口

d1=> SELECT run_command_on_workers($cmd$ show port; $cmd$);
    run_command_on_workers    
------------------------------
 (192.168.40.143,5432,t,5432)
 (192.168.40.147,5432,t,5432)
(2 rows)

d1=>

4、添加新的worker节点

新增一个worker节点192.168.40.132,先在这个节点上完成 《童虎学习笔记》5分钟入门PG分布式集群Citus的"安装Citus"的内容完成基础配置

在cn节点上添加这个worker节点

d1=# select * from citus_add_node('192.168.40.132', 5432);
NOTICE:  Replicating reference table "t2" to the node 192.168.40.132:5432
NOTICE:  Replicating reference table "t22" to the node 192.168.40.132:5432
 citus_add_node 
----------------
              5
(1 row)

这个操作会将参考表都同步到这个新增的worker节点,但分布表不会同步

可以通过如下命令查看现有节点情况

SELECT * FROM citus_get_active_worker_nodes();

5、添加非激活worker节点

也可以用下面的命令添加一个非激活状态的worker节点,这个节点连参考表都不会同步

d1=# select * from citus_add_inactive_node('192.168.40.132', 5432);

接下来激活这个节点,它会自动同步参考表

d1=# select citus_activate_node('192.168.40.132', 5432);
 citus_activate_node 
---------------------
                   6
(1 row)

6、将节点变为非激活状态

d1=# select citus_disable_node('192.168.40.132', 5432);
 citus_disable_node 
--------------------
 
(1 row)

7、删除worker节点

d1=# select * from citus_remove_node('192.168.40.132', 5432);
 citus_remove_node 
-------------------
 
(1 row)

8、查看Citus表占用的所有分片大小

d1=# SELECT pg_size_pretty(citus_relation_size('t1'));
 pg_size_pretty 
----------------
 4416 kB
(1 row)

d1=# SELECT pg_size_pretty(citus_relation_size('t2'));
 pg_size_pretty 
----------------
 13 MB
(1 row)

可以看到t1表是分布表大小是4M,t2表是参考表,每个shard都有一个全量复本,所以大小比t1大。其中有3个函数可以计算表大小,函数之间的区别见下表

citus_relation_size 含main fork, 但不含visibility map和free space map
citus_table_size  citus_relation_size + visibility map和free space map, 但不含索引
citus_total_relation_size citus_table_size + 索引

9、移动shard

192.168.40.143上的shard 102209,我希望将它移到192.168.40.147这台服务器上

192.168.40.143

d1=# \d
            List of relations
 Schema |     Name     | Type  |  Owner   
--------+--------------+-------+----------
 public | citus_tables | view  | postgres
 public | t11_102203   | table | postgres
 public | t11_102206   | table | postgres
 public | t11_102209   | table | postgres
 public | t11_102212   | table | postgres
 public | t11_102215   | table | postgres
 public | t11_102218   | table | postgres
 public | t11_102221   | table | postgres
 public | t11_102224   | table | postgres
 public | t11_102227   | table | postgres
 public | t11_102230   | table | postgres
 public | t11_102233   | table | postgres
 public | t1_102171    | table | postgres
 public | t1_102174    | table | postgres
 public | t1_102177    | table | postgres
 public | t1_102180    | table | postgres
 public | t1_102183    | table | postgres
 public | t1_102186    | table | postgres
 public | t1_102189    | table | postgres
 public | t1_102192    | table | postgres
 public | t1_102195    | table | postgres
 public | t1_102198    | table | postgres
 public | t1_102201    | table | postgres
 public | t22_102073   | table | postgres
 public | t2_102072    | table | postgres
(25 rows)

命令如下

SELECT citus_move_shard_placement(102209, '192.168.40.143', 5432, '192.168.40.147', 5432);

查看2台服务器shard分布情况

192.168.40.143

d1=# \d
            List of relations
 Schema |     Name     | Type  |  Owner   
--------+--------------+-------+----------
 public | citus_tables | view  | postgres
 public | t11_102203   | table | postgres
 public | t11_102206   | table | postgres
 public | t11_102212   | table | postgres
 public | t11_102215   | table | postgres
 public | t11_102218   | table | postgres
 public | t11_102221   | table | postgres
 public | t11_102224   | table | postgres
 public | t11_102227   | table | postgres
 public | t11_102230   | table | postgres
 public | t11_102233   | table | postgres
 public | t1_102171    | table | postgres
 public | t1_102174    | table | postgres
 public | t1_102180    | table | postgres
 public | t1_102183    | table | postgres
 public | t1_102186    | table | postgres
 public | t1_102189    | table | postgres
 public | t1_102192    | table | postgres
 public | t1_102195    | table | postgres
 public | t1_102198    | table | postgres
 public | t1_102201    | table | postgres
 public | t22_102073   | table | postgres
 public | t2_102072    | table | postgres
(23 rows)

192.168.40.147

d1=# \d
            List of relations
 Schema |     Name     | Type  |  Owner   
--------+--------------+-------+----------
 public | citus_tables | view  | postgres
 public | t11_102204   | table | postgres
 public | t11_102207   | table | postgres
 public | t11_102209   | table | postgres
 public | t11_102210   | table | postgres
 public | t11_102213   | table | postgres
 public | t11_102216   | table | postgres
 public | t11_102219   | table | postgres
 public | t11_102222   | table | postgres
 public | t11_102225   | table | postgres
 public | t11_102228   | table | postgres
 public | t11_102231   | table | postgres
 public | t1_102172    | table | postgres
 public | t1_102175    | table | postgres
 public | t1_102177    | table | postgres
 public | t1_102178    | table | postgres
 public | t1_102181    | table | postgres
 public | t1_102184    | table | postgres
 public | t1_102187    | table | postgres
 public | t1_102190    | table | postgres
 public | t1_102193    | table | postgres
 public | t1_102196    | table | postgres
 public | t1_102199    | table | postgres
 public | t22_102073   | table | postgres
 public | t2_102072    | table | postgres
(25 rows)

可见102209已经移到了192.168.40.147服务器上

10、查看各节点的连接数情况

d1=# SELECT * from citus_remote_connection_stats();
    hostname    | port | database_name | connection_count_to_node 
----------------+------+---------------+--------------------------
 192.168.40.143 | 5432 | d1            |                        2
 192.168.40.147 | 5432 | d1            |                        2
 192.168.40.132 | 5432 | d1            |                        2
(3 rows)

《童虎学习笔记》5分钟Citus之SQL命令参考相关推荐

  1. 《童虎学习笔记》PostgreSQL超简单新手入门教程

    总目录:https://blog.csdn.net/tonghu_note/article/details/124333034 第1节 3分钟学会在linux下安装PostgreSQL 第2节 2分钟 ...

  2. 《童虎学习笔记》20分钟实战ProxySQL MGR高可用及读写分离架构

    本文章配套视频 https://www.ixigua.com/7086085500540289572?id=7087546160079962660 本专栏全部文章 https://blog.csdn. ...

  3. 《童虎学习笔记》5分钟了解Citus核心系统表

    本专栏全部文章 https://blog.csdn.net/tonghu_note/category_11713514.html 总目录 https://blog.csdn.net/tonghu_no ...

  4. 《童虎学习笔记》5分钟入门PG分布式集群Citus

    本专栏全部文章 https://blog.csdn.net/tonghu_note/category_11713514.html 总目录 https://blog.csdn.net/tonghu_no ...

  5. 《童虎学习笔记》14分钟结合ProxySQL处理超半数MGR节点故障

    本文章配套视频 https://www.ixigua.com/7086085500540289572?id=7088719800846778910 本专栏全部文章 https://blog.csdn. ...

  6. 《童虎学习笔记》3分钟学会PostgreSQL实时监控利器pgCenter

       本文章配套视频 https://www.ixigua.com/7077056019024904717?id=7078684048586965512 本专栏全部文章 https://blog.cs ...

  7. 《童虎学习笔记》15分钟ShardingSphere搭建PostgreSQL分库分表

    本文章配套视频 https://www.ixigua.com/7077056019024904717?id=7082741456641163790 本专栏全部文章 https://blog.csdn. ...

  8. 《童虎学习笔记》11分钟学会MySQL基于时间点的恢复(gtid方式)

       本文章配套视频 https://www.ixigua.com/7092706197576516110 本专栏全部文章 https://blog.csdn.net/tonghu_note/cate ...

  9. 《童虎学习笔记》3分钟学会如何优雅的重启MySQL MGR集群

     本文章配套视频 https://www.ixigua.com/7086085500540289572?id=7083884629215674911 本专栏全部文章 https://blog.csdn ...

最新文章

  1. Python实现tab文件操作
  2. VS如何新建一个基于对话框的MFC工程并添加按钮
  3. 【BCFTOOLS】按样本拆分VCF文件
  4. php curl向另一个页面post,一个PHP CURL的POST提交遇到的问题
  5. Intellij IDEA + Maven——jar项目改成war项目相互转换
  6. 【ES11(2020)】Dynamic Import 动态引入
  7. PC端 java 开发蓝牙所遇到的问题
  8. java 旅行家的预算_旅行家的预算
  9. java解析xml文件失败,在Java中解析大型XML文件时找不到文件异常
  10. 历史上的今天:美团网正式上线;Dropbox 的创始人出生;PS2 游戏机问世
  11. class属性表种类集合,以及字段详解
  12. oracle混音插件教程,【图片】【教学】waves混音插件官方教学贴,长期更新_混音吧_百度贴吧...
  13. kdj的matlab代码,8个字符的Kdj股票技术指标公式源代码(插图)
  14. iPhone照片备份与恢复
  15. 母亲节是在每年五月份的第二个星期日,给定年份,求出当年母亲节的日期
  16. codeforce Zebras(思维 + 模拟)
  17. Django-QuerySet之first(),last(),latest(),earliest()
  18. Linux:ftrace: 为什么有些函数没有在available_filter_functions
  19. 123457123456#0#-----com.cym.shuXue02--前拼后广--开心学数学
  20. 22.1.2是否存在三升序列

热门文章

  1. 苹果手机软件升级密码_密码太多总是忘?不如试试这7个密保工具
  2. 金融机构银行架构变迁
  3. python汉字同义词替换_python同义词替换
  4. 编程将10进制转换2进制(将十进制转换为二进制)
  5. 有哪些好的上报crash工具:推荐crashlytics
  6. 火山引擎ImageX图片服务纯代码使用方法额度领取
  7. 新浪博客服务器是不是在维护,新浪博客外推接单,新浪博客界面为什么总是打不开?...
  8. [PM]产品经理的主要职责有哪些?
  9. python中print format的用法-python format用法详解
  10. 这就是搜索引擎——搜索引擎索引(1)