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

TRUNCATE

TRUNCATE <refpurpose>empty a table or set of tables</refpurpose> — 1テーブルまたはテーブル群を空にする

概要

TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
    [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]

説明

<title>Description</title>

<command>TRUNCATE</command> quickly removes all rows from a set of tables. It has the same effect as an unqualified <command>DELETE</command> on each table, but since it does not actually scan the tables it is faster. Furthermore, it reclaims disk space immediately, rather than requiring a subsequent <command>VACUUM</command> operation. This is most useful on large tables. TRUNCATEはテーブル群から全ての行を素早く削除します。 各テーブルに対して条件指定のないDELETEコマンドの実行と同じ効果を持ちますが、実際にテーブルを走査しない分、このコマンドの方が高速です。 さらに、その後にVACUUM操作を行うことなく、このコマンドはディスク領域を即座に回収します。 このコマンドは、大きなテーブルを対象とする場合に最も有用です。

パラメータ

<title>Parameters</title>
name

The name (optionally schema-qualified) of a table to truncate. If <literal>ONLY</literal> is specified before the table name, only that table is truncated. If <literal>ONLY</literal> is not specified, the table and all its descendant tables (if any) are truncated. Optionally, <literal>*</literal> can be specified after the table name to explicitly indicate that descendant tables are included. 空にするテーブルの名前です(スキーマ修飾名も可)。 テーブル名の前にONLYが指定されている場合、そのテーブルのみを空にします。 ONLYが指定されていない場合、そのテーブルとそのすべての子テーブル(もしあれば)を空にします。 オプションで、テーブル名の後に*を指定することで、明示的に継承するテーブルも含まれることを示すことができます。

RESTART IDENTITY

Automatically restart sequences owned by columns of the truncated table(s). 消去されるテーブルの列により所有されるシーケンスを自動的に再開始させます。

CONTINUE IDENTITY

Do not change the values of sequences. This is the default. シーケンスの値を変更しません。これがデフォルトです。

CASCADE

Automatically truncate all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to <literal>CASCADE</literal>. 指定されたテーブル、または、CASCADEにより削除対象テーブルとされたテーブルを参照する外部キーを持つテーブルすべてを自動的に空にします。

RESTRICT

Refuse to truncate if any of the tables have foreign-key references from tables that are not listed in the command. This is the default. 外部キーにより対象のテーブルを参照するテーブルのいずれかがこのコマンドで指定されていない場合、操作を拒否します。 これがデフォルトです。

注釈

<title>Notes</title>

You must have the <literal>TRUNCATE</literal> privilege on a table to truncate it. テーブルを空にするためにはそのテーブルにTRUNCATE権限を持たなければなりません。

<command>TRUNCATE</command> acquires an <literal>ACCESS EXCLUSIVE</literal> lock on each table it operates on, which blocks all other concurrent operations on the table. When <literal>RESTART IDENTITY</literal> is specified, any sequences that are to be restarted are likewise locked exclusively. If concurrent access to a table is required, then the <command>DELETE</command> command should be used instead. TRUNCATEは操作対象の各テーブルに対するACCESS EXCLUSIVEロックを獲得します。 これは、この他のそのテーブルに対する同時操作をすべてブロックします。 RESTART IDENTITYが指定された場合、初期化対象のシーケンスがあると、それは同様に排他ロックされます。 テーブルへの同時アクセスが必要ならば、代わりに DELETEコマンドを使用しなければなりません。

<command>TRUNCATE</command> cannot be used on a table that has foreign-key references from other tables, unless all such tables are also truncated in the same command. Checking validity in such cases would require table scans, and the whole point is not to do one. The <literal>CASCADE</literal> option can be used to automatically include all dependent tables &mdash; but be very careful when using this option, or else you might lose data you did not intend to! Note in particular that when the table to be truncated is a partition, siblings partitions are left untouched, but cascading occurs to all referencing tables and all their partitions with no distinction. そのテーブルが他のテーブルから外部キーで参照されている場合、その同じコマンドでそれらのテーブルをすべて空にするように指定していない限り、TRUNCATEを使用することはできません。 このような場合に有効性を検査するならばテーブルスキャンが必要になりますが、テーブルスキャンを行うのであれば、このコマンドの利点がなくなるからです。 CASCADEオプションを使用して、自動的にすべての依存テーブルを含めることができます。 しかし、意図しないデータ損失の可能性がありますので、このオプションを使用する時には十分に注意してください。 空にするテーブルがパーティションの場合、兄弟のパーティションには手をつけませんが、参照しているテーブルすべてとそのパーティションすべてに対しては、区別することなくカスケードが起こります。

