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 | |
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:
| |
SET ( <parameter> [= <value>]) | Modifies table properties. Common scenarios include the following:
| |
WITH (<rebuild_parameter> [= <value>]) | Sets parameters for the REBUILD task. Common parameters include the following:
| |
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 thisquery_idto 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 thequery_idis 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_partitioningproperty of the parent table.Properties such as
keep_alivefor child tables.After a physical partitioned table is rebuilt, properties that were set independently on child tables, such as
bitmap_columnsanddictionary_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.
Run REBUILD.
ASYNC REBUILD TABLE rebuild_test WITH ( binlog_mode ) <your_action>;For a REBUILD task that includes the
binlog_modeparameter, the task automatically pauses after theset_readonlystep 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)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:
|
status | The sub-task status.
|
progress | The sub-task progress: |
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:

Stop and restart a REBUILD task
Pause an asynchronous
REBUILDtask.SUSPEND '<query_id>';Resume an asynchronous task that was paused using the
CANCELcommand.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);