Postgresql 中文操作指南
Synopsis
TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
Description
TRUNCATE 快速地从一组表中删除所有行。它对每个表都有相同的效果,但由于它实际上并没有扫描表,因此它更快。此外,它会立即回收磁盘空间,而不是需要后续的 VACUUM 操作。这在大型表上是最有用的。
TRUNCATE quickly removes all rows from a set of tables. It has the same effect as an unqualified DELETE 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 VACUUM operation. This is most useful on large tables.
Parameters
-
name
-
The name (optionally schema-qualified) of a table to truncate. If ONLY is specified before the table name, only that table is truncated. If ONLY is not specified, the table and all its descendant tables (if any) are truncated. Optionally, * can be specified after the table name to explicitly indicate that descendant tables are included.
-
-
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 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.
-
Notes
您必须在表上具有 TRUNCATE 权限才能截断它。
You must have the TRUNCATE privilege on a table to truncate it.
TRUNCATE 会获取它操作的每个表上的 ACCESS EXCLUSIVE 锁,它会阻塞表上的所有其他并发操作。当指定 RESTART IDENTITY 时,将要重新启动的所有序列也会被锁定。如果需要并发访问表,则应使用 DELETE 命令。
TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates on, which blocks all other concurrent operations on the table. When RESTART IDENTITY is specified, any sequences that are to be restarted are likewise locked exclusively. If concurrent access to a table is required, then the DELETE command should be used instead.
TRUNCATE 不能用于具有来自其他表的外部键引用的表,除非同时也在同一命令中截断了所有此类表。在这种情况下检查有效性将需要扫描表,而我们的目标是避免这样做。 CASCADE 选项可用于自动包含所有从属表——但在使用此选项时要非常小心,否则您可能会丢失不想丢失的数据!特别注意,当要截断的表是一个分区时,同级分区不会受到任何影响,但连锁会发生在所有引用表及其所有分区上,没有任何区别。
TRUNCATE 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 CASCADE option can be used to automatically include all dependent tables — 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 不会触发可能存在于表上的任何 ON DELETE 触发器。但它会触发 ON TRUNCATE 触发器。如果在任何表上定义了 ON TRUNCATE 触发器,则在发生任何截断之前会触发所有 BEFORE TRUNCATE 触发器,并且在执行最后一次截断和重置任何序列后会触发所有 AFTER TRUNCATE 触发器。这些触发器将按表的处理顺序触发(首先是命令中列出的表,然后是由于连锁添加的表)。
TRUNCATE will not fire any ON DELETE triggers that might exist for the tables. But it will fire ON TRUNCATE triggers. If ON TRUNCATE triggers are defined for any of the tables, then all BEFORE TRUNCATE triggers are fired before any truncation happens, and all AFTER TRUNCATE 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 不是 MVCC 安全的。截断后,如果他们使用在截断之前拍摄的快照,对于并发事务,表将显示为空。有关更多详细信息,请参见 Section 13.6 。
TRUNCATE 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 Section 13.6 for more details.
TRUNCATE 对于表中的数据是事务安全的:如果周围的事务不提交,则截断将安全回滚。
TRUNCATE 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.
当指定 RESTART IDENTITY 时,隐式的 ALTER SEQUENCE RESTART 操作也会以事务方式完成;也就是说,如果周围的事务不提交,它们将被回滚。请注意,如果在事务回滚之前对重新启动的序列执行任何其他序列操作,这些操作对序列的影响将被回滚,但它们对 currval() 的影响不会被回滚;也就是说,在事务 currval() 之后,仍会继续反映在失败事务中获取的最后一个序列值,即使该序列本身可能不再与其一致。这类似于失败事务后的 currval() 的通常行为。
When RESTART IDENTITY is specified, the implied ALTER SEQUENCE RESTART 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 currval(); that is, after the transaction currval() 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 currval() after a failed transaction.
TRUNCATE 可用于外部表(如果外部数据包装器支持),例如,请参见 postgres_fdw 。
TRUNCATE can be used for foreign tables if supported by the foreign data wrapper, for instance, see postgres_fdw.
Examples
删减表@ {s0} 和 @ {s1}:
Truncate the tables bigtable and fattable:
TRUNCATE bigtable, fattable;
同样地,重置任何关联的序列发生器:
The same, and also reset any associated sequence generators:
TRUNCATE bigtable, fattable RESTART IDENTITY;
删减表@ {s2},并级联到通过外键约束引用 @ {s3} 的任何表:
Truncate the table othertable, and cascade to any tables that reference othertable via foreign-key constraints:
TRUNCATE othertable CASCADE;
Compatibility
SQL:2008 标准包括具有语法 @ {s5} 的 TRUNCATE 命令。从句 @ {s6}/ RESTART IDENTITY 也出现在该标准中,但具有略有不同的相关含义。该命令的某些并发行为由标准定义为实现,因此,如果需要,应考虑上述注意事项并将其与其他实现进行比较。
The SQL:2008 standard includes a TRUNCATE command with the syntax TRUNCATE TABLE _tablename_. The clauses CONTINUE IDENTITY/RESTART IDENTITY 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.