All Products
Search
Document Center

Hologres:Dynamic partitioning

Last Updated:May 23, 2024

The dynamic partitioning feature of Hologres automatically creates and manages child partitioned tables based on the dynamic partitioning rules that you configure when you create a partitioned table. You do not need to configure all partitions when you create the partitioned table. The dynamic partitioning feature can also migrate data in partitions that meet the specified conditions to devices of the Infrequent Access (IA) storage class. This helps Hologres reduce storage costs without degrading query performance.

Background information

The dynamic partitioning feature is optimized in different versions to provide more functionalities.

  • Hologres V1.3 and later allow you to configure dynamic partitioning rules. The system runs scheduled tasks to create child partitioned tables in advance and delete expired child partitioned tables based on the dynamic partitioning rules.

  • Hologres V1.3.37 and later support dynamic management of tiered storage of hot and cold data. This way, cold storage can be automatically used for data. This helps reduce storage costs. For more information, see Tiered storage of hot data and cold data.

  • Hologres V2.1.11 and later allow you to configure columns of the DATE type as partition key columns.

  • Hologres V2.2 and later allow you to configure the schd_start_time property when you configure dynamic partitioning for a table. You can use this property to specify the time when partitions are scheduled to be created, deleted, or stored in the cold storage tier.

Limits

  • Hologres allows you to import data to a child table rather than a parent table.

    Note

    Realtime Compute for Apache Flink allows you to import data to a parent table in Hologres in real time. For more information, see Write data to a partitioned result table in Hologres in real time.

  • Each partitioning rule can be used to create only one partitioned table.

  • The PARTITION BY clause supports only list partitioning. The partition key must be a single column.

  • If a partitioned table has a primary key, the partition key must be a subset of the primary key.

  • You can configure the schd_start_time property only for parent partitioned tables but not child partitioned tables.

Configure dynamic partitioning

Syntax

You can configure dynamic partitioning for a partitioned table when you create the table. You can also configure dynamic partitioning for an existing partitioned table. The following sample code shows the syntax:

  • Syntax supported in Hologres V2.1 and later:

    • Configure dynamic partitioning for a partitioned table when you create the table.

      -- Configure dynamic partitioning for a partitioned table when you create the table.
      CREATE TABLE [if not exists] [<schema_name>.]<table_name>  ([
        {
         <column_name> <column_type> [ <column_constraints>, [...]]
         | <table_constraints>
         [, ...]
        }
      ])
      PARTITION BY LIST(<column_name>)
      WITH (
         auto_partitioning_enable = 'xxx',
         auto_partitioning_time_unit = 'xxx',
         auto_partitioning_time_zone = 'xxx',
         auto_partitioning_num_precreate = 'xxx',
         auto_partitioning_num_retention = 'xxx',
         auto_partitioning_num_hot='xxx',
         auto_partitioning_schd_start_time = 'xxx'
      );
    • Modify dynamic partitioning settings for an existing partitioned table.

      -- Modify dynamic partitioning settings for an existing partitioned table.
      ALTER TABLE [<schema_name>.]<table_name> SET (
         auto_partitioning_enable = 'xxx',
         auto_partitioning_time_unit = 'xxx',
         auto_partitioning_time_zone = 'xxx',
         auto_partitioning_num_precreate = 'xxx',
         auto_partitioning_num_retention = 'xxx',
         auto_partitioning_num_hot='xxx',
         auto_partitioning_schd_start_time = 'xxx'
      );
      
  • Syntax supported in all Hologres versions:

    • Configure dynamic partitioning for a partitioned table when you create the table.

      -- Configure dynamic partitioning for a partitioned table when you create the table.
      BEGIN;
      CREATE TABLE [if not exists] [<schema_name>.]<table_name>  ([
        {
         <column_name> <column_type> [ <column_constraints>, [...]]
         | <table_constraints>
         [, ...]
        }
      ])
      PARTITION BY LIST(<column_name>);
      CALL set_table_property('[<schema_name>.]<table_name>', 'auto_partitioning.enable', 'xxx');
      CALL set_table_property('[<schema_name>.]<table_name>', 'auto_partitioning.time_unit', 'xxx');
      CALL set_table_property('[<schema_name>.]<table_name>', 'auto_partitioning.time_zone', 'xxx');
      CALL set_table_property('[<schema_name>.]<table_name>', 'auto_partitioning.num_precreate', 'xxx');
      CALL set_table_property('[<schema_name>.]<table_name>', 'auto_partitioning.num_retention', 'xxx');
      CALL set_table_property('[<schema_name>.]<table_name>', 'auto_partitioning.num_retention', 'xxx');
      CALL set_table_property('[<schema_name>.]<table_name>', 'auto_partitioning.num_hot', 'xxx');
      CALL set_table_property ('[<schema_name>.]<table_name>', 'auto_partitioning.schd_start_time', 'xxx');
      COMMIT;
    • Modify dynamic partitioning settings for an existing partitioned table.

      -- Modify dynamic partitioning settings for an existing partitioned table.
      CALL set_table_property('[<schema_name>.]<table_name>', 'auto_partitioning.enable', 'xxx');
      CALL set_table_property('[<schema_name>.]<table_name>', 'auto_partitioning.num_precreate', 'xxx');
      CALL set_table_property('[<schema_name>.]<table_name>', 'auto_partitioning.num_retention', 'xxx');

