All Products
Search
Document Center

Hologres:REBUILD (Beta)

Last Updated:Dec 11, 2025

In Hologres, you can use the ALTER TABLE syntax to modify certain table structures, table properties, and column properties. However, you cannot use ALTER TABLE to modify properties that affect table storage. Hologres V3.1 and later versions support the REBUILD syntax, which lets you modify various table parameters. This topic describes how to use the REBUILD command in Hologres.

Syntax

Command format

ASYNC REBUILD TABLE [ IF EXISTS ] <table_name>
    [ WITH ( <rebuild_parameter> [= <value>] [, ... ] )]  
    <action> [, ... ];
    
WHERE action IS ONE OF:
    ADD [ COLUMN ] <column_name> <data_type> [ column_constraint [ ... ] ]
    ALTER [ COLUMN ] <column_name> [ SET DATA ] TYPE <data_type> [ USING <expression> ]
    ALTER [ COLUMN ] <column_name> SET DEFAULT <expression>
    ALTER [ COLUMN ] <column_name> DROP DEFAULT
    ALTER [ COLUMN ] <column_name> { SET | DROP } NOT NULL
    ALTER PRIMARY KEY (<column_name> [, ...])
    TO [LOGICAL] PARTITION [BY LIST(<column_name> [, <column_name>])]
    SET ( <parameter> [= <value>] [, ... ] )

WHERE rebuild_parameter IS ONE OF:
    keep_source
    binlog_mode
    rebuild_guc_<guc_name> = '<guc_value>'

Parameters

Parameter

Sub-parameter

Description

ASYNC

Specifies that the REBUILD task runs asynchronously. The command returns a query_id. Use the query_id to monitor the task status. Synchronous execution is not supported.

table_name

The name of the target table to rebuild.

column_name

The name of a column in the target table.

data_type

The data type of the column.

action

ADD COLUMN

Adds a column. You can add a NOT NULL column and set a default value.

ALTER COLUMN TYPE

Modifies the data type of a column.

ALTER COLUMN SET/DROP DEFAULT

Sets or removes the default value for a column. Existing NULL values are not changed.

ALTER COLUMN SET/DROP NOT NULL

Sets or removes the NOT NULL constraint for a column.

ALTER PRIMARY KEY

Modifies the primary key of the table. If the new primary key causes a data conflict, the asynchronous task reports an error. Monitor the task status.

TO [LOGICAL] PARTITION

Converts the table to a logical or physical partitioned table. The following scenarios are supported:

  • Convert a standard table to a physical partitioned table. You must specify a partition key.

  • Convert a standard table to a logical partitioned table. You must specify a partition key.

  • Modify the partition key of a physical partitioned table.

  • Convert a physical partitioned table to a logical partitioned table. You can optionally modify the partition key.

  • Converting a logical partitioned table to a physical partitioned table is not supported.

SET ( <parameter> [= <value>])

Modifies table properties. Common scenarios include the following:

  • Modify the distribution key (distribution_key).

  • Modify the segment key (event_time_column).

  • Modify the cluster index (clustering_key).

  • Modify the table storage format (orientation): convert between row store, column store, and row-column hybrid store.

  • Modify the table_group that the table belongs to.

  • All other table properties can be modified.

  • You do not need to use REBUILD to modify bitmap indexes (bitmap_columns) or dictionary-encoded columns (dictionary_encoding_columns). Use the ALTER TABLE syntax instead.

WITH (<rebuild_parameter> [= <value>])

Sets parameters for the REBUILD task. Common parameters include the following:

  • keep_source: No value is required. After the conversion, the source table is not deleted. It is renamed to tmp_rebuild_old_<query_id>_<unique_id>_<table_name>.

  • binlog_mode: No value is required. Lets you run REBUILD on a table that has binary logging (Binlog) enabled. To avoid data loss, follow the steps in the Usage examples section.

  • rebuild_guc_hg_computing_resource='serverless': Uses Serverless resources to run the REBUILD task. This avoids using your instance resources and improves task stability.

  • rebuild_guc_<guc_name>='<guc_value>': Sets other Grand Unified Configuration (GUC) parameters. For more information, see GUC parameters.

Notes

  • Only asynchronous (ASYNC) execution is supported. This prevents long-running connections.

  • When you submit a REBUILD task, it quickly returns a query_id. You can use this query_id to check the status of the REBUILD task. If the task submission does not complete, it may be because many asynchronous tasks are running on your instance. In this case, wait for a period of time and then check the task status after the query_id is returned.

  • Modifying table parameters with the REBUILD command involves data redistribution and consumes compute resources. To ensure business stability, run REBUILD tasks during off-peak hours or use Serverless compute resources.

  • During a REBUILD task, one step sets the table to read-only. During this time, you cannot write data to the table.

  • The REBUILD command does not support tables that depend on dynamic tables or materialized views. However, it does support tables that depend on standard views.

  • To modify multiple parameters, modify them in a single REBUILD task to reduce overhead.

  • After you rebuild a physical partitioned table, the dynamic partition management properties are not inherited. You must reconfigure these properties after the REBUILD task is complete. These properties include the following:

    • The auto_partitioning property of the parent table.

    • Properties such as keep_alive for child tables.

    • After a physical partitioned table is rebuilt, properties that were set independently on child tables, such as bitmap_columns and dictionary_encoding_columns, are not inherited. Instead, their values will be consistent with the parent table.

  • REBUILD does not currently support the following tables:

    • Tables with specific column properties, such as columnar storage optimization for JSONB columns, or column constraints for vector columns.

    • Tables with full-text indexes or global secondary indexes.

    • Tables with Serial or Bigserial columns.

Usage examples

Run REBUILD on a table without binary logging

