{"id":237434,"date":"2022-03-24T10:30:52","date_gmt":"2022-03-24T02:30:52","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=237434"},"modified":"2022-03-11T11:32:05","modified_gmt":"2022-03-11T03:32:05","slug":"citus-of-distributed-postgresql","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/citus-of-distributed-postgresql.html","title":{"rendered":"\u5206\u5e03\u5f0fPostgreSQL\u4e4bCitus"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u6bcf\u4e2acluster\u90fd\u6709\u4e00\u4e2a\u79f0\u4e3acoordinator(\u534f\u8c03\u5668) \u7684\u7279\u6b8a\u8282\u70b9\uff08\u5176\u4ed6\u8282\u70b9\u79f0\u4e3a worker\u8282\u70b9\uff09\u3002\u5e94\u7528\u7a0b\u5e8f\u5c06\u5b83\u4eec\u7684\u67e5\u8be2\u53d1\u9001\u5230coordinator\u8282\u70b9\uff0ccoordinator\u8282\u70b9\u5c06\u5176\u8f6c\u53d1\u7ed9\u76f8\u5173\u7684worker\u5e76\u7d2f\u79ef\u7ed3\u679c\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u8282\u70b9<\/strong><\/div>\n Citus \u662f\u4e00\u79cd PostgreSQL \u6269\u5c55\uff0c\u5b83\u5141\u8bb8\u6570\u636e\u5e93\u670d\u52a1\u5668(\u79f0\u4e3a\u8282\u70b9)\u5728\u201c\u65e0\u5171\u4eab(shared nothing)\u201d\u67b6\u6784\u4e2d\u76f8\u4e92\u534f\u8c03\u3002\u8fd9\u4e9b\u8282\u70b9\u5f62\u6210\u4e00\u4e2a\u96c6\u7fa4\uff0c\u5141\u8bb8 PostgreSQL \u4fdd\u5b58\u6bd4\u5355\u53f0\u8ba1\u7b97\u673a\u4e0a\u66f4\u591a\u7684\u6570\u636e\u548c\u4f7f\u7528\u66f4\u591a\u7684 CPU \u5185\u6838\u3002\u8fd9\u79cd\u67b6\u6784\u8fd8\u5141\u8bb8\u901a\u8fc7\u7b80\u5355\u5730\u5411\u96c6\u7fa4\u6dfb\u52a0\u66f4\u591a\u8282\u70b9\u6765\u6269\u5bb9\u6570\u636e\u5e93\u3002<\/p>\n \u6269\u5c55:https:\/\/www.postgresql.org\/docs\/current\/external-extensions.html<\/p>\n Coordinator \u4e0e Worker<\/strong><\/span><\/div>\n \u6bcf\u4e2a cluster \u90fd\u6709\u4e00\u4e2a\u79f0\u4e3a coordinator(\u534f\u8c03\u5668) \u7684\u7279\u6b8a\u8282\u70b9(\u5176\u4ed6\u8282\u70b9\u79f0\u4e3a worker \u8282\u70b9)\u3002\u5e94\u7528\u7a0b\u5e8f\u5c06\u5b83\u4eec\u7684\u67e5\u8be2\u53d1\u9001\u5230 coordinator \u8282\u70b9\uff0ccoordinator \u8282\u70b9\u5c06\u5176\u8f6c\u53d1\u7ed9\u76f8\u5173\u7684 worker \u5e76\u7d2f\u79ef\u7ed3\u679c\u3002<\/p>\n \u5bf9\u4e8e\u6bcf\u4e2a\u67e5\u8be2\uff0ccoordinator \u8981\u4e48\u5c06\u5176\u8def\u7531\u5230\u5355\u4e2a worker \u8282\u70b9\uff0c\u8981\u4e48\u5c06\u5176\u5e76\u884c\u5316\u5230\u591a\u4e2a\u8282\u70b9\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6240\u9700\u6570\u636e\u662f\u4f4d\u4e8e\u5355\u4e2a\u8282\u70b9\u4e0a\u8fd8\u662f\u591a\u4e2a\u8282\u70b9\u4e0a\u3002coordinator \u901a\u8fc7\u67e5\u9605\u5176\u5143\u6570\u636e\u8868\u77e5\u9053\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u8fd9\u4e9b Citus \u7279\u5b9a\u8868\u8ddf\u8e2a worker \u8282\u70b9\u7684 DNS \u540d\u79f0\u548c\u8fd0\u884c\u72b6\u51b5\uff0c\u4ee5\u53ca\u8de8\u8282\u70b9\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n \u5206\u5e03\u5f0f\u6570\u636e<\/strong><\/div>\n Citus \u96c6\u7fa4\u4e2d\u6709\u4e09\u79cd\u7c7b\u578b\u7684\u8868\uff0c\u6bcf\u79cd\u8868\u90fd\u4ee5\u4e0d\u540c\u65b9\u5f0f\u5b58\u50a8\u5728\u8282\u70b9\u4e2d\uff0c\u5e76\u4e14\u7528\u4e8e\u4e0d\u540c\u7684\u76ee\u7684\u3002<\/p>\n \u7c7b\u578b 1\uff1a\u5206\u5e03\u5f0f\u8868<\/strong><\/span><\/div>\n \u7b2c\u4e00\u79cd\u7c7b\u578b\uff0c\u4e5f\u662f\u6700\u5e38\u89c1\u7684\uff0c\u662f\u5206\u5e03\u5f0f\u8868\u3002\u5bf9\u4e8e SQL \u8bed\u53e5\u800c\u8a00\uff0c\u5b83\u4eec\u770b\u4f3c\u662f\u666e\u901a\u7684\u8868\uff0c\u4f46\u5728 worker \u8282\u70b9\u4e4b\u95f4\u6c34\u5e73\u5206\u533a\u3002<\/p>\n <\/p>\n \u8fd9\u91cc table \u7684\u884c\u5b58\u50a8\u5728 worker \u7684\u8868 table_1001\u3001table_1002 \u7b49\u4e2d\u3002\u7ec4\u4ef6 worker \u8868\u79f0\u4e3a\u5206\u7247(shards)\u3002<\/p>\n Citus \u4f7f\u7528\u4f7f\u7528\u5206\u7247\u7b97\u6cd5\u5c06\u884c\u5206\u914d\u5230\u5206\u7247\u3002\u57fa\u4e8e\u8868\u5217(\u79f0\u4e3a\u5206\u5e03\u5217(distribution column))\u7684\u503c\u6267\u884c\u5206\u914d\uff0c\u6b64\u5206\u914d\u5177\u6709\u786e\u5b9a\u6027\u3002\u96c6\u7fa4\u7ba1\u7406\u5458\u5728\u5206\u5e03\u8868\u65f6\u5fc5\u987b\u6307\u5b9a\u6b64\u5217\u3002\u505a\u51fa\u6b63\u786e\u7684\u9009\u62e9\uff0c\u8fd9\u4e00\u70b9\u5bf9\u4e8e\u6027\u80fd\u548c\u529f\u80fd\u6709\u91cd\u8981\u5f71\u54cd\u3002<\/p>\n \u7c7b\u578b 2\uff1a\u5f15\u7528\u8868<\/strong><\/span><\/div>\n \u5f15\u7528\u8868 \u662f\u4e00\u79cd\u5206\u5e03\u5f0f\u8868\uff0c\u5176\u5168\u90e8\u5185\u5bb9\u90fd\u96c6\u4e2d\u5230\u5355\u4e2a\u5206\u7247\u4e2d\uff0c\u5e76\u5728\u6bcf\u4e2a worker \u4e0a\u590d\u5236\u3002\u56e0\u6b64\uff0c\u5bf9\u4efb\u4f55 worker \u7684\u67e5\u8be2\u90fd\u53ef\u4ee5\u5728\u672c\u5730\u8bbf\u95ee \u5f15\u7528 \u4fe1\u606f\uff0c\u65e0\u9700\u4ece\u53e6\u4e00\u4e2a\u8282\u70b9\u8bf7\u6c42\u884c\uff0c\u56e0\u6b64\u4e5f\u4e0d\u4f1a\u4ea7\u751f\u6b64\u7c7b\u7f51\u7edc\u5f00\u9500\u3002\u5f15\u7528\u8868\u6ca1\u6709\u5206\u5e03\u5217\uff0c\u56e0\u4e3a\u65e0\u9700\u533a\u5206\u6bcf\u884c\u7684\u5404\u4e2a\u5206\u7247\u3002<\/p>\n \u5f15\u7528\u8868 \u901a\u5e38\u5f88\u5c0f\uff0c\u7528\u4e8e\u5b58\u50a8\u4e0e\u5728\u4efb\u4f55\u5de5\u4f5c\u8282\u70b9\u4e0a\u8fd0\u884c\u7684\u67e5\u8be2\u76f8\u5173\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8ba2\u5355\u72b6\u6001\u6216\u4ea7\u54c1\u7c7b\u522b\u7b49\u679a\u4e3e\u503c\u3002<\/p>\n \u5f53\u4e0e \u5f15\u7528\u8868 \u4ea4\u4e92\u65f6\uff0c\u6211\u4eec\u4f1a\u81ea\u52a8\u5bf9\u4e8b\u52a1\u6267\u884c\u4e24\u9636\u6bb5\u63d0\u4ea4 (2PC)\u3002\u8fd9\u610f\u5473\u7740 Citus \u786e\u4fdd\u60a8\u7684\u6570\u636e\u59cb\u7ec8\u5904\u4e8e\u4e00\u81f4\u72b6\u6001\uff0c\u65e0\u8bba\u60a8\u662f\u5728\u5199\u5165\u3001\u4fee\u6539\u8fd8\u662f\u5220\u9664\u5b83\u3002<\/p>\n 2PC:https:\/\/en.wikipedia.org\/wiki\/Two-phase_commit_protocol<\/p>\n \u7c7b\u578b 3\uff1a\u672c\u5730\u8868<\/strong><\/span><\/div>\n \u5f53\u60a8\u4f7f\u7528 Citus \u65f6\uff0c\u60a8\u8fde\u63a5\u5e76\u4e0e\u4e4b\u4ea4\u4e92\u7684 coordinator \u8282\u70b9\u662f\u5b89\u88c5\u4e86 Citus \u6269\u5c55\u7684\u5e38\u89c4 PostgreSQL \u6570\u636e\u5e93\u3002\u56e0\u6b64\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u666e\u901a\u8868\u5e76\u9009\u62e9\u4e0d\u5bf9\u5176\u8fdb\u884c\u5206\u7247\u3002\u8fd9\u5bf9\u4e8e\u4e0d\u53c2\u4e0e\u8fde\u63a5\u67e5\u8be2\u7684\u5c0f\u578b\u7ba1\u7406\u8868\u5f88\u6709\u7528\u3002\u4e00\u4e2a\u793a\u4f8b\u662f\u7528\u4e8e\u5e94\u7528\u7a0b\u5e8f\u767b\u5f55\u548c\u8eab\u4efd\u9a8c\u8bc1\u7684\u7528\u6237\u8868\u3002<\/p>\n \u521b\u5efa\u6807\u51c6 PostgreSQL \u8868\u5f88\u5bb9\u6613\uff0c\u56e0\u4e3a\u5b83\u662f\u9ed8\u8ba4\u503c\u3002\u8fd9\u662f\u4f60\u8fd0\u884c CREATE TABLE \u65f6\u5f97\u5230\u7684\u3002\u5728\u51e0\u4e4e\u6bcf\u4e2a Citus \u90e8\u7f72\u4e2d\uff0c\u6211\u4eec\u90fd\u4f1a\u770b\u5230\u6807\u51c6 PostgreSQL \u8868\u4e0e distributed \u548c reference \u8868\u5171\u5b58\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5982\u524d\u6240\u8ff0\uff0cCitus \u672c\u8eab\u4f7f\u7528\u672c\u5730\u8868\u6765\u4fdd\u5b58\u96c6\u7fa4\u5143\u6570\u636e\u3002<\/p>\n Shards<\/strong><\/span><\/div>\n \u4e0a\u4e00\u8282\u5c06\u5206\u7247\u63cf\u8ff0\u4e3a\u5728 worker \u8282\u70b9\u5185\u7684\u8f83\u5c0f\u8868\u4e2d\u5305\u542b\u5206\u5e03\u5f0f\u8868\u7684\u884c\u7684\u5b50\u96c6\u3002\u672c\u8282\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u6280\u672f\u7ec6\u8282\u3002<\/p>\n \u534f\u8c03\u5668\u4e0a\u7684 pg_dist_shard \u5143\u6570\u636e\u8868\u5305\u542b\u7cfb\u7edf\u4e2d\u6bcf\u4e2a\u5206\u5e03\u5f0f\u8868\u7684\u6bcf\u4e2a\u5206\u7247\u7684\u884c\u3002\u8be5\u884c\u4e0e\u5206\u7247 ID \u76f8\u5339\u914d\uff0c\u5206\u7247 ID \u7684\u8303\u56f4\u662f\u4e00\u7ec4\u54c8\u5e0c\u6574\u6570 (shardminvalue, shardmaxvalue)\u3002<\/p>\n \r\nSELECT * from pg_dist_shard;\r\n logicalrelid | shardid | shardstorage | shardminvalue | shardmaxvalue\r\n---------------+---------+--------------+---------------+---------------\r\n github_events | 102026 | t | 268435456 | 402653183\r\n github_events | 102027 | t | 402653184 | 536870911\r\n github_events | 102028 | t | 536870912 | 671088639\r\n github_events | 102029 | t | 671088640 | 805306367\r\n (4 rows)\r\n<\/pre>\n |