Parameter description

Important

Before you execute the CREATE TABLE WITH statement to configure dynamic partitioning, replace the periods (.) in parameter names with underscores (_). For example, before you execute the CREATE TABLE WITH statement to configure dynamic partitioning in Hologres V2.1, replace auto_partitioning.enable with auto_partitioning_enable.

Parameter

Required

Description

Modifiable

auto_partitioning.enable

No

Specifies whether to enable dynamic partitioning. Valid values:

  • true: enables dynamic partitioning.

  • false: disables dynamic partitioning. This is the default value.

Yes

auto_partitioning.time_unit

Yes

The time unit for dynamic partitioning. Valid values:

  • HOUR

  • DAY

  • MONTH

  • QUARTER

  • YEAR

For example, if you set this parameter to DAY, the system automatically creates or deletes partitions by day.

No

auto_partitioning.time_zone

No

The time zone for dynamic partitioning. If you do not configure this parameter, the time zone in which your Hologres instance is located is used. If you specify a time zone, partitions are dynamically managed based on the specified time zone.

You can execute the following SQL statement to query the valid values of this parameter. Different time zones have different time offsets. The valid values of this parameter are displayed in the name column returned. Example: Asia/Shanghai.

SELECT * FROM pg_timezone_names;

No

auto_partitioning.num_precreate

No

The number of partitions that you want to create in advance. Valid values:

  • 0: No partitions are created in advance.

  • [1, 512]: The specified number of partitions are created in advance based on the current point in time. We recommend that you specify a number greater than or equal to 2. Default value: 4.

Note

For example, if the current date is January 10, 2022 and you specify time_unit = DAY, num_precreate = 3 for a table, partitions that correspond to January 10, January 11, and January 12 of 2022 are created in advance.

Yes

auto_partitioning.num_retention

No

The number of historical partitions that you want to retain. Valid values:

  • 0: No historical partitions are retained.

  • -1: All historical partitions are retained. This is the default value.

  • A positive integer: The specified number of historical partitions are retained. The maximum value is 512.

You can execute the set hg_experimental_auto_part_max_maintained_partitions=<value>; statement to adjust the maximum number of historical partitions that can be retained. The maximum value is 8760.

Note

For example, if the current date is January 10, 2022 and you specify <time_unit = DAY,num_retention = 3> for a table, partitions that correspond to January 9, January 8, and January 7 of 2022 are retained, and partitions that correspond to dates before January 7, 2022 are deleted.

Yes

auto_partitioning.num_hot

No

The number of hot partitions that you want to retain. Valid values:

  • 0: No hot partitions are retained.

  • -1: All hot partitions are retained. This is the default value.

  • A positive integer: The specified number of hot partitions are retained. The maximum value is 512.

