pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: On partitioning, PKs and FKs
Date: Thu, 8 Jul 2021 06:30:18 -0500
Message-ID: <9cb3fdc8-8732-8db6-5342-bbde540872ea@gmail.com> (raw)
In-Reply-To: <CAD1W9HW+u8uouPfV9=UED4-TyXGL_3ffbhMU53iPUUGkpg9ETA@mail.gmail.com>
References: <CAD1W9HW+u8uouPfV9=UED4-TyXGL_3ffbhMU53iPUUGkpg9ETA@mail.gmail.com>

On 7/8/21 3:42 AM, Wiwwo Staff wrote:
> Hi!
> I have a big table bigTable which I partitioned by hash on field columnX, 
> by creating bigTable_0, bigTable_1etc.
> Since I need a PK on bigTable.id, and table is not partitioned by id, and 
> columnX is not unique, i added PK on bigTable_0.id, bigTable_1.id etc. So 
> far, so good.
>
> Now I have anotherTable, which has column bigTable_id referencing bigTable.id.
> Creating FK anotherTable__bigTable_FK on anotherTable.bigTable_id-you 
> guess :D- fails with
>
>     [42830] ERROR: there is no unique constraint matching given keys for
>     referenced table "bigTable"
>
> Also creating a FK pointing to one of the hash tables bigTable_0.id, 
> bigTable_1.id etc. also fails with
>
>     ERROR:  23503: insert or update on table "anotherTable" violates
>     foreign key constraint "anotherTable__bigTable_FK".
>
>
> Do I have a way out? :)

https://www.postgresql.org/docs/12/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE

Section "5.11.2.3. Limitations" says, "Unique constraints (and hence primary 
keys) on partitioned tables must include all the partition key columns."

Thus, the bigTable PK must be on id, columnX, (No, I don't like it either.)

-- 
Angular momentum makes the world go 'round.

view thread (8+ messages)  latest in thread

Message-ID: <9cb3fdc8-8732-8db6-5342-bbde540872ea@gmail.com>
Permalink:  ../9cb3fdc8-8732-8db6-5342-bbde540872ea@gmail.com/
Also on:    postgresql.org/message-id/9cb3fdc8-8732-8db6-5342-bbde540872ea@gmail.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ronljohnsonjr@gmail.com
  Subject: Re: On partitioning, PKs and FKs
  In-Reply-To: <9cb3fdc8-8732-8db6-5342-bbde540872ea@gmail.com>
  References: <CAD1W9HW+u8uouPfV9=UED4-TyXGL_3ffbhMU53iPUUGkpg9ETA@mail.gmail.com> <9cb3fdc8-8732-8db6-5342-bbde540872ea@gmail.com>

* 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