Re: Postgres 11: Table Partitioning and Primary Keys

"David G. Johnston" <[email protected]> Mon, 8 Jul 2019 23:59:32 -0700
Newsgroups gmane.comp.db.postgresql.devel.general,gmane.comp.db.postgresql.devel.documentation
Message-ID <CAKFQuwZJntbGmdMyY8=Af7-g=-MCu_ZdZU-a9ygub+umcRrjdQ@mail.gmail.com>
--00000000000008e616058d3a1b3f
Content-Type: text/plain; charset="UTF-8"

On Mon, Jul 8, 2019 at 11:34 PM Michael Paquier <[email protected]> wrote:

> On Mon, Jul 08, 2019 at 08:12:18PM -0700, David G. Johnston wrote:
> > Reads a bit backward.  How about:
> >
> > "As uniqueness can only be enforced within an individual partition when
> > defining a primary key on a partitioned table all columns present in the
> > partition key must also exist in the primary key."
>
> Yes, I was not really inspired on this one.
>
> Looking closely at the code in DefineIndex() (and as Rajkumar has
> mentioned upthread for unique constraints) this can happen for primary
> keys, unique constraints and exclusion constraints.  So we had better
> mention all three of them.  I am not sure that we need to be explicit
> about the uniqueness part though, let's say the following:
> "When defining a primary key, a unique constraint or an exclusion
> constraint on a partitioned table, all the columns present in the
> constraint definition must be included in the partition key."
>
>
That isn't true, it needs to be reversed at least:

"Table-scoped constraints defined on a partitioned table - primary key,
unique, and exclusion - must include the partition key columns because the
enforcement of such constraints is performed independently on each
partition."

The complaint here is the user puts a PK id column on their partitioned
table and wonders why they need the partition key columns to also be in the
PK.  The answer is the description provided above - with the reminder (or
initial cluing in depending) to the reader that this limitation exists
because we do not implement global constraints/indexes but instead the
definition on the partitioned table is simply copied to all of its
partitions.  For me this seems worthy of recapping at this location (I
haven't gone looking for a nice cross-reference link to put there).

David J.

--00000000000008e616058d3a1b3f
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div class=3D"gmail_default" style=3D"fon=
t-family:arial,helvetica,sans-serif"><span style=3D"font-family:Arial,Helve=
tica,sans-serif">On Mon, Jul 8, 2019 at 11:34 PM Michael Paquier &lt;<a hre=
f=3D"mailto:[email protected]">[email protected]</a>&gt; wrote:</span><=
br></div></div><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote"=
 style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);p=
adding-left:1ex">On Mon, Jul 08, 2019 at 08:12:18PM -0700, David G. Johnsto=
n wrote:<br>
&gt; Reads a bit backward.=C2=A0 How about:<br>
&gt; <br>
&gt; &quot;As uniqueness can only be enforced within an individual partitio=
n when<br>
&gt; defining a primary key on a partitioned table all columns present in t=
he<br>
&gt; partition key must also exist in the primary key.&quot;<br>
<br>
Yes, I was not really inspired on this one.<br>
<br>
Looking closely at the code in DefineIndex() (and as Rajkumar has<br>
mentioned upthread for unique constraints) this can happen for primary<br>
keys, unique constraints and exclusion constraints.=C2=A0 So we had better<=
br>
mention all three of them.=C2=A0 I am not sure that we need to be explicit<=
br>
about the uniqueness part though, let&#39;s say the following:<br>
&quot;When defining a primary key, a unique constraint or an exclusion<br>
constraint on a partitioned table, all the columns present in the<br>
constraint definition must be included in the partition key.&quot;<br><br><=
/blockquote><div><br></div><div class=3D"gmail_default" style=3D"font-famil=
y:arial,helvetica,sans-serif">That isn&#39;t true, it needs to be reversed =
at least:</div><div class=3D"gmail_default" style=3D"font-family:arial,helv=
etica,sans-serif"><br></div><div class=3D"gmail_default" style=3D"font-fami=
ly:arial,helvetica,sans-serif">&quot;Table-scoped constraints defined on a =
partitioned table - primary key, unique, and exclusion - must include the p=
artition key columns because the enforcement of such constraints is perform=
ed independently on each partition.&quot;</div><div class=3D"gmail_default"=
 style=3D"font-family:arial,helvetica,sans-serif"><br></div><div class=3D"g=
mail_default" style=3D"font-family:arial,helvetica,sans-serif">The complain=
t here is the user puts a PK id column on their partitioned table and wonde=
rs why they need the partition key columns to also be in the PK.=C2=A0 The =
answer is the description provided above - with the reminder (or initial cl=
uing in depending) to the reader that this limitation exists because we do =
not implement global constraints/indexes but instead the definition on the =
partitioned table is simply copied to all of its partitions.=C2=A0 For me t=
his seems worthy of recapping at this location (I haven&#39;t gone looking =
for a nice cross-reference link to put there).</div><div class=3D"gmail_def=
ault" style=3D"font-family:arial,helvetica,sans-serif"><br></div><div class=
=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif">David J=
.</div><div class=3D"gmail_default" style=3D"font-family:arial,helvetica,sa=
ns-serif"><br></div></div></div>

--00000000000008e616058d3a1b3f--