You can drop a partition in a partitioned table as needed. Dropping a partition will delete the partition definition and data in the partition.
Support for dropping a partition
In the Oracle 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 whether a partitioned table or subpartitioned table supports dropping a partition.
| 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
Before you drop a partition, make sure that no active transaction or query is running on the partition to be dropped. Otherwise, an error may be returned or an unexpected situation may occur. You can query the oceanbase.GV$OB_TRANSACTION_PARTICIPANTS view in the sys tenant to obtain the context of ongoing transactions.
Impact on global indexes
In the Oracle mode of OceanBase Database, when you drop a partition in a partitioned table or subpartitioned table that has global indexes, you must specify the UPDATE GLOBAL INDEXES clause in the ALTER TABLE statement to update the global indexes. If you do not specify the UPDATE GLOBAL INDEXES clause, the global indexes on the partitioned table will be unavailable after the partition is dropped.
In OceanBase Database V4.3.5, from V4.3.5 BP2, when you drop a RANGE or LIST partition (including a primary or subpartition) that supports dropping partitions and specify the UPDATE GLOBAL INDEXES clause, the system does not rebuild the global indexes. Instead, it uses a background compaction to lazily maintain the indexes to ensure their validity, thereby avoiding query interruptions or performance degradation caused by invalid indexes. Note that this optimization applies only to regular global indexes. The following scenarios cannot trigger this optimization:
- The primary partition is a HASH partition (even if the subpartition is a RANGE or LIST partition), and you truncate the subpartition.
- The partitioning key is an expression rather than a regular column.
Hidden parameter for controlling global index behavior during partition DDL operations
Starting from OceanBase Database V4.3.5 BP2, OceanBase Database V4.3.5 introduces a tenant-level hidden parameter _ob_enable_truncate_partition_preserve_global_index to control whether global indexes remain valid during TRUNCATE or DROP operations on a partition (referred to as partition DDL operations). For tenants upgraded to V4.3.5, the default value of this parameter is False. For newly created tenants, the default value is True. The following table describes the behavior of this parameter.
| Parameter value | Behavior during partition DDL operations | Result after partition DDL operations |
|---|---|---|
| True | Only the TRUNCATE/DROP partitions are unavailable for DML operations. Other partitions of the table are available for DML operations. |
Global indexes remain valid: You can execute DML operations on the table and all index tables, and the global indexes related to the table remain valid. |
| False | Only the TRUNCATE/DROP partitions are unavailable for DML operations. Other partitions of the table are available for DML operations. |
Global indexes become invalid: You can execute DML operations on the table and all index tables, but the global indexes related to the table become invalid or are rebuilt. |
Here are some examples:
Global indexes remain valid.
obclient> ALTER SYSTEM SET "_ob_enable_truncate_partition_preserve_global_index" = True;Global 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 this operation, OceanBase Database adds an exclusive lock on the target partition.
| Operation type | Impact during the drop operation |
|---|---|
| Concurrent DDL | Prohibited or blocked. No other DDL operations can be performed on the same table. |
| Concurrent DML |
|
Concurrent query (SELECT) |
Not affected. You can execute queries normally. |
Drop a primary partition
You can drop a primary 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
You can drop one or more primary partitions, but you cannot drop all primary partitions.
Dropping a primary partition will delete the data in the primary partition. If you only want to delete data, you can use the
TRUNCATEstatement.
Examples
Drop the
M202005andM202006primary partitions 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
p0primary partition 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 will delete the subpartition definition and data in the 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 primary partition of the t2_f_rr partitioned table and update the global index information.
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
