PostgreSQL 의 postgresfdw 확장 설명

post gres 를 통 해fdw 확장,원 격 데이터베이스 테이블 접근
환경 준비
가상 컴퓨터(node 107):centos 7,PostgreSQL 10
원 격 서버(바 이 두 클 라 우 드 서비스 BBC):centos 7,PostgreSQL 10
로 컬 가상 컴퓨터 에서 원 격 서버 에 접근 하 는 데이터 시트 입 니 다.
2.연결 설정
(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
일반 사용자 라면/postgresfdw 단독 인증 필요
grant usage on foreign data wrapper postgres_사용자 이름
(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=#
정의 이름 은 fspostgres_bbc 의 외부 서비스,options 는 원 격 PostgreSQL 데이터 원본 연결 옵션 을 설정 하고 호스트 이름,포트,데이터베이스 이름 을 설정 합 니 다.
(3)외부 서비스 에 맵 사용 자 를 만들어 야 합 니 다.

mydb=# create user mapping for postgres server 
fs_postgres_bbc options(user 'postgres',password 'password');
CREATE USER MAPPING
mydb=#
for 뒤에 node 107 데이터베이스 사용 자 를 연결 합 니 다.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)node 107 에 외부 테이블 만 들 기:

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)node 107 에서 원 격 BBC 에 접근 한 데이터

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

mydb=#
mydb 에서 원 격 데이터베이스 에 접근 할 수 있 는 데 이 터 를 볼 수 있 습 니 다.
잘못 보고 하면 pghba.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=#

3.postgresfdw 외부 테이블 지원 쓰기 동작
postgres_fdw 외부 테이블 은 처음에는 읽 기만 지원 하고 PostgreSQL 9.3 버 전 은 쓰기 만 지원 합 니 다.
쓰기 작업 은 보증 이 필요 합 니 다.1.매 핑 된 사용 자 는 쓰기 권한 이 있 습 니 다.2.버 전 은 9.3 이상 이 필요 합 니 다.
node 107 노드 에서 데 이 터 를 삭제 한 후 데 이 터 를 삽입 하고 마지막 으로 업데이트 합 니 다.원 격 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 가 첨삭 검 사 를 진행 하 다.
4.postgresfdw 는 취 합 함수 하 추 를 지원 합 니 다.
PostgreSQL 10 은 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=#

remote sql:원 격 라 이브 러 리 에서 실 행 된 SQL 입 니 다.이 SQL 은 취 합 된 SQL 입 니 다.집합 은 원 격 으로 실 행 됩 니 다.
PostgreSQL 9.6 에서 테스트 하려 면 원 격 으로 로 컬 로 전송 해 야 합 니 다.
작은 매듭
물리 표 와 외부 표 는 이름 이 같 을 수 없습니다.왜냐하면 pgclass 의 대상 이름 이 유일한 키 이기 때 문 입 니 다.
외부 테이블 은 데 이 터 를 저장 하지 않 습 니 다.
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.만약 잘못 이 있 거나 완전히 고려 하지 않 은 부분 이 있다 면 아낌없이 가르침 을 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기