带有citus扩展名的Postgresql分片不起作用 [英] Postgresql sharding with citus extension not working

查看:289
本文介绍了带有citus扩展名的Postgresql分片不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 创建表人员

pid bigint NOT NULL,
名称字符变化(100),
address_pid bigint非空,
address_type字符变化(100),
CONSTRAINT id_pkey主键(pid),
CONSTRAINT addr_id UNIQUE(address_pid ),
CONSTRAINT addr_type_id UNIQUE(address_type,address_pid)
);

这是我的分片查询:

 选择create_distributed_table('person','pid'); 

抛出的错误是:

 错误:分布式关系不能具有不包含分区列
的UNIQUE,EXCLUDE或PRIMARY KEY约束

有人可以帮我分片这类桌子吗?



@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主键(pid),
CONSTRAINT idx_searchres_order唯一(search_pid,search_order),
CONSTRAINT fk_searchres_res FOREIGN KEY(resource_pid)
参考public.table1(res_id)匹配简单
开更新无操作
开不删除任何动作,
限制fk_sea rchres_search外键(search_pid)
参考public.table2(pid)匹配简单
不需要更新
不需要删除

假定table1和table2已被分片。

解决方案

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

 创建表人员

pid bigint NOT NULL,
名称字符变化(100),
address_pid bigint非空,
address_type字符变化(100),
CONSTRAINT id_pkey主键(pid),
CONSTRAINT addr_id UNIQUE(pid, address_pid),
CONSTRAINT addr_type_id UNIQUE(pid,address_type,address_pid)
);


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.

解决方案

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

这篇关于带有citus扩展名的Postgresql分片不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