通過postgres_fdw 擴展,訪問遠程數(shù)據(jù)庫表
虛擬機(node107):centos7、PostgreSQL10
遠程服務(wù)器(百度云服務(wù)BBC): centos7、PostgreSQL10
在本地虛擬機上訪問遠程服務(wù)器的數(shù)據(jù)表。
[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": 權(quán)限不夠 psql (10.7) Type "help" for help. mydb=# CREATE EXTENSION postgres_fdw; CREATE EXTENSION
如果是普通用戶使用 ·postgres_fdw 需要單獨授權(quán)
grant usage on foreign data wrapper postgres_fdw to 用戶名
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的外部服務(wù),options 設(shè)置遠程PostgreSQL數(shù)據(jù)源連接選項,通常設(shè)置主機名、端口、數(shù)據(jù)庫名稱。
mydb=# create user mapping for postgres server fs_postgres_bbc options(user 'postgres',password 'password'); CREATE USER MAPPING mydb=#
for 后面接的是 node107 的數(shù)據(jù)庫用戶,options 里接的是遠程PostgreSQL數(shù)據(jù)庫的用戶和密碼。password 注意修改成自己的
其實想訪問遠程數(shù)據(jù)庫,無非就是知道連接信息。包括host、port、dbname、user、password
technology=# select * from public.customers where id 5; id | name ----+------- 1 | name1 2 | name2 3 | name3 4 | name4 (4 rows) technology=# -- schemaname = public
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和表名
mydb=# select * from ft_customers where id 5; id | name ----+------- 1 | name1 2 | name2 3 | name3 4 | name4 (4 rows) mydb=#
可以看見在mydb上能夠訪問遠程數(shù)據(jù)庫上 的數(shù)據(jù)了。
如果出現(xiàn)報錯,如報pg_hba.conf 文件沒有訪問策略,在需要在對修改配置文件。
create table orders ( id int PRIMARY KEY, customerid int ); INSERT INTO orders(id,customerid) VALUES(1,1),(2,2); SELECT * FROM orders; -- 和外部表關(guān)聯(lián)查詢。 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=#
postgres_fdw 外部表一開始只支持讀,PostgreSQL9.3 版本開始支持可寫。
寫操作需要保證:1. 映射的用戶對有寫權(quán)限;2. 版本需要9.3 以上。
在node107結(jié)點上線刪除數(shù)據(jù),后再插入數(shù)據(jù)、最后更新。并查看遠程BBC數(shù)據(jù)庫表情況
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 進行增刪改查。
PostgreSQL10 增強了postgres_fdw 擴展模塊的特性,可以將聚合、關(guān)聯(lián)操作下推到遠程PostgreSQL數(shù)據(jù)庫進行,而之前的版本是將外部表相應(yīng)的遠程數(shù)據(jù)全部取到本地再做聚合,10版本這個心特性大幅度減少了從遠程傳輸?shù)奖镜貛斓臄?shù)據(jù)量。提升了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=#
remote sql: 遠程庫上執(zhí)行的SQL,此SQL為聚合查詢的SQL。聚合是在遠程上執(zhí)行的。
如果在PostgreSQL9.6 測試,則需要從遠程傳輸?shù)奖镜夭趴梢浴?/p>
物理表和外部表不能同名,因為pg_class的對象名稱唯一鍵的緣故
外部表不會存儲數(shù)據(jù)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。