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

CREATE FOREIGN TABLE

CREATE FOREIGN TABLE <refpurpose>define a new foreign table</refpurpose> — 新しい外部テーブルを定義する

概要

CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [
  { column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]
    | table_constraint }
    [, ... ]
] )
[ INHERITS ( parent_table [, ... ] ) ]
  SERVER server_name
[ OPTIONS ( option 'value' [, ... ] ) ]

CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name
  PARTITION OF parent_table [ (
  { column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]
    | table_constraint }
    [, ... ]
) ]
{ FOR VALUES partition_bound_spec | DEFAULT }
  SERVER server_name
[ OPTIONS ( option 'value' [, ... ] ) ]


<phrase>where <replaceable class="parameter">column_constraint</replaceable> is:</phrase>

ここでcolumn_constraintは以下の通りです。

[ CONSTRAINT constraint_name ]
{ NOT NULL |
  NULL |
  CHECK ( expression ) [ NO INHERIT ] |
  DEFAULT default_expr |
  GENERATED ALWAYS AS ( generation_expr ) STORED }


<phrase>and <replaceable class="parameter">table_constraint</replaceable> is:</phrase>

またtable_constraintは以下の通りです。

[ CONSTRAINT constraint_name ]
CHECK ( expression ) [ NO INHERIT ]


<phrase>and <replaceable class="parameter">partition_bound_spec</replaceable> is:</phrase>

またpartition_bound_specは以下の通りです。

IN ( partition_bound_expr [, ...] ) |
FROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )
  TO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |
WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )

説明

<title>Description</title>

<command>CREATE FOREIGN TABLE</command> creates a new foreign table in the current database. The table will be owned by the user issuing the command. CREATE FOREIGN TABLEは現在のデータベース内に新しい外部テーブルを作成します。 このテーブルはコマンドを発行したユーザにより所有されます。

If a schema name is given (for example, <literal>CREATE FOREIGN TABLE myschema.mytable ...</literal>) then the table is created in the specified schema. Otherwise it is created in the current schema. The name of the foreign table must be distinct from the name of any other relation (table, sequence, index, view, materialized view, or foreign table) in the same schema. スキーマ名が指定された場合(例えばCREATE FOREIGN TABLE myschema.mytable ...)、テーブルは指定されたスキーマ内に作成されます。 そうでなければ現在のスキーマ内に作成されます。 外部テーブルの名前は同じスキーマ内にある他のリレーション(テーブル、シーケンス、インデックス、ビュー、マテリアライズドビュー、外部テーブル)と異なるものでなければなりません。

<command>CREATE FOREIGN TABLE</command> also automatically creates a data type that represents the composite type corresponding to one row of the foreign table. Therefore, foreign tables cannot have the same name as any existing data type in the same schema. CREATE FOREIGN TABLEはまた自動的に外部テーブルの1行に対応する複合型を表すデータ型を作成します。 したがって外部テーブルは同じスキーマ内の既存のデータ型の名前と同じものを持つことができません。

If <literal>PARTITION OF</literal> clause is specified then the table is created as a partition of <literal>parent_table</literal> with specified bounds. PARTITION OF句が指定された場合、テーブルはparent_tableの指定された境界のパーティションとして作られます。

To be able to create a foreign table, you must have <literal>USAGE</literal> privilege on the foreign server, as well as <literal>USAGE</literal> privilege on all column types used in the table. 外部テーブルを作成するためには、外部サーバに対するUSAGE権限とテーブルで使用される列の型すべてに対するUSAGE権限を持たなければなりません。

パラメータ

<title>Parameters</title>
IF NOT EXISTS

Do not throw an error if a relation with the same name already exists. A notice is issued in this case. Note that there is no guarantee that the existing relation is anything like the one that would have been created. 同じ名前のリレーションがすでに存在していてもエラーとしません。 この場合注意が発せられます。 既存のリレーションが作成しようとしたものと何かしら似たものであることは保証されません。

table_name

The name (optionally schema-qualified) of the table to be created. 作成するテーブルの名前です(スキーマ修飾も可)。

column_name

The name of a column to be created in the new table. 新しいテーブルで作成される列の名前です。

data_type

The data type of the column. This can include array specifiers. For more information on the data types supported by <productname>PostgreSQL</productname>, refer to <xref linkend="datatype"/>. 列のデータ型です。 これには、配列指定子を含めることができます。 PostgreSQLでサポートされるデータ型の情報に関する詳細は第8章を参照してください。

