Re: Unique constraint across all partitions?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Ron <ronljohnsonjr(at)gmail(dot)com>
Cc: Pgsql-admin <pgsql-admin(at)lists(dot)postgresql(dot)org>
Subject: Re: Unique constraint across all partitions?
Date: 2021-01-26 00:01:45
Message-ID: CAKFQuwZx-3A4Oqk69DTBk0MaVa9W7f=LPnmQpx1Q5Nj+=9PPVw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Mon, Jan 25, 2021 at 4:02 PM Ron <ronljohnsonjr(at)gmail(dot)com> wrote:

> On 1/25/21 4:44 PM, S Bob wrote:
>
> > Is it possible to enforce a unique constraint across all partitions for
> > either declarative or inheritance based partitioning? Preferably
> > declarative based.
>
> You can create unique indexes on parent tables. Or am I misunderstanding
> you?
>

You seem to misunderstand how partitions and unique constraints work.

Each partition gets its own copy of the unique index/constraint and ensures
that duplicates do not appear therein. If the specification for the unique
constraint is something like "id bigint" then the value 5 can appear on
multiple partitions just fine - the partition key(s) effectively become
part of the unique constraint/index even though the values are not actually
stored in the index.

So, yes, so long as your unique constraint takes into consideration the
partitioning scheme it will enforce uniqueness across all of the
partitions. But the answer to the full, but possibly implied, question is
no.

David J.

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Ron 2021-01-26 00:09:32 Re: Unique constraint across all partitions?
Previous Message Ron 2021-01-25 23:02:02 Re: Unique constraint across all partitions?