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

23.6. テーブル空間 #

<title>Tablespaces</title>

Tablespaces in <productname>PostgreSQL</productname> allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects. PostgreSQLのテーブル空間により、データベース管理者はデータベースオブジェクトを表すファイルを格納できるファイルシステム上の場所を定義できます。 テーブル空間を一度作成すると、データベースオブジェクトを作成する時に名前により参照できます。

By using tablespaces, an administrator can control the disk layout of a <productname>PostgreSQL</productname> installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured. テーブル空間を使用することで、管理者はPostgreSQLインストレーションのディスクレイアウトを制御できます。 これは、少なくとも2つの点で有用です。 1つは、クラスタを初期化したパーティションもしくはボリュームの容量が不足し、拡張を行うことができない場合、システムを再構成するまで、別のパーティションにテーブル空間を作成して、このテーブル空間を使用できます。

Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system. もう1つは、テーブル空間により、管理者はデータベースオブジェクトの使用パターンに基づいてデータ格納場所を調整できることです。 例えば、非常によく使用されるインデックスを、例えば、高価なソリッドステートディスクなどの高速・高可用性ディスクに格納できます。 同時に、めったに使用されない保存用のデータや性能が求められていない保存用のデータを格納するテーブルを低価格・低速ディスクシステムに格納することもできます。

警告

Even though located outside the main PostgreSQL data directory, tablespaces are an integral part of the database cluster and <emphasis>cannot</emphasis> be treated as an autonomous collection of data files. They are dependent on metadata contained in the main data directory, and therefore cannot be attached to a different database cluster or backed up individually. Similarly, if you lose a tablespace (file deletion, disk failure, etc.), the database cluster might become unreadable or unable to start. Placing a tablespace on a temporary file system like a RAM disk risks the reliability of the entire cluster. 主PostgreSQLデータディレクトリの外に位置していたとしても、テーブル空間はデータベースクラスタの不可欠な部分であり、データファイルの自律的な集合として扱うことはできません。 それらは主データディレクトリに含まれるメタデータに依存しますので、異なるデータベースクラスタに所属させたり、個別にバックアップしたりすることはできません。 同様に、テーブル空間を失えば(ファイル削除、ディスクの破損など)、データベースクラスタは読み取れなくなったり、開始できなくなったりするかもしれません。 テーブル空間をRAMディスクのような一時ファイルシステムに置くことは、クラスタ全体の信頼性を危険にさらします。

To define a tablespace, use the <xref linkend="sql-createtablespace"/> command, for example:<indexterm><primary>CREATE TABLESPACE</primary></indexterm>: テーブル空間を定義するにはCREATE TABLESPACEコマンドを使用してください。 以下に例を示します。

CREATE TABLESPACE fastspace LOCATION '/ssd1/postgresql/data';

The location must be an existing, empty directory that is owned by the <productname>PostgreSQL</productname> operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory. The location must not be on removable or transient storage, as the cluster might fail to function if the tablespace is missing or lost. この場所は、既存かつ空のディレクトリでなければならず、また、PostgreSQLオペレーティングシステムユーザが所有していなければなりません。 その後に、テーブル空間内に作成されるオブジェクトはすべてこのディレクトリ以下のファイルに格納されます。 テーブル空間が見つからなかったり失われたりするとクラスタは機能しませんので、この場所は取り外し可能な記憶装置や一時的な記憶装置上にあってはいけません。

注記

There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, <productname>PostgreSQL</productname> does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use. 通常、論理ファイルシステム内の個々のファイルの場所を制御することができませんので、1つの論理ファイルシステムに複数のテーブル空間を作成することは意味がありません。 しかし、PostgreSQLにはこうした制限はありません。 実際、システムのファイルシステムの境を直接意識しません。 単に使用することを宣言したディレクトリにファイルを格納します。

Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the <literal>CREATE</literal> privilege on it. テーブル空間自体の生成はデータベーススーパーユーザが行う必要があります。 しかし、その後に、データベース一般ユーザにそのテーブル空間を使用させることができます。 これを行うには、ユーザにテーブル空間に対するCREATE権限を与えてください。

Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the <literal>CREATE</literal> privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace <literal>space1</literal>: テーブル、インデックス、およびデータベース全体は特定のテーブル空間に割り当て可能です。 これを行うには、指定テーブル空間にCREATE権限を持つユーザが関連するコマンドにテーブル空間をパラメータとして渡す必要があります。 例えば、以下はspace1テーブル空間にあるテーブルを作成しています。