COLLATE collation

The <literal>COLLATE</literal> clause assigns a collation to the column (which must be of a collatable data type). If not specified, the column data type's default collation is used. COLLATE句は列(照合可能なデータ型でなければなりません)の照合順序を指定します。 指定されなければ、列のデータ型のデフォルトの照合順序が使用されます。

INHERITS ( parent_table [, ... ] )

The optional <literal>INHERITS</literal> clause specifies a list of tables from which the new foreign table automatically inherits all columns. Parent tables can be plain tables or foreign tables. See the similar form of <link linkend="sql-createtable"><command>CREATE TABLE</command></link> for more details. オプションでINHERITS句を使い、新しい外部テーブルが自動的にすべての列を継承するテーブルのリストを指定できます。 親テーブルには通常のテーブルまたは外部テーブルが指定できます。 詳しくはCREATE TABLEの類似の構文を参照してください。

PARTITION OF parent_table { FOR VALUES partition_bound_spec | DEFAULT }

This form can be used to create the foreign table as partition of the given parent table with specified partition bound values. See the similar form of <link linkend="sql-createtable"><command>CREATE TABLE</command></link> for more details. Note that it is currently not allowed to create the foreign table as a partition of the parent table if there are <literal>UNIQUE</literal> indexes on the parent table. (See also <link linkend="sql-altertable"><command>ALTER TABLE ATTACH PARTITION</command></link>.) この形式は、与えられた親テーブルのパーティションとして指定されたパーティション境界値を持つ外部テーブルを作成するために使うことができます。 より詳細についてはCREATE TABLEの類似の形式を参照してください。 現在のところに親テーブルにUNIQUEインデックスがある場合、親テーブルのパーティションとして外部テーブルを作成することは認められていないことに注意してください。 (ALTER TABLE ATTACH PARTITIONも参照してください。)

CONSTRAINT constraint_name

An optional name for a column or table constraint. If the constraint is violated, the constraint name is present in error messages, so constraint names like <literal>col must be positive</literal> can be used to communicate helpful constraint information to client applications. (Double-quotes are needed to specify constraint names that contain spaces.) If a constraint name is not specified, the system generates a name. 列制約またはテーブル制約の名前(省略可)です。 制約に違反した時、エラーメッセージ内に制約名が表示されるので、col must be positiveのような制約名を使って、クライアントアプリケーションに役立つ制約情報を通知することができます。 (空白文字を含む制約名を指定するには二重引用符を使う必要があります。) 制約名が指定されなければ、システムが名前を生成します。

NOT NULL

The column is not allowed to contain null values. その列がNULL値を持てないことを指定します。

NULL

The column is allowed to contain null values. This is the default. その列がNULL値を持てることを指定します。 これがデフォルトです。

This clause is only provided for compatibility with non-standard SQL databases. Its use is discouraged in new applications. この句は非標準的なSQLデータベースとの互換性のためだけに提供されています。 新しいアプリケーションでこれを使用するのはお勧めしません。

CHECK ( expression ) [ NO INHERIT ]

The <literal>CHECK</literal> clause specifies an expression producing a Boolean result which each row in the foreign table is expected to satisfy; that is, the expression should produce TRUE or UNKNOWN, never FALSE, for all rows in the foreign table. A check constraint specified as a column constraint should reference that column's value only, while an expression appearing in a table constraint can reference multiple columns. CHECK句では、外部テーブルの各行が満たすと期待される論理値の結果を生成する式を指定します。 つまり、式は外部テーブルのすべての行に対して、TRUEまたはUNKNOWNを生成し、決してFALSEにはなりません。 列制約として指定したチェック制約はその列の値だけを参照しますが、テーブル制約として使われる式は複数の列を参照することができます。

Currently, <literal>CHECK</literal> expressions cannot contain subqueries nor refer to variables other than columns of the current row. The system column <literal>tableoid</literal> may be referenced, but not any other system column. 現在のところ、CHECKの式は副問い合わせを含むことや、現在の行の列以外の変数を参照することはできません。 システム列tableoidを参照することはできますが、それ以外のシステム列を参照することはできません。

A constraint marked with <literal>NO INHERIT</literal> will not propagate to child tables. NO INHERITと印を付けられた制約は、子テーブルに継承されません。

DEFAULT default_expr

