Thread
-
BUG #18949: 分区表创建主键或者唯一约束需要有分区键
PG Bug reporting form <noreply@postgresql.org> — 2025-06-07T09:29:06Z
The following bug has been logged on the website: Bug reference: 18949 Logged by: 余 大海 Email address: 1157357742@qq.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 中也并没有对此的改进。
-
Re: BUG #18949: 分区表创建主键或者唯一约束需要有分区键
Dilip Kumar <dilipbalaut@gmail.com> — 2025-06-07T15:55:24Z
On Sat, Jun 7, 2025 at 9:12 PM PG Bug reporting form <noreply@postgresql.org> wrote: > > The following bug has been logged on the website: > > Bug reference: 18949 > Logged by: 余 大海 > Email address: 1157357742@qq.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