Hologres supports modifying some table structures, table properties, and column properties through ALTER TABLE syntax. However, for properties that affect table storage, ALTER TABLE syntax is not supported. Starting from Hologres V3.1, the REBUILD syntax is supported. With the REBUILD syntax, you can flexibly modify various parameters of a table. This topic describes how to use REBUILD in Hologres.
Syntax
Statement 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-item | Description |
ASYNC |
The REBUILD task is executed asynchronously. After the task is executed, it returns a |
|
table_name | The name of the target table to be rebuilt. | |
column_name | The column name of the target table. | |
data_type | The data type of the column. | |
action | ADD COLUMN | Adds a column. You can add NOT NULL columns and set default values. |
ALTER COLUMN TYPE | Modifies the data type of a column. | |
ALTER COLUMN SET/DROP DEFAULT | Sets or removes the default value for a column. NULL values in existing data will not change. | |
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 there is a data conflict with the new primary key, an error will be reported during asynchronous execution. Monitor the execution status of the task promptly. | |
TO [LOGICAL] PARTITION |
Converts the table to a logical/physical partitioned table, supporting the following scenarios:
|
|
SET ( <parameter> [= <value>]) |
Modifies table properties. Common scenarios:
|
|
WITH (<rebuild_parameter> [= <value>]) |
Sets REBUILD task-related parameters. Common parameters:
|
|
Precautions
Only asynchronous execution (ASYNC) is supported, which does not require long-term connection occupation.
-
After you submit a REBUILD task, it is executed quickly and returns the
query_idof the task. You can use thisquery_idto check the execution status of a REBUILD task. If the task does not complete successfully for a while after submission, this may be because a large number of asynchronous scheduling tasks are running on the current instance. We recommend that you wait for a period of time and check the task execution status after thequery_idis returned. Using the REBUILD feature to modify table parameters involves underlying data redistribution, which consumes computing resources. Therefore, it is recommended that you execute REBUILD tasks during off-peak business hours, or use serverless computing resources for this operation to ensure business stability.
-
During a
REBUILDtask, the table is read-only and not writable. Starting from Hologres V4.1,REBUILDusesDynamic Tabletechnology for incremental updates, which significantly shortens the read-only window. The target table must meet the following requirements to avoid a long write downtime:-
The table must have a primary key before the
REBUILDoperation, and the new primary key must include all columns from the original primary key. -
The table must use column-oriented storage or row-column hybrid storage before the
REBUILDoperation. -
The table must not contain any generated columns after the
REBUILDoperation. -
If the table is a physical partitioned table before the
REBUILDoperation, its partition key must remain unchanged. -
If the table becomes a logical partitioned table after the
REBUILDoperation, it can have only one partition key.
-
-
To reduce overhead, modify multiple parameters in a single
REBUILDtask. -
After a physical partitioned table is rebuilt, its dynamic partition management properties are not inherited. You must manually set the following properties after the rebuild is complete:
-
The
auto_partitioningproperty of the parent table. -
The child table's
keep_aliveand other attributes. -
After you rebuild a physical partitioned table, any properties that were independently set on its child tables are not inherited and instead revert to the settings of the parent table, such as
bitmap_columnsanddictionary_encoding_columns.
-
-
REBUILDis not supported for the following tables:-
Tables with special column property settings, such as columnar storage optimization for JSONB columns, or column constraints, such as vector columns.
-
Tables with a full-text index or a global secondary index.
-
Tables that contain columns of the
SerialorBigserialdata type. -
Tables that are referenced by a
Dynamic Tableor a materialized view. Tables referenced by a view are supported.
-
Examples
Rebuild a table without Binlog
-- 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 non-null column with 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 a serverless resource to run the REBUILD task, set the distribution_key and clustering_key for the table, and change the storage to row-column hybrid.
ASYNC REBUILD TABLE rebuild_test
WITH (
rebuild_guc_hg_computing_resource = 'serverless'
)
SET (
distribution_key = 'a',
clustering_key = 'a',
orientation = 'row,column'
);
-- Convert a non-partitioned table to a logical partitioned table, set the partition key to ds, and add a NOT NULL constraint to the ds column.
ASYNC REBUILD TABLE rebuild_test
ALTER COLUMN ds SET NOT NULL,
TO LOGICAL PARTITION BY LIST(ds);
Rebuild a table with Binlog
REBUILD does not preserve historical Binlog data. Therefore, REBUILD is not supported by default for tables with Binlog enabled. You must specify the binlog_mode parameter and follow the steps below to ensure that downstream systems have fully consumed the historical Binlog data.
-
Run the
REBUILDcommand.ASYNC REBUILD TABLE rebuild_test WITH ( binlog_mode ) <YOUR_ACTION>; -
If the
binlog_modeparameter is set for a REBUILD task, the task automatically pauses after theset_readonlystep is complete. You can run an SQL query to check the progress. At this point, the system has set the table to read-only. This means that data cannot be written 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) -
Wait for the downstream client to finish consuming all existing Binlog data. Then, manually resume the
REBUILDtask by running the following SQL statement. During this process, the source table remains in read-only mode and does not generate new Binlog data.RESUME '<query_id>';After the
REBUILDtask is complete, Binlog is automatically enabled for the new table. You can then restart the downstream Binlog consumption task and start consuming fromlsn = 0.
Monitoring and O&M
View the execution status of a REBUILD task
The REBUILD task runs asynchronously. After the task is successfully submitted, it returns a success status and a query_id. You need to query the hologres.rebuild_progress system table to view the status of the asynchronous subtasks. The REBUILD operation for the table is complete only when all subtasks are successful. The command is as follows:
SELECT * FROM hologres.rebuild_progress('<rebuild_query_id>');
The following table describes the columns of the system table.
Column name | Description |
job_name | The query_id of the REBUILD task. |
step_id | The step ID. The subtasks of REBUILD are executed sequentially according to step IDs. |
step |
The step name:
|
status | The status of the subtask.
|
progress |
Subtask progress: |
start_time | The start time of the subtask. |
end_time | The end time of the subtask. |
queryid | The query_id of the subtask. |
pid | The service process ID. |
message | The message of the subtask. If the subtask reports an error, the error message is recorded in this field. |
The following figure shows the sample result.

Stop and restart REBUILD tasks
-
Stop the
REBUILDasynchronous task.SUSPEND '<query_id>'; -
Resumes the asynchronous task that was set to
CANCEL.RESUME '<query_id>';
Handle REBUILD task exceptions
If a REBUILD task is interrupted by an error or you manually stop it by using the SUSPEND command, you can either resume it with the RESUME command or follow these steps to terminate the task and restore the source table.
-
Run the following command to clean up the temporary tables created during the
REBUILDprocess.CALL hg_clean_rebuild_tmp_tables('<query_id>'); -
If the task was interrupted after the source table became read-only, run the appropriate command to re-enable writes.
-
For Hologres versions earlier than V4.1:
ALTER TABLE <table_name> SET (readonly = false); -
For Hologres V4.1 and later:
ALTER TABLE <table_name> SET RESET (ddl_options,write_options);
-