All Products
Search
Document Center

PolarDB:Truncate a partition

Last Updated:Aug 22, 2024

In PolarDB for PostgreSQL (Compatible with Oracle), you can truncate a partition, as you would truncate a table. This topic describes the syntax and provides a sample statement.

Syntax

ALTER TABLE <table_name> TRUNCATE PARTITION <partition_name>;
ALTER TABLE <table_name> TRUNCATE SUBPARTITION <subpartition_name>;

The TRUNCATE operation deletes all rows of the specified table, which is similar to DELETE. However, TRUNCATE may be faster because it doesn't scan the table.

After rows of a table are deleted using TRUNCATE, disk space is immediately reclaimed, without the need for you to perform a VACUUM operation.

Example

Truncate the p1 partition.

CREATE TABLE hash_partitions_sales (deptno NUMBER, deptname VARCHAR(32))
     PARTITION BY HASH(deptno)
       (PARTITION p1 , PARTITION p2 ,
        PARTITION p3 , PARTITION p4 , PARTITION p5);

ALTER TABLE hash_partitions_sales TRUNCATE PARTITION p1 ;