pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
To: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: Pg Hackers <pgsql-hackers@postgresql.org>
Subject: Re: unique indexes on partitioned tables
Date: Mon, 29 Jan 2018 17:29:53 +0900
Message-ID: <7219558d-c02e-aed4-e169-1dd7c77dfc44@lab.ntt.co.jp> (raw)
In-Reply-To: <92ad77b0-2a6f-a96c-4a3c-67ef6ae65742@lab.ntt.co.jp>
References: <20180122225559.7pbzomvgp5iwmath@alvherre.pgsql>
	<92ad77b0-2a6f-a96c-4a3c-67ef6ae65742@lab.ntt.co.jp>

On 2018/01/29 16:28, Amit Langote wrote:
> create table p (a int, b int) partition by list (a);
> create table p1 partition of p for values in (1) partition by range (b);
> create table p11 partition of p1 for values from (1) to (10);
> create table p2 partition of p for values in (2);
> 
> create unique index on p (a);
> ERROR:  insufficient columns in UNIQUE constraint definition
> DETAIL:  UNIQUE constraint on table "p1" lacks column "b" which is part of
> the partition key.
> 
> It seems that after recursing to p1 which is itself partitioned,
> DefineIndex() mistakenly looks for column b (which is in the p1's
> partition key) in the unique key.  I think that's unnecessary.
> DefineIndex() should check that only once, that is, before recursing.

Hmm, scratch that...

> Am I missing something?

Yes, I am.

create table p (a int, b int) partition by list (a);
create table p1 partition of p for values in (1) partition by range (b);
create table p11 partition of p1 for values from (1) to (10);
create table p12 partition of p1 for values from (10) to (20);
create table p2 partition of p for values in (2);

-- after applying my delta patch
create unique index on p (a);

insert into p values (1, 1);   -- unique index p11 (a) says all fine
insert into p values (1, 10);  -- unique index p12 (a) says all fine

That can't be right, because p (a) is no longer unique.

So, a unique key on a partitioned table must include the partition key
columns of *all* downstream partitioned tables, as your patch correctly
enforces.  Sorry about the noise.

That said, I think that it might be a good idea to include the above
detail in the documentation of CREATE INDEX and ALTER TABLE ADD UNIQUE.

Thanks,
Amit





view thread (21+ messages)  latest in thread

Message-ID: <7219558d-c02e-aed4-e169-1dd7c77dfc44@lab.ntt.co.jp>
Permalink:  ../7219558d-c02e-aed4-e169-1dd7c77dfc44@lab.ntt.co.jp/
Also on:    postgresql.org/message-id/7219558d-c02e-aed4-e169-1dd7c77dfc44@lab.ntt.co.jp

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: Langote_Amit_f8@lab.ntt.co.jp, alvherre@alvh.no-ip.org
  Subject: Re: unique indexes on partitioned tables
  In-Reply-To: <7219558d-c02e-aed4-e169-1dd7c77dfc44@lab.ntt.co.jp>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox