分布式 citusnode
以前文章中已经部署了postgresql10,安装目录/home/pg10/soft。sql
安装 yum -y install libcurl-devel openssl-devel zlib-devel readline-devel libevent-devel curl
建立cn 和 worker 节点分布式
[pg10@data01 ~]$ mkdir -p /data01/pg10/{citus_cn,citus_worker01,citus_worker02} ide
初始化 cn 节点路径为 ~/citus_cn,worker节点分别为 ~/citus_worker01,~/citus_worker02post
[pg10@data01 ~]$ initdb -D /data01/pg10/citus_cnflex
[pg10@data01 ~]$ initdb -D /data01/pg10/citus_worker01ui
[pg10@data01 ~]$ initdb -D /data01/pg10/citus_worker02url
修改cn 和dn 节点下的 postgresql.auto.conf每一个节点分别添加以下内容prototype
echo "PORT=1921">> /data01/pg10/citus_cn/postgresql.auto.conf
echo "PORT=1922">> /data01/pg10/citus_worker01/postgresql.auto.conf
echo "PORT=1923">> /data01/pg10/citus_worker02/postgresql.auto.conf
echo "listen_addresses='*'">> /data01/pg10/citus_cn/postgresql.auto.conf
echo "listen_addresses='*'">> /data01/pg10/citus_worker01/postgresql.auto.conf
echo "listen_addresses='*'">> /data01/pg10/citus_worker02/postgresql.auto.conf
启动三个节点
[pg10@data01 ~]$ pg_ctl start -D /data01/pg10/citus_cn -l /tmp/cnlog
[pg10@data01 ~]$ pg_ctl start -D /data01/pg10/citus_worker01 -l /tmp/dn1log
[pg10@data01 ~]$ pg_ctl start -D /data01/pg10/citus_worker02 -l /tmp/dn2log
--链接到每一个实例下建立 postgres 超级用户
[pg10@data01 pg10]$ pg_ctl start -D /data01/pg10/citus_cn -l /tmp/cnlog
waiting for server to start.... done
server started
[pg10@data01 pg10]$ pg_ctl start -D /data01/pg10/citus_worker01 -l /tmp/dn1log
waiting for server to start.... done
server started
[pg10@data01 pg10]$ pg_ctl start -D /data01/pg10/citus_worker02 -l /tmp/dn2log
waiting for server to start.... done
server started
[pg10@data01 pg10]$ psql -d postgres -p1921
psql (10.14)
Type "help" for help.
postgres=# CREATE USER postgres WITH SUPERUSER PASSWORD '1qaz@WSX';
CREATE ROLE
postgres=# \q
[pg10@data01 pg10]$ psql -d postgres -p1922
psql (10.14)
Type "help" for help.
postgres=# CREATE USER postgres WITH SUPERUSER PASSWORD '1qaz@WSX';
CREATE ROLE
postgres=# \q
[pg10@data01 pg10]$ psql -d postgres -p1923
psql (10.14)
Type "help" for help.
postgres=# CREATE USER postgres WITH SUPERUSER PASSWORD '1qaz@WSX';
CREATE ROLE
postgres=# \q
最新版本不兼容pg10版本
[pg10@data01 citus-master]$ ./autogen.sh
[pg10@data01 citus-master]$ ./configure
checking for a sed that does not truncate output... /bin/sed
checking for gawk... gawk
checking for flex... /bin/flex
checking for pg_config... /home/pg10/soft/bin/pg_config
configure: error: Citus is not compatible with the detected PostgreSQL version 10.
换成9.0版本编译
[pg10@data01 citus-9.0.0]$ ./autogen.sh
[pg10@data01 citus-9.0.0]$ ./configure
checking for a sed that does not truncate output... /bin/sed
checking for gawk... gawk
checking for flex... /bin/flex
checking for pg_config... /home/pg10/soft/bin/pg_config
configure: building against PostgreSQL 10
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking size of void *... 8
checking whether gcc supports -Wall... yes
checking whether gcc supports -Wextra... yes
checking whether gcc supports -Wno-unused-parameter... yes
checking whether gcc supports -Wno-sign-compare... yes
checking whether gcc supports -Wno-missing-field-initializers... yes
checking whether gcc supports -Wno-clobbered... yes
checking whether gcc supports -Wno-gnu-variable-sized-type-not-at-end... yes
checking whether gcc supports -Wdeclaration-after-statement... yes
checking whether gcc supports -Wendif-labels... yes
checking whether gcc supports -Wmissing-format-attribute... yes
checking whether gcc supports -Wmissing-declarations... yes
checking whether gcc supports -Wmissing-prototypes... yes
checking whether gcc supports -Wshadow... yes
checking whether gcc supports -Werror=vla... yes
checking for curl_global_init in -lcurl... yes
checking curl/curl.h usability... yes
checking curl/curl.h presence... yes
checking for curl/curl.h... yes
configure: creating ./config.status
config.status: creating Makefile.global
config.status: creating src/include/citus_config.h
config.status: creating src/include/citus_version.h
[pg10@data01 citus-9.0.0]$
--链接到每一个实例下,
--给每一个实例下的 pg_hba.conf 添加以下
[pg10@data01 ~]$ egrep 0.0.0.0 /data01/pg10/citus_{cn,worker01,worker02}/pg_hba.conf
/data01/pg10/data_cn/pg_hba.conf:host all all 0.0.0.0/0 trust
/data01/pg10/data_worker01/pg_hba.conf:host all all 0.0.0.0/0 trust
/data01/pg10/data_worker02/pg_hba.conf:host all all 0.0.0.0/0 trust
进行reload
[pg10@data01 ~]$ for pgdata in citus_cn citus_worker01 citus_worker02;do
> pg_ctl reload -D /data01/pg10/$pgdata;
> done
server signaled
server signaled
server signaled
在 postgresql.conf 中添加 预载库,并从新启动
for pgdata in citus_cn citus_worker01 citus_worker02;do
echo "shared_preload_libraries = 'citus'" >> /data01/pg10/$pgdata/postgresql.conf;
pg_ctl restart -D /data01/pg10/$pgdata -l /tmp/$pgdata
done
[pg10@data01 ~]$ for pgdata in citus_cn citus_worker01 citus_worker02;do
> echo "shared_preload_libraries = 'citus'" >> /data01/pg10/$pgdata/postgresql.conf;
> pg_ctl restart -D /data01/pg10/$pgdata -l /tmp/$pgdata
> done
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started
for pt in 1921 1922 1923;do
psql -U postgres -d postgres -p ${pt} -c "CREATE EXTENSION IF NOT EXISTS citus"
done
[pg10@data01 ~]$ for pt in 1921 1922 1923;do
> psql -U postgres -d postgres -p ${pt} -c "CREATE EXTENSION IF NOT EXISTS citus"
> done
CREATE EXTENSION
CREATE EXTENSION
CREATE EXTENSION
[pg10@data01 ~]$
SELECT master_add_node('10.10.10.101',1922);
SELECT master_add_node('10.10.10.101',1923);
在每一个节点建立 citus扩展
[pg10@data01 ~]$ for pt in 1921 1922 1923;do
> psql -U postgres -d postgres -p ${pt} -c "CREATE EXTENSION IF NOT EXISTS citus"
> done
CREATE EXTENSION
CREATE EXTENSION
CREATE EXTENSION
在cn 节点添加 worker
[pg10@data01 ~]$ psql -U postgres -d postgres -p 1921
psql (10.14)
Type "help" for help.
postgres=# SELECT master_add_node('10.10.10.101',1922);
master_add_node
-----------------
1
(1 row)
postgres=# SELECT master_add_node('10.10.10.101',1923);
master_add_node
-----------------
2
(1 row)
查看添加结果
postgres=# SELECT * FROM master_get_active_worker_nodes();
node_name | node_port
--------------+-----------
10.10.10.101 | 1923
10.10.10.101 | 1922
(2 rows)
查看默认分片数量
postgres=# show citus.shard_count ;
citus.shard_count
-------------------
32
(1 row)
建立分布表
postgres=# CREATE TABLE shiptrack(gid serial primary key,
postgres(# tracktime timestamp without time zone);
CREATE TABLE
指定分布键
postgres=# SELECT * FROM create_distributed_table('shiptrack','gid');
create_distributed_table
--------------------------
(1 row)