TRUNCATE(7)

πŸ“¦ version 14.24, in opensuse-leap 16.0

TRUNCATE(7) πŸ’…βœ¨

Okay so bestie, listen up β€” this page is all about the TRUNCATE command from the PostgreSQL 14.24 Documentation. It's giving "delete everything but make it fashion" and honestly? Iconic. Let's get into it.


πŸŽ€ NAME πŸŽ€

TRUNCATE β€” empty a table or set of tables. Literally just yeets all the rows out. Poof. Gone. Bye. πŸ’¨


✨ SYNOPSIS ✨

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

That's the whole vibe. Now let's break it down so you actually know what you're doing, bestie. 🫢


🌸 DESCRIPTION 🌸

So like, TRUNCATE quickly removes all rows from a set of tables. It has the same effect as an unqualified DELETE on each table, but here's the slay: since it does not actually scan the tables, it's faster. No cap.

And it gets better β€” it reclaims disk space immediately, rather than requiring a subsequent VACUUM operation. The way that's such a win. This is most useful on large tables, fr fr. πŸ”₯


πŸ’– 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. It's giving "we're doing the whole family tree or just you?" 🌳
  • RESTART IDENTITY β€” Automatically restart sequences owned by columns of the truncated table(s). Like a factory reset for your ID counters. πŸ”„
  • CONTINUE IDENTITY β€” Do not change the values of sequences. This is the default. It's the "keep calm and carry on" option. πŸ’…
  • 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. It's the "and everyone associated with them" option. We love a thorough queen. πŸ‘‘
  • 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. Safe, sensible, the responsible friend in the group chat. πŸ™ˆ

πŸ¦‹ NOTES πŸ¦‹

Okay, this is where it gets real, so pay attention bestie. πŸ‘€

You must have the TRUNCATE privilege on a table to truncate it. No privilege, no truncation. Simple as that.

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. Basically, TRUNCATE is that friend who needs everyone's full attention and will not be interrupted. πŸ’€

⚠️ 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. It's giving "one wrong move and it's all gone." 🚨

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). So there is a whole order of operations moment happening, okay? 🎬

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.5 for more details. It's the "what happens in truncate stays in truncate" situation. πŸͺž

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. So like, it's reversible if you play your cards right. πŸ’–

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. This is literally the most confusing part of the whole page but it's important so read it twice. πŸ˜΅β€πŸ’«

TRUNCATE can be used for foreign tables if supported by the foreign data wrapper, for instance, see postgres_fdw. International slay. 🌍


⭐ EXAMPLES ⭐

Truncate the tables bigtable and fattable:

TRUNCATE bigtable, fattable;

The same, and also reset any associated sequence generators:

TRUNCATE bigtable, fattable RESTART IDENTITY;

Truncate the table othertable, and cascade to any tables that reference othertable via foreign-key constraints:

TRUNCATE othertable CASCADE;

See? Simple. Clean. Devastating in the best way. πŸ’…


🌈 COMPATIBILITY 🌈

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. TL;DR: the standard has it, but we do it our way, and that's okay. πŸŽ€


🌸 SEE ALSO 🌸

DELETE(7) β€” the other way to clear things out, but slower and more of a whole process. Check it out if you need the aesthetic of a full table scan. πŸ¦‹


Okay bestie, that's the whole page! This deep dive into TRUNCATE was brought to you by PostgreSQL 14.24 Documentation πŸ’œ β€” remember: truncate responsibly, and maybe don't CASCADE unless you're feeling brave. You got this! πŸ‘‘βœ¨

πŸ“¦ where this page comes from