auto_partitioning.schd_start_time

No

The scheduled time for dynamic partitioning. If you do not configure this parameter and you set the auto_partitioning.time_unit parameter to HOUR, dynamic partitioning is performed at the beginning of each hour. If you do not configure this parameter and you set the auto_partitioning.time_unit parameter to a value other than HOUR, dynamic partitioning is performed at 00:00:01 on a specific day.

Naming conventions for child partitioned tables

The auto_partitioning.time_unit parameter for dynamic partitioning can be set to HOUR, DAY, MONTH, QUARTER, or YEAR. The name of a child partitioned table that is automatically created by using the dynamic partitioning feature consists of the name of its parent partitioned table and a time suffix, in the {parent_table}_{time_suffix} format. The time suffix follows a specific format and is generated based on the scheduled time and time unit for dynamic partitioning. The following table describes the relationships between time suffixes and the scheduled time for dynamic partitioning.

Time unit

Format of the time suffix

Example

Scheduled time for dynamic partitioning

HOUR

YYYYMMDDHH24

2022030117

The dynamic partitioning task is scheduled to run at the beginning of each hour. For example, the task is run at 01:00:01 on March 1, 2022.

DAY

YYYYMMDD

20220301

The dynamic partitioning task is scheduled to run at 00:00:01 every day. For example, the task is run at 00:00:01 on March 1, 2022.

MONTH

YYYYMM

202203

The dynamic partitioning task is scheduled to run at 00:00:01 on the first day of each month. For example, the task is run at 00:00:01 on March 1, 2022 and at 00:00:01 on April 1, 2022.

QUARTER

YYYYQ

20221, 20222, 20223, or 20224, which indicates the first, second, third, or fourth quarter of 2022.

The dynamic partitioning task is scheduled to run at 00:00:01 on the first day of each quarter. For example, the task is run at 00:00:01 on January 1, 2022.

YEAR

YYYY

2022 or 2023, which indicates the yearly partition for 2022 or 2023.

The dynamic partitioning task is scheduled to run at 00:00:01 on the first day of each year. For example, the task is run at 00:00:01 on January 1, 2022.

Examples

The following SQL statements provide examples on how to configure dynamic partitioning with auto_partitioning.time_unit set to DAY. In the examples, partitions are created for the next three days and historical partitions for the previous two days are retained based on the current date in the time zone of Asia/Shanghai.

Syntax supported in Hologres V2.1 and later

  1. Create a partitioned table named tbl1.

    -- Create a partitioned table and configure dynamic partitioning for the table in Hologres V2.1.
    
    CREATE TABLE tbl1 (
        c1 text NOT NULL,
        c2 text 
    )
    PARTITION BY LIST (c2)
    WITH (
       auto_partitioning_enable = 'true',
       auto_partitioning_time_unit = 'DAY',
       auto_partitioning_time_zone = 'Asia/Shanghai',
       auto_partitioning_num_precreate = '3',
       auto_partitioning_num_retention = '2'
    );
    
  2. Insert data into partitions.

    INSERT INTO tbl1 (c1, c2) VALUES ('Data 1', '20231212');
    INSERT INTO tbl1 (c1, c2) VALUES ('Data 2', '20231213');
    INSERT INTO tbl1 (c1, c2) VALUES ('Data 3', '20231214');
  3. Query data.

    SELECT * FROM tbl1 WHERE c2='20231212';

    The following result is returned:

    c1	     c2
    Data 1   20231212

