更新时间:2026-07-28 GMT+08:00
分享

单节点表在线转换操作

规格约束

  1. 单节点表仅支持创建为复制表类型,不支持创建为分布表(指定分布列)。
  2. 单节点表不支持创建全局二级索引(GSI)。含有GSI索引的分布表转化成单节点后,GSI索引会转换为普通唯一索引;单节点表转Hash分布表时,不包含分布键的唯一索引会转换为GSI索引。
  3. 仅支持单节点表与分布表相互转换,不支持修改分布列。
  4. 单节点表与分布表相互转换时,不支持逻辑复制。
  5. 仅支持单节点表与分布表在线相互转换,不支持离线。
  6. 单节点表转换为分布式表,仅支持转换为Hash分布、Range分布或List分布的分布表。同理,仅支持Hash分布表、Range分布表以及List分布表转化为单节点表。
  7. 仅支持在系统预置的单节点组Node Group上创建单节点表。
  8. 单节点表在线转换,仅支持普通表和分区表,其他表类型均不支持(包括二级分区表、段页式表、hash bucket表、临时表、unlogged表等)。
  9. 单节点表和分布式表相互转换,DDL功能可并发读写。长事务可能会阻塞在线DDL,建议避免在长事务存在时进行在线DDL。本特性执行后期将阻塞DQL操作,以及阻塞DML操作。
  10. 该特性同时受在线DDL特性约束影响。

前置条件

设置GUC参数single_node_table_strategy不为FORBID,GUC参数的具体使用请参见《参考》中“数据库运行参数说明 > GUC参数说明 > 单节点表”章节。

使用示例

  • 分布表转单节点表
    -- 创建分布表。
    gaussdb=# CREATE TABLE test (c1 int , c2 int, c3 numeric(64,8)) WITH (storage_type = astore) DISTRIBUTE BY HASH(c2) PARTITION BY RANGE (c2)
              (PARTITION P1 VALUES LESS THAN (5),
              PARTITION P2 VALUES LESS THAN (10),
              PARTITION P3 VALUES LESS THAN (20));
    CREATE TABLE
    gaussdb=# \d+ test
                                Table "public.test"
     Column |     Type      | Modifiers | Storage | Stats target | Description
    --------+---------------+-----------+---------+--------------+-------------
     c1     | integer       |           | plain   |              |
     c2     | integer       |           | plain   |              |
     c3     | numeric(64,8) |           | main    |              |
    Partition By RANGE(c2)
    Number of partitions: 3 (View pg_partition to check each partition range.)
    Has OIDs: no
    Distribute By: HASH(c2)
    Location Nodes: ALL DATANODES
    Options: orientation=row, storage_type=astore, compression=no
    
    -- 将分布表转化为单节点表。
    gaussdb=# ALTER TABLE ONLINE test DISTRIBUTE BY REPLICATION TO GROUP sys_single_group1;
    ALTER TABLE
    -- 查看单节点表。
    gaussdb=# \d+ test
                                Table "public.test"
     Column |     Type      | Modifiers | Storage | Stats target | Description
    --------+---------------+-----------+---------+--------------+-------------
     c1     | integer       |           | plain   |              |
     c2     | integer       |           | plain   |              |
     c3     | numeric(64,8) |           | main    |              |
    Partition By RANGE(c2)
    Number of partitions: 3 (View pg_partition to check each partition range.)
    Has OIDs: no
    Distribute By: REPLICATION
    Location Nodes: datanode1
    Options: orientation=row, storage_type=astore, compression=no, is_single_node=true, logical_repl_node=-1
    
    gaussdb=# DROP TABLE IF EXISTS test;
    DROP TABLE
  • 单节点表转换为分布表
    -- 创建单节点表。
    gaussdb=#  DROP TABLE IF EXISTS test;
    DROP TABLE
    gaussdb=# CREATE TABLE test (c1 int , c2 int, c3 numeric(64,8)) WITH (storage_type = astore) DISTRIBUTE BY REPLICATION TO GROUP sys_single_group2 PARTITION BY RANGE (c2)
              (PARTITION P1 VALUES LESS THAN (5),
              PARTITION P2 VALUES LESS THAN (10),
              PARTITION P3 VALUES LESS THAN (20));
    CREATE TABLE
    gaussdb=# \d+ test
                                Table "public.test"
     Column |     Type      | Modifiers | Storage | Stats target | Description
    --------+---------------+-----------+---------+--------------+-------------
     c1     | integer       |           | plain   |              |
     c2     | integer       |           | plain   |              |
     c3     | numeric(64,8) |           | main    |              |
    Partition By RANGE(c2)
    Number of partitions: 3 (View pg_partition to check each partition range.)
    Has OIDs: no
    Distribute By: REPLICATION
    Location Nodes: datanode2
    Options: orientation=row, storage_type=astore, is_single_node=true, logical_repl_node=-2, compression=no
    
    -- 将单节点表转化为分布表。
    gaussdb=# ALTER TABLE ONLINE test DISTRIBUTE BY HASH(c2);
    ALTER TABLE
    -- 查看分布表
    gaussdb=# \d+ test
                                Table "public.test"
     Column |     Type      | Modifiers | Storage | Stats target | Description
    --------+---------------+-----------+---------+--------------+-------------
     c1     | integer       |           | plain   |              |
     c2     | integer       |           | plain   |              |
     c3     | numeric(64,8) |           | main    |              |
    Partition By RANGE(c2)
    Number of partitions: 3 (View pg_partition to check each partition range.)
    Has OIDs: no
    Distribute By: HASH(c2)
    Location Nodes: ALL DATANODES
    Options: orientation=row, storage_type=astore, compression=no
    
    gaussdb=#  DROP TABLE IF EXISTS test;
    DROP TABLE

相关文档