You can drop a partition in a partitioned table as needed. When you drop a partition, the definition and data in the partition are also dropped.
Support for deleting a partition in a partitioned table
In Oracle-compatible mode of OceanBase Database:
For a partitioned table, you can drop a RANGE partition or a LIST partition, but you cannot drop a HASH partition.
For a subpartitioned table, you can drop a RANGE or LIST subpartition.
The following table describes the support for deleting a partition in a partitioned table in Oracle-compatible mode of OceanBase Database.
Partitioned table |
Partition type |
Drop a partition |
Drop a subpartition |
|---|---|---|---|
| Partitioned table | RANGE / LIST | Supported | - |
| Partitioned table | HASH | Not supported | - |
| Subpartitioned table | RANGE + RANGE / RANGE + LIST | Supported | Supported |
| Subpartitioned table | RANGE + HASH | Supported | Not supported |
| Subpartitioned table | LIST + RANGE / LIST + LIST | Supported | Supported |
| Subpartitioned table | LIST + HASH | Supported | Not supported |
| Subpartitioned table | HASH + RANGE / HASH + LIST | Not supported | Supported |
| Subpartitioned table | HASH + HASH | Not supported | Not supported |
Considerations
When you drop a partition, make sure that no active transaction or query is running on the partition. Otherwise, an error may be returned when you execute an SQL statement, or an unexpected situation may occur. You can query the oceanbase.GV$OB_TRANSACTION_PARTICIPANTS view in the sys tenant to obtain the status of transactions that are not yet completed.
Impact on global indexes
In Oracle-compatible mode, when you drop a partition in a partitioned table that has global indexes, you must specify the UPDATE GLOBAL INDEXES clause in the ALTER TABLE statement. If you do not specify this clause, the global indexes on the partitioned table will be unavailable after the partition is dropped.
For a table that is partitioned by RANGE, INTERVAL, or LIST (including partitioned and subpartitioned tables) and supports partition deletion, when you specify the UPDATE GLOBAL INDEXES clause, the system does not rebuild the global indexes. Instead, it uses a lazy maintenance strategy to keep the indexes valid by deleting data in the background. This avoids query interruptions or performance degradation caused by index invalidation. Note that this optimization applies only to regular global indexes. The following scenarios cannot trigger this optimization:
- The partitioned table is partitioned by HASH (even if the subpartitions are RANGE or LIST). In this case, you cannot truncate a subpartition.
- The partitioning key is an expression, not a regular column.
Note
For V4.4.2, the INTERVAL partitioned table supports the UPDATE GLOBAL INDEXES clause starting from V4.4.2 BP1.
Hidden configurations for controlling global index behavior during partition DDL
For V4.3.5, OceanBase Database introduces a tenant-level hidden configuration item _ob_enable_truncate_partition_preserve_global_index starting from V4.3.5 BP2. This configuration item controls whether to allow global indexes to remain valid during TRUNCATE/DROP operations on a partition (referred to as partition DDL). For upgraded tenants, the default value is False, and for newly created tenants, the default value is True. The behavior is as follows:
Configuration item value |
Behavior during partition DDL execution |
Result after partition DDL execution |
|---|---|---|
| True | Only the TRUNCATE/DROP partition does not allow DML operations. Other partitions of the table can perform DML operations. |
Global indexes remain valid: DML operations can be performed on the table and all index tables, and the global indexes related to the table remain valid. |
| False | Only the TRUNCATE/DROP partition does not allow DML operations. Other partitions of the table can perform DML operations. |
Global indexes become invalid: DML operations can be performed on the table and all index tables, but the global indexes related to the table become invalid or are rebuilt. |
Examples:
Indexes remain valid.
obclient> ALTER SYSTEM SET "_ob_enable_truncate_partition_preserve_global_index" = True;Indexes become invalid.
obclient> ALTER SYSTEM SET "_ob_enable_truncate_partition_preserve_global_index" = False;
Impact on concurrent DDL/DML/queries
Dropping a partition is an offline DDL operation. During the partition drop, OceanBase Database adds an exclusive lock at the partition level on the target partition.
Operation type |
Impact during partition drop |
|---|---|
| Concurrent DDL | Prohibited or blocked. Any other DDL operation on the same table cannot be executed concurrently. |
| Concurrent DML |
|
Concurrent query (SELECT) |
Not affected. Can be executed normally. |
Drop a partition
You can drop a partition in a partitioned table or subpartitioned table as needed.
Syntax
ALTER TABLE table_name DROP PARTITION partition_name_list [UPDATE GLOBAL INDEXES];
partition_name_list:
partition_name [, partition_name ...]
Note
When dropping a partition, you can drop one or more partitions, but not all partitions.
Dropping a partition also drops the data in the partition. If you want to drop only the data, you can use the
TRUNCATEstatement.
Examples
Drop the
M202005andM202006partitions in thetbl1_rpartitioned table.obclient> CREATE TABLE tbl1_r(log_id INT,log_date DATE NOT NULL DEFAULT SYSDATE) PARTITION BY RANGE(log_date) (PARTITION M202001 VALUES LESS THAN(TO_DATE('2020/02/01','YYYY/MM/DD')) , PARTITION M202002 VALUES LESS THAN(TO_DATE('2020/03/01','YYYY/MM/DD')) , PARTITION M202003 VALUES LESS THAN(TO_DATE('2020/04/01','YYYY/MM/DD')) , PARTITION M202004 VALUES LESS THAN(TO_DATE('2020/05/01','YYYY/MM/DD')) , PARTITION M202005 VALUES LESS THAN(TO_DATE('2020/06/01','YYYY/MM/DD')) , PARTITION M202006 VALUES LESS THAN(TO_DATE('2020/07/01','YYYY/MM/DD')) , PARTITION M202007 VALUES LESS THAN(TO_DATE('2020/08/01','YYYY/MM/DD')) , PARTITION M202008 VALUES LESS THAN(TO_DATE('2020/09/01','YYYY/MM/DD')) , PARTITION M202009 VALUES LESS THAN(TO_DATE('2020/10/01','YYYY/MM/DD')) , PARTITION M202010 VALUES LESS THAN(TO_DATE('2020/11/01','YYYY/MM/DD')) , PARTITION M202011 VALUES LESS THAN(TO_DATE('2020/12/01','YYYY/MM/DD')) , PARTITION M202012 VALUES LESS THAN(TO_DATE('2021/01/01','YYYY/MM/DD')) , PARTITION MMAX VALUES LESS THAN (MAXVALUE) ); Query OK, 0 rows affected obclient> ALTER TABLE tbl1_r DROP PARTITION M202005,M202006; Query OK, 0 rows affectedDrop the
p0partition in thet2_f_rrpartitioned table.obclient> CREATE TABLE t2_f_rr(col1 INT,col2 INT) PARTITION BY RANGE(col1) SUBPARTITION BY RANGE(col2) (PARTITION p0 VALUES LESS THAN(100) (SUBPARTITION sp0 VALUES LESS THAN(2020), SUBPARTITION sp1 VALUES LESS THAN(2021) ), PARTITION p1 VALUES LESS THAN(200) (SUBPARTITION sp2 VALUES LESS THAN(2020), SUBPARTITION sp3 VALUES LESS THAN(2021), SUBPARTITION sp4 VALUES LESS THAN(2022) ) ); Query OK, 0 rows affected obclient> ALTER TABLE t2_f_rr DROP PARTITION p0; Query OK, 0 rows affected
Drop a subpartition
You can drop a subpartition in a subpartitioned table as needed. Dropping a subpartition also drops the definition and data in that subpartition.
Syntax
ALTER TABLE table_name DROP SUBPARTITION subpartition_name_list [ UPDATE GLOBAL INDEXES ];
subpartition_name_list:
subpartition_name[, subpartition_name ...]
Examples
Drop the sp5 and sp6 subpartitions in the p1 partition of the t2_f_rr subpartitioned table and update the global indexes.
obclient> CREATE TABLE t2_f_rr(col1 INT,col2 INT)
PARTITION BY RANGE(col1)
SUBPARTITION BY RANGE(col2)
(PARTITION p0 VALUES LESS THAN(100)
(SUBPARTITION sp0 VALUES LESS THAN(2020),
SUBPARTITION sp1 VALUES LESS THAN(2021)
),
PARTITION p1 VALUES LESS THAN(200)
(SUBPARTITION sp2 VALUES LESS THAN(2020),
SUBPARTITION sp3 VALUES LESS THAN(2021),
SUBPARTITION sp4 VALUES LESS THAN(2022),
SUBPARTITION sp5 VALUES LESS THAN(2023),
SUBPARTITION sp6 VALUES LESS THAN(2024)
)
);
Query OK, 0 rows affected
obclient> ALTER TABLE t2_f_rr DROP SUBPARTITION sp5,sp6 UPDATE GLOBAL INDEXES;
Query OK, 0 rows affected
