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 1nx7Zb-0005JN-Fh for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Jun 2022 13:42:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nx7ZY-0006d5-Hl for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Jun 2022 13:42:40 +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 1nx7ZY-0006cw-38 for pgsql-hackers@lists.postgresql.org; Fri, 03 Jun 2022 13:42:40 +0000 Received: from mail1.dalibo.net ([212.83.143.11] helo=mail.dalibo.com) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nx7ZV-0007ya-Cx for pgsql-hackers@postgresql.org; Fri, 03 Jun 2022 13:42:39 +0000 Received: from karst (larco.ioguix.net [78.202.0.6]) by mail.dalibo.com (Postfix) with ESMTPSA id 3DA441F9C8 for ; Fri, 3 Jun 2022 15:42:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=dalibo.com; s=a; t=1654263754; bh=RYBxs5wIIuKbAIqusU9BWPTg72KdwrcodLFC5Wu2pXo=; h=Date:From:To:Subject:From; b=f8dgUac7SRpq4QRE22O3eoiPrhF7MspDbeZ7NPXbydIRvHfnornh1Sh+I2XDMsOr7 jv1YOKGNSbqKFrTbIth6jwEc2f3J0xMVZAsmDzM+r/HGX+Ev0Gt9juBQciUQb/cIPc M39w+1tdbXPpmKtpp4pdFDc0PzA7u6k4tt91wFb8= Date: Fri, 3 Jun 2022 15:42:32 +0200 From: Jehan-Guillaume de Rorthais To: pgsql-hackers Subject: Self FK oddity when attaching a partition Message-ID: <20220603154232.1715b14c@karst> Organization: Dalibo MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/EU_bqBvW_.4MD7uYUTHHR/p" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --MP_/EU_bqBvW_.4MD7uYUTHHR/p Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi all, While studying the issue discussed in thread "Detaching a partition with a FK on itself is not possible"[1], I stumbled across an oddity while attaching a partition having the same multiple self-FK than the parent table. Only one of the self-FK is found as a duplicate. Find in attachment some SQL to reproduce the scenario. Below the result of this scenario (constant from v12 to commit 7e367924e3). Why "child1_id_abc_no_part_fkey" is found duplicated but not the three others? From pg_constraint, only "child1_id_abc_no_part_fkey" has a "conparentid" set. conname | conparentid | conrelid | confrelid -----------------------------+-------------+----------+----------- child1_id_abc_no_part_fkey | 16901 | 16921 | 16921 child1_id_def_no_part_fkey | 0 | 16921 | 16921 child1_id_ghi_no_part_fkey | 0 | 16921 | 16921 child1_id_jkl_no_part_fkey | 0 | 16921 | 16921 parent_id_abc_no_part_fkey | 16901 | 16921 | 16894 parent_id_abc_no_part_fkey | 0 | 16894 | 16894 parent_id_abc_no_part_fkey1 | 16901 | 16894 | 16921 parent_id_def_no_part_fkey | 16906 | 16921 | 16894 parent_id_def_no_part_fkey | 0 | 16894 | 16894 parent_id_def_no_part_fkey1 | 16906 | 16894 | 16921 parent_id_ghi_no_part_fkey | 0 | 16894 | 16894 parent_id_ghi_no_part_fkey | 16911 | 16921 | 16894 parent_id_ghi_no_part_fkey1 | 16911 | 16894 | 16921 parent_id_jkl_no_part_fkey | 0 | 16894 | 16894 parent_id_jkl_no_part_fkey | 16916 | 16921 | 16894 parent_id_jkl_no_part_fkey1 | 16916 | 16894 | 16921 (16 rows) Table "public.child1" [...] Partition of: parent FOR VALUES IN ('1') Partition constraint: ((no_part IS NOT NULL) AND (no_part = '1'::smallint)) Indexes: "child1_pkey" PRIMARY KEY, btree (id, no_part) Check constraints: "child1" CHECK (no_part = 1) Foreign-key constraints: "child1_id_def_no_part_fkey" FOREIGN KEY (id_def, no_part) REFERENCES child1(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT "child1_id_ghi_no_part_fkey" FOREIGN KEY (id_ghi, no_part) REFERENCES child1(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT "child1_id_jkl_no_part_fkey" FOREIGN KEY (id_jkl, no_part) REFERENCES child1(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT TABLE "parent" CONSTRAINT "parent_id_abc_no_part_fkey" FOREIGN KEY (id_abc, no_part) REFERENCES parent(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT TABLE "parent" CONSTRAINT "parent_id_def_no_part_fkey" FOREIGN KEY (id_def, no_part) REFERENCES parent(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT TABLE "parent" CONSTRAINT "parent_id_ghi_no_part_fkey" FOREIGN KEY (id_ghi, no_part) REFERENCES parent(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT TABLE "parent" CONSTRAINT "parent_id_jkl_no_part_fkey" FOREIGN KEY (id_jkl, no_part) REFERENCES parent(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT Referenced by: TABLE "child1" CONSTRAINT "child1_id_def_no_part_fkey" FOREIGN KEY (id_def, no_part) REFERENCES child1(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT TABLE "child1" CONSTRAINT "child1_id_ghi_no_part_fkey" FOREIGN KEY (id_ghi, no_part) REFERENCES child1(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT TABLE "child1" CONSTRAINT "child1_id_jkl_no_part_fkey" FOREIGN KEY (id_jkl, no_part) REFERENCES child1(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT TABLE "parent" CONSTRAINT "parent_id_abc_no_part_fkey" FOREIGN KEY (id_abc, no_part) REFERENCES parent(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT TABLE "parent" CONSTRAINT "parent_id_def_no_part_fkey" FOREIGN KEY (id_def, no_part) REFERENCES parent(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT TABLE "parent" CONSTRAINT "parent_id_ghi_no_part_fkey" FOREIGN KEY (id_ghi, no_part) REFERENCES parent(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT TABLE "parent" CONSTRAINT "parent_id_jkl_no_part_fkey" FOREIGN KEY (id_jkl, no_part) REFERENCES parent(id, no_part) ON UPDATE RESTRICT ON DELETE RESTRICT Regards, [1] https://www.postgresql.org/message-id/flat/20220321113634.68c09d4b%40karst#83c0880a1b4921fcd00d836d4e6bceb3 --MP_/EU_bqBvW_.4MD7uYUTHHR/p Content-Type: application/sql Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=self-fk-after-part-attach.sql RFJPUCBUQUJMRSBJRiBFWElTVFMgcGFyZW50LCBjaGlsZDE7CgpDUkVBVEUgVEFCTEUgcGFyZW50 ICgKICAgIGlkIGJpZ2ludCBOT1QgTlVMTCBkZWZhdWx0IDEsCiAgICBub19wYXJ0IHNtYWxsaW50 IE5PVCBOVUxMLAogICAgaWRfYWJjIGJpZ2ludCwKICAgIGlkX2RlZiBiaWdpbnQsCiAgICBpZF9n aGkgYmlnaW50LAogICAgaWRfamtsIGJpZ2ludCwKICAgIHRzX2NyZWF0aW9uIHRpbWVzdGFtcCB3 aXRob3V0IHRpbWUgem9uZSBkZWZhdWx0IG5vdygpICBOT1QgTlVMTCwKICAgIEZPUkVJR04gS0VZ IChpZF9hYmMsIG5vX3BhcnQpIFJFRkVSRU5DRVMgcGFyZW50KGlkLCBub19wYXJ0KSBPTiBVUERB VEUgUkVTVFJJQ1QgT04gREVMRVRFIFJFU1RSSUNULAogICAgRk9SRUlHTiBLRVkgKGlkX2RlZiwg bm9fcGFydCkgUkVGRVJFTkNFUyBwYXJlbnQoaWQsIG5vX3BhcnQpIE9OIFVQREFURSBS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