🐘

PostgreSQLのpostgres_fdw拡張機能の実行

postgres_fdwの実行

拡張機能の有効化

CREATE EXTENSION postgres_fdw;

外部サーバの定義

test1データベースに対しての外部サーバを作成する

CREATE SERVER fdw_app FOREIGN DATA WRAPPER postgres_fdw OPTIONS (dbname 'test1');

外部サーバのユーザーマップ定義

CREATE USER MAPPING FOR public SERVER fdw_app OPTIONS (user 'postgres', password 'postgres');

外部テーブルの作成

fdw_app外部サーバを使用してt2テーブルを外部テーブルとして作成する

CREATE FOREIGN TABLE t2 (a integer, b text,c text, d timestamp with time zone) SERVER fdw_app;

外部テーブルの場合はtypeがforeign tableになる模様

postgres=> \d
                List of relations
 Schema |    Name     |     Type      |  Owner   
--------+-------------+---------------+----------
 public | t1          | table         | postgres
 public | t2          | foreign table | postgres
 public | v_dblink_t1 | view          | postgres

IMPORT FOREIGN SCHEMA を使用して外部テーブルの作成も可能。

検索

postgres=> select count(*) from t2;
   count   
-----------
 100000000
(1 row)

postgres_fdwの特徴

  • プッシュダウン機能
    • SQL文に含まれるWHERE句(絞り込み処理)、ORDER BY句(ソート処理)などをリモート側で実行される
      • dblinkと違う明確なメリット
  • 更新が可能
    • ON CONFLICT DO UPDATE句のあるINSERT文をサポートしていない

postgres_fdwの注意点

  • トランザクションの制御
    • リモート側のCOMMITのタイミング、トランザクション分離レベルで注意が必要(詳細は参考のリンクを)
  • 性能面
    • 通信量次第で遅くなる傾向にある
    • リモート実行オプションでfetch_sizeが100なので、必要に応じてチューニングする必要がある

参考

F.33. postgres_fdw https://www.postgresql.jp/document/13/html/postgres-fdw.html

PostgreSQL 9.6 の postgres_fdw について検証してみた | SIOS Tech. Lab https://tech-lab.sios.jp/archives/8641#i

外部データとの連携 ~FDWで様々なデータソースとつなぐ~|PostgreSQLインサイド : 富士通 https://www.fujitsu.com/jp/products/software/resources/feature-stories/postgres/article-index/fdw-overview/

GitHubで編集を提案