Syntax supported in all Hologres versions

  1. Create a partitioned table named tbl1.

    -- Create a partitioned table and configure dynamic partitioning for the table.
    BEGIN;
    CREATE TABLE tbl1 (
        c1 text NOT NULL,
        c2 text 
    )
    PARTITION BY LIST (c2);
    CALL set_table_property ('tbl1', 'auto_partitioning.enable', 'true');
    CALL set_table_property ('tbl1', 'auto_partitioning.time_unit', 'DAY');
    CALL set_table_property ('tbl1', 'auto_partitioning.time_zone', 'Asia/Shanghai');
    CALL set_table_property ('tbl1', 'auto_partitioning.num_precreate', '3');
    CALL set_table_property ('tbl1', 'auto_partitioning.num_retention', '2');
    COMMIT;
  2. Insert data into partitions.

    INSERT INTO tbl1 (c1, c2) VALUES ('Data 1', '20231212');
    INSERT INTO tbl1 (c1, c2) VALUES ('Data 2', '20231213');
    INSERT INTO tbl1 (c1, c2) VALUES ('Data 3', '20231214');
  3. Query data.

    SELECT * FROM tbl1 WHERE c2='20231212';

    The following result is returned:

    c1	     c2
    Data 1   20231212

The following table describes the execution results.

Time

Event

Result

2023-12-12 09:00:00

SQL statements are executed to create a parent partitioned table and its child partitioned tables.

  • The parent partitioned table named tbl1 is created.

  • The child partitioned tables named tbl1_20231212, tbl1_20231213, and tbl1_20231214 are created.

2023-12-13 00:00:00

The system automatically creates a child partitioned table.

  • A child partitioned table named tbl1_20231215 is created.

2023-12-14 00:00:00

The system automatically creates a child partitioned table.

  • A child partitioned table named tbl1_20231216 is created.

2023-12-15 00:00:00

The system automatically creates a child partitioned table and deletes another child partitioned table.

  • A child partitioned table named tbl1_20231217 is created.

  • The child partitioned table named tbl1_20231212 is deleted.

2023-12-16 00:00:00

The system automatically creates a child partitioned table and deletes another child partitioned table.

  • A child partitioned table named tbl1_20231218 is created.

  • The child partitioned table named tbl1_20231213 is deleted.

Scenarios

Retain a child partitioned table

By default, the system creates and deletes child partitioned tables based on the configurations of dynamic partitioning. Child partitioned tables are automatically deleted after they expire. You may want to retain the data in an important child partitioned table. For example, you want to retain the e-commerce data generated during Double 11 to perform year-on-year or period-over-period analysis. In such scenarios, Hologres allows you to add the keep_alive property to the child partitioned tables that you want to retain to prevent the tables from being automatically deleted.

  • Syntax supported in Hologres V2.1 and later:

    -- Add the keep_alive property to a child partitioned table.
    ALTER TABLE [<schema_name>.]<table_name> SET (keep_alive = 'true');
    
    -- Remove the keep_alive property from a child partitioned table. After you remove the keep_alive property, the child partitioned table is deleted after the table expires.
    ALTER TABLE [<schema_name>.]<table_name> SET (keep_alive = 'false');
    
  • Syntax supported in all Hologres versions:

    -- Add the keep_alive property to a child partitioned table.
    call set_table_property('[<schema_name>.]<table_name>', 'keep_alive', 'true');
    
    -- Remove the keep_alive property from a child partitioned table. After you remove the keep_alive property, the child partitioned table is deleted after the table expires.
    call set_table_property('[<schema_name>.]<table_name>', 'keep_alive', 'false');

Dynamically manage storage modes of partitioned tables

You can enable tiered storage of hot data and cold data for partitioned tables to reduce costs and optimize query performance in an efficient manner. For example, you can store data in the previous N historical partitions that are frequently accessed in the hot storage tier, and store data in the M partitions that are created earlier than the N partitions in the cold storage tier. This helps reduce storage costs. The partitions that are created earlier than the M and N partitions are dynamically deleted.

Create a partitioned table and configure dynamic partitioning for the table

In this example, a partitioned table with dynamic partitioning configured is created. A partition is created for each day. Data in the previous 7 historical partitions is stored in the hot storage tier, and data in the 23 historical partitions that are created earlier than the 7 partitions is stored in the cold storage tier. Partitions that are created earlier than the 30 historical partitions are deleted. Sample code:

