万梅资源网 Design By www.ubjie.com

通过postgres_fdw 扩展,访问远程数据库表

一、环境准备

虚拟机(node107):centos7、PostgreSQL10

远程服务器(百度云服务BBC): centos7、PostgreSQL10

在本地虚拟机上访问远程服务器的数据表。

二、配置连接

(1)创建扩展: 在本地107这个节点上创建扩展。

[root@107 ~]# su postgre
su: user postgre does not exist
[root@107 ~]# su postgres
bash-4.2$ psql mydb postgres
could not change directory to "/root": 权限不够
psql (10.7)
Type "help" for help.

mydb=# CREATE EXTENSION postgres_fdw;
CREATE EXTENSION

如果是普通用户使用 ·postgres_fdw 需要单独授权

grant usage on foreign data wrapper postgres_fdw to 用户名

(2) 创建 foreign server 外部服务器,外部服务是指连接外部数据源的连接信息

mydb=# create server fs_postgres_bbc 
foreign data wrapper postgres_fdw options(host '182.61.136.109',port '5432',dbname 'technology');
mydb=#

定义名称为 fs_postgres_bbc的外部服务,options 设置远程PostgreSQL数据源连接选项,通常设置主机名、端口、数据库名称。

(3)需要给外部服务创建映射用户

mydb=# create user mapping for postgres server 
fs_postgres_bbc options(user 'postgres',password 'password');
CREATE USER MAPPING
mydb=#

for 后面接的是 node107 的数据库用户,options 里接的是远程PostgreSQL数据库的用户和密码。password 注意修改成自己的

其实想访问远程数据库,无非就是知道连接信息。包括host、port、dbname、user、password

(4)BBC上准备数据。

technology=# select * from public.customers where id < 5;
 id | name 
----+-------
 1 | name1
 2 | name2
 3 | name3
 4 | name4
(4 rows)

technology=# 
-- schemaname = public

(5) 在node107上创建外部表:

mydb=# create foreign table ft_customers 
(
 id int4 primary key ,
 name varchar(200)
 ) server fs_postgres_bbc options (schema_name 'public',table_name 'customers');



错误: 外部表上不支持主键约束

第1行create foreign table ft_customers (id int4 primary key , nam...
            ^
mydb=#

可以看见,外部表不支持主键约束。想想也是合理

mydb=# create foreign table ft_customers (
 id int4 , 
 name varchar(200)
) server fs_postgres_bbc options (schema_name 'public',table_name 'customers');
CREATE FOREIGN TABLE
mydb=#

options 选项中: 需要指定外部表的schema和表名

(6)在node107上去访问远程BBC的数据

mydb=# select * from ft_customers where id < 5;
 id | name 
----+-------
 1 | name1
 2 | name2
 3 | name3
 4 | name4
(4 rows)

mydb=#

可以看见在mydb上能够访问远程数据库上 的数据了。

如果出现报错,如报pg_hba.conf 文件没有访问策略,在需要在对修改配置文件。

(7)本地数据库表与远程数据库表进行进行关联查询

create table orders (
 id int PRIMARY KEY,
 customerid int
);

INSERT INTO orders(id,customerid) VALUES(1,1),(2,2);

SELECT * FROM orders;

-- 和外部表关联查询。
mydb=# SELECT o.*,c.*
mydb-# FROM orders o
mydb-# INNER JOIN ft_customers c ON o.customerid = c.id
mydb-# WHERE c.id < 10000;
 id | customerid | id | name 
----+------------+----+-------
 1 |   1 | 1 | name1
 2 |   2 | 2 | name2
(2 rows)

mydb=#

PostgreSQL 中的postgres_fdw扩展详解

三、postgres_fdw 外部表支持写操作

postgres_fdw 外部表一开始只支持读,PostgreSQL9.3 版本开始支持可写。

写操作需要保证:1. 映射的用户对有写权限;2. 版本需要9.3 以上。

在node107结点上线删除数据,后再插入数据、最后更新。并查看远程BBC数据库表情况

mydb=# select count(*) from ft_customers;
 count 
----------
 10000000
(1 row)

mydb=# delete from ft_customers where id = 9999999;
DELETE 1
mydb=# select count(*) from ft_customers;
 count 
---------
 9999999
(1 row)

mydb=# insert into ft_customers values(9999999,'name1');
INSERT 0 1
mydb=# select count(*) from ft_customers;
 count 
----------
 10000000
(1 row)

mydb=# select * from ft_customers where id = 9999999;
 id | name 
---------+-------
 9999999 | name1
(1 row)

mydb=# update ft_customers set name = 'name999' where id = 9999999;
UPDATE 1
mydb=# select * from ft_customers where id = 9999999;
 id | name 
---------+---------
 9999999 | name999
(1 row)

mydb=#

可以看见对ft_customers 进行增删改查。

四、postgres_fdw支持聚合函数下推

PostgreSQL10 增强了postgres_fdw 扩展模块的特性,可以将聚合、关联操作下推到远程PostgreSQL数据库进行,而之前的版本是将外部表相应的远程数据全部取到本地再做聚合,10版本这个心特性大幅度减少了从远程传输到本地库的数据量。提升了postgres_fdw外部表上聚合查询的性能。

mydb=# EXPLAIN(ANALYZE on,VERBOSE on) select id,count(*) from ft_customers where id < 100 group by id;
            QUERY PLAN            
----------------------------------------------------------------------------------------------------
 Foreign Scan (cost=104.88..157.41 rows=199 width=12) (actual time=16.725..16.735 rows=99 loops=1)
 Output: id, (count(*))
 Relations: Aggregate on (public.ft_customers)
 Remote SQL: SELECT id, count(*) FROM public.customers WHERE ((id < 100)) GROUP BY 1
 Planning time: 0.247 ms
 Execution time: 249.410 ms
(6 rows)

mydb=#

PostgreSQL 中的postgres_fdw扩展详解

remote sql: 远程库上执行的SQL,此SQL为聚合查询的SQL。聚合是在远程上执行的。

如果在PostgreSQL9.6 测试,则需要从远程传输到本地才可以。

小结

物理表和外部表不能同名,因为pg_class的对象名称唯一键的缘故

外部表不会存储数据。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

万梅资源网 Design By www.ubjie.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
万梅资源网 Design By www.ubjie.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。