【问题标题】:Attach partition LIST to existing table in postgres 11将分区 LIST 附加到 postgres 11 中的现有表
【发布时间】:2019-07-15 11:48:34
【问题描述】:

我正在尝试更改表以在 postgres 11 中使用分区列表。我已经尝试了几个小时,但我不断收到错误。

我有一个巨大的表,客户,有 (client_id, customer_id, value)。

我已经创建了一个新的空表 clients,方法是将旧表重命名为 clients_old,然后使用:CREATE TABLE clients( like clients_old including all) 创建新表。

从这里开始,我在尝试添加 LIST 分区时被卡住了。

我已经尝试过:

ALTER TABLE Clients attach PARTITION BY LIST  (client_id) --> fail;
ALTER TABLE Clients attach PARTITION  LIST  (client_id) --> fail;
ALTER TABLE Clients ADD PARTITION  LIST  (client_id) --> fail;

我应该使用什么语法来更改表以使用分区?

【问题讨论】:

    标签: postgresql database-partitioning


    【解决方案1】:

    Quote from the manual

    无法将常规表转换为分区表,反之亦然

    因此,您不能将现有的非分区表更改为分区表。

    您需要创建一个新的分区表(使用不同的名称),创建所有必要的分区,然后将数据从旧表复制到新的分区表。

    类似:

    create table clients_partitioned
    (
      .... all columns ...
    )
    PARTITION BY LIST  (client_id);
    

    然后创建分区:

    create table clients_1 
       partition of clients_partioned
       for values in (1,2,3);
    
    create table clients_1 
       partition of clients_partioned
       for values in (4,5,6);
    

    然后复制数据:

    insert into clients_partitioned
    select *
    from clients;
    

    完成后,您可以删除旧表并重命名新表:

    drop table clients;
    alter table clients_partitioned rename to clients;
    

    不要忘记重新创建外键和索引。

    【讨论】:

    • 我认为应该是 FOR VALUES 而不是 VALUES
    • 关于引用 current 文档版本的引用 It is not possible to turn a regular table into a partitioned table or vice versa。我不知道 2019 年是什么版本,但在 2022 年,这条线出现在第 14 版中。尽管我对此表示怀疑,但它可能在你的那一年已经过时了。
    【解决方案2】:

    我必须添加for 标签才能添加分区:

    create table clients_1 
    partition of clients_partioned
    for values in (4,5,6);
    

    因为没有for 是语法错误。

    【讨论】:

      猜你喜欢
      • 2020-01-29
      • 1970-01-01
      • 2019-05-27
      • 2023-02-22
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 2019-09-06
      • 2022-07-12
      相关资源
      最近更新 更多