In OceanBase Database, you can use the DYNAMIC_PARTITION_POLICY clause to specify the dynamic partition management attributes of a table during creation.
This topic describes how to create a dynamically partitioned table by using an SQL statement.
Limitations and considerations
You must specify at least one partition definition when you create a dynamically partitioned table in the current version.
The following types of partitioned tables support dynamic partition management:
Range-partitioned tables whose partitioning key is of the
date,timestamp,timestamp with local time zone, ornumbertype (withBIGINT_PRECISIONprecision specified).List-partitioned tables whose partitioning key is a string-type time column. The string partitioning key can be of the
VARCHAR2orCHARtype, and its format is determined by the value ofTIME_UNIT:- When
TIME_UNIT = 'year': The string partitioning key is in theYYYYformat, for example,2026. - When
TIME_UNIT = 'month': The string partitioning key is in theYYYYMMformat, for example,202608. - When
TIME_UNIT = 'day': The string partitioning key is in theYYYYMMDDformat, for example,20260819. - When
TIME_UNIT = 'hour': The string partitioning key is in theYYYYMMDDHHformat, for example,2026081910.
Note
For V4.4.2, dynamic partition management is supported for list-partitioned tables whose partitioning key is a string-type time column starting from V4.4.2 BP3.
- When
Dynamic partition management can only be enabled for a table's partitions or for the partitions of a subpartitioned table.
Pre-created partitions and deleted expired partitions after table creation are not executed immediately. You must wait for the next automatic scheduling of a dynamic partition management task or manually schedule one. For more information about scheduling dynamic partition management tasks, see Dynamic partition management tasks.
Privilege requirements
You must have the CREATE TABLE privilege to create a dynamically partitioned table. For more information about OceanBase Database privileges, see Privilege types in Oracle-compatible mode.
Syntax
The SQL statement for creating a dynamically partitioned table is as follows:
CREATE TABLE [IF NOT EXISTS] table_name (table_definition_list)
DYNAMIC_PARTITION_POLICY [=] (dynamic_partition_policy_list)
partition_option;
dynamic_partition_policy_list:
dynamic_partition_policy_option [, dynamic_partition_policy_option ...]
dynamic_partition_policy_option:
ENABLE = {true | false}
| TIME_UNIT = {'hour' | 'day' | 'week' | 'month' | 'year'}
| PRECREATE_TIME = {'-1' | '0' | 'n {hour | day | week | month | year}'}
| EXPIRE_TIME = {'-1' | '0' | 'n {hour | day | week | month | year}'}
| TIME_ZONE = {'default' | 'time_zone'}
| BIGINT_PRECISION = {'none' | 'us' | 'ms' | 's'}
Description of dynamic partition management attribute parameters
Attribute |
Description |
Required |
Modifiable |
|---|---|---|---|
| ENABLE | Indicates whether to enable dynamic partition management. Valid values:
|
No | Yes |
| TIME_UNIT | The time unit for partitioning, which is the interval at which partition boundaries are automatically created. Valid values:
|
Yes | No |
| PRECREATE_TIME | Specifies the time before which partitions are pre-created. When dynamic partition management is scheduled, partitions are pre-created so that max_partition_upper_bound > now() + precreate_time. Valid values:
Note
|
No | Yes |
| EXPIRE_TIME | Indicates the partition expiration time. When dynamic partition management is scheduled, all partitions that meet the condition partition_upper_bound < now() - expire_time are deleted. Valid values:
|
No | Yes |
| TIME_ZONE | Indicates whether the current time is earlier than the time type (date、timestampWhen the partitioning key is of the (DATE, TIME) type, the time zone information is used. Valid values:
|
No | No |
| BIGINT_PRECISION | meansnumberThe timestamp precision of the partitioning key. Valid values:
|
No | No |
For more detailed information about the parameters in the table creation syntax, see CREATE TABLE.
Restrictions on dynamic partition parameters
PRECREATE_TIME: Up to 2,048 partitions can be pre-created.TIME_ZONE: TheTIME_ZONEparameter can specify a custom time zone offset only when the partitioning key is of thedateortimestamptype. For other types, the value can only bedefault.BIGINT_PRECISION: This parameter can be set tous,ms, orsonly when the partitioning key is of thenumbertype. For other types, the value can only benone.- The values of
TIME_UNIT,TIME_ZONE, andBIGINT_PRECISIONcannot be modified after they are specified.
Examples
Range partitioning example
Create a dynamically partitioned table named test_tbl1 with RANGE partitioning. Set the dynamic partitioning properties as follows:
- Partitions are divided by hour.
- Pre-create partitions for the next 3 hours from the current time.
- Delete partitions whose upper boundary is earlier than 1 day before the current time.
- Use Beijing Time (UTC+8) to determine time, ensuring that partition boundaries and expiration logic are based on this time zone.
- The partitioning key
col2is of theTIMESTAMPtype, so there is no need to configure timestamp precision fornumber.
CREATE TABLE test_tbl1 (col1 INT, col2 TIMESTAMP)
DYNAMIC_PARTITION_POLICY(
ENABLE = true,
TIME_UNIT = 'hour',
PRECREATE_TIME = '3 hour',
EXPIRE_TIME = '1 day',
TIME_ZONE = '+8:00',
BIGINT_PRECISION = 'none')
PARTITION BY RANGE (col2)(
PARTITION P0 VALUES LESS THAN (TIMESTAMP '2024-11-11 13:30:00')
);
Note
The actions of pre-creating partitions and deleting expired partitions after the table is created are not executed immediately. You must wait for the next automatic scheduling of a dynamic partition management task or manually schedule one. For more information, see Dynamic partition management task.
List partitioning example
Create a dynamically partitioned table named test_tbl2 with LIST partitioning. The partitioning key is a string time column in the format YYYYMMDD. Set the dynamic partitioning properties as follows:
- Partitions are automatically created at daily intervals.
- Partitions are divided by day.
- Pre-create partitions for the next 3 days from the current time.
- Delete partitions whose upper boundary is earlier than 30 days before the current time.
- Use Beijing Time (UTC+8) to determine time, ensuring that partition boundaries and expiration logic are based on this time zone.
- The partitioning key
col2is of theVARCHAR2type, so there is no need to configure timestamp precision forbigint.
obclient> CREATE TABLE test_tbl2 (col1 INT, col2 VARCHAR2(20))
DYNAMIC_PARTITION_POLICY(
ENABLE = true,
TIME_UNIT = 'day',
PRECREATE_TIME = '3 day',
EXPIRE_TIME = '30 day',
TIME_ZONE = '+8:00',
BIGINT_PRECISION = 'none'
)
PARTITION BY LIST (col2) (
PARTITION p_20260801 VALUES ('20260801')
);