PL/Proxy和PostgreSQL集群的结构关系可以用下图清楚地表示
下面是详细的安装方法:
以下操作是在三台不同机器上执行的情况,其中plproxy节点的机器名是P1,数据库节点的机器名分别是D1和D2。机器硬件配置如下,同时需要Linux-4.2、postgresql-8.3.0和plproxy-2.0.4,pgbouncer的安装过程略去。
plproxy节点: hostname: P1 inet addr:10.0.0.1 OS: Linux 2.6.9-42.ELsmp CPU:Intel(R) Xeon(R) CPU L5320 @ 1.86GHz MemTotal: 514440 kB
node1节点: hostname:D1 inet addr:10.0.0.2 OS: Linux 2.6.9-42.ELsmp CPU:Intel(R) Xeon(R) CPU L5320 @ 1.86GHz MemTotal: 254772 kB
node2节点: hostname:D2 inet addr:10.0.0.3 OS: Linux 2.6.9-42.ELsmp CPU:Intel(R) Xeon(R) CPU L5320 @ 1.86GHz MemTotal: 254772 kB
1. 在P1, D1,D2上安装postgresql-8.3.0,并创建URTCluster数据库
gunzip postgresql-8.3.0.tar.gz tar xf postgresql-8.3.0.tar cd postgresql-8.3.0 ./configure –prefix=/home/y/pgsql –with-perl gmake gmake check sudo gmake install
sudo adduser postgres sudo mkdir /home/y/pgsql/data sudo chown postgres /home/y/pgsql/data
sudo -u postgres /home/y/pgsql/bin/initdb -D /home/y/pgsql/data sudo -u postgres /home/y/pgsql/bin/postgres -D /home/y/pgsql/data >logfile 2>&1 &
sudo -u postgres /home/y/pgsql/bin/createdb URTCluster
sudo -u postgres /home/y/pgsql/bin/psql -d URTCluster
sudo vim /home/y/pgsql/data/postgresql.conf listen_addresses = ‘*’ port = 5432
sudo vim /home/y/pgsql/data/pg_hba.conf host URTCluster postgres 10.0.0.0/16 trust
sudo -u postgres /home/y/pgsql/bin/pg_ctl -D /home/y/pgsql/data stop sudo -u postgres /home/y/pgsql/bin/postgres -D /home/y/pgsql/data >logfile 2>&1 & sudo -u postgres /home/y/pgsql/bin/pg_ctl -D /home/y/pgsql/data reload
2. 在P1上安装plproxy-2.0.4
echo $PATH
gunzip plproxy-2.0.4.tar.gz tar xf plproxy-2.0.4.tar cd plproxy-2.0.4 gmake sudo gmake install
sudo -u postgres /home/y/pgsql/bin/psql -f /home/y/pgsql/share/contrib/plproxy.sql URTCluster
3. 在P1, D1,D2上安装plpgsql
sudo -u postgres /home/y/pgsql/bin/createlang plpgsql URTCluster
4. 在P1上创建schema
sudo -u postgres /home/y/pgsql/bin/psql -d URTCluster URTCluster=# create schema plproxy;
5. 在P1上初始化设置
CREATE OR REPLACE FUNCTION plproxy.get_cluster_partitions(cluster_name text) RETURNS SETOF text AS $$ BEGIN IF cluster_name =’URTCluster’ THEN RETURN NEXT ‘dbname=URTCluster host=10.0.0.2′; RETURN NEXT ‘dbname=URTCluster host=10.0.0.3′; RETURN; END IF; RAISE EXCEPTION ‘Unknown cluster’; END; $$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION plproxy.get_cluster_version(cluster_name text) RETURNS int4 AS $$ BEGIN IF cluster_name = ‘URTCluster’ THEN RETURN 1; END IF; RAISE EXCEPTION ‘Unknown cluster’; END; $$ LANGUAGE plpgsql;
create or replace function plproxy.get_cluster_config(cluster_name text, out key text, out val text) returns setof record as $$ begin key := ’statement_timeout’; val := 60; return next; return; end; $$ language plpgsql;
sudo -u postgres /home/y/pgsql/bin/psql -f URTClusterInit.sql -d URTCluster -h 10.0.0.1
6. 在D1,D2节点上设置
CREATE TABLE users ( username text, email text );
CREATE OR REPLACE FUNCTION insert_user(i_username text, i_emailaddress text) RETURNS integer AS $$ INSERT INTO users (username, email) VALUES ($1,$2); SELECT 1; $$ LANGUAGE SQL;
sudo -u postgres /home/y/pgsql/bin/psql -f URTClusterNodesInit_1.sql -h 10.0.0.2 -d URTCluster sudo -u postgres /home/y/pgsql/bin/psql -f URTClusterNodesInit_1.sql -h 10.0.0.3 -d URTCluster
7. 在P1节点上设置
CREATE OR REPLACE FUNCTION insert_user(i_username text, i_emailaddress text) RETURNS integer AS $$ CLUSTER ‘URTCluster’; RUN ON hashtext(i_username); $$ LANGUAGE plproxy;
CREATE OR REPLACE FUNCTION get_user_email(i_username text) RETURNS text AS $$ CLUSTER ‘URTCluster’; RUN ON hashtext(i_username) ; SELECT email FROM users WHERE username = i_username; $$ LANGUAGE plproxy;
sudo -u postgres /home/y/pgsql/bin/psql -f URTClusterProxyExec_1.sql -h 10.0.0.1 -d URTCluster
8. 在P1上测试结果
SELECT insert_user(’Sven’,’sven@somewhere.com’);
SELECT insert_user(’Marko’, ‘marko@somewhere.com’);
SELECT insert_user(’Steve’,’steve@somewhere.cm’);
SELECT get_user_email(’Sven’); SELECT get_user_email(’Marko’); SELECT get_user_email(’Steve’);