外部データラッパー(Foreign Data Wrapper、FDW)は、他のPostgreSQLサーバーや外部のデータソースをあたかも自分のデータベース内のテーブルであるかのように扱う仕組みである。ここではpostgres_fdw拡張を使い、別のPostgreSQLサーバーのテーブルを外部テーブルとして参照する構成を例にする。

testdb=# CREATE EXTENSION postgres_fdw;
testdb=# CREATE SERVER remote_pg FOREIGN DATA WRAPPER postgres_fdw
testdb-#   OPTIONS (host 'localhost', dbname 'testdb', port '5432');
testdb=# CREATE USER MAPPING FOR postgres SERVER remote_pg OPTIONS (user 'postgres');
testdb=# CREATE SCHEMA fdw_schema;
testdb=# IMPORT FOREIGN SCHEMA public LIMIT TO (users)
testdb-#   FROM SERVER remote_pg INTO fdw_schema;

\dewコマンドで外部データラッパー一覧を確認する

インストール済みの外部データラッパー自体の一覧は\dewで確認する。

testdb=# \dew
                    List of foreign-data wrappers
     Name     | Owner |       Handler        |       Validator
--------------+-------+----------------------+------------------------
 postgres_fdw | postgres   | postgres_fdw_handler | postgres_fdw_validator
(1 row)

HandlerValidatorは、それぞれデータの読み書き処理とオプションの妥当性検証を担当する内部関数である。

\desコマンドで外部サーバー一覧を確認する

外部データラッパーを使って定義した接続先(サーバー)の一覧は\desで確認する。

testdb=# \des
         List of foreign servers
   Name    | Owner | Foreign-data wrapper
-----------+-------+----------------------
 remote_pg | postgres   | postgres_fdw
(1 row)

+を付けると接続オプションも表示される。

testdb=# \des+ remote_pg
                                                          List of foreign servers
   Name    | Owner | Foreign-data wrapper | Access privileges | Type | Version |                       FDW options
-----------+-------+----------------------+-------------------+------+---------+------------------------------------------------------------
 remote_pg | postgres   | postgres_fdw         |                   |      |         | (host 'localhost', dbname 'testdb', port '5432')
(1 row)

FDW options列に接続先ホスト・データベース名・ポート番号が表示される。接続先の設定ミスを調査する際に確認する。

\deuコマンドでユーザーマッピング一覧を確認する

外部サーバーへ接続する際にどのローカルロールがどのリモートユーザーとして認証されるかは、\deuで確認する。

testdb=# \deu
 List of user mappings
  Server   | User name
-----------+-----------
 remote_pg | postgres
(1 row)

パスワードなどの認証情報そのものは表示されない。

\detコマンドで外部テーブル一覧を確認する

外部テーブルの一覧は\detで確認する。他の\d系コマンドと同様、パターンを指定しない場合は現在のsearch_pathにあるスキーマだけが対象になる。

testdb=# \det
List of foreign tables
 Schema | Table | Server
--------+-------+--------
(0 rows)

外部テーブルをfdw_schemaスキーマへインポートしているため、search_pathにそのスキーマを含めないと何も表示されない。スキーマ名を含めたパターンを指定すると見つかる。

testdb=# \det fdw_schema.*
     List of foreign tables
   Schema   | Table |  Server
------------+-------+-----------
 fdw_schema | users | remote_pg
(1 row)

\dE(大文字のE)を使うと、通常のテーブルと同じ一覧形式で外部テーブルだけをType列のforeign tableとして表示できる。

testdb=# \dE fdw_schema.*
             List of relations
   Schema   | Name  |     Type      | Owner
------------+-------+---------------+-------
 fdw_schema | users | foreign table | postgres
(1 row)

参考: 【PostgreSQL】SQLでテーブル一覧を取得する

\d 外部テーブル名で列とオプションの詳細を確認する

\dに外部テーブル名を指定すると、列定義に加えてFDW optionsとリモート側の対応関係を確認できる。

testdb=# \d fdw_schema.users
                     Foreign table "fdw_schema.users"
 Column |  Type   | Collation | Nullable | Default |      FDW options
--------+---------+-----------+----------+---------+-----------------------
 id     | integer |           | not null |         | (column_name 'id')
 name   | text    |           | not null |         | (column_name 'name')
 email  | text    |           |          |         | (column_name 'email')
Server: remote_pg
FDW options: (schema_name 'public', table_name 'users')

Server行でどの外部サーバー経由か、FDW options行でリモート側のスキーマ名・テーブル名がわかる。IMPORT FOREIGN SCHEMAで自動生成した外部テーブルは、列ごとのcolumn_nameオプションでリモート側の列名との対応が明示される。

参考