Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1SEg-0004Ri-0x for pgsql-general@arkaria.postgresql.org; Thu, 08 Jul 2021 11:30:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m1SEe-0006bH-LY for pgsql-general@arkaria.postgresql.org; Thu, 08 Jul 2021 11:30:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1SEe-0006aP-21 for pgsql-general@lists.postgresql.org; Thu, 08 Jul 2021 11:30:28 +0000 Received: from mail-ot1-x32f.google.com ([2607:f8b0:4864:20::32f]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m1SEX-00052D-DX for pgsql-general@lists.postgresql.org; Thu, 08 Jul 2021 11:30:26 +0000 Received: by mail-ot1-x32f.google.com with SMTP id i12-20020a05683033ecb02903346fa0f74dso5479401otu.10 for ; Thu, 08 Jul 2021 04:30:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=/NuJ//zd/KdqVADedMmvAK03oZSlIfov7KNMuanZjGY=; b=AYeizMbe5K3N01eyDj91uMg77uFtR5bcUuVavXI8S09rjq2q4Son7WfRC1psxWvUZl /CP1az82wu4G34aNKkz9de4X1D3BNBX3ZAItR3f9vD/WnRBDMqavpicWq9buY7LO7FZO oH61j+GM+vBzjGreM+dsGMKgvqBsD3FSg0Y4vCLZ8GEBc8PvLgkUzynMS/ArfTmswAWS Ik1aFzXHBF3XXT6Jblgk3XVqnYTtmplqaHRcYf4XD8JqUBfPsMh0NXM8tMfQA0Tr1QkI 05NvxW0fqGmtCi7jr+5jAo9zhyZdlPokcSfyNn8KiKDFhxODV+ELclw2QO/THf+o2Zn7 pvcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=/NuJ//zd/KdqVADedMmvAK03oZSlIfov7KNMuanZjGY=; b=N4YTYaSYDE5YwkstKwuCKz6qQivUoB4+aIZ2kbr7wIuZdAwPXVYz5fViSeRuFCPka4 0XOaLdqjxeWERMP2EOuzQDk51TwaUw1qY9+Yp50AXUfxk4bN5PI8W/YOYZOUH6CpY+AA F7IJGQLIy/x9m/logyr7Mgeb1hGeTguQs5nsx4Zvf7Jwv3qayZ7jA3TRfG+NkKjzUgDP VIZo/ufLfDy2DpJ/6KFxkZgUBApRH7yr6jhtZD7skHEOkRVqb28JRngSdEF39bns/CR5 6Mjd4CiZ72W7Mtkco8ks+JZSOxSoTQ5deDDcva3W2sj9WRzkWg7/vBotrbG/MRc+d9nW mehQ== X-Gm-Message-State: AOAM532ndEbHKoiBEWYybTR1h3noalG5ViYVYu1IZ0Y+PFyDL3rWbmAi 0e6kaEKRvQdzJ40YY+VsDzEdaDwJSTSLmA== X-Google-Smtp-Source: ABdhPJwClXiXI+PshEfZ1aoBasU5wj9SO/xAG/x65Vbtj1eoqOqiwNMWUZ20mnP7b2QkjosLdtuCmw== X-Received: by 2002:a9d:6358:: with SMTP id y24mr1113666otk.101.1625743820268; Thu, 08 Jul 2021 04:30:20 -0700 (PDT) Received: from [192.168.88.10] (ip174-67-141-204.no.no.cox.net. [174.67.141.204]) by smtp.googlemail.com with ESMTPSA id v203sm470150oib.37.2021.07.08.04.30.19 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 08 Jul 2021 04:30:19 -0700 (PDT) Subject: Re: On partitioning, PKs and FKs To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <9cb3fdc8-8732-8db6-5342-bbde540872ea@gmail.com> Date: Thu, 8 Jul 2021 06:30:18 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------3691FF92E26166FEC8C41E73" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------3691FF92E26166FEC8C41E73 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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. --------------3691FF92E26166FEC8C41E73 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit 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_1 etc.
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.
--------------3691FF92E26166FEC8C41E73--