Postgres2015全国用户大会将于11月20至21日在北京丽亭华苑酒店召开。本次大会嘉宾阵容强大,国内顶级PostgreSQL数据库专家将悉数到场,并特邀欧洲、俄罗斯、日本、美国等国家和地区的数据库方面专家助阵:
|
PostgreSQL 9.3.1plproxy 2.x
plrpoxy库 :hostaddr 172.16.3.150port 1921user proxypassword proxydbname proxyschema digoal // 这个schema名和数据节点一致, 可以省去写target的步骤.
数据节点 :hostaddr 172.16.3.150port 1921user digoal // plproxy将使用digoal用户连接数据节点.password digoal
dbname db0schema digoaldbname db1schema digoaldbname db2schema digoaldbname db3schema digoal
tar -zxvf plproxy-d703683.tar.gzmv plproxy-d703683 /opt/soft_bak/postgresql-9.3.1/contribcd /opt/soft_bak/postgresql-9.3.1/contrib/plproxy-d703683[root@db-172-16-3-150 plproxy-d703683]# export PATH=/home/pg93/pgsql9.3.1/bin:$PATH[root@db-172-16-3-150 plproxy-d703683]# which pg_config[root@db-172-16-3-150 plproxy-d703683]# gmake clean[root@db-172-16-3-150 plproxy-d703683]# gmake[root@db-172-16-3-150 plproxy-d703683]# gmake install
pg93@db-172-16-3-150-> psqlpsql (9.3.1)Type "help" for help.postgres=# create role proxy nosuperuser login encrypted password 'proxy';CREATE ROLEdigoal=# create database proxy;CREATE DATABASEdigoal=# \c proxyYou are now connected to database "proxy" as user "postgres".proxy=# create extension plproxy;CREATE EXTENSION
proxy=# grant all on database proxy to proxy;GRANTproxy=# \c proxy proxyYou are now connected to database "proxy" as user "digoal".
proxy=> create schema digoal;CREATE SCHEMA
proxy=> \c postgres postgresYou are now connected to database "postgres" as user "postgres".postgres=# create role digoal nosuperuser login encrypted password 'digoal';postgres=# create database db0;postgres=# create database db1;postgres=# create database db2;postgres=# create database db3;
postgres=# grant all on database db0 to digoal;postgres=# grant all on database db1 to digoal;postgres=# grant all on database db2 to digoal;postgres=# grant all on database db3 to digoal;
proxy=> \c proxy postgresYou are now connected to database "proxy" as user "postgres".proxy=#CREATE SERVER cluster_srv1 FOREIGN DATA WRAPPER plproxy options(connection_lifetime '1800',p0 'dbname=db0 hostaddr=172.16.3.150 port=1921 application_name=test',p1 'dbname=db1 hostaddr=172.16.3.150 port=1921',p2 'dbname=db2 hostaddr=172.16.3.150 port=1921',p3 'dbname=db3 hostaddr=172.16.3.150 port=1921');
proxy=# grant usage on FOREIGN server cluster_srv1 to proxy;GRANT
proxy=# create user mapping for proxy server cluster_srv1 options (user 'digoal');CREATE USER MAPPING
vi $PGDATA/pg_hba.confhost db0 digoal 172.16.3.150/32 trusthost db1 digoal 172.16.3.150/32 trusthost db2 digoal 172.16.3.150/32 trusthost db3 digoal 172.16.3.150/32 trustpg_ctl reload
proxy=# CREATE OR REPLACE FUNCTION digoal.dy(sql text)RETURNS SETOF recordLANGUAGE plproxySTRICTAS $function$cluster 'cluster_srv1';run on all;$function$;proxy=# grant execute on function digoal.dy(text) to proxy;GRANT
proxy=# \c db0 digoaldb0=#CREATE OR REPLACE FUNCTION digoal.dy(sql text)RETURNS SETOF recordLANGUAGE plpgsqlSTRICTAS $function$declarerec record;beginfor rec in execute sql loopreturn next rec;end loop;return;end;$function$;db0=# \c db1 digoal...db1=# \c db2 digoal...db2=# \c db3 digoal...
proxy=> select * from digoal.dy('select count(*) from pg_class') as t(i int8);i-----293293293293(4 rows)proxy=> select sum(i) from digoal.dy('select count(*) from pg_class') as t(i int8);sum------1172(1 row)
postgres=# select * from pg_stat_activity where usename='digoal';datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | waiting | state | query-------+---------+------+----------+---------+------------------+--------------+-----------------+-------------+-------------------------------+------------+-------------------------------+-------------------------------+---------+-------+-----------------------------------------------------91246 | db0 | 8171 | 91250 | digoal | test | 172.16.3.150 | | 47937 | 2013-11-22 17:23:26.138425+08 | | 2013-11-22 17:27:05.539286+08 | 2013-11-22 17:27:05.539745+08 | f | idle | select i::int8 from digoal.dy($1::text) as (i int8)91247 | db1 | 8172 | 91250 | digoal | | 172.16.3.150 | | 47938 | 2013-11-22 17:23:26.138688+08 | | 2013-11-22 17:27:05.53938+08 | 2013-11-22 17:27:05.539874+08 | f | idle | select i::int8 from digoal.dy($1::text) as (i int8)91248 | db2 | 8173 | 91250 | digoal | | 172.16.3.150 | | 47939 | 2013-11-22 17:23:26.138957+08 | | 2013-11-22 17:27:05.53938+08 | 2013-11-22 17:27:05.539841+08 | f | idle | select i::int8 from digoal.dy($1::text) as (i int8)91249 | db3 | 8174 | 91250 | digoal | | 172.16.3.150 | | 47940 | 2013-11-22 17:23:26.139178+08 | | 2013-11-22 17:27:05.539366+08 | 2013-11-22 17:27:05.539793+08 | f | idle | select i::int8 from digoal.dy($1::text) as (i int8)(4 rows)
postgres=# \c proxy postgresYou are now connected to database "proxy" as user "postgres".proxy=# alter server cluster_srv1 options (set p1 'dbname=db1 hostaddr=172.16.3.150 port=1921 application_name=abc');ALTER SERVER
proxy=> select sum(i) from digoal.dy('select count(*) from pg_class') as t(i int8);sum------1172(1 row)
proxy=# select * from pg_stat_activity where usename='digoal';datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | waiting | state | query-------+---------+------+----------+---------+------------------+--------------+-----------------+-------------+-------------------------------+------------+-------------------------------+-------------------------------+---------+-------+-----------------------------------------------------91246 | db0 | 8245 | 91250 | digoal | test | 172.16.3.150 | | 47941 | 2013-11-22 17:30:36.933077+08 | | 2013-11-22 17:30:36.936784+08 | 2013-11-22 17:30:36.938837+08 | f | idle | select i::int8 from digoal.dy($1::text) as (i int8)91248 | db2 | 8247 | 91250 | digoal | | 172.16.3.150 | | 47943 | 2013-11-22 17:30:36.933502+08 | | 2013-11-22 17:30:36.936783+08 | 2013-11-22 17:30:36.938981+08 | f | idle | select i::int8 from digoal.dy($1::text) as (i int8)91249 | db3 | 8248 | 91250 | digoal | | 172.16.3.150 | | 47944 | 2013-11-22 17:30:36.933731+08 | | 2013-11-22 17:30:36.937147+08 | 2013-11-22 17:30:36.939015+08 | f | idle | select i::int8 from digoal.dy($1::text) as (i int8)91247 | db1 | 8246 | 91250 | digoal | abc | 172.16.3.150 | | 47942 | 2013-11-22 17:30:36.933288+08 | | 2013-11-22 17:30:36.93757+08 | 2013-11-22 17:30:36.939299+08 | f | idle | select i::int8 from digoal.dy($1::text) as (i int8)(4 rows)
proxy=# \c db0 digoaldb0=> create table t(id int);CREATE TABLEdb0=> \c db1You are now connected to database "db1" as user "digoal".db1=> create table t(id int);CREATE TABLEdb1=> \c db2You are now connected to database "db2" as user "digoal".db2=> create table t(id int);CREATE TABLEdb2=> \c db3You are now connected to database "db3" as user "digoal".db3=> create table t(id int);CREATE TABLE
\c db0 digoaldb0=> create or replace function digoal.f_test4() returns int as $$declarebegininsert into t(id) values (1);return 0;end;$$ language plpgsql strict;db1=> create or replace function digoal.f_test4() returns int as $$declarebegininsert into t(id) values (1);return 1;end;$$ language plpgsql strict;db2=> create or replace function digoal.f_test4() returns int as $$declarebegininsert into t(id) values (1);return 2;end;$$ language plpgsql strict;db3=> create or replace function digoal.f_test4() returns int as $$declarebegininsert into t(id) values (1);return 3;end;$$ language plpgsql strict;
proxy=> \c proxy postgrescreate or replace function digoal.f_test4() returns int as $$cluster 'cluster_srv1';run on 0; -- 在指定的数据节点上运行, 本例可以设置为0到3, 顺序和创建的server中的配置顺序一致. p0, p1, p2, p3$$ language plproxy strict;proxy=# grant execute on function digoal.f_test4() to proxy;GRANTproxy=# \c proxy proxyYou are now connected to database "proxy" as user "proxy".proxy=> select * from digoal.f_test4();f_test4---------0(1 row)
proxy=# create or replace function digoal.f_test4() returns int as $$cluster 'cluster_srv1';run on 4;$$ language plproxy strict;CREATE FUNCTIONproxy=# \c proxy proxyYou are now connected to database "proxy" as user "proxy".proxy=> select * from digoal.f_test4();ERROR: PL/Proxy function digoal.f_test4(0): part number out of range
proxy=> \c proxy postgresYou are now connected to database "proxy" as user "postgres".proxy=# create or replace function digoal.f_test4() returns int as $$cluster 'cluster_srv1';run on any;$$ language plproxy strict;CREATE FUNCTIONproxy=# \c proxy proxyYou are now connected to database "proxy" as user "proxy".proxy=> select * from digoal.f_test4();f_test4---------0(1 row)
proxy=> select * from digoal.f_test4();f_test4---------3(1 row)
proxy=> select * from digoal.f_test4();f_test4---------2(1 row)
proxy=> select * from digoal.f_test4();f_test4---------3(1 row)
run on function() 则使用比特计算得到运行节点.proxy=> create or replace function digoal.f(int) returns int as $$select $1;$$ language sql strict;CREATE FUNCTIONproxy=> \c proxy postgresYou are now connected to database "proxy" as user "postgres".proxy=# create or replace function digoal.f_test4() returns int as $$cluster 'cluster_srv1';run on digoal.f(10);$$ language plproxy strict;CREATE FUNCTIONproxy=> select digoal.f_test4();f_test4---------2(1 row)proxy=> \c proxy postgresYou are now connected to database "proxy" as user "postgres".proxy=# create or replace function digoal.f_test4() returns int as $$cluster 'cluster_srv1';run on digoal.f(11);$$ language plproxy strict;CREATE FUNCTIONproxy=# \c proxy proxyYou are now connected to database "proxy" as user "proxy".proxy=> select digoal.f_test4();f_test4---------3(1 row)proxy=> \c proxy postgresYou are now connected to database "proxy" as user "postgres".proxy=# create or replace function digoal.f_test4() returns int as $$cluster 'cluster_srv1';run on digoal.f(-11);$$ language plproxy strict;CREATE FUNCTIONproxy=# \c proxy proxyYou are now connected to database "proxy" as user "proxy".proxy=> select digoal.f_test4();f_test4---------1(1 row)
proxy=> \c proxy postgresYou are now connected to database "proxy" as user "postgres".proxy=# create or replace function digoal.f_test4() returns int as $$cluster 'cluster_srv1';run on all;$$ language plproxy strict;ERROR: PL/Proxy function digoal.f_test4(0): RUN ON ALL requires set-returning functionproxy=# drop function digoal.f_test4();DROP FUNCTIONproxy=# create or replace function digoal.f_test4() returns setof int as $$cluster 'cluster_srv1';run on all;$$ language plproxy strict;CREATE FUNCTIONproxy=# grant execute on function digoal.f_test4() to proxy;GRANTproxy=# \c proxy proxyYou are now connected to database "proxy" as user "proxy".proxy=> select digoal.f_test4();f_test4---------0123(4 rows)