将分区 LIST 附加到 postgres 11 中的现有表

Tro*_*els 3 postgresql database-partitioning

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

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

我已经创建了一个新的空表,客户端,通过重命名旧表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;
Run Code Online (Sandbox Code Playgroud)

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

a_h*_*ame 6

从手册中引用

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

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

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

就像是:

create table clients_partitioned
(
  .... all columns ...
)
PARTITION BY LIST  (client_id);
Run Code Online (Sandbox Code Playgroud)

然后创建分区:

create table clients_1 
   partition of clients_partioned
   values in (1,2,3);

create table clients_1 
   partition of clients_partioned
   values in (4,5,6);
Run Code Online (Sandbox Code Playgroud)

然后复制数据:

insert into clients_partitioned
select *
from clients;
Run Code Online (Sandbox Code Playgroud)

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

drop table clients;
alter table clients_partitioned rename to clients;
Run Code Online (Sandbox Code Playgroud)

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