replication表
各个datanode节点中,表的数据完全相同。也就是说,在插入数据时,系统会分别在每个datanode节点插入相同数据。读数据时,只需要读任意一个datanode节点上的数据即可。建表语法如下:
postgres=# CREATE TABLE log2 (id numeric NOT NULL,stamp timestamp with time zone,user_id numeric) distribute by replication;
postgres=# copy log2 from '/u02/tmp/log.csv' with csv;
replication表数据分布如下图:
不论在哪个数据节点查询,显示的结果都一样的。
为了提高集群可用性,以下配置数据节点的热备以便进行故障转移切换。
4.1 新增Slave数据节点在gtm节点操作,新增两个slave数据节点,如下:
[postgres@pg01 ~]$ pgxc_ctl -c /u01/pgxl/pgxc_ctl/pgxc_ctl.conf /bin/bash Installing pgxc_ctl_bash script as /u01/pgxl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /u01/pgxl/pgxc_ctl_bash. Reading configuration using /u01/pgxl/pgxc_ctl_bash --home /u01/pgxl --configuration /u01/pgxl/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /u01/pgxl PGXC add datanode slave datanode1 pg05 5436 5437 /u01/pgxl/nodes/dnslave /u01/pgxl/nodes/dn_slave_war /u01/pgxl/nodes/dn_archlog PGXC add datanode slave datanode2 pg06 5436 5437 /u01/pgxl/nodes/dnslave /u01/pgxl/nodes/dn_slave_war /u01/pgxl/nodes/dn_archlog 4.2 主备节点切换检查所有服务运行正常:
[postgres@pg01 ~]$ pgxc_ctl -c /u01/pgxl/pgxc_ctl/pgxc_ctl.conf monitor all /bin/bash Installing pgxc_ctl_bash script as /u01/pgxl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /u01/pgxl/pgxc_ctl_bash. Reading configuration using /u01/pgxl/pgxc_ctl_bash --home /u01/pgxl --configuration /u01/pgxl/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /u01/pgxl Running: gtm master Running: gtm slave Running: gtm proxy gtm_pxy1 Running: gtm proxy gtm_pxy2 Running: coordinator master coord1 Running: coordinator slave coord1 Running: coordinator master coord2 Running: coordinator slave coord2 Running: datanode master datanode1 Running: datanode slave datanode1 Running: datanode master datanode2 Running: datanode slave datanode2当前的主节点是pg03和pg04,如下图:
切换主备节点: [postgres@pg01 ~]$ pgxc_ctl -c /u01/pgxl/pgxc_ctl/pgxc_ctl.conf monitor all /bin/bash Installing pgxc_ctl_bash script as /u01/pgxl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /u01/pgxl/pgxc_ctl_bash. Reading configuration using /u01/pgxl/pgxc_ctl_bash --home /u01/pgxl --configuration /u01/pgxl/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /u01/pgxl Running: gtm master Running: gtm slave Running: gtm proxy gtm_pxy1 Running: gtm proxy gtm_pxy2 Running: coordinator master coord1 Running: coordinator slave coord1 Running: coordinator master coord2 Running: coordinator slave coord2 Running: datanode master datanode1 Running: datanode slave datanode1 Running: datanode master datanode2 Running: datanode slave datanode2 [postgres@pg01 ~]$ pgxc_ctl -c /u01/pgxl/pgxc_ctl/pgxc_ctl.conf failover datanode datanode1 datanode2 /bin/bash Installing pgxc_ctl_bash script as /u01/pgxl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /u01/pgxl/pgxc_ctl_bash. Reading configuration using /u01/pgxl/pgxc_ctl_bash --home /u01/pgxl --configuration /u01/pgxl/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /u01/pgxl Failover specified datanodes. Failover the datanode datanode1. Failover datanode datanode1 using GTM itself Actual Command: ssh postgres@pg05 "( pg_ctl promote -Z datanode -D /u01/pgxl/nodes/dnslave ) > /tmp/pg01_STDOUT_4264_0 2>&1" < /dev/null > /dev/null 2>&1 Bring remote stdout: scp postgres@pg05:/tmp/pg01_STDOUT_4264_0 /tmp/STDOUT_4264_1 > /dev/null 2>&1 Actual Command: ssh postgres@pg05 "( pg_ctl restart -w -Z datanode -D /u01/pgxl/nodes/dnslave -o -i; sleep 1 ) > /tmp/pg01_STDOUT_4264_2 2>&1" < /dev/null > /dev/null 2>&1 Bring remote stdout: scp postgres@pg05:/tmp/pg01_STDOUT_4264_2 /tmp/STDOUT_4264_3 > /dev/null 2>&1 2018-11-23 14:52:34.655 CST [26323] LOG: listening on IPv4 address "0.0.0.0", port 5436 2018-11-23 14:52:34.655 CST [26323] LOG: listening on IPv6 address "::", port 5436 2018-11-23 14:52:34.657 CST [26323] LOG: listening on Unix socket "/tmp/.s.PGSQL.5436" 2018-11-23 14:52:34.675 CST [26323] LOG: redirecting log output to logging collector process 2018-11-23 14:52:34.675 CST [26323] HINT: Future log output will appear in directory "pg_log". ALTER NODE pgxc_pool_reload ------------------ t (1 row) EXECUTE DIRECT pgxc_pool_reload ------------------ t (1 row) EXECUTE DIRECT pgxc_pool_reload ------------------ t (1 row) ALTER NODE pgxc_pool_reload ------------------ t (1 row) Failover the datanode datanode2. Failover datanode datanode2 using GTM itself Actual Command: ssh postgres@pg06 "( pg_ctl promote -Z datanode -D /u01/pgxl/nodes/dnslave ) > /tmp/pg01_STDOUT_4264_4 2>&1" < /dev/null > /dev/null 2>&1 Bring remote stdout: scp postgres@pg06:/tmp/pg01_STDOUT_4264_4 /tmp/STDOUT_4264_5 > /dev/null 2>&1 Actual Command: ssh postgres@pg06 "( pg_ctl restart -w -Z datanode -D /u01/pgxl/nodes/dnslave -o -i; sleep 1 ) > /tmp/pg01_STDOUT_4264_6 2>&1" < /dev/null > /dev/null 2>&1 Bring remote stdout: scp postgres@pg06:/tmp/pg01_STDOUT_4264_6 /tmp/STDOUT_4264_7 > /dev/null 2>&1 2018-11-23 14:52:38.607 CST [26317] LOG: listening on IPv4 address "0.0.0.0", port 5436 2018-11-23 14:52:38.607 CST [26317] LOG: listening on IPv6 address "::", port 5436 2018-11-23 14:52:38.609 CST [26317] LOG: listening on Unix socket "/tmp/.s.PGSQL.5436" 2018-11-23 14:52:38.628 CST [26317] LOG: redirecting log output to logging collector process 2018-11-23 14:52:38.628 CST [26317] HINT: Future log output will appear in directory "pg_log". ALTER NODE pgxc_pool_reload ------------------ t (1 row) EXECUTE DIRECT pgxc_pool_reload ------------------ t (1 row) EXECUTE DIRECT pgxc_pool_reload ------------------ t (1 row) ALTER NODE pgxc_pool_reload ------------------ t (1 row) Done. [postgres@pg01 ~]$ pgxc_ctl -c /u01/pgxl/pgxc_ctl/pgxc_ctl.conf monitor all /bin/bash Installing pgxc_ctl_bash script as /u01/pgxl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /u01/pgxl/pgxc_ctl_bash. Reading configuration using /u01/pgxl/pgxc_ctl_bash --home /u01/pgxl --configuration /u01/pgxl/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /u01/pgxl Running: gtm master Running: gtm slave Running: gtm proxy gtm_pxy1 Running: gtm proxy gtm_pxy2 Running: coordinator master coord1 Running: coordinator slave coord1 Running: coordinator master coord2 Running: coordinator slave coord2 Running: datanode master datanode1 Running: datanode master datanode2