CREATEORREPLACEFUNCTION func_ddl(sqltext) RETURNS SETOF textAS $$ CLUSTER 'proxy_srv'; RUN ON ALL; $$ LANGUAGE plproxy;
DML 函数
1 2 3 4 5
CREATEORREPLACEFUNCTION func_dml(sqltext) RETURNS SETOF textAS $$ CLUSTER 'proxy_srv'; RUN ON ANY; $$ LANGUAGE plproxy;
query 函数
1 2 3 4 5
CREATEORREPLACEFUNCTION func_query(sqltext) RETURNS SETOF RECORDAS $$ CLUSTER 'proxy_srv'; RUN ON ALL; $$ LANGUAGE plproxy;
二 db0,db1 数据节点执行
DDL 函数
1 2 3 4 5 6 7
CREATEORREPLACEFUNCTION func_ddl(sqltext) RETURNSintegerAS $$ begin executesql; return 1; end; $$ LANGUAGE plpgsql;
DML 函数
1 2 3 4 5 6 7
CREATEORREPLACEFUNCTION func_dml(sqltext) RETURNSintegerAS $$ begin executesql; return 1; end; $$ LANGUAGE plpgsql;
query 函数
1 2 3 4 5 6 7 8 9 10 11 12
CREATEORREPLACEFUNCTION func_query(sqltext) RETURNS SETOF RECORDAS $$ DECLARE rec RECORD; BEGIN FOR rec INEXECUTEsql LOOP RETURNNEXT rec; ENDLOOP; RETURN; END; $$ LANGUAGE plpgsql;
三 测试
DDL 函数测试 ,proxy 结点执行
1 2 3 4 5 6 7 8
proxy=> select func_ddl('create table tbl_proxy1(id int4,name text,create_time timestamp(0) without time zone default clock_timestamp())'); NOTICE: PL/Proxy: dropping stale conn NOTICE: PL/Proxy: dropping stale conn func_ddl ---------- 1 1 (2 rows
db0,db1 验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
db0=> /dt tbl_proxy1 List of relations Schema | Name | Type | Owner --------+------------+-------+------- public | tbl_proxy1 | table | proxy (1 row) db0=> /c db1 You are now connected to database "db1" as user "proxy". db1=> /dt tbl_proxy1 List of relations Schema | Name | Type | Owner --------+------------+-------+------- public | tbl_proxy1 | table | proxy (1 row)
db1=> select * from tbl_proxy1 ; id | name | create_time ----+------+--------------------- 1 | a | 2014-09-18 18:45:08 2 | b | 2014-09-18 18:45:55 (2 rows) db1=> /c db0 You are now connected to database "db0" as user "proxy". db0=> select * from tbl_proxy1 ; id | name | create_time ----+------+--------------------- 3 | c | 2014-09-18 18:45:58 (1 row)
query 测试
1 2 3 4 5 6 7
proxy=> SELECT * FROM func_query('SELECT * FROM tbl_proxy1;') AS (id integer, name text,create_time timestamp(0) without time zone ); id | name | create_time ----+------+--------------------- 2 | b | 2014-09-1818:55:13 3 | c | 2014-09-1818:55:17 1 | a | 2014-09-1818:55:07 (3 rows)
[pg93@db1 script]$ cat insert.sh #!/bin/bash for ((i=1;i<=100000;i++)); do echo"select func_dml('insert into tbl_proxy1(id,name) values(${i},''${i}_test'')');"; done
db1=> select count(*) from tbl_proxy1; count ------- 50020 (1 row) db1=> /c db0 You are now connected to database "db0" as user "proxy". db0=> select count(*) from tbl_proxy1; count ------- 49980 (1 row)
proxy=> select func_ddl('create unique index uk_proxy_id on tbl_proxy1 using btree (id);'); func_ddl ---------- 1 1 (2 rows) db0=> /d tbl_proxy1 Table"public.tbl_proxy1" Column | Type | Modifiers -------------+--------------------------------+--------------------------- id | integer | name | text | create_time | timestamp(0) without time zone | default clock_timestamp() Indexes: "uk_proxy_id" UNIQUE, btree (id) db0=> /c db1 You are now connected to database "db1"as user "proxy".
db1=> /d tbl_proxy1 Table"public.tbl_proxy1" Column | Type | Modifiers -------------+--------------------------------+--------------------------- id | integer | name | text | create_time | timestamp(0) without time zone | default clock_timestamp() Indexes: "uk_proxy_id" UNIQUE, btree (id)
备注: 索引已在数据节点创建。
proxy 节点上查询
1 2 3 4 5 6
proxy=> explain analyze SELECT * FROM func_query('SELECT id,name FROM tbl_proxy1 where id=1;') AS (id integer, name text); QUERY PLAN -------------------------------------------------------------------------------------------------------------- Function Scan on func_query (cost=0.25..10.25 rows=1000width=36) (actual time=1.803..1.805 rows=1 loops=1) Total runtime: 1.847 ms (2 rows)
数据节点查询
1 2 3 4 5 6 7
db1=> explain analyze SELECT id,name FROM tbl_proxy1 where id=1; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------- Index Scan using uk_proxy_id on tbl_proxy1 (cost=0.29..4.31 rows=1 width=14) (actual time=0.032..0.034 rows=1 loops=1) Index Cond: (id = 1) Total runtime: 0.086 ms (3 rows)