The <literal>DEFAULT</literal> clause assigns a default data value for the column whose column definition it appears within. The value is any variable-free expression (subqueries and cross-references to other columns in the current table are not allowed). The data type of the default expression must match the data type of the column. DEFAULT句は、 列定義の中に現れる、列に対するデフォルトデータ値を割り当てます。 値は変数がない任意の式(副問い合わせおよび、現在のテーブル内の他の列へのクロス参照は許されません)です。 デフォルト式のデータ型は列のデータ型とマッチしなければなりません。

The default expression will be used in any insert operation that does not specify a value for the column. If there is no default for a column, then the default is null. デフォルト式は、列に対する値指定がないすべての挿入操作で使用されます。 列に対するデフォルトがない場合、デフォルトはNULLです。

GENERATED ALWAYS AS ( generation_expr ) STORED

This clause creates the column as a <firstterm>generated column</firstterm>. The column cannot be written to, and when read the result of the specified expression will be returned. この句は、列を生成列として作成します。 その列に書き込むことはできず、読み出された場合に指定された式の結果が返されます。

The keyword <literal>STORED</literal> is required to signify that the column will be computed on write. (The computed value will be presented to the foreign-data wrapper for storage and must be returned on reading.) キーワードSTOREDは、列が書き込み時に計算されることを指定するのに必要です。 (計算された値は保存用に外部データラッパーへと送られ、読み込み時には返されなければなりません。)

The generation expression can refer to other columns in the table, but not other generated columns. Any functions and operators used must be immutable. References to other tables are not allowed. 生成式はテーブル内の他の列を参照できますが、他の生成列は参照できません。 使われている関数や演算子はimmutableでなければなりません。 他のテーブルへの参照はできません。

server_name

The name of an existing foreign server to use for the foreign table. For details on defining a server, see <xref linkend="sql-createserver"/>. 外部テーブル用に使用される既存の外部サーバの名前です。 外部サーバの詳細についてはCREATE SERVERを参照してください。

OPTIONS ( option 'value' [, ...] )

Options to be associated with the new foreign table or one of its columns. The allowed option names and values are specific to each foreign data wrapper and are validated using the foreign-data wrapper's validator function. Duplicate option names are not allowed (although it's OK for a table option and a column option to have the same name). 新しい外部テーブルまたはその列の1つに関連するオプションです。 設定可能なオプションの名前と値は外部データラッパーそれぞれに固有なものであり、外部データラッパーの検証関数を用いて検証されます。 重複するオプション名は許されません(しかしテーブルオプションと列オプションでは同じ名前を持たせることはできます)。

注釈

<title>Notes</title>

Constraints on foreign tables (such as <literal>CHECK</literal> or <literal>NOT NULL</literal> clauses) are not enforced by the core <productname>PostgreSQL</productname> system, and most foreign data wrappers do not attempt to enforce them either; that is, the constraint is simply assumed to hold true. There would be little point in such enforcement since it would only apply to rows inserted or updated via the foreign table, and not to rows modified by other means, such as directly on the remote server. Instead, a constraint attached to a foreign table should represent a constraint that is being enforced by the remote server. 外部テーブル上の制約(CHECK句やNOT NULL句など)はPostgreSQLのコアシステムによって強制されませんし、ほとんどの外部データラッパーもそれを強制しようとはしません。 つまり、制約は単にそれが成り立つと仮定されるものです。 制約は外部テーブルの機能を使って行を挿入あるいは更新するときにのみ適用され、リモートサーバ上で直接更新するなど、他の手段による行の更新には適用されませんから、それを強制することにはあまり意味はありません。 その代わりに、外部テーブルに指定する制約は、リモートサーバによって強制される制約を表現するものであるべきです。

Some special-purpose foreign data wrappers might be the only access mechanism for the data they access, and in that case it might be appropriate for the foreign data wrapper itself to perform constraint enforcement. But you should not assume that a wrapper does that unless its documentation says so. 一部の特別な目的の外部データラッパーは、それがアクセス対象のデータにアクセスするための唯一の機構であり、またその場合、外部データラッパーそれ自体にとって、制約の強制を実行することが適切なことがあります。 ただし、ラッパーのドキュメントにそのように書いてあるのでなければ、それを仮定しない方が良いでしょう。

