バージョンごとのドキュメント一覧

F.16. file_fdw — サーバのファイルシステムにあるデータファイルにアクセスする #

<title>file_fdw &mdash; access data files in the server's file system</title>

The <filename>file_fdw</filename> module provides the foreign-data wrapper <function>file_fdw</function>, which can be used to access data files in the server's file system, or to execute programs on the server and read their output. The data file or program output must be in a format that can be read by <command>COPY FROM</command>; see <xref linkend="sql-copy"/> for details. Access to data files is currently read-only. file_fdwモジュールは、サーバのファイルシステムにあるデータファイルにアクセスするのに使用できる外部データラッパーfile_fdwを提供します。 サーバのファイルにアクセスしたり、サーバ上のプログラムを実行して出力を読み取ったりできます。 データファイルはCOPY FROMで読むことのできるフォーマットでなければなりません。 詳細は COPY を参照してください。 データファイルへのアクセスは現時点では読み取り専用です。

A foreign table created using this wrapper can have the following options: このラッパーで作成された外部テーブルには以下のオプションを設定することができます。

filename

Specifies the file to be read. Relative paths are relative to the data directory. Either <literal>filename</literal> or <literal>program</literal> must be specified, but not both. 読み取るファイルを指定します。 相対パスはデータディレクトリからの相対パスです。 filenameprogramのどちらかを指定できますが、両方は指定できません。

program

Specifies the command to be executed. The standard output of this command will be read as though <command>COPY FROM PROGRAM</command> were used. Either <literal>program</literal> or <literal>filename</literal> must be specified, but not both. 実行するコマンドを指定します。 このコマンドの標準出力をCOPY FROM PROGRAMが使用されたかのように読み込みます。 programfilenameのどちらかを指定できますが、両方は指定できません。

format

Specifies the data format, the same as <command>COPY</command>'s <literal>FORMAT</literal> option. データフォーマットを指定するもので、COPYFORMATオプションと同じです。

header

Specifies whether the data has a header line, the same as <command>COPY</command>'s <literal>HEADER</literal> option. データがヘッダ行を持つか指定するもので、COPYHEADERオプションと同じです。

delimiter

Specifies the data delimiter character, the same as <command>COPY</command>'s <literal>DELIMITER</literal> option. データの区切り文字を指定するもので、COPYDELIMITERオプションと同じです。

quote

Specifies the data quote character, the same as <command>COPY</command>'s <literal>QUOTE</literal> option. データの引用符文字を指定するもので、COPYQUOTEオプションと同じです。

escape

Specifies the data escape character, the same as <command>COPY</command>'s <literal>ESCAPE</literal> option. データのエスケープ文字を指定するもので、COPYESCAPEオプションと同じです。

null

Specifies the data null string, the same as <command>COPY</command>'s <literal>NULL</literal> option. データのNULL文字列を指定するもので、COPYNULLオプションと同じです。

encoding

Specifies the data encoding, the same as <command>COPY</command>'s <literal>ENCODING</literal> option. データのエンコーディングを指定するもので、COPYENCODINGオプションと同じです。

Note that while <command>COPY</command> allows options such as <literal>HEADER</literal> to be specified without a corresponding value, the foreign table option syntax requires a value to be present in all cases. To activate <command>COPY</command> options typically written without a value, you can pass the value TRUE, since all such options are Booleans. COPYではHEADERといったオプションを対応する値なしで指定できるのに対して、外部データラッパーの構文では全ての場合において値を指定する必要がある点に注意してください。 通常の値なしで指定されるCOPYオプションを有効にするには、そのようなオプションはすべてbooleanであるため、代わりにTRUEを渡すことができます。

A column of a foreign table created using this wrapper can have the following options: このラッパーを使って作られた外部テーブルのカラムは、以下のオプションを持つことができます。

force_not_null

This is a Boolean option. If true, it specifies that values of the column should not be matched against the null string (that is, the table-level <literal>null</literal> option). This has the same effect as listing the column in <command>COPY</command>'s <literal>FORCE_NOT_NULL</literal> option. これはbooleanオプションです。 真の場合は、このカラムの値はNULL文字列(これはテーブルレベルのnullオプションです)と比較されません。 これは、COPYFORCE_NOT_NULLオプションに列名を指定するのと同じ効果があります。

force_null

This is a Boolean option. If true, it specifies that values of the column which match the null string are returned as <literal>NULL</literal> even if the value is quoted. Without this option, only unquoted values matching the null string are returned as <literal>NULL</literal>. This has the same effect as listing the column in <command>COPY</command>'s <literal>FORCE_NULL</literal> option. これはbooleanオプションです。 真の場合、NULL文字列と一致するこのカラムの値は、たとえ引用符で括られていたとしてもNULLと返されます。 このオプションがなければ、NULL文字列と一致する引用符で括られていない値のみがNULLと返されます。 これは、COPYFORCE_NULLオプションに列名を指定するのと同じ効果があります。

