Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sgtNz-009ohK-59 for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Aug 2024 22:00:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sgtNv-00FMOz-RD for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Aug 2024 22:00:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sgtNv-00FMOq-6K for pgsql-hackers@lists.postgresql.org; Wed, 21 Aug 2024 22:00:56 +0000 Received: from fout1-smtp.messagingengine.com ([103.168.172.144]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sgtNr-000q25-Jk for pgsql-hackers@postgresql.org; Wed, 21 Aug 2024 22:00:55 +0000 Received: from phl-compute-05.internal (phl-compute-05.nyi.internal [10.202.2.45]) by mailfout.nyi.internal (Postfix) with ESMTP id 02963138FF35; Wed, 21 Aug 2024 18:00:51 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Wed, 21 Aug 2024 18:00:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1724277650; x= 1724364050; bh=l1d+GAqy/j+P64EfWLr+4KmeJ43iUmq9Mf+/P3WoXds=; b=J qksSniQhvr8u+XFz0DKOovsg1B8cvtK6Pi5NZoqEeo6jvCp4RTA/86TC3KW0SKD6 zBaPVgYp31Py4ndJ5HsLvo7Za9uo3WbnCngPFAopJI6h0bbBIYMp06P/Bbdoq25/ hWhuVQE+a+o5sOCx1esy1dzpcZC+qBMw3F1eERvLeD3xL7ov+r644NrN8WXR5hzl DTme0FsS4PR3c+07tuwA2D3OkHf7d4qMBXU08CVOedb6i6KzylerhXOfG1TXLemX GPojv0J1Waphh9JkwmUasmU3AKFPVKGfKsMgVP6ZpVnPCGYWfgYC71Cube0Sf/+H 7KeeZm8ikxVPyj3jDZyvg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudduledgtdehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkgggtugfgjgesmhekreertddtjeen ucfhrhhomheptehlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhh drnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhepudelkeekgfdttdettdekgfdu vdeiffelteegjeeihfetheekveeftedtuddvhfeunecuffhomhgrihhnpegvnhhtvghrph hrihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgr ihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgpdhnsggprh gtphhtthhopeejpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehjghgurhesuggr lhhisghordgtohhmpdhrtghpthhtohepvgigtghluhhsihhonhesghhmrghilhdrtghomh dprhgtphhtthhopehtnhgurhifrghnghesghhmrghilhdrtghomhdprhgtphhtthhopeii hhhjfihpkhhusehgmhgrihhlrdgtohhmpdhrtghpthhtohepghhuihhllhgruhhmvgeslh gvlhgrrhhgvgdrihhnfhhopdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrshesphho shhtghhrvghsqhhlrdhorhhgpdhrtghpthhtohepthhhohhmrghsrdgsrggvhhhlvghrvd esshgssgdrtghh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 21 Aug 2024 18:00:49 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1724277646; bh=2OdzoC0XMiaoY5kRQd+x6Tn0qLNp4r9cFY1oS5mQkGs=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=gAs4EGHFdEgO+IeIlJJuDyH8770dyUKmJOVvdWzutP9l2cXtl9RA73MRkENYW8zFT yrDmAk8O34IaKRgQC4izC0CUgtwl2YQ9YU7LdGt7Z+sczq79lQQsH8kGacbpD1kBpl vcv/zeE0sQ5p4zwai0uahplGa7mf8p+UDH+nyuJDdO8/OHUJ5rKCPXGRNwWf6fuAM5 6UGANzvK73knW8yKUtGrrqSsd9cbb5UIMUsGy0JYIjHn+XAWxxSuspi9FOJ84z2vuP i8idz/uALmQPaG0TpukT8gMbsqx7g+4PEkjH530A17yAiXrMDAv1p77m20++sh6Jr4 Wdhm4lCTc+/dw== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id E98CFE9; Wed, 21 Aug 2024 18:00:46 -0400 (-04) Date: Wed, 21 Aug 2024 18:00:46 -0400 From: Alvaro Herrera To: Tender Wang Cc: Junwang Zhao , Jehan-Guillaume de Rorthais , "pgsql-hackers@postgresql.org" , Alexander Lakhin , Baehler Thomas SBB CFF FFS , Guillaume Lelarge Subject: Re: [BUG] Fix DETACH with FK pointing to a partitioned table fails Message-ID: <202408212200.xm4zwv6sxifd@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="cnnevf6dxumszimk" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <202408200225.lasmb6pskolh@alvherre.pgsql> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --cnnevf6dxumszimk Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2024-Aug-19, Alvaro Herrera wrote: > I haven't pushed it yet, mostly because of being unsure about not doing > anything for the oldest branches (14 and back). Last night, after much mulling on this, it occurred to me that one easy way out of this problem for the old branches, without having to write more code, is to simply remove the constraint from the partition when it's detached (but only if they reference a partitioned relation). It's not a great solution, but at least we're no longer leaving bogus catalog entries around. That would be like the attached patch, which was cut from 14 and applies cleanly to 12 and 13. I'd throw in a couple of tests and call it a day. (TBH the idea of leaving the partition without a foreign key feels to me like travelling in a car without a seat belt -- it feels instinctively dangerous. This is why I went such lengths to keep FKs on detach initially. But I'm not inclined to spend more time on this issue. However ... what about fixing catalog content that's already broken after past detach ops?) -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ --cnnevf6dxumszimk Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="v14-0001-drop-constraint-when-detaching.patch" From e95fde451ee73b89f1a5ba50b7f2e34f5fb98bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Herrera?= Date: Wed, 21 Aug 2024 17:47:03 -0400 Subject: [PATCH v14] drop constraint when detaching --- src/backend/commands/tablecmds.c | 79 +++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index c39e6799ca..7ef904f63e 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -18143,8 +18143,8 @@ DetachPartitionFinalize(Relation rel, Relation partRel, bool concurrent, DropClonedTriggersFromPartition(RelationGetRelid(partRel)); /* - * Detach any foreign keys that are inherited. This includes creating - * additional action triggers. + * Detach any foreign keys that are inherited -- or, if they reference + * partitioned tables, drop them. */ fks = copyObject(RelationGetFKeyList(partRel)); foreach(cell, fks) @@ -18152,7 +18152,6 @@ DetachPartitionFinalize(Relation rel, Relation partRel, bool concurrent, ForeignKeyCacheInfo *fk = lfirst(cell); HeapTuple contup; Form_pg_constraint conform; - Constraint *fkconstraint; contup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(fk->conoid)); if (!HeapTupleIsValid(contup)) @@ -18167,34 +18166,62 @@ DetachPartitionFinalize(Relation rel, Relation partRel, bool concurrent, continue; } - /* unset conparentid and adjust conislocal, coninhcount, etc. */ + /* Mark the constraint as independent */ ConstraintSetParentConstraint(fk->conoid, InvalidOid, InvalidOid); /* - * Make the action triggers on the referenced relation. When this was - * a partition the action triggers pointed to the parent rel (they - * still do), but now we need separate ones of our own. + * If the constraint references a partitioned table, just drop the + * constraint, because it's more work to preserve the constraint + * correctly. + * + * If it references a plain table, then we can create the action + * triggers and it'll be okay. */ - fkconstraint = makeNode(Constraint); - fkconstraint->contype = CONSTRAINT_FOREIGN; - fkconstraint->conname = pstrdup(NameStr(conform->conname)); - fkconstraint->deferrable = conform->condeferrable; - fkconstraint->initdeferred = conform->condeferred; - fkconstraint->location = -1; - fkconstraint->pktable = NULL; - fkconstraint->fk_attrs = NIL; - fkconstraint->pk_attrs = NIL; - fkconstraint->fk_matchtype = conform->confmatchtype; - fkconstraint->fk_upd_action = conform->confupdtype; - fkconstraint->fk_del_action = conform->confdeltype; - fkconstraint->old_conpfeqop = NIL; - fkconstraint->old_pktable_oid = InvalidOid; - fkconstraint->skip_validation = false; - fkconstraint->initially_valid = true; + if (get_rel_relkind(fk->confrelid) == RELKIND_PARTITIONED_TABLE) + { + ObjectAddress constraddr; - createForeignKeyActionTriggers(partRel, conform->confrelid, - fkconstraint, fk->conoid, - conform->conindid); + /* make the dependency deletions above visible */ + CommandCounterIncrement(); + + /* + * And drop the constraint and its triggers; if this partition is + * partitioned, then the corresponding objects in the partitions + * too. + */ + ObjectAddressSet(constraddr, ConstraintRelationId, fk->conoid); + performDeletion(&constraddr, DROP_CASCADE, 0); + } + else + { + Constraint *fkconstraint; + + /* + * Make the action triggers on the referenced relation. When this + * was a partition the action triggers pointed to the parent rel + * (they still do), but now we need separate ones of our own. + */ + fkconstraint = makeNode(Constraint); + fkconstraint->contype = CONSTRAINT_FOREIGN; + fkconstraint->conname = pstrdup(NameStr(conform->conname)); + fkconstraint->deferrable = conform->condeferrable; + fkconstraint->initdeferred = conform->condeferred; + fkconstraint->location = -1; + fkconstraint->pktable = NULL; + fkconstraint->fk_attrs = NIL; + fkconstraint->pk_attrs = NIL; + fkconstraint->fk_matchtype = conform->confmatchtype; + fkconstraint->fk_upd_action = conform->confupdtype; + fkconstraint->fk_del_action = conform->confdeltype; + fkconstraint->old_conpfeqop = NIL; + fkconstraint->old_pktable_oid = InvalidOid; + fkconstraint->skip_validation = false; + fkconstraint->initially_valid = true; + + createForeignKeyActionTriggers(partRel, conform->confrelid, + fkconstraint, fk->conoid, + conform->conindid); + } ReleaseSysCache(contup); } -- 2.39.2 --cnnevf6dxumszimk--