分类:
2008-11-26 21:31:49
psql testproxy ..............用psql客户端连接数据库
create schema plproxy; ........生成schema.
vi MyClusterInit.sql,然后把下面的内容保存:(去掉注释)
################# begin ###################
CREATE OR REPLACE FUNCTION plproxy.get_cluster_partitions(cluster_name text)
RETURNS SETOF text AS $$
BEGIN
IF cluster_name = 'MyCluster' THEN
RETURN NEXT 'dbname=test1 host=192.168.1.190';<----节点ip
RETURN NEXT 'dbname=test2 host=192.168.1.193';<----节点ip
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 = 'MyCluster' 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;
################ end #################
create or replace function public.dquery(query text) returns setof record as $$.......................在数据节点上生成一个表usertable,
declare
ret record;
begin
for ret in execute query loop
return next ret;
end loop;
return;
end;
$$ language plpgsql;
create or replace function public.ddlExec(query text) returns integer as $$
declare
ret integer;
begin
execute query;
return 1;
end;
$$ language plpgsql;
create or replace function public.dmlExec(query text) returns integer as $$
declare
ret integer;
begin
execute query;
return 1;
end;
$$ language plpgsql;
############################## end ######################
psql -f 这个文件名 -d database name -h ip地址
4. 然后在proxy上建立相同的函数,用于集群检索.建立,执行方法同上:
CREATE OR REPLACE FUNCTION public.dquery(query text) RETURNS setof
record AS $$
CLUSTER 'MyCluster';
RUN ON ALL;
$$ LANGUAGE plproxy;
CREATE OR REPLACE FUNCTION public.ddlexec(query text) RETURNS setof integer
AS $$
CLUSTER 'MyCluster';
RUN ON ALL;
$$ LANGUAGE plproxy;
CREATE OR REPLACE FUNCTION public.dmlexec(query text) RETURNS setof integer
AS $$
CLUSTER 'MyCluster';
RUN ON ANY;
$$ LANGUAGE plproxy;
########################### done ###########################
5. 测试
方法:在proxy,nodes上建立相同的表
用select,insert,del 在proxy执行.然后每个node上都有响应..
代码: select * from public.ddlexec(
'create table usertable(id primary key,
username varchar(20)'
);