0

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

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

任何人都可以帮助我对这些表进行分片吗?

@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 已经分片。

4

1 回答 1

1

此时在 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)
);
于 2017-04-27T14:38:39.250 回答