<command>COPY</command>'s <literal>FORCE_QUOTE</literal> option is currently not supported by <literal>file_fdw</literal>. COPYFORCE_QUOTEオプションはfile_fdwでは現在サポートされていません。

These options can only be specified for a foreign table or its columns, not in the options of the <literal>file_fdw</literal> foreign-data wrapper, nor in the options of a server or user mapping using the wrapper. これらのオプションは外部テーブルまたはその列にのみ指定可能で、file_fdw外部データラッパーやそれを使用するサーバ、ユーザマッピングのオプションには指定できません。

Changing table-level options requires being a superuser or having the privileges of the role <literal>pg_read_server_files</literal> (to use a filename) or the role <literal>pg_execute_server_program</literal> (to use a program), for security reasons: only certain users should be able to control which file is read or which program is run. In principle regular users could be allowed to change the other options, but that's not supported at present. どのファイルが読み込まれ、どのプログラムが実行されるかをコントロールできるのは一定のユーザのみであるべきというセキュリティ上の理由から、テーブルレベルのオプションを変更するにはスーパーユーザであるか、ロールのpg_read_server_files(ファイル名を使う)やpg_execute_server_program(プログラムを使う)の権限を持っていることが必要です。 原則としては非スーパーユーザはその他のオプションを変更することを許されてもよいのですが、現時点ではサポートされていません。

When specifying the <literal>program</literal> option, keep in mind that the option string is executed by the shell. If you need to pass any arguments to the command that come from an untrusted source, you must be careful to strip or escape any characters that might have special meaning to the shell. For security reasons, it is best to use a fixed command string, or at least avoid passing any user input in it. programオプションが指定されたとき、オプションの文字列がシェルによって実行されることに注意してください。 信頼できないソースをコマンド引数に渡す場合、シェルにとって特別な意味を持つ可能性のある文字を取り除くかエスケープするように注意する必要があります。 セキュリティ上の理由から、固定のコマンド文字列を使用するか、少なくともユーザー入力を渡さないようにすることをお勧めします。

For a foreign table using <literal>file_fdw</literal>, <command>EXPLAIN</command> shows the name of the file to be read or program to be run. For a file, unless <literal>COSTS OFF</literal> is specified, the file size (in bytes) is shown as well. file_fdwを使用する外部テーブルでは、EXPLAINは読み込むファイルの名前又は実行しているプログラムを表示します。 COSTS OFFが指定されない場合は(バイト単位の)ファイルサイズも表示されます。

例F.1 PostgreSQL CSV ログ用の外部テーブル作成

<title>Create a Foreign Table for PostgreSQL CSV Logs</title>

One of the obvious uses for <literal>file_fdw</literal> is to make the PostgreSQL activity log available as a table for querying. To do this, first you must be <link linkend="runtime-config-logging-csvlog">logging to a CSV file,</link> which here we will call <literal>pglog.csv</literal>. First, install <literal>file_fdw</literal> as an extension: file_fdwの明確な用途の一つはPostgreSQLの活動ログをテーブルとして検索できるようにすることです。 これを実現するには、ここではpglog.csvと呼ぶCSVファイルにログを記録している必要があります。 まず、file_fdwを拡張機能としてインストールします。

CREATE EXTENSION file_fdw;

Then create a foreign server: 続いて外部サーバを作成します。

CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;

Now you are ready to create the foreign data table. Using the <command>CREATE FOREIGN TABLE</command> command, you will need to define the columns for the table, the CSV file name, and its format: これで外部テーブルを作成する準備ができました。 CREATE FOREIGN TABLEコマンドを使って、テーブルのカラム、CSVファイル名とそのフォーマットを定義する必要があるでしょう。

CREATE FOREIGN TABLE pglog (
  log_time timestamp(3) with time zone,
  user_name text,
  database_name text,
  process_id integer,
  connection_from text,
  session_id text,
  session_line_num bigint,
  command_tag text,
  session_start_time timestamp with time zone,
  virtual_transaction_id text,
  transaction_id bigint,
  error_severity text,
  sql_state_code text,
  message text,
  detail text,
  hint text,
  internal_query text,
  internal_query_pos integer,
  context text,
  query text,
  query_pos integer,
  location text,
  application_name text,
  backend_type text,
  leader_pid integer,
  query_id bigint
) SERVER pglog
OPTIONS ( filename 'log/pglog.csv', format 'csv' );

That's it &mdash; now you can query your log directly. In production, of course, you would need to define some way to deal with log rotation. これで全てです。 もうあなたはログに直接検索を実行することができます。 実運用においては、もちろんログローテーションを処理する方法を定義する必要があるでしょう。