<command>TRUNCATE</command> will not fire any <literal>ON DELETE</literal> triggers that might exist for the tables. But it will fire <literal>ON TRUNCATE</literal> triggers. If <literal>ON TRUNCATE</literal> triggers are defined for any of the tables, then all <literal>BEFORE TRUNCATE</literal> triggers are fired before any truncation happens, and all <literal>AFTER TRUNCATE</literal> triggers are fired after the last truncation is performed and any sequences are reset. The triggers will fire in the order that the tables are to be processed (first those listed in the command, and then any that were added due to cascading). TRUNCATEは、テーブルにON DELETEトリガがあっても、それを発行しません。 しかし、ON TRUNCATEトリガを発行します。 テーブルのいずれかにON TRUNCATEトリガが定義されている場合、何らかの消去が行われる前にすべてのBEFORE TRUNCATEトリガが発行されます。 また、最後の消去がなされ、シーケンスが初期化された後すべてのAFTER TRUNCATEトリガが発行されます。 トリガは処理されるテーブルの順番(コマンドに列挙されたものが先、その後にカスケードのために追加されたもの)に発行されます。

<command>TRUNCATE</command> is not MVCC-safe. After truncation, the table will appear empty to concurrent transactions, if they are using a snapshot taken before the truncation occurred. See <xref linkend="mvcc-caveats"/> for more details. TRUNCATEはMVCC的に安全ではありません。 同時実行中のトランザクションが、削除の前に取得したスナップショットを使っている場合、削除の後、テーブルはそのトランザクションからは空に見えます。 (詳しくは13.6を参照してください。)

<command>TRUNCATE</command> is transaction-safe with respect to the data in the tables: the truncation will be safely rolled back if the surrounding transaction does not commit. テーブル内のデータという観点では、TRUNCATEはトランザクション的に安全です。 前後のトランザクションがコミットされなければ消去は安全にロールバックされます。

When <literal>RESTART IDENTITY</literal> is specified, the implied <command>ALTER SEQUENCE RESTART</command> operations are also done transactionally; that is, they will be rolled back if the surrounding transaction does not commit. Be aware that if any additional sequence operations are done on the restarted sequences before the transaction rolls back, the effects of these operations on the sequences will be rolled back, but not their effects on <function>currval()</function>; that is, after the transaction <function>currval()</function> will continue to reflect the last sequence value obtained inside the failed transaction, even though the sequence itself may no longer be consistent with that. This is similar to the usual behavior of <function>currval()</function> after a failed transaction. RESTART IDENTITYが指定された場合、暗黙的にALTER SEQUENCE RESTART操作がトランザクション的に行われます。 つまりそれを囲むトランザクションがコミットされなければ、ロールバックされます。 トランザクションがロールバックされる前に、初期化したシーケンスに対してさらにシーケンス操作を行う場合には注意してください。 シーケンスに対するこれらの操作の影響はロールバックされますが、currval()への影響はロールバックされません。 つまりトランザクションの後、currval()は、シーケンス自体と値とが一貫性のない状態になっていたとしても、失敗したトランザクションの内側で得た最後のシーケンス値を継続して反映します。 これは、失敗したトランザクションの後のcurrval()の通常の動作と同じです。

<command>TRUNCATE</command> can be used for foreign tables if supported by the foreign data wrapper, for instance, see <xref linkend="postgres-fdw"/>. TRUNCATEは、外部データラッパーによりサポートされていれば、外部テーブルに対して使えます。例えば、postgres_fdwを参照してください。

<title>Examples</title>

Truncate the tables <literal>bigtable</literal> and <literal>fattable</literal>: bigtableテーブルおよびfattableテーブルを空にします。

TRUNCATE bigtable, fattable;

The same, and also reset any associated sequence generators: 以下も同じですが、ここでは関連するシーケンスジェネレータをすべてリセットします。

TRUNCATE bigtable, fattable RESTART IDENTITY;

Truncate the table <literal>othertable</literal>, and cascade to any tables that reference <literal>othertable</literal> via foreign-key constraints: othertableテーブル、および、外部キー制約によりothertableを参照するすべてのテーブルを空にします。

TRUNCATE othertable CASCADE;

互換性

<title>Compatibility</title>

The SQL:2008 standard includes a <command>TRUNCATE</command> command with the syntax <literal>TRUNCATE TABLE <replaceable>tablename</replaceable></literal>. The clauses <literal>CONTINUE IDENTITY</literal>/<literal>RESTART IDENTITY</literal> also appear in that standard, but have slightly different though related meanings. Some of the concurrency behavior of this command is left implementation-defined by the standard, so the above notes should be considered and compared with other implementations if necessary. 標準SQL:2008には、TRUNCATE TABLE tablenameという構文のTRUNCATEコマンドが含まれます。 CONTINUE IDENTITY/RESTART IDENTITY句も標準に記載され、関連してはいるのですが、若干異なります。 標準では、このコマンドの同時実行に関する動作の一部は実装に依存するものとされています。 このため、上記注釈を検討し、必要に応じて他の実装と比べなければなりません。

関連項目

<title>See Also</title> DELETE