具有 citus 扩展名的 Postgresql 分片不起作用

Postgresql sharding with citus extension not working

我正在使用带有 citus 扩展的 Postgresql 进行分片,但无法像下面那样分片 tables。 table 下面有一个主键和 2 个唯一键。我正在尝试使用主键对列进行分片,即 pid注意:我不允许更改table结构。这些 table 是由工具创建的。

CREATE TABLE person 
(
    pid bigint NOT NULL,
    name character varying(100),
    address_pid bigint NOT NULL,
    address_type character varying(100),
    CONSTRAINT id_pkey PRIMARY KEY (pid),
    CONSTRAINT addr_id UNIQUE (address_pid),
    CONSTRAINT addr_type_id UNIQUE (address_type, address_pid)
);

这是我的分片查询:

select create_distributed_table('person', 'pid');

它抛出的错误是:

Error: Distributed relations cannot have UNIQUE, EXCLUDE, or PRIMARY KEY constraints that do not include the partition column

任何人都可以帮我分片这些 tables 吗?

@CraigKerstiens 这个问题的补充:

当我们有多个像这样的外键时如何处理分片。

CREATE TABLE table
(
    pid bigint NOT NULL,
    search_order integer NOT NULL,
    resource_pid bigint NOT NULL,
    search_pid bigint NOT NULL,
    CONSTRAINT hfj_search_result_pkey PRIMARY KEY (pid),
    CONSTRAINT idx_searchres_order UNIQUE (search_pid, search_order),
    CONSTRAINT fk_searchres_res FOREIGN KEY (resource_pid)
        REFERENCES public.table1 (res_id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT fk_searchres_search FOREIGN KEY (search_pid)
        REFERENCES public.table2 (pid) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
)

假设 table1 和 table2 已经分片。

此时在 Citus 中,您不能有一个不包含您正在分区的列的唯一约束。在这种情况下,可以强制地址对于个人 ID 是唯一的,但不是全局唯一的。为此,您可以:

CREATE TABLE person 
(
    pid bigint NOT NULL,
    name character varying(100),
    address_pid bigint NOT NULL,
    address_type character varying(100),
    CONSTRAINT id_pkey PRIMARY KEY (pid),
    CONSTRAINT addr_id UNIQUE (pid, address_pid),
    CONSTRAINT addr_type_id UNIQUE (pid, address_type, address_pid)
);