Hologres supports the ALTER TABLE syntax to modify certain table schemas, table properties, and column properties. However, ALTER TABLE does not support modifying properties that affect table storage. Starting from Hologres V3.1, you can use the REBUILD syntax to flexibly modify various table parameters. This topic describes how to use REBUILD 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>'Parameter description
Parameter | Subkey | Description |
ASYNC | Specifies that the REBUILD task runs asynchronously. After you run the command, the task's | |
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 | Changes the data type of a column. | |
ALTER COLUMN SET/DROP DEFAULT | Sets or removes the default value for a column. The NULL values in existing data are not changed. | |
ALTER COLUMN SET/DROP NOT NULL | Sets or removes the NOT NULL constraint for a column. | |
ALTER PRIMARY KEY | Changes the primary key of the table. If the new primary key causes a data conflict, an error is reported during the asynchronous execution. Monitor the task execution 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:
| |
Usage notes
Only asynchronous execution is supported. This prevents connections from being occupied for a long time.
After you submit a REBUILD task, the submission process completes quickly and returns the task's
query_id. You can use thisquery_idto view the execution status of the REBUILD task. If the submission does not complete, this may be because many asynchronous scheduling tasks are running in the current instance. In this case, wait for thequery_idto be returned before checking the task execution status.Modifying table parameters with REBUILD involves data redistribution and consumes computing resources. Run REBUILD tasks during off-peak hours or use Serverless computing resources to ensure business stability.
During the execution of a REBUILD task, the table is in a read-only state for a period of time, and data cannot be written to it. Starting from Hologres V4.1, incremental updates based on Dynamic Table technology significantly reduce the read-only duration during a rebuild. The target table must meet the following requirements. Otherwise, write operations will be suspended for a long time:
The target table has a primary key before the rebuild, and the new primary key includes the original primary key.
The target table is a column-store or row-column hybrid store table before the rebuild.
The target table does not contain generated columns after the rebuild.
If the target table is a physical partitioned table before the rebuild, the partition key must remain unchanged after the rebuild.
If the target table is a logical partitioned table after the rebuild, it can have only one partition key.
To reduce overhead, modify multiple parameters in a single REBUILD task.
After a physical partitioned table is rebuilt, properties for dynamic partition management are not inherited. You must set them after the REBUILD task is complete. These properties include the following:
The
auto_partitioningproperty of the parent table.Properties of child tables, such as
keep_alive.After a physical partitioned table is rebuilt, properties that were independently set for child tables are not inherited. The properties of the child tables become consistent with those of the parent table. Examples include
bitmap_columnsanddictionary_encoding_columns.
You cannot run REBUILD on tables in the following cases:
Tables with specific column properties, such as column-store optimization for JSONB columns, or column constraints, such as vector fields.
Tables with full-text indexes or global secondary indexes.
Tables that contain columns of the Serial or Bigserial type.
Tables that have Dynamic Table or materialized view dependencies are not supported. Tables that have standard view dependencies are supported.
Usage examples
Run REBUILD on a table without binary logging enabled
-- 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 storage format 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 the non-partitioned table to a logical partitioned table, set the partition key to ds, and add the NOT NULL property 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 enabled
REBUILD does not retain historical binary logging data. By default, you cannot run REBUILD on a table that has binary logging enabled. To run the task, you must specify the binlog_mode parameter and follow these steps. This ensures that the downstream client has fully consumed all historical binary logging data.
Run REBUILD.
ASYNC REBUILD TABLE rebuild_test WITH ( binlog_mode ) <YOUR_ACTION>;For a REBUILD task with the
binlog_modeparameter set, the task automatically pauses after theset_readonlystep is complete. You can query the progress using the following SQL statement. At this point, the system has set the table to read-only. Data cannot be written to the table, which means no new binary logging 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 binary logging data. Then, manually run the following SQL statement to resume the REBUILD task. While the task resumes, the source table remains read-only, and no new binary logging data is generated.
RESUME '<query_id>';After the REBUILD task is successfully run, binary logging is automatically enabled for the new table. You can then restart the downstream binary logging consumption task and start consuming the binary logs of the new table from
lsn = 0.
Monitoring and O&M
View the execution status of a REBUILD task
REBUILD tasks run asynchronously. After a task is submitted, the system returns a success status and a query_id. You must query the hologres.rebuild_progress system table to view the status of the asynchronous subtasks. The table is considered rebuilt only after all subtasks are successful. The command is as follows:
SELECT * FROM hologres.rebuild_progress('<rebuild_query_id>');The columns of the system table and their descriptions are as follows:
Column name | Description |
job_name | The query_id of the REBUILD task. |
step_id | The step ID. The subtasks of the REBUILD task are run sequentially based on this ID. |
step | The step name:
|
status | The status of the subtask.
|
progress | The progress of the subtask: |
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 an error occurs in the subtask, the error message is recorded in this field. |
The following is an example of the result:

Stop and restart a REBUILD task
Stop an asynchronous
REBUILDtask.SUSPEND '<query_id>';Resumes an asynchronous task that was canceled by
CANCEL.RESUME '<query_id>';
Handle REBUILD task exceptions
If a REBUILD task is interrupted by an error, or if you manually run the SUSPEND command to pause the task, you can run the RESUME command to continue the task. You can also follow these steps to end the task and recover the source table.
Run the following command to clean up the temporary tables generated during the REBUILD process.
CALL hg_clean_rebuild_tmp_tables('<query_id>');If the source table was set to read-only by the REBUILD task before the task was interrupted, run the following command to remove the read-only state and resume write operations.
ALTER TABLE <table_name> SET (readonly = false);