Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eg4pd-0004wq-Q1 for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jan 2018 08:30:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eg4pc-0002vg-HZ for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jan 2018 08:30:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eg4pc-0002v8-Ae for pgsql-hackers@lists.postgresql.org; Mon, 29 Jan 2018 08:30:24 +0000 Received: from tama50.ecl.ntt.co.jp ([129.60.39.147]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eg4pP-00048A-Ky for pgsql-hackers@postgresql.org; Mon, 29 Jan 2018 08:30:22 +0000 Received: from vc2.ecl.ntt.co.jp (vc2.ecl.ntt.co.jp [129.60.86.154]) by tama50.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id w0T8U2cV016389; Mon, 29 Jan 2018 17:30:02 +0900 Received: from vc2.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id B443B60292; Mon, 29 Jan 2018 17:30:02 +0900 (JST) Received: from jcms-pop21.ecl.ntt.co.jp (jcms-pop21.ecl.ntt.co.jp [129.60.87.134]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id B2EAF60283; Mon, 29 Jan 2018 17:30:02 +0900 (JST) Received: from [127.0.0.1] (unknown [129.60.241.61]) by jcms-pop21.ecl.ntt.co.jp (Postfix) with ESMTPSA id AD2774006AE; Mon, 29 Jan 2018 17:30:02 +0900 (JST) Subject: Re: unique indexes on partitioned tables From: Amit Langote References: <20180122225559.7pbzomvgp5iwmath@alvherre.pgsql> <92ad77b0-2a6f-a96c-4a3c-67ef6ae65742@lab.ntt.co.jp> Message-ID: <7219558d-c02e-aed4-e169-1dd7c77dfc44@lab.ntt.co.jp> Date: Mon, 29 Jan 2018 17:29:53 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <92ad77b0-2a6f-a96c-4a3c-67ef6ae65742@lab.ntt.co.jp> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CC-Mail-RelayStamp: 1 To: Alvaro Herrera , Pg Hackers X-TM-AS-MML: disable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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