Postgres-XL集群的搭建和测试详解
CentOS6.5 192.168.0.101
CentOS6.5 192.168.0.102
CentOS6.5 192.168.0.103
CentOS6.5 192.168.0.104
cnode1 (gtm)
cnode2 (gtm_proxy,coordinator,datanode)
cnode3 (gtm_proxy,coordinator,datanode)
cnode4 (gtm_proxy,coordinator,datanode)
cnode5 (gtm_proxy,coordinator,datanode)
192.168.0.101 cnode1
192.168.0.102 cnode2
192.168.0.103 cnode3
192.168.0.104 cnode4
# yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl
yum -y install flex
yum -y install bison
yum -y install openjade.x86_64
yum -y install jadetex.noarch
yum -y install docbook*
# tar -zxvf postgres-xl-v9.2-src.tar.gz
# cd postgres-xl
# ./configure --prefix=/usr/local/pgxl-9.2
# make
# make install
# tar -zxvf postgres-xl-v9.2-src.tar.gz
# cd /opt/pgxl/postgres-xl/contrib/pgxc_ctl/
# make
# make install
export PGHOME=/usr/local/pgxl-9.2
export PGUSER=postgres
export LD_LIBRARY_PATH=$PGHOME/lib
export PATH=$PGHOME/bin:$PATH
chown -R postgres:postgres /usr/local/pgxl-9.2
scp authorized_keys postgres@cnode2:/home/postgres/.ssh/
scp authorized_keys postgres@cnode3:/home/postgres/.ssh/
scp authorized_keys postgres@cnode4:/home/postgres/.ssh/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
scp test_scp.txt postgres@cnode2:/home/postgres/
scp test_scp.txt postgres@cnode3:/home/postgres/
scp test_scp.txt postgres@cnode4:/home/postgres/
mkdir -p /data/pg/pg92data
chown -R postgres:postgres /data/pg/pg92data
其次:
#在cnode1节点(gtm)上创建集群的配置文件:pgxc_ctl.conf
#在目录/home/postgres/pgxc_ctl目录中创建pgxc_ctl.conf文件
#每一个节点的配置信息的具体含义参考:http://files.postgres-xl.org/documentation/pgxc_ctl.html
#pgxc_ctl.conf 的内容如下:
===========================
#user and path
pgxcOwner=postgres
pgxcUser=$pgxcOwner
pgxcInstallDir=/usr/pgxl-9.2
#gtm and gtmproxy
gtmMasterDir=/usr/pgxl-9.2/gtm
gtmMasterPort=6666
gtmMasterServer=cnode1
gtmSlave=n
#gtm proxy
gtmProxy=y
gtmProxyDir=/data/pg/pg92data
gtmProxyNames=(gtm_pxy1 gtm_pxy2 gtm_pxy3)
gtmProxyServers=(cnode2 cnode3 cnode4)
gtmProxyPorts=(20001 20001 20001)
gtmProxyDirs=($gtmProxyDir/gtm_pxy1 $gtmProxyDir/gtm_pxy2 $gtmProxyDir/gtm_pxy3)
gtmPxyExtraConfig=none
gtmPxySpecificExtraConfig=(none none none)
#coordinator
coordMasterDir=/data/pg/pg92data
coordNames=(coord1 coord2 coord3)
coordPorts=(5432 5432 5432)
poolerPorts=(20010 20010 20010)
coordPgHbaEntries=(172.17.210.0/32)
coordMasterServers=(cnode2 cnode3 cnode4)
coordMasterDirs=($coordMasterDir/coord1 $coordMasterDir/coord2 $coordMasterDir/coord3)
coordMaxWALsernder=0
coordMaxWALSenders=($coordMaxWALsernder $coordMaxWALsernder $coordMaxWALsernder)
coordSlave=n
coordSpecificExtraConfig=(none none none)
coordSpecificExtraPgHba=(none none none)
#datanode
datanodeNames=(datanode1 datanode2 datanode3)
datanodePorts=(15432 15432 15432)
datanodePoolerPorts=(20012 20012 20012)
datanodePgHbaEntries=(172.17.210.0/32)
datanodeMasterServers=(cnode2 cnode3 cnode4)
datanodeMasterDir=/data/pg/pg92data
datanodeMasterDirs=($datanodeMasterDir/datanode1 $datanodeMasterDir/datanode2 $datanodeMasterDir/datanode3)
datanodeMaxWalSender=0
datanodeMaxWALSenders=($datanodeMaxWalSender $datanodeMaxWalSender $datanodeMaxWalSender)
datanodeSlave=n
primaryDatanode=datanode1
pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf init all
host all all 192.168.0.0/32 trust
host all all 192.168.0.0/32 trust
host all all 0.0.0.0/0 md5
pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf start all
pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf stop all
psql -p 5432 postgres //访问协调节点
psql -p 15432 postgres //访问数据节点
drop node coord1;
drop node coord2;
drop node coord3;
drop node datanode1;
drop node datanode2;
drop node datanode3;
create node coord1 with(TYPE=coordinator,HOST='cnode2',PORT=5432);
create node coord2 with(TYPE=coordinator,HOST='cnode3',PORT=5432);
create node coord3 with(TYPE=coordinator,HOST='cnode4',PORT=5432);
create node datanode1 with(TYPE=datanode,HOST='cnode2',PORT=15432,primary=true);
create node datanode2 with(TYPE=datanode,HOST='cnode3',PORT=15432,primary=false);
create node datanode3 with(TYPE=datanode,HOST='cnode4',PORT=15432,primary=false);
alter node coord1 with(TYPE=coordinator,HOST='cnode2',PORT=5432);
alter node coord2 with(TYPE=coordinator,HOST='cnode3',PORT=5432);
alter node coord3 with(TYPE=coordinator,HOST='cnode4',PORT=5432);
alter node datanode1 with(TYPE=datanode,HOST='cnode2',PORT=15432,primary=true);
alter node datanode2 with(TYPE=datanode,HOST='cnode3',PORT=15432,primary=false);
alter node datanode3 with(TYPE=datanode,HOST='cnode4',PORT=15432,primary=false);
select pgxc_pool_reload();
select * from pgxc_node;
ALTER USER postgres WITH PASSWORD '12345678';
create role postgres nosuperuser login encrypted password 'belle';
create tablespace tbs_book_shopping owner postgres location '/data/pg/pg92data/tbs/tbs_bs';
create database book_shopping with owner postgres template template0 encoding 'UTF8' tablespace tbs_book_shopping;
\c book_shopping postgres
psql -h cnode2 -p 5432 postgres postgres -c "create node group gp1 with(datanode2,datanode3)";
psql -h cnode3 -p 5432 postgres postgres -c "create node group gp1 with(datanode2,datanode3)";
psql -h cnode4 -p 5432 postgres postgres -c "create node group gp1 with(datanode2,datanode3)";
psql -h cnode2 -p 15432 postgres postgres -c "create node group gp1 with(datanode2,datanode3)";
psql -h cnode3 -p 15432 postgres postgres -c "create node group gp1 with(datanode2,datanode3)";
psql -h cnode4 -p 15432 postgres postgres -c "create node group gp1 with(datanode2,datanode3)";
postgres=# create table t1(id serial8 primary key, info text, crt_time timestamp) distribute by hash(id) to group gp1;
NOTICE: CREATE TABLE will create implicit sequence "t1_id_seq" for serial column "t1.id"
ERROR: PGXC Group gp1: group not defined
psql -h cnode2 -p 5432 postgres postgres -c "select pgxc_pool_reload();";
psql -h cnode3 -p 5432 postgres postgres -c "select pgxc_pool_reload();";
psql -h cnode4 -p 5432 postgres postgres -c "select pgxc_pool_reload();";
psql -h cnode2 -p 5432 postgres postgres -c "select * from pgxc_group;";
psql -h cnode3 -p 5432 postgres postgres -c "select * from pgxc_group;";
psql -h cnode4 -p 5432 postgres postgres -c "select * from pgxc_group;";
psql -h cnode2 -p 15432 postgres postgres -c "select * from pgxc_group;";
psql -h cnode3 -p 15432 postgres postgres -c "select * from pgxc_group;";
psql -h cnode4 -p 15432 postgres postgres -c "select * from pgxc_group;";
psql -h cnode2 -p 5432 postgres postgres -c "delete from pgxc_group where group_name = 'gp1';";
psql -h cnode3 -p 5432 postgres postgres -c "delete from pgxc_group where group_name = 'gp1';";
psql -h cnode4 -p 5432 postgres postgres -c "delete from pgxc_group where group_name = 'gp1';";
psql -p 5432 postgres
\c book_shopping postgres
create table t1(id serial8 primary key, info text, crt_time timestamp) distribute by hash(id) to group gp1;
insert into t1 (info, crt_time) select md5(random()::text), clock_timestamp() from generate_series(1,1000);
select count(*) from t1;
select * from t1 limit 5;
id | info | crt_time
----+----------------------------------+----------------------------
1 | 2a85488b8f67cc249fb004345e3d387e | 2015-06-04 10:59:01.600481
2 | cbc0b99298a92fb883b1df90ec9d2555 | 2015-06-04 10:59:01.60165
5 | f254966bd9229619e9061fd2e7ea9bac | 2015-06-04 10:59:01.603552
6 | b3b5db57115da5abb5c25b8084329804 | 2015-06-04 10:59:01.604219
8 | 4ded54b3d846c5bfd608dbe95f39d2a7 | 2015-06-04 10:59:01.605467
在datanode1中可以知道,id为1的数据在cnode2种:
book_shopping=# select * from t1 where id=1;
id | info | crt_time
----+----------------------------------+----------------------------
1 | 2a85488b8f67cc249fb004345e3d387e | 2015-06-04 10:59:01.600481
(1 row)
update t1 set info='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' where id = 1;
book_shopping=# update t1 set info='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' where id = 1;
UPDATE 1
book_shopping=# select * from t1 where id=1;
id | info | crt_time
----+----------------------------------+----------------------------
1 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 2015-06-04 10:59:01.600481
(1 row)
book_shopping=# select * from t1;
id | info | crt_time
------+----------------------------------+----------------------------
2 | cbc0b99298a92fb883b1df90ec9d2555 | 2015-06-04 10:59:01.60165
5 | f254966bd9229619e9061fd2e7ea9bac | 2015-06-04 10:59:01.603552
6 | b3b5db57115da5abb5c25b8084329804 | 2015-06-04 10:59:01.604219
8 | 4ded54b3d846c5bfd608dbe95f39d2a7 | 2015-06-04 10:59:01.605467
9 | 9e3035492918569a707fdae0c83ad9fd | 2015-06-04 10:59:01.606088
12 | 9836e9da92da44eb1d51ad8e8d9cec67 | 2015-06-04 10:59:01.607926
13 | 94b6fbea895cd530d6e109613270f2a5 | 2015-06-04 10:59:01.608486
15 | 4a35e75632b17c67f8233d60c3b13cb1 | 2015-06-04 10:59:01.609578
17 | cf9b88c7e50a2a984cabfd131b308c19 | 2015-06-04 10:59:01.610672
19 | 7dafeea2571910171af39e5d7f2eb7d5 | 2015-06-04 10:59:01.611785
21 | 2e5dd218b9521fcb5f0d653f09740fb7 | 2015-06-04 10:59:01.612912
23 | e91fd409c1a1f80e701e0a962243484e | 2015-06-04 10:59:01.614065
26 | 49a68c54f8c7e76a9770793706dc9796 | 2015-06-04 10:59:01.615807
28 | 79699ca1b507c700548560e477d9bf0c | 2015-06-04 10:59:01.616982
40 | 0eb75ff919396521584f2b833253b834 | 2015-06-04 10:59:01.623801
41 | 0302e9fbb3e7bdc714c1584191edb136 | 2015-06-04 10:59:01.624409
42 | 97692d97b5c9c11ca6108181f3573334 | 2015-06-04 10:59:01.624949
44 | 90eddaf542e0485a96fa73fc78b059dc | 2015-06-04 10:59:01.626069
46 | f2cafc526979aa2e86a434e5de9929ed | 2015-06-04 10:59:01.627223
49 | 137e1311d07f117a2249b961eabe4001 | 2015-06-04 10:59:01.628939
50 | 9323ec806c550b537e202fd5e61d8a24 | 2015-06-04 10:59:01.629536
52 | dc41ba4e2046ae348b2ce01033b46efe | 2015-06-04 10:59:01.630633
56 | 27ba4928937806bae5cf6c0359ab9a03 | 2015-06-04 10:59:01.632916
57 | f50ede190d3383c060fe3829c7accb79 | 2015-06-04 10:59:01.6335
62 | 16d2634d35b11d5dcd020083a12ee6eb | 2015-06-04 10:59:01.636402
64 | 840c97d994cd9ea6fee86c6b1b5e43a1 | 2015-06-04 10:59:01.637562
66 | aa82ea0624c44a8838e2fb7a3cb24a90 | 2015-06-04 10:59:01.638696
67 | 40535733b65ab6a5023c7d7d142c435e | 2015-06-04 10:59:01.639286
68 | c9496a076e2fbcca2a781dc665007219 | 2015-06-04 10:59:01.639821
70 | 2f0f80cf8d2f6d06d591bf68fac1c253 | 2015-06-04 10:59:01.640937
74 | 95b9eee52187d3131cd7e125eba809e1 | 2015-06-04 10:59:01.643187
76 | 32e43e6ae96bf320775beccb61b7a48f | 2015-06-04 10:59:01.644315
77 | 2d327bcc9cbbb4e155d1e8671ed71f71 | 2015-06-04 10:59:01.644962
80 | 896a5f50e47b01bab9942ca099e2aa67 | 2015-06-04 10:59:01.646691
81 | b3d5808db7235d5927055838d4666492 | 2015-06-04 10:59:01.647239
83 | fc7a8ddffdb5d4d91f8f9c0238e3a577 | 2015-06-04 10:59:01.648333
84 | c1c4ddbadeaf595a94dd1120aeb1479e | 2015-06-04 10:59:01.648877
85 | d617f8bac6d368fa46f1c00d55834349 | 2015-06-04 10:59:01.649451
87 | 11f591e7526e7145df6737090ba32556 | 2015-06-04 10:59:01.650531
90 | e0287526bdf0c9f277db3d9a94489e68 | 2015-06-04 10:59:01.652254
99 | 2197bf1dc04d3d327ec1b1440dac8249 | 2015-06-04 10:59:01.657322
101 | 5319875171df0c35e5e4111c0a8dbea4 | 2015-06-04 10:59:01.658466
more
select * from t1 where info='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
book_shopping=# select * from t1 where info='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
id | info | crt_time
----+----------------------------------+----------------------------
1 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 2015-06-04 10:59:01.600481
(1 row)
delete from t1 where info = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
book_shopping=# delete from t1 where info = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
ERROR: cannot execute DELETE in a read-only transaction
book_shopping=# delete from t1 where info = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
DELETE 1
book_shopping=# select count(1) from t1;
count
-------
999
(1 row)
create table t2(id serial8 primary key, info text, crt_time timestamp) DISTRIBUTE BY REPLICATION;
insert into t2 (info, crt_time) select md5(random()::text), clock_timestamp() from generate_series(1,200);
select count(*) from t2;
select * from t2 limit 10;
book_shopping=# select * from t2 limit 10;
id | info | crt_time
----+----------------------------------+----------------------------
1 | 6251581ce28a39e92d322a00880772df | 2015-06-04 11:33:37.100742
2 | 35b39c91f678ce7acb406a62ab2a15af | 2015-06-04 11:33:37.101695
3 | ad7657b5ddb5cc2273f925432c8fee40 | 2015-06-04 11:33:37.102295
4 | f7f0b7bf1ae3d02b34f61e3b500dfe70 | 2015-06-04 11:33:37.102902
5 | b661a1208585e01c8abcd7bc699c3ac4 | 2015-06-04 11:33:37.103556
6 | 3b3434e38f5916fd86a14cef94060885 | 2015-06-04 11:33:37.104154
7 | 8b2be24600a401b3d1770134243bc3b7 | 2015-06-04 11:33:37.104757
8 | 597cc7d88f19dc58bf0be793d12514b7 | 2015-06-04 11:33:37.105326
9 | 4b73d76881b3b33719165797b9c34534 | 2015-06-04 11:33:37.105898
10 | d1b1b5ae9d22cfd132b8811bf256be94 | 2015-06-04 11:33:37.10648
(10 rows)
update t2 set info='bbbbbbbbbbbbbbbbbbbbbbb' where id = 1;
select * from t2 where id = 1;
book_shopping=# select * from t2 where id = 1;
id | info | crt_time
----+-------------------------+----------------------------
1 | bbbbbbbbbbbbbbbbbbbbbbb | 2015-06-04 11:33:37.100742
(1 row)
delete from t2 where id = 1;
book_shopping=# delete from t2 where id = 1;
DELETE 1
book_shopping=# select * from t2 where id = 1;
id | info | crt_time
----+------+----------
(0 rows)
create or replace function selectinto(id int) returns varchar as
$BODY$
declare
sql varchar;
str varchar;
re record;
begin
sql = 'select info from t1 where id=' || id;
for re in execute sql loop
str = str || re.info;
end loop;
return str;
end
$BODY$
language plpgsql;