{"Entry":{"collection":"sql","key":"truncate","name":"TRUNCATE","aliases":["truncate"],"metadata":{"aliases":["truncate"],"changed_in":["8.1","8.2","8.4"],"changes":[{"from":"6.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"added","synopsis":null,"to":"7.0"},{"from":"7.0","purpose_changed":false,"renamed":{"from_file":"sql-truncate.htm","to_file":"sql-truncate.html"},"sections":{"added":[],"changed":[],"removed":[]},"status":"changed","synopsis":null,"to":"7.1"},{"from":"7.1","purpose_changed":true,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"7.2"},{"from":"7.3","purpose_changed":false,"renamed":null,"sections":{"added":["parameters","notes","examples"],"changed":["description","compatibility"],"removed":["usage"]},"status":"changed","synopsis":null,"to":"7.4"},{"from":"8.0","purpose_changed":true,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples"],"removed":[]},"status":"changed","synopsis":{"added":["TRUNCATE [ TABLE ] name [, ...]"],"removed":[]},"to":"8.1"},{"from":"8.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes","examples"],"removed":[]},"status":"changed","synopsis":{"added":["TRUNCATE [ TABLE ] name [, ...] [ CASCADE | RESTRICT ]"],"removed":[]},"to":"8.2"},{"from":"8.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","notes","examples"],"removed":[]},"status":"changed","synopsis":null,"to":"8.3"},{"from":"8.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]","[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]"],"removed":[]},"to":"8.4"},{"from":"8.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.0"},{"from":"9.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"9.1"},{"from":"9.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.5"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":["see_also"],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"10"},{"from":"11","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"12"},{"from":"13","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"14"},{"from":"14","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"15"}],"content_hash":"f228e2b38919a6276cc84778d946942c80d007c637047472b2f42e26142db239","editorial":{},"first_version":"7.0","group":"table","imported_at":"2026-09-30T17:43:35.853044+08:00","last_version":"20","name":"TRUNCATE","object":"","position":0,"present_in":["7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"empty a table or set of tables","purpose_zh":"","related":["delete"],"slug":"truncate","source_rev":"a709ab85","synopsis":"TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]\n[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]","verb":"TRUNCATE"}},"Definition":{"Collection":"sql","Key":"truncate","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"truncate","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-TRUNCATE","file":"sql-truncate.html","lang":"en","name":"TRUNCATE","purpose":"empty a table or set of tables","purpose_zh":"","related":["delete"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e quickly removes all rows from a set of tables. It has the same effect as an unqualified \u003ccode class=\"command\"\u003eDELETE\u003c/code\u003e 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 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e operation. This is most useful on large tables.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name (optionally schema-qualified) of a table to truncate. If \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e is specified before the table name, only that table is truncated. If \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e is not specified, the table and all its descendant tables (if any) are truncated. Optionally, \u003ccode class=\"literal\"\u003e*\u003c/code\u003e can be specified after the table name to explicitly indicate that descendant tables are included.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESTART IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eAutomatically restart sequences owned by columns of the truncated table(s).\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCONTINUE IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eDo not change the values of sequences. This is the default.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eAutomatically truncate all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to \u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESTRICT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eRefuse to truncate if any of the tables have foreign-key references from tables that are not listed in the command. This is the default.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003eYou must have the \u003ccode class=\"literal\"\u003eTRUNCATE\u003c/code\u003e privilege on a table to truncate it.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e acquires an \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e lock on each table it operates on, which blocks all other concurrent operations on the table. When \u003ccode class=\"literal\"\u003eRESTART IDENTITY\u003c/code\u003e is specified, any sequences that are to be restarted are likewise locked exclusively. If concurrent access to a table is required, then the \u003ccode class=\"command\"\u003eDELETE\u003c/code\u003e command should be used instead.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e 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 \u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e 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.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e will not fire any \u003ccode class=\"literal\"\u003eON DELETE\u003c/code\u003e triggers that might exist for the tables. But it will fire \u003ccode class=\"literal\"\u003eON TRUNCATE\u003c/code\u003e triggers. If \u003ccode class=\"literal\"\u003eON TRUNCATE\u003c/code\u003e triggers are defined for any of the tables, then all \u003ccode class=\"literal\"\u003eBEFORE TRUNCATE\u003c/code\u003e triggers are fired before any truncation happens, and all \u003ccode class=\"literal\"\u003eAFTER TRUNCATE\u003c/code\u003e 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).\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e 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 \u003ca href=\"/docs/18/mvcc-caveats.html\" title=\"13.6. Caveats\"\u003eSection 13.6\u003c/a\u003e for more details.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e 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.\u003c/p\u003e\u003cp\u003eWhen \u003ccode class=\"literal\"\u003eRESTART IDENTITY\u003c/code\u003e is specified, the implied \u003ccode class=\"command\"\u003eALTER SEQUENCE RESTART\u003c/code\u003e 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 \u003ccode class=\"function\"\u003ecurrval()\u003c/code\u003e; that is, after the transaction \u003ccode class=\"function\"\u003ecurrval()\u003c/code\u003e 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 \u003ccode class=\"function\"\u003ecurrval()\u003c/code\u003e after a failed transaction.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e can be used for foreign tables if supported by the foreign data wrapper, for instance, see \u003ca href=\"/docs/18/postgres-fdw.html\" title=\"F.38. postgres_fdw — access data stored in external PostgreSQL servers\"\u003epostgres_fdw\u003c/a\u003e.\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eTruncate the tables \u003ccode class=\"literal\"\u003ebigtable\u003c/code\u003e and \u003ccode class=\"literal\"\u003efattable\u003c/code\u003e:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eTRUNCATE bigtable, fattable;\n\u003c/pre\u003e\u003cp\u003eThe same, and also reset any associated sequence generators:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eTRUNCATE bigtable, fattable RESTART IDENTITY;\n\u003c/pre\u003e\u003cp\u003eTruncate the table \u003ccode class=\"literal\"\u003eothertable\u003c/code\u003e, and cascade to any tables that reference \u003ccode class=\"literal\"\u003eothertable\u003c/code\u003e via foreign-key constraints:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eTRUNCATE othertable CASCADE;\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003eThe SQL:2008 standard includes a \u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e command with the syntax \u003ccode class=\"literal\"\u003eTRUNCATE TABLE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e. The clauses \u003ccode class=\"literal\"\u003eCONTINUE IDENTITY\u003c/code\u003e/\u003ccode class=\"literal\"\u003eRESTART IDENTITY\u003c/code\u003e 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.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/delete/?v=18\" title=\"DELETE\"\u003e\u003cspan class=\"refentrytitle\"\u003eDELETE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"TRUNCATE [ TABLE ] [ ONLY ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [ * ] [, ... ]\n    [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]","synopsis_text":"TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]\n[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"truncate","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"TRUNCATE","Summary":"清空一个表或一组表","BodyHTML":"\u003cpre\u003eTRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]\n[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eTRUNCATE\u003c/code\u003e可以快速移除一组表中的所有行。它的效果与对每个表执行不带条件的\u003ccode\u003eDELETE\u003c/code\u003e相同，但由于它并不实际扫描这些表，因此速度更快。此外，它会立即回收磁盘空间，而不是要求后续再执行一次\u003ccode\u003eVACUUM\u003c/code\u003e操作。这一点对于大表尤其有用。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要截断的表名（可以是模式限定的）。如果在表名前指定了 \u003ccode\u003eONLY\u003c/code\u003e，则只截断该表。如果未指定\u003ccode\u003eONLY\u003c/code\u003e，则该表及其所有后代表（如果有）都会被截断。也可以在表名后指定 \u003ccode\u003e*\u003c/code\u003e，以显式表明包含后代表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eRESTART IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e自动重新启动由被截断表的列拥有的 sequence。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eCONTINUE IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e不更改 sequence 的值。这是默认值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eCASCADE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e自动截断所有通过外键引用指定表的表，以及通过外键引用因 \u003ccode\u003eCASCADE\u003c/code\u003e而加入截断范围的表的其他表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eRESTRICT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果任一表具有来自命令中未列出表的外键引用，则拒绝截断。这是默认值。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e要截断一个表，你必须拥有该表上的\u003ccode\u003eTRUNCATE\u003c/code\u003e权限。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eTRUNCATE\u003c/code\u003e会在其操作的每个表上获取 \u003ccode\u003eACCESS EXCLUSIVE\u003c/code\u003e锁，这会阻塞该表上的所有其他并发操作。当指定\u003ccode\u003eRESTART IDENTITY\u003c/code\u003e时，任何需要重新启动的 sequence 也会同样被排他锁定。如果需要对某个表进行并发访问，则应改用 \u003ccode\u003eDELETE\u003c/code\u003e命令。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eTRUNCATE\u003c/code\u003e不能用于被其他表通过外键引用的表，除非所有这类表也在同一条命令中被截断。因为在这种情况下检查有效性将需要扫描表，而该命令的意义恰恰在于避免这样做。\u003ccode\u003eCASCADE\u003c/code\u003e 选项可用于自动包含所有依赖表 — 但使用该选项时一定要非常小心，否则你可能会丢失并非有意删除的数据！特别要注意的是，当要截断的表是一个分区时，其同级分区不会受到影响，但级联会无差别地扩展到所有引用它的表以及这些表的全部分区。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eTRUNCATE\u003c/code\u003e不会引发这些表上可能存在的任何 \u003ccode\u003eON DELETE\u003c/code\u003e触发器，但会引发 \u003ccode\u003eON TRUNCATE\u003c/code\u003e触发器。如果这些表中的任何一个定义了\u003ccode\u003eON TRUNCATE\u003c/code\u003e触发器，那么所有 \u003ccode\u003eBEFORE TRUNCATE\u003c/code\u003e触发器都会在任何截断发生之前引发，而所有\u003ccode\u003eAFTER TRUNCATE\u003c/code\u003e触发器都会在最后一次截断完成且所有 sequence 被重置之后引发。触发器会按照表的处理顺序引发（先是命令中列出的表，然后是由于级联而加入的表）。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eTRUNCATE\u003c/code\u003e不是多版本并发控制（MVCC）安全的。截断之后，如果并发事务使用的是在截断发生前取得的快照，该表对这些并发事务而言将表现为空。详见\u003ca href=\"/docs/18/mvcc-caveats.html\" rel=\"nofollow\"\u003e第 13.6 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e就表中数据而言，\u003ccode\u003eTRUNCATE\u003c/code\u003e是事务安全的：如果外围事务没有提交，截断将被安全地回滚。\u003c/p\u003e\u003cp\u003e当指定\u003ccode\u003eRESTART IDENTITY\u003c/code\u003e时，隐含的 \u003ccode\u003eALTER SEQUENCE RESTART\u003c/code\u003e操作也会以事务方式执行；也就是说，如果外围事务没有提交，它们也会被回滚。请注意，如果在事务回滚前又对这些重启后的 sequence 执行了额外操作，这些操作对 sequence 本身的影响会被回滚，但对\u003ccode\u003ecurrval()\u003c/code\u003e的影响不会被回滚。也就是说，事务结束后，\u003ccode\u003ecurrval()\u003c/code\u003e仍将反映失败事务内取得的最后一个 sequence 值，即使 sequence 本身可能已经不再与之保持一致。这与失败事务之后\u003ccode\u003ecurrval()\u003c/code\u003e的通常行为类似。\u003c/p\u003e\u003cp\u003e如果外部数据包装器支持，\u003ccode\u003eTRUNCATE\u003c/code\u003e也可以用于外部表，例如参见\u003ca href=\"/docs/18/postgres-fdw.html\" rel=\"nofollow\"\u003epostgres_fdw\u003c/a\u003e。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e截断表\u003ccode\u003ebigtable\u003c/code\u003e和 \u003ccode\u003efattable\u003c/code\u003e：\u003c/p\u003e\u003cpre\u003eTRUNCATE bigtable, fattable;\n\u003c/pre\u003e\u003cp\u003e同样的操作，并重置所有相关联的 sequence 生成器：\u003c/p\u003e\u003cpre\u003eTRUNCATE bigtable, fattable RESTART IDENTITY;\n\u003c/pre\u003e\u003cp\u003e截断表\u003ccode\u003eothertable\u003c/code\u003e，并级联到任何通过外键约束引用\u003ccode\u003eothertable\u003c/code\u003e的表：\u003c/p\u003e\u003cpre\u003eTRUNCATE othertable CASCADE;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003eSQL:2008 标准包括了一个\u003ccode\u003eTRUNCATE\u003c/code\u003e命令，语法是\u003ccode\u003eTRUNCATE TABLE \u003cem\u003e\u003ccode\u003etablename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e。子句 \u003ccode\u003eCONTINUE IDENTITY\u003c/code\u003e/\u003ccode\u003eRESTART IDENTITY\u003c/code\u003e 在该标准中也有出现，但其含义虽相关却略有不同。该命令的某些并发行为在标准中被留作实现定义，因此必要时应结合上述注解并与其他实现进行比较。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/wiki/sql/delete/?v=18\" title=\"DELETE\" rel=\"nofollow\"\u003e\u003cspan\u003eDELETE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"735f617c7a3fc8e3151a3a7b104592855903df6a49c605f8b365ef910042d33b","Payload":{"purpose_zh":"清空一个表或一组表","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e可以快速移除一组表中的所有行。它的效果与对每个表执行不带条件的\u003ccode class=\"command\"\u003eDELETE\u003c/code\u003e相同，但由于它并不实际扫描这些表，因此速度更快。此外，它会立即回收磁盘空间，而不是要求后续再执行一次\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e操作。这一点对于大表尤其有用。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要截断的表名（可以是模式限定的）。如果在表名前指定了 \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e，则只截断该表。如果未指定\u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e，则该表及其所有后代表（如果有）都会被截断。也可以在表名后指定 \u003ccode class=\"literal\"\u003e*\u003c/code\u003e，以显式表明包含后代表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESTART IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e自动重新启动由被截断表的列拥有的 sequence。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCONTINUE IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e不更改 sequence 的值。这是默认值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e自动截断所有通过外键引用指定表的表，以及通过外键引用因 \u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e而加入截断范围的表的其他表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESTRICT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果任一表具有来自命令中未列出表的外键引用，则拒绝截断。这是默认值。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e要截断一个表，你必须拥有该表上的\u003ccode class=\"literal\"\u003eTRUNCATE\u003c/code\u003e权限。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e会在其操作的每个表上获取 \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e锁，这会阻塞该表上的所有其他并发操作。当指定\u003ccode class=\"literal\"\u003eRESTART IDENTITY\u003c/code\u003e时，任何需要重新启动的 sequence 也会同样被排他锁定。如果需要对某个表进行并发访问，则应改用 \u003ccode class=\"command\"\u003eDELETE\u003c/code\u003e命令。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e不能用于被其他表通过外键引用的表，除非所有这类表也在同一条命令中被截断。因为在这种情况下检查有效性将需要扫描表，而该命令的意义恰恰在于避免这样做。\u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e 选项可用于自动包含所有依赖表 — 但使用该选项时一定要非常小心，否则你可能会丢失并非有意删除的数据！特别要注意的是，当要截断的表是一个分区时，其同级分区不会受到影响，但级联会无差别地扩展到所有引用它的表以及这些表的全部分区。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e不会引发这些表上可能存在的任何 \u003ccode class=\"literal\"\u003eON DELETE\u003c/code\u003e触发器，但会引发 \u003ccode class=\"literal\"\u003eON TRUNCATE\u003c/code\u003e触发器。如果这些表中的任何一个定义了\u003ccode class=\"literal\"\u003eON TRUNCATE\u003c/code\u003e触发器，那么所有 \u003ccode class=\"literal\"\u003eBEFORE TRUNCATE\u003c/code\u003e触发器都会在任何截断发生之前引发，而所有\u003ccode class=\"literal\"\u003eAFTER TRUNCATE\u003c/code\u003e触发器都会在最后一次截断完成且所有 sequence 被重置之后引发。触发器会按照表的处理顺序引发（先是命令中列出的表，然后是由于级联而加入的表）。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e不是多版本并发控制（MVCC）安全的。截断之后，如果并发事务使用的是在截断发生前取得的快照，该表对这些并发事务而言将表现为空。详见\u003ca href=\"/docs/18/mvcc-caveats.html\" title=\"13.6. 注意事项\"\u003e第 13.6 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e就表中数据而言，\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e是事务安全的：如果外围事务没有提交，截断将被安全地回滚。\u003c/p\u003e\u003cp\u003e当指定\u003ccode class=\"literal\"\u003eRESTART IDENTITY\u003c/code\u003e时，隐含的 \u003ccode class=\"command\"\u003eALTER SEQUENCE RESTART\u003c/code\u003e操作也会以事务方式执行；也就是说，如果外围事务没有提交，它们也会被回滚。请注意，如果在事务回滚前又对这些重启后的 sequence 执行了额外操作，这些操作对 sequence 本身的影响会被回滚，但对\u003ccode class=\"function\"\u003ecurrval()\u003c/code\u003e的影响不会被回滚。也就是说，事务结束后，\u003ccode class=\"function\"\u003ecurrval()\u003c/code\u003e仍将反映失败事务内取得的最后一个 sequence 值，即使 sequence 本身可能已经不再与之保持一致。这与失败事务之后\u003ccode class=\"function\"\u003ecurrval()\u003c/code\u003e的通常行为类似。\u003c/p\u003e\u003cp\u003e如果外部数据包装器支持，\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e也可以用于外部表，例如参见\u003ca href=\"/docs/18/postgres-fdw.html\" title=\"F.38. postgres_fdw — 访问存储在外部 PostgreSQL 服务器中的数据\"\u003epostgres_fdw\u003c/a\u003e。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e截断表\u003ccode class=\"literal\"\u003ebigtable\u003c/code\u003e和 \u003ccode class=\"literal\"\u003efattable\u003c/code\u003e：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eTRUNCATE bigtable, fattable;\n\u003c/pre\u003e\u003cp\u003e同样的操作，并重置所有相关联的 sequence 生成器：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eTRUNCATE bigtable, fattable RESTART IDENTITY;\n\u003c/pre\u003e\u003cp\u003e截断表\u003ccode class=\"literal\"\u003eothertable\u003c/code\u003e，并级联到任何通过外键约束引用\u003ccode class=\"literal\"\u003eothertable\u003c/code\u003e的表：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eTRUNCATE othertable CASCADE;\n\u003c/pre\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003eSQL:2008 标准包括了一个\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e命令，语法是\u003ccode class=\"literal\"\u003eTRUNCATE TABLE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e。子句 \u003ccode class=\"literal\"\u003eCONTINUE IDENTITY\u003c/code\u003e/\u003ccode class=\"literal\"\u003eRESTART IDENTITY\u003c/code\u003e 在该标准中也有出现，但其含义虽相关却略有不同。该命令的某些并发行为在标准中被留作实现定义，因此必要时应结合上述注解并与其他实现进行比较。\u003c/p\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/delete/?v=18\" title=\"DELETE\"\u003e\u003cspan class=\"refentrytitle\"\u003eDELETE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"TRUNCATE [ TABLE ] [ ONLY ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [ * ] [, ... ]\n    [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]","synopsis_text":"TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]\n[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
