Postgresql sharding with citus extension not working

可紊 提交于 2019-12-12 05:53:11

问题


I am using Postgresql with citus extension for sharding and unable to shard tables like below. Below table has a primary key and 2 unique keys. I am trying to shard against column with primary key i.e pid. Note: I am not allowed to change the table structure. These tables are created by tool.

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)
);

This my sharding query:

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

Error it throw is:

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

Can anyone help me with sharding these kind of tables?

@CraigKerstiens Addition to this question:

How to handle sharding when we have multiple foreign keys like this one.

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
)

Assuming that table1 and table2 are already sharded.


回答1:


Within Citus at this time you cannot have a unique constraint that doesn't include they column you are partitioning on. In this case, it'd be possible to enforce addresses were unique to the person id, but not globally unique. To do that you could:

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)
);


来源:https://stackoverflow.com/questions/43660398/postgresql-sharding-with-citus-extension-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!