In OceanBase Database, you can specify the dynamic partition management attribute of a table by using DYNAMIC_PARTITION_POLICY during table creation.
This topic describes how to create a dynamic partitioned table by using SQL statements.
Note
For OceanBase Database V4.3.5, the DYNAMIC_PARTITION_POLICY parameter can be used to create a dynamical partitioned table starting from V4.3.5 BP2.
Limitations and considerations
- In the current version, creating a dynamic partitioned table requires at least one partition definition.
- Only RANGE partitioned tables with a partitioning key of type
date,timestamp,timestamp with local time zone, ornumber(with specifiedBIGINT_PRECISIONprecision) support enabling dynamic partition management. - Dynamic partition management can only be enabled for the partitions of a partitioned table and subpartitioned table.
- After you precreate partitions or delete expired partitions, the partitions are not immediately created or deleted. Instead, a dynamic partition management task is automatically scheduled to run next, or you can manually schedule a dynamic partition management task. For more information, see Dynamic partition management tasks.
Privilege requirements
To create a dynamic partitioned table, you must have the CREATE TABLE privilege. For more information about the privileges in OceanBase Database, 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'}
Dynamic partition management attributes
| Attribute | Description | Required? | Modifiable? |
|---|---|---|---|
| ENABLE | Specifies whether to enable dynamic partition management. Valid values:
|
No | Yes |
| TIME_UNIT | The time unit for partitioning, which indicates the interval of automatic partition boundaries. Valid values:
|
Yes | No |
| PRECREATE_TIME | The time when partitions are pre-created. In each scheduled dynamic partition management task, partitions are pre-created to ensure that max_partition_upper_bound > now() + precreate_time. Valid values:
Note
|
No | Yes |
| EXPIRE_TIME | The partition expiration time. In each scheduled dynamic partition management task, all partitions whose upper boundaries are earlier than now() - expire_time will be deleted. Valid values:
|
No | Yes |
| TIME_ZONE | The time zone to be used for comparing the current time with the partitioning key values of the date and timestamp data types. Valid values:
|
No | No |
| BIGINT_PRECISION | The timestamp precision of the number-typed partitioning key. Valid values:
|
No | No |
For more information about table creation syntax and parameter descriptions, see CREATE TABLE.
Parameter limitations for dynamic partitions
PRECREATE_TIME: You can precreate at most 2,048 partitions.TIME_ZONE: If the partitioning key is of thedateortimestamptype, you can specify a custom time zone offset for theTIME_ZONEparameter. For other types, the value of theTIME_ZONEparameter must bedefault.BIGINT_PRECISION: This parameter can only be set tous,ms, orswhen the partitioning key type isnumber. For other partitioning key types, the value of this parameter must benone.- The values of the
TIME_UNIT,TIME_ZONE, andBIGINT_PRECISIONparameters specified cannot be modified.
Examples
Create a dynamic partitioned table named test_tbl1 and set the dynamic partitioning attributes as follows:
- Partition boundaries are divided by hour.
- Pre-create partitions for the 3 hours following the current time.
- Delete partitions whose upper boundary is earlier than 1 day before the current time.
- Use Beijing time (UTC+8) to determine the time, ensuring that partition boundaries and expiration logic are based on this time zone.
- The partition key
col2is of typeTIMESTAMP, so there is no need to configure the 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 will not be executed immediately after the table is created. You need to wait for the next automatic scheduling of the dynamic partition management task, or manually trigger the dynamic partition management task. For more information, see Dynamic partition management task.