假设IP为172.32.148.154的OpenGauss机器连接IP为172.32.148.155的机器,查询155中的表public.emp,155的端口号为31001,数据库名postgres,用户postgres/Postgres123:

在154机器上执行:

CREATE EXTENSION postgres_fdw;

CREATE SERVER foreign_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '172.32.148.155', port '31001', dbname 'postgres');

在155机器上执行:
gs_guc generate -S Postgres123 -D $GAUSSHOME/bin -o usermapping

在154机器上执行:

CREATE USER MAPPING FOR postgres SERVER foreign_server OPTIONS (user 'postgres', password 'Postgres123');

CREATE FOREIGN TABLE femp (
empno int,
ename varchar(10),
job varchar(9),
mgr numeric(4),
hiredate date,
sal numeric(7,2),
comm numeric(7,2),
deptno numeric(2)
)
SERVER foreign_server
OPTIONS (schema_name 'public', table_name 'emp');

然后就可以在154上查询femp,femp和154上的本地表JOIN,令人吃惊的是,可以在154上将femp包含在事务中,其中对femp的未提交修改,并不会写到155上,就好像154的femp表做了缓存一样,只有154上commit时,155上才会修改,好像跨机器实现了事务的并发控制。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