Although <productname>PostgreSQL</productname> does not attempt to enforce constraints on foreign tables, it does assume that they are correct for purposes of query optimization. If there are rows visible in the foreign table that do not satisfy a declared constraint, queries on the table might produce errors or incorrect answers. It is the user's responsibility to ensure that the constraint definition matches reality. PostgreSQLでは外部テーブルの制約を強制しませんが、問い合わせの最適化という目的のため、制約が正しいということを仮定します。 外部テーブルで、宣言された制約を満たさない行が可視の状態で存在する場合、そのテーブルに対する問い合わせはエラーになったり誤った結果をもたらしたりするかもしれません。 制約の定義が現実に即したものであることを保証するのは、ユーザの責任です。

注意

When a foreign table is used as a partition of a partitioned table, there is an implicit constraint that its contents must satisfy the partitioning rule. Again, it is the user's responsibility to ensure that that is true, which is best done by installing a matching constraint on the remote server. 外部テーブルがパーティション化テーブルのパーティションとして使われていれば、その内容はパーティショニングの規則を満たさねばならないという暗黙の制約があります。 ここでもまた、それが真であることを保証するのは、ユーザの責任です。これにはリモートサーバに一致する制約を設定するのが最善です。

Within a partitioned table containing foreign-table partitions, an <command>UPDATE</command> that changes the partition key value can cause a row to be moved from a local partition to a foreign-table partition, provided the foreign data wrapper supports tuple routing. However it is not currently possible to move a row from a foreign-table partition to another partition. An <command>UPDATE</command> that would require doing that will fail due to the partitioning constraint, assuming that that is properly enforced by the remote server. 外部テーブルパーティションを含むパーティション化テーブル内では、パーティションキー値を変更するUPDATEは、外部データラッパーがタプルルーティングをサポートしていれば、行をローカルパーティションから外部テーブルパーティションへと移動する原因となる可能性があります。 しかしながら、行を外部テーブルパーティションから別のパーティションに移動することは現在のところできません。 そのようなことを要求するUPDATEは、リモートサーバにより適切に強制されるのであれば、パーティショニングの制約のために失敗するでしょう。

Similar considerations apply to generated columns. Stored generated columns are computed on insert or update on the local <productname>PostgreSQL</productname> server and handed to the foreign-data wrapper for writing out to the foreign data store, but it is not enforced that a query of the foreign table returns values for stored generated columns that are consistent with the generation expression. Again, this might result in incorrect query results. 似たような配慮は生成列に適用されます。 保存生成列は、ローカルのPostgreSQLサーバ上で挿入されたり更新されたりした時に計算され、外部データ保存領域へ書き出すために外部データラッパーへと渡されますが、外部テーブルへの問い合わせが生成式と矛盾しない保存生成列の値を返すことは強制されていません。 ここでも、問い合わせの結果が正しくないということになる可能性があります。

<title>Examples</title>

Create foreign table <structname>films</structname>, which will be accessed through the server <structname>film_server</structname>: サーバfilm_serverを通してアクセスされる、外部テーブルfilmsを作成します。

CREATE FOREIGN TABLE films (
    code        char(5) NOT NULL,
    title       varchar(40) NOT NULL,
    did         integer NOT NULL,
    date_prod   date,
    kind        varchar(10),
    len         interval hour to minute
)
SERVER film_server;

Create foreign table <structname>measurement_y2016m07</structname>, which will be accessed through the server <structname>server_07</structname>, as a partition of the range partitioned table <structname>measurement</structname>: 範囲パーティションテーブルmeasurementのパーティションとして、サーバserver_07を通してアクセスされる外部テーブルmeasurement_y2016m07を作成します。

CREATE FOREIGN TABLE measurement_y2016m07
    PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')
    SERVER server_07;

互換性

<title>Compatibility</title>

The <command>CREATE FOREIGN TABLE</command> command largely conforms to the <acronym>SQL</acronym> standard; however, much as with <link linkend="sql-createtable"><command>CREATE TABLE</command></link>, <literal>NULL</literal> constraints and zero-column foreign tables are permitted. The ability to specify column default values is also a <productname>PostgreSQL</productname> extension. Table inheritance, in the form defined by <productname>PostgreSQL</productname>, is nonstandard. CREATE FOREIGN TABLEはおおよそ標準SQLに準拠します。 しかしCREATE TABLEとほとんど同様、NULL制約とゼロ列の外部テーブルが許されます。 列のデフォルト値を指定する機能もPostgreSQLの拡張です。 PostgreSQLが定義する形式のテーブルの継承は標準とは異なります。

関連項目

<title>See Also</title> ALTER FOREIGN TABLE, DROP FOREIGN TABLE, CREATE TABLE, CREATE SERVER, IMPORT FOREIGN SCHEMA