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 1entk1-0007VU-6a for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Feb 2018 22:16:57 +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 1entk0-0002uh-7W for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Feb 2018 22:16:56 +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 1entjz-0002uY-Rz for pgsql-hackers@lists.postgresql.org; Mon, 19 Feb 2018 22:16:56 +0000 Received: from mail-qt0-x22b.google.com ([2607:f8b0:400d:c0d::22b]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1entjv-0001y9-4h for pgsql-hackers@lists.postgresql.org; Mon, 19 Feb 2018 22:16:54 +0000 Received: by mail-qt0-x22b.google.com with SMTP id c19so14105913qtm.7 for ; Mon, 19 Feb 2018 14:16:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=eHi3r2o1fs8cZCYjda12SXTHXuMl5xTLMiGORtxNfz0=; b=aqlKpib1ygTdl3YXIQo3Zi86D27Z/OUV9IkeGiEgVKWJpDad7OmcoZXObtB0xRDpXg TqInZhqKYG0jg8F3W9bh7ogKdPmvl3jz+Kua6pZvyo+a+yalyJHZh9hxQWU3sOEaJrrD 2FqNg0d9Jzq+bXkASz+xUp/qYGiVb35waX60PfSUo4LmoisfqYGKPf0YFZNIaWaWz2zU 2+PW0+3kGtpiHeHt4LSAPfIAyFISNo+dTIDXLfnlJlv4dNfiZbSOUOFnFizmPUTdjn7+ sxd1+gxjaueOjujYxfSTUc4IWTB9ypNgoxKFNL22SBlUVa8DOIoqWs1nWmGoUVGYvz+w MTFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=eHi3r2o1fs8cZCYjda12SXTHXuMl5xTLMiGORtxNfz0=; b=S9WuZpdKRYITd97cZSZtBckSlS9pjG3H6cIzEIpT8oQPScP7zI30YnKAPcw9RE9EAg eBPiu60edAryRssIuA8xMWLjAMBkjXQDq0g/D4KCwvN5DkXRrdBCjxTJONRx7y7twPSU BT0fLr3AKkF1ythZyhtHBakzMJUUmCDv2WT1QdDmLbr8KxbfEQinpHPVGsK8L89FByB7 CSsD3p6EV7uOLaKkXYrgN0Vu66awOj5KW3DBBCIoLwZvDRCvOO2ZMi5Udg7RH8kV/Olg 4CUyzYk6PNDo91Kbu8mIKGqvdcwoQJ/nMzP6RcXRC56Fe+/S8BvFVqmnHIO6zkUxaeXZ iqgw== X-Gm-Message-State: APf1xPBlDFDu2trtNO0gSyjbJj5bLvaZKdO+us+D1udB5xeaEK4yrZv2 tuendsO7FzNZXDM+HYwMkCz/jILUa/uXV8wLmEw= X-Google-Smtp-Source: AH8x2261OjJCsDRz7IkGIPUfaVcTWmAn+6WwkG0Hs7qPIwesHnop2gLSmXOaElpe9AU+VySrZGX2idDSZaMQI+2TF80= X-Received: by 10.200.50.2 with SMTP id x2mr26443416qta.130.1519078608593; Mon, 19 Feb 2018 14:16:48 -0800 (PST) MIME-Version: 1.0 Received: by 10.12.177.34 with HTTP; Mon, 19 Feb 2018 14:16:48 -0800 (PST) In-Reply-To: References: From: "David G. Johnston" Date: Mon, 19 Feb 2018 15:16:48 -0700 Message-ID: Subject: Re: pgsql: Allow UNIQUE indexes on partitioned tables To: Alvaro Herrera Cc: PostgreSQL Hackers Content-Type: multipart/alternative; boundary="001a11434486d9eb080565980cc1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --001a11434486d9eb080565980cc1 Content-Type: text/plain; charset="UTF-8" I found the following change to be confusing. /doc/src/sgml/ref/alter_table.sgml + + Additional restrictions apply when unique indexes are applied to + partitioned tables; see . + That paragraph appears in the section covering "ALTER TABLE name ADD table_constraint_using_index" However, the code says: /src/backend/commands/tablecmds.c + /* + * Doing this on partitioned tables is not a simple feature to implement, + * so let's punt for now. + */ + if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("ALTER TABLE / ADD CONSTRAINT USING INDEX is not supported on partitioned tables"))); I was expecting the doc for ADD CONSTRAINT USING INDEX to note the limitation explicitly - in lieu of the above paragraph. Also, I cannot reason out what the following limitation means: /doc/src/sgml/ref/create_table.sgml + If any partitions are in turn partitioned, all columns of each partition + key are considered at each level below the UNIQUE + constraint. As an aside, adding a link to "Data Definiton/Table Partitioning" from at least CREATE TABLE ... PARTITION BY; and swapping "PARTITION BY" and "PARTITION OF" in the Parameters section of that page - one must partition by a table before one can partition it (and also the synopsis lists them in the BY before OF order), would be helpful. David J. On Mon, Feb 19, 2018 at 1:40 PM, Alvaro Herrera wrote: > Allow UNIQUE indexes on partitioned tables > > If we restrict unique constraints on partitioned tables so that they > must always include the partition key, then our standard approach to > unique indexes already works --- each unique key is forced to exist > within a single partition, so enforcing the unique restriction in each > index individually is enough to have it enforced globally. Therefore we > can implement unique indexes on partitions by simply removing a few > restrictions (and adding others.) > > Discussion: https://postgr.es/m/20171222212921.hi6hg6pem2w2t36z@alvherre. > pgsql > Discussion: https://postgr.es/m/20171229230607.3iib6b62fn3uaf47@alvherre. > pgsql > Reviewed-by: Simon Riggs, Jesper Pedersen, Peter Eisentraut, Jaime > Casanova, Amit Langote > > Branch > ------ > master > > Details > ------- > https://git.postgresql.org/pg/commitdiff/eb7ed3f3063401496e4aa4bd68fa33 > f0be31a72f > > Modified Files > -------------- > doc/src/sgml/ddl.sgml | 9 +- > doc/src/sgml/ref/alter_table.sgml | 15 +- > doc/src/sgml/ref/create_index.sgml | 5 + > doc/src/sgml/ref/create_table.sgml | 18 +- > src/backend/bootstrap/bootparse.y | 2 + > src/backend/catalog/index.c | 50 ++++- > src/backend/catalog/pg_constraint.c | 76 +++++++ > src/backend/catalog/toasting.c | 4 +- > src/backend/commands/indexcmds.c | 125 +++++++++-- > src/backend/commands/tablecmds.c | 71 ++++++- > src/backend/parser/analyze.c | 7 + > src/backend/parser/parse_utilcmd.c | 31 +-- > src/backend/tcop/utility.c | 1 + > src/bin/pg_dump/t/002_pg_dump.pl | 65 ++++++ > src/include/catalog/index.h | 5 +- > src/include/catalog/pg_constraint_fn.h | 4 +- > src/include/commands/defrem.h | 1 + > src/include/parser/parse_utilcmd.h | 3 +- > src/test/regress/expected/alter_table.out | 8 - > src/test/regress/expected/create_index.out | 6 + > src/test/regress/expected/create_table.out | 12 -- > src/test/regress/expected/indexing.out | 294 > +++++++++++++++++++++++++- > src/test/regress/expected/insert_conflict.out | 2 +- > src/test/regress/sql/alter_table.sql | 2 - > src/test/regress/sql/create_index.sql | 6 + > src/test/regress/sql/create_table.sql | 8 - > src/test/regress/sql/indexing.sql | 172 ++++++++++++++- > 27 files changed, 907 insertions(+), 95 deletions(-) > > --001a11434486d9eb080565980cc1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I found the following change to be confusing.
<= br>
/doc/src/sgml/ref/alter_table.sgml
+=C2=A0 =C2=A0= =C2=A0 =C2=A0<para>
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 Additional = restrictions apply when unique indexes are applied to
+=C2=A0 =C2= =A0 =C2=A0 =C2=A0 partitioned tables; see <xref linkend=3D"sql-crea= tetable" />.
+=C2=A0 =C2=A0 =C2=A0 =C2=A0</para>

That paragraph appears in the section covering "ALTER= TABLE name=C2=A0ADD table_constraint_using_index"

<= div class=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif= ">However, the code says:

/src/backend/commands/tablecm= ds.c
+=C2=A0 =C2=A0/*
=
+=C2=A0 =C2=A0 * Doing this on partitioned tables is not a simple featur= e to implement,
+=C2=A0 =C2=A0 * so let's punt for now.
+=C2=A0 =C2=A0 */
+=C2=A0 =C2=A0if (rel->rd_rel->relkind =3D= =3D RELKIND_PARTITIONED_TABLE)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0ereport(= ERROR,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(err= code(ERRCODE_FEATURE_NOT_SUPPORTED),
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 errmsg("ALTER TABLE / ADD CONSTRAINT USING= INDEX is not supported on partitioned tables")));

<= div class=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif= ">I was expecting the doc for ADD CONSTRAINT USING INDEX to note the limita= tion explicitly - in lieu of the above paragraph.

Also= , I cannot reason out what the following limitation means:

/doc/src/sgml/ref/create_table.sgml
+=C2=A0 =C2=A0 =C2=A0 If any partitions are in turn partitioned, = all columns of each partition
+=C2=A0 =C2= =A0 =C2=A0 key are considered at each level below the <literal>UNIQUE= </literal>
+=C2=A0 =C2=A0 =C2=A0 co= nstraint.


As an aside, adding a lin= k to "Data Definiton/Table Partitioning" from at least CREATE TAB= LE ... PARTITION BY; and swapping "PARTITION BY" and "PARTIT= ION OF" in the Parameters section of that page - one must partition by= a table before one can partition it (and also the synopsis lists them in t= he BY before OF order), would be helpful.

David J.
=


On Mon, Feb 19, 2018 at 1:40 PM, Alvaro Herrera <alvhe= rre@alvh.no-ip.org> wrote:
= Allow UNIQUE indexes on partitioned tables

If we restrict unique constraints on partitioned tables so that they
must always include the partition key, then our standard approach to
unique indexes already works --- each unique key is forced to exist
within a single partition, so enforcing the unique restriction in each
index individually is enough to have it enforced globally.=C2=A0 Therefore = we
can implement unique indexes on partitions by simply removing a few
restrictions (and adding others.)

Discussion: https://postgr.es/m/20171222212921.hi6hg6pem2w2t36z@alvherre.pgsql
Discussion: https://postgr.es/m/= 20171229230607.3iib6b62fn3uaf47@alvherre.pgsql
Reviewed-by
: Simon Riggs, Jesper Pedersen, Peter Eisentraut, Jaime
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Casanova, Amit Langote

Branch
------
master

Details
-------
https://git.postgre= sql.org/pg/commitdiff/eb7ed3f3063401496e4aa4bd68fa33f0be31a7= 2f

Modified Files
--------------
doc/src/sgml/ddl.sgml=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 =C2=A09 +-
doc/src/sgml/ref/alter_table.sgml=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0|=C2=A0 15 +-
doc/src/sgml/ref/create_index.sgml=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 |=C2=A0 =C2=A05 +
doc/src/sgml/ref/create_table.sgml=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 |=C2=A0 18 +-
src/backend/bootstrap/bootparse.y=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0|=C2=A0 =C2=A02 +
src/backend/catalog/index.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0|=C2=A0 50 ++++-
src/backend/catalog/pg_constraint.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0|=C2=A0 76 +++++++
src/backend/catalog/toasting.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 |=C2=A0 =C2=A04 +-
src/backend/commands/indexcmds.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 | 125 +++++++++--
src/backend/commands/tablecmds.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 |=C2=A0 71 ++++++-
src/backend/parser/analyze.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A07 +
src/backend/parser/parse_utilcmd.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 |=C2=A0 31 +--
src/backend/tcop/utility.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A01 +
src/bin/pg_dump/t/002_pg_dump.pl=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 |=C2=A0 65 ++++++
src/include/catalog/index.h=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0|=C2=A0 =C2=A05 +-
src/include/catalog/pg_constraint_fn.h=C2=A0 =C2=A0 =C2=A0 =C2=A0 |=C2= =A0 =C2=A04 +-
src/include/commands/defrem.h=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0|=C2=A0 =C2=A01 +
src/include/parser/parse_utilcmd.h=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 |=C2=A0 =C2=A03 +-
src/test/regress/expected/alter_table.out=C2=A0 =C2=A0 =C2=A0|=C2=A0 = =C2=A08 -
src/test/regress/expected/create_index.out=C2=A0 =C2=A0 |=C2=A0 =C2=A0= 6 +
src/test/regress/expected/create_table.out=C2=A0 =C2=A0 |=C2=A0 12 --<= br> src/test/regress/expected/indexing.out=C2=A0 =C2=A0 =C2=A0 =C2=A0 | 29= 4 +++++++++++++++++++++++++-
src/test/regress/expected/insert_conflict.out |=C2=A0 =C2=A02 +-
src/test/regress/sql/alter_table.sql=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= |=C2=A0 =C2=A02 -
src/test/regress/sql/create_index.sql=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0|=C2=A0 =C2=A06 +
src/test/regress/sql/create_table.sql=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0|=C2=A0 =C2=A08 -
src/test/regress/sql/indexing.sql=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0| 172 ++++++++++++++-
27 files changed, 907 insertions(+), 95 deletions(-)


--001a11434486d9eb080565980cc1--