BEGIN;
CREATE TABLE tbl2(	
  c1 text not null, 
  c2 text
)
PARTITION BY LIST(c2);
CALL set_table_property('tbl2', 'auto_partitioning.enable', 'true');
CALL set_table_property('tbl2', 'auto_partitioning.time_unit', 'DAY');
CALL set_table_property('tbl2', 'auto_partitioning.num_precreate', '3');
CALL set_table_property('tbl2', 'auto_partitioning.num_hot', '7');
CALL set_table_property('tbl2', 'auto_partitioning.num_retention', '30');
COMMIT;

The following figure shows the configuration result.

效果

Modify a storage policy

You can use the auto_partitioning.num_hot parameter to specify the number of partitions in the hot storage tier. Take note that you cannot migrate a partition from the cold storage tier to the hot storage tier. In this example, a partitioned table is created on July 1, 2022. Sample code:

BEGIN;
CREATE TABLE tbl_p(
  c1 text not null,
  c2 text
)
PARTITION BY LIST(c2);
CALL set_table_property('tbl_p', 'auto_partitioning.enable', 'true');
CALL set_table_property('tbl_p', 'auto_partitioning.time_unit', 'DAY');
CALL set_table_property('tbl_p', 'auto_partitioning.num_precreate', '3');
CALL set_table_property('tbl_p', 'auto_partitioning.num_hot', '3');
CALL set_table_property('tbl_p', 'auto_partitioning.num_retention', '10');
COMMIT;

Partition storage may be changed in the following scenarios:

  • Scenario 1: Increase the number of partitions in the hot storage tier

    To increase the number of partitions in the hot storage tier to 4, execute the following statement:

    CALL set_table_property('tbl_p', 'auto_partitioning.num_hot', '4');

    Child partitioned tables that are stored in the cold storage tier are not migrated to the hot storage tier. The following figure shows the configuration effect.示例

  • Scenario 2: Decrease the number of partitions in the hot storage tier

    To decrease the number of partitions in the hot storage tier to 2, execute the following statement:

    CALL set_table_property('tbl_p', 'auto_partitioning.num_hot', '2');

    Child partitioned tables that are stored in the hot storage tier are migrated to the cold storage tier. The following figure shows the configuration effect.示例

Convert a partitioned table that uses the cold storage mode into a partitioned table that has dynamic partitions

In this example, a partitioned table that uses the cold storage mode is converted into a partitioned table that contains dynamic partitions. Partitions that are created in the previous 7 days are stored in the hot storage tier. Procedure:

  1. Prepare data.

    -- Create a partitioned table and set the storage mode of the table to cold storage.
    BEGIN;
    CREATE TABLE tbl2(	
      c1 text not null, 
      c2 text
    )
    PARTITION BY LIST(c2);
    CALL set_table_property('tbl2', 'storage_mode', 'cold');
    create table tbl2_20230808 partition of tbl2 for values in('20230808');
    create table tbl2_20230809 partition of tbl2 for values in('20230809');
    create table tbl2_20230810 partition of tbl2 for values in('20230810');
    create table tbl2_20230817 partition of tbl2 for values in('20230817');
    COMMIT;
  2. Convert the table partitions into dynamic partitions and store partitions that are created in the previous 7 days in the hot storage tier.

    begin;
    CALL set_table_property('tbl2', 'storage_mode', 'hot'); -- Set the storage mode of the parent partitioned table to hot storage.
    CALL set_table_property('tbl2_20230810', 'storage_mode', 'cold'); -- Set the storage mode of partitions that do not require storage mode conversion to cold storage.
    CALL set_table_property('tbl2_20230809', 'storage_mode', 'cold');
    CALL set_table_property('tbl2_20230808', 'storage_mode', 'cold');
    CALL set_table_property('tbl2', 'auto_partitioning.enable', 'true');
    CALL set_table_property('tbl2', 'auto_partitioning.time_unit', 'DAY');
    CALL set_table_property('tbl2', 'auto_partitioning.num_precreate', '3');
    CALL set_table_property('tbl2', 'auto_partitioning.num_hot', '7');
    CALL set_table_property('tbl2', 'auto_partitioning.num_retention', '10');
    commit;

Query dynamic partitioning configurations