CREATE TABLE foo(i int) TABLESPACE space1;

Alternatively, use the <xref linkend="guc-default-tablespace"/> parameter: 他の方法として、以下のようにdefault_tablespaceパラメータの使用があります。

SET default_tablespace = space1;
CREATE TABLE foo(i int);

When <varname>default_tablespace</varname> is set to anything but an empty string, it supplies an implicit <literal>TABLESPACE</literal> clause for <command>CREATE TABLE</command> and <command>CREATE INDEX</command> commands that do not have an explicit one. default_tablespaceが空文字以外の何かに設定された場合、この値が、明示的なテーブル空間の指定がないCREATE TABLEコマンドやCREATE INDEXコマンドの暗黙的な TABLESPACE句として適用されます。

There is also a <xref linkend="guc-temp-tablespaces"/> parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created. temp_tablespacesというパラメータも存在します。 これは、一時テーブルとそのインデックス、大規模データ集合のソートなどを目的に使用される一時ファイルの場所を決定するものです。 これは、テーブル空間名を1つだけ指定するものではなく、テーブル空間名のリストを取ることができます。 このため、一時的なオブジェクトに関連する負荷を、複数のテーブル空間にわたって分散することができます。 一時的なオブジェクトを作成する度に、このリストから無作為に要素が選択されます。

The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no <literal>TABLESPACE</literal> clause is given and no other selection is specified by <varname>default_tablespace</varname> or <varname>temp_tablespaces</varname> (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from. データベースに関連付けされたテーブル空間は、そのデータベースのシステムカタログを格納するために使用されます。 さらに、TABLESPACE句を付けずに、または、(適切な)default_tablespacetemp_tablespacesで指定された選択肢がなければ、データベース内に作成されたテーブルやインデックスのデフォルトのテーブル空間としても使用されます。 テーブル空間の指定なしで作成されたデータベースは、コピー元のテンプレートデータベースのテーブル空間と同じものを使用します。

Two tablespaces are automatically created when the database cluster is initialized. The <literal>pg_global</literal> tablespace is used for shared system catalogs. The <literal>pg_default</literal> tablespace is the default tablespace of the <literal>template1</literal> and <literal>template0</literal> databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a <literal>TABLESPACE</literal> clause in <command>CREATE DATABASE</command>). データベースクラスタが初期化される時に2つのテーブル空間が自動的に作成されます。 pg_globalテーブル空間は共有システムカタログ用に使用されます。 pg_defaultテーブル空間はtemplate1template0データベースのデフォルトテーブル空間です (したがって、CREATE DATABASETABLESPACE句で変更されない限り、このテーブル空間が同様に他のデータベースに対するデフォルトのテーブル空間になります)。

Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed. テーブル空間は、一度作成すると、要求しているユーザが十分な権限を持っていればすべてのデータベースから使用できます。 これは、テーブル空間を使用するすべてのデータベースのすべてのオブジェクトが削除されるまで、そのテーブル空間を削除できないことを意味します。

To remove an empty tablespace, use the <xref linkend="sql-droptablespace"/> command. 空のテーブル空間を削除するには、DROP TABLESPACEコマンドを使用してください。

To determine the set of existing tablespaces, examine the <link linkend="catalog-pg-tablespace"><structname>pg_tablespace</structname> </link> system catalog, for example 既存のテーブル空間群を求めるには、以下の例のようにpg_tablespaceを確認してください。

SELECT spcname FROM pg_tablespace;

The <xref linkend="app-psql"/> program's <literal>\db</literal> meta-command is also useful for listing the existing tablespaces. また、psqlプログラムの\dbメタコマンドも既存のテーブル空間を列挙する際に役に立ちます。

The directory <filename>$PGDATA/pg_tblspc</filename> contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Under no circumstances perform this operation while the server is running. Note that in PostgreSQL 9.1 and earlier you will also need to update the <structname>pg_tablespace</structname> catalog with the new locations. (If you do not, <literal>pg_dump</literal> will continue to output the old tablespace locations.) $PGDATA/pg_tblspcディレクトリには、そのクラスタで定義された非組み込みテーブル空間1つひとつを指し示すシンボリックリンクがあります。 推奨はしませんが、こうしたリンクを手作業で再定義してテーブル空間のレイアウトを調整することができます。 2点警告します。 これをサーバが実行している時に行わないでください。 PostgreSQL 9.1およびそれ以前の場合、pg_tablespaceカタログを新規の場所に更新してください。 (これを行わないと、pg_dumpは古いテーブル空間の場所を出力し続けます。)