首页 > 解决方案 > Postgresql 按列分区,没有主键

问题描述

我希望能够通过worksite_uuid. 但worksite_uuid需要可以为空,并且我需要uuid成为主键。

我能够创建分区的唯一方法是 if worksite_uuidis not nullable 像这样:

CREATE TABLE test (
   uuid uuid DEFAULT uuid_generate_v1mc(),
   worksite_uuid text,
   notes text
)
PARTITION BY LIST(worksite_uuid)

//then add worksite_uuid and uuid as a primary key

create table test_worksite1 partition of test for values in ('1');
create table test_worksite2 partition of test for values in ('2');

有谁知道我如何创建一个仅uuid作为主键的分区并使其可以为worksite_uuid空?

--

示例:我不能这样做

CREATE TABLE test (
   uuid uuid DEFAULT uuid_generate_v1mc() PRIMARY KEY,
   worksite_uuid text,
   notes text
)
PARTITION BY LIST(worksite_uuid)

我收到以下错误:

查询 1 错误:错误:分区表上的唯一约束必须包括所有分区列详细信息:表“test”上的 PRIMARY KEY 约束缺少作为分区键一部分的列“worksite_uuid”。

标签: postgresqldatabase-partitioning

解决方案


为 NULL 值创建分区是没有问题的:

create table test_worksite_null partition of test for values in (NULL);

但是不可能有一个可以为空的主键列。你就是做不到。

我看到了两种方法:

  1. 你生活没有主键。相反,在每个单独的分区上创建主键。这不能保证全球唯一性,但几乎可以。

  2. 您使用另一个值而不是 NULL,例如 -1。


推荐阅读