-- Create a table and import data.
CREATE TABLE rebuild_test (
    a TEXT,
    b TEXT,
    ds TEXT
);

INSERT INTO rebuild_test VALUES ('1', '1', '2025-04-01'), ('2', '2', '2025-04-02'), ('3', '3', '2025-04-03');

-- Add a NOT NULL column and set a default value.
ASYNC REBUILD TABLE rebuild_test ADD COLUMN c text NOT NULL DEFAULT 'a';

-- Change the primary key to column a.
ASYNC REBUILD TABLE rebuild_test ALTER PRIMARY KEY (a);

-- Use Serverless resources to run the Rebuild task. 
-- Set the distribution_key and clustering_key for the table, and change the orientation to row-column hybrid store.
ASYNC REBUILD TABLE rebuild_test 
WITH (
    rebuild_guc_hg_computing_resource = 'serverless'
)
SET (
    distribution_key = 'a',
    clustering_key = 'a',
    orientation = 'row,column'
);

-- Convert the standard table to a logical partitioned table. 
-- Set the partition key to ds and add the NOT NULL constraint to the ds column.
ASYNC REBUILD TABLE rebuild_test 
    ALTER COLUMN ds SET NOT NULL,
    TO LOGICAL PARTITION BY LIST(ds);

Run REBUILD on a table with binary logging

The REBUILD command does not retain historical binary logging (Binlog) data. Therefore, you cannot run REBUILD on a table with binary logging enabled by default. To perform this operation, you must configure the binlog_mode parameter in the REBUILD task and follow the steps below. This process ensures that downstream clients have consumed all historical Binlog data before the table is rebuilt.

  1. Run REBUILD.

    ASYNC REBUILD TABLE rebuild_test 
    WITH (
      binlog_mode
    )
    <your_action>;
  2. For a REBUILD task that includes the binlog_mode parameter, the task automatically pauses after the set_readonly step is complete. You can check the progress by running the following SQL command. At this point, the system has set the table to read-only. You cannot write data to the table, and no new Binlog data is generated.

    postgres=# SELECT step, status, progress FROM hologres.rebuild_progress('<query_id>');
                 step              | status | progress 
    -------------------------------+--------+----------
     prepare                       | done   | 1/1
     create_tmp_table              | done   | 1/1
     get_src_table_snapshot        | done   | 1/1
     insert                        | done   | 1/1
     set_readonly                  | done   | 1/1
     check_snapshot                |        | 0/1
     re-insert                     |        | -
     check_additional_child_table  |        | -
     create_additional_child_table |        | -
     insert_additional_child_table |        | -
     swap                          |        | 0/1
    (11 rows)
  3. Wait for the downstream client to finish consuming the existing Binlog data. Then, manually run the following SQL command to resume the REBUILD task. During this process, the source table remains read-only, and no new Binlog data is generated.

    RESUME '<query_id>';

    After the REBUILD task is successfully completed, binary logging is automatically enabled for the new table. You can then restart the downstream Binlog consumption task. The consumption of the new table's Binlog must start from lsn = 0.

Monitoring and O&M

Check the REBUILD task status

REBUILD tasks run asynchronously. When you submit a task, the system returns a success status for the submission and a query_id. You must check the status of the asynchronous sub-tasks in the hologres.rebuild_progress system table. The REBUILD operation is complete only after all sub-tasks have succeeded. You can use the following command to check the status:

SELECT * FROM hologres.rebuild_progress('<rebuild_query_id>');

The columns of the system table and their descriptions are as follows:

Column

Description

job_name

The query_id of the REBUILD task.

step_id

The step ID. The sub-tasks of REBUILD are executed sequentially according to this ID.

step

The step name:

  • prepare: Task preparation.

  • create_tmp_table: Create a temporary table.

  • get_src_table_snapshot: Get a data snapshot of the source table.

  • insert: Import existing data into the temporary table.

  • set_readonly: Set the table to be rebuilt to read-only and stop data writes.

  • check_snapshot: Check if the current data snapshot of the table is the same as the one obtained in the third step. If not, proceed to the re-insert step.

  • re-insert: Import incremental data.

  • check_additional_child_table: Check if any new child tables have been created by the user since the REBUILD started. This applies only to physical partitioned tables.

  • create_additional_child_table: Create potential new child tables for the temporary table. This applies only to physical partitioned tables.

  • insert_additional_child_table: Import existing data into the new temporary child tables. This applies only to physical partitioned tables.

  • swap: Replace the source table with the temporary table.

status

The sub-task status.

  • done: Completed.

  • doing: In progress.

  • NULL: This step is not required, or it is not yet known if this step is required.

  • error: Failed. Check the error message in the message field.

progress

The sub-task progress: m/n. n is the number of sub-steps to be executed (usually positively correlated with the number of partitions), and m is the number of sub-steps currently executed.

start_time

The start time of the sub-task.

end_time

The end time of the sub-task.

queryid

The query_id of the sub-task.

pid

The service process ID.

message

The message of the sub-task. If a sub-task reports an error, the error message is recorded in this field.

The following is a sample result:

opopo

Stop and restart a REBUILD task

  • Pause an asynchronous REBUILD task.

    SUSPEND '<query_id>';
  • Resume an asynchronous task that was paused using the CANCEL command.

    RESUME '<query_id>';

Handle REBUILD task exceptions

If a REBUILD task is interrupted by an error or manually paused with the SUSPEND command, you can use the RESUME command to continue the task. Alternatively, you can follow these steps to end the task and recover the source table.

  • Use the following command to clean up the temporary tables created during the REBUILD process.

    CALL hg_clean_rebuild_tmp_tables('<query_id>');
  • If the task was interrupted after the source table was set to read-only, use the following command to restore write operations to the table.

    ALTER TABLE <table_name> SET (readonly = false);