You can execute the following SQL statement to query dynamic partitioning configurations for a table in a database.

SELECT
    nsp_name AS schema_name,
    tbl_name AS table_name,
    ENABLE,
    time_unit,
    time_zone,
    num_precreate,
    num_retention,
    b.usename AS create_user,
    cret_time,
    schd_start_time,
    options
FROM
    hologres.hg_partitioning_config AS a
    LEFT JOIN pg_user AS b ON a.cret_user = b.usesysid;

The following table describes the parameters in the statement.

Parameter

Description

schema_name

The name of the schema.

table_name

The name of the table.

ENABLE

Indicates whether dynamic partitioning is enabled.

time_unit

The time unit for dynamic partitioning.

time_zone

The time zone for dynamic partitioning.

num_precreate

The number of partitions to be created in advance.

num_retention

The number of historical partitions to be retained.

create_user

The user that is used to create the partitioned table.

cret_time

The time when the partitioned table was created.

schd_start_time

The nearest point in time when partitioning is scheduled.

The following figure shows the query result.

image

Query the logs for creating and deleting child partitioned tables

The logs for creating and deleting child partitioned tables are not included in query logs. You can execute the following SQL statement to query the logs for creating and deleting child partitioned tables:

SELECT                           
    relname,
    relowner,
    schdtime,
    trigtime,
    status,
    message,
    precreate,
    discard
FROM
    hologres.hg_partitioning_log 

The following table describes the parameters in the statement.

Parameter

Description

relname

The name of the partitioned table and the schema to which the partitioned table belongs, in the Schema name.Table name format.

relowner

The owner of the partitioned table.

schdtime

The scheduled time for dynamic partitioning.

trigtime

The actual time when dynamic partitioning was performed.

status

The status of the dynamic partitioning task.

message

The description.

precreate

The name of the child partitioned table that is created.

discard

The name of the child partitioned table that is deleted.

The following figure shows the query result.

image

FAQ

How do I enable dynamic partitioning for an existing partitioned table?

Execute the following SQL statements to enable dynamic partitioning for an existing partitioned table.

-- Syntax in Hologres V2.1
ALTER TABLE auto_part_old SET (
   auto_partitioning_enable = 'true',
   auto_partitioning_time_unit = 'HOUR',
   auto_partitioning_time_zone = 'PRC',
   auto_partitioning_num_precreate = '4',
   auto_partitioning_num_retention = '-1',
   auto_partitioning_num_hot = '-1'
);

-- Syntax in all Hologres versions
BEGIN;
CALL set_table_property('auto_part_old', 'auto_partitioning.enable', 'true');
CALL set_table_property('auto_part_old', 'auto_partitioning.time_unit', 'HOUR');
CALL set_table_property('auto_part_old', 'auto_partitioning.time_zone', 'PRC');
CALL set_table_property('auto_part_old', 'auto_partitioning.num_precreate', '4');
CALL set_table_property('auto_part_old', 'auto_partitioning.num_retention', '-1');
CALL set_table_property('auto_part_old', 'auto_partitioning.num_hot', '-1');
COMMIT;
Important

The auto_partitioning.time_unit and auto_partitioning.time_zone parameters are the core parameters of the dynamic partitioning feature. You can configure the parameters only once after a Hologres instance is upgraded. Then, the values of the parameters cannot be changed.

If I enable dynamic partitioning for an existing partitioned table, are the existing child partitioned tables automatically deleted?

Hologres determines whether to delete a child partitioned table based on the table name. If the name of a child partitioned table follows the {parent_table}_{time_suffix} naming rule, the child partitioned table is automatically deleted after the table expires. Otherwise, the child partitioned table is not automatically deleted.

I execute SQL statements to create a partitioned table and configure dynamic partitioning for the table with the num_precreate parameter set to 3. However, a parent partitioned table is created but no child partitioned tables are created. Why?

By default, Hologres performs checks every 10 minutes on new tasks for which dynamic partitioning is configured. The child partitioned tables are created within 10 minutes. You can check the child partitioned tables later.