Re: BUG #18949: 分区表创建主键或者唯一约束需要有分区键

From: Dilip Kumar <dilipbalaut(at)gmail(dot)com>
To: 1157357742(at)qq(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18949: 分区表创建主键或者唯一约束需要有分区键
Date: 2025-06-07 15:55:24
Message-ID: CAFiTN-tidhsiGx+SWqb7mCnYz3X2EthOvRBw097fPRdOX8R6Eg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Sat, Jun 7, 2025 at 9:12 PM PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
>
> The following bug has been logged on the website:
>
> Bug reference: 18949
> Logged by: 余 大海
> Email address: 1157357742(at)qq(dot)com
> PostgreSQL version: 16.2
> Operating system: centos7.9
> Description:
>
> postgres=# CREATE TABLE hash_sales (
> postgres(# id INT,
> postgres(# sale_date DATE,
> postgres(# amount NUMERIC,
> postgres(# PRIMARY KEY (id) -- 主键必须包含分区键
> postgres(# ) PARTITION BY hash(sale_date );
> ERROR: unique constraint on partitioned table must include all partitioning
> columns
> DETAIL: PRIMARY KEY constraint on table "hash_sales" lacks column
> "sale_date" which is part of the partition key.
> 在oracle中,以上SQL语句可以创建成功,并能保证ID的全局唯一性,但是在PG15\16\17中,分区表创建主键或者唯一约束都需要有分区键,这样会导致ID字段的无法保证全局唯一性。PG18released
> 中也并没有对此的改进。

PostgreSQL's partitioned tables have a known limitation: unique
constraints (including primary keys) must include all columns of the
partitioning key. This isn't a bug, but a design constraint.
For example, if you partition hash_sales by sale_date, your primary
key must include sale_date. as shown below

CREATE TABLE hash_sales (
id INT,
sale_date DATE,
amount NUMERIC,
PRIMARY KEY (sale_date, id)
) PARTITION BY hash(sale_date);

--
Regards,
Dilip Kumar
Google

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2025-06-07 16:05:07 Re: Function Error Response Is Not Clear
Previous Message Thomas Munro 2025-06-07 10:11:09 Re: BUG #18940: PostgreSQL 18beta1 fails 'collate.windows.win1252' regression when building with MSYS/mingw