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 1t8kDa-005kQ3-Ja for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Nov 2024 17:53:22 +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 1t8kDX-007vNt-DT for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Nov 2024 17:53:19 +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 1t8kDW-007vNk-9w for pgsql-hackers@lists.postgresql.org; Wed, 06 Nov 2024 17:53:19 +0000 Received: from fout-a3-smtp.messagingengine.com ([103.168.172.146]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t8kDR-000XvU-QO for pgsql-hackers@postgresql.org; Wed, 06 Nov 2024 17:53:18 +0000 Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfout.phl.internal (Postfix) with ESMTP id A567D138025E; Wed, 6 Nov 2024 12:53:12 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-04.internal (MEProxy); Wed, 06 Nov 2024 12:53:12 -0500 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-sender :x-me-sender:x-sasl-enc; s=fm3; t=1730915592; x=1731001992; bh=Z 4/MzQW41vIHjLh/1R4BbR5VYRKmS4c/e5DPOCQz4B4=; b=Ouia0UNvy5YdE5eqQ eztA1DQ5DsAIx7VpAaj1UzHQFtAYLgSEr8MYbQ2EbVn36s8MaCI0M7qqyFgrQ8XH 2rw0el0QsssflWByUa5lxLXFqkm3KFmXFGwaPiAO+blNMNlvnU2o+C3RR6oBypQ3 A1pwVy3kgjl3Sfph2+gMa0onyhGYV29g8Uiccd6wR+c0sDB5NjsZ4DusfwVcxYIx TIXEji71+T0QvlqKmKOXYDuSrNo1IUjElg/kt2vFSe1+wrN8kmm69GBJDCLeOnu2 43HPAAFLJZx406C6UqGk5HbqWa+NUQ/RdUN7FHpCspxPVEBGOPciNt00I5bAxik6 5u5Xg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrtddvgddutdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeen ucfhrhhomheptehlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhh drnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffh jeejhffgieeuueekjeekfffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrph hrihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgr ihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgpdhnsggprh gtphhtthhopeeipdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehjghgurhesuggr lhhisghordgtohhmpdhrtghpthhtohepvgigtghluhhsihhonhesghhmrghilhdrtghomh dprhgtphhtthhopehtnhgurhifrghnghesghhmrghilhdrtghomhdprhgtphhtthhopehg uhhilhhlrghumhgvsehlvghlrghrghgvrdhinhhfohdprhgtphhtthhopehpghhsqhhlqd hhrggtkhgvrhhssehpohhsthhgrhgvshhqlhdrohhrghdprhgtphhtthhopehtghhlsehs shhsrdhpghhhrdhprgdruhhs X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 6 Nov 2024 12:53:11 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1730915588; bh=mJySsDaiViOoSbpMzu3WR4jLyiBCG7hpZFu3nO9My2I=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=D1LSwjBEFWFp0vbR1WkdopsTcwwoJSWI1a8tzqG2ln/hM23Y9NiNQAhvFaAIwvXqe e4s1r8FYrcdPksKlaRgIyKYEyVH3vq7yGBesG9D+O6Xa0AOR3X3TkdGoMONb9AT5KA enrwTuK9GxFyod8wKMFf0S5VEn5VybAbqvu6o3OQgA/SAnvmsuRlyARFIFVqyZsKLy 7ZE1XGZlEXFTwecf5GXWDEfoo0DEss7pB3fiNfwh6g4Y7vo0fMyjfyRtXHezg3wzNx v7MTbOKQaHv3nTCrRyGHKLU1mokATf5SRHGOVNkc94dEHCwlUzCnnxQchbw0KTV5uS 9If5TXJ1At3nA== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 078CB9B; Wed, 6 Nov 2024 18:53:08 +0100 (CET) Date: Wed, 6 Nov 2024 18:53:07 +0100 From: Alvaro Herrera To: Tom Lane Cc: Tender Wang , Alexander Lakhin , Jehan-Guillaume de Rorthais , "pgsql-hackers@postgresql.org" , Guillaume Lelarge Subject: Re: [BUG] Fix DETACH with FK pointing to a partitioned table fails Message-ID: <202411061753.ufb6tepg5cr4@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <202411061538.oko7rvbfp2gh@alvherre.pgsql> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-Nov-06, Alvaro Herrera wrote: > Perhaps one more task for me is to figure out a way to get a list of all > the constraints that are broken because of this ... let me see if I can > figure that out. It's gotta be something like this, SELECT conrelid::regclass AS "constrained table", conname as constraint, confrelid::regclass AS "references" FROM pg_constraint WHERE contype = 'f' and conparentid = 0 AND (SELECT count(*) FROM pg_constraint p2 WHERE conparentid = pg_constraint.oid) <> (SELECT count(*) FROM pg_inherits WHERE inhparent = pg_constraint.conrelid OR inhparent = pg_constraint.confrelid); Essentially, top-level constraints should have as many children constraint as direct partitions each partitioned table has. Ideally here you'd get an empty set, but you won't if the DETACH problem has occurred. I'll test this further later or maybe tomorrow as time allows. A quick test rig for this is: create table pk (a int primary key) partition by list (a); create table pk1 partition of pk for values in (1); create table pk2367 partition of pk for values in (2, 3, 6, 7) partition by list (a); create table pk67 partition of pk2367 for values in (6, 7) partition by list (a); create table pk2 partition of pk2367 for values in (2); create table pk3 partition of pk2367 for values in (3); create table pk6 partition of pk67 for values in (6); create table pk7 partition of pk67 for values in (7); create table pk45 partition of pk for values in (4, 5) partition by list (a); create table pk4 partition of pk45 for values in (4); create table pk5 partition of pk45 for values in (5); create table fk (a int references pk) partition by list (a); create table fk1 partition of fk for values in (1); create table fk2367 partition of fk for values in (2, 3, 6, 7) partition by list (a); create table fk67 partition of fk2367 for values in (6, 7) partition by list (a); create table fk2 partition of fk2367 for values in (2); create table fk3 partition of fk2367 for values in (3); create table fk6 partition of fk67 for values in (6); create table fk7 partition of fk67 for values in (7); create table fk45 partition of fk for values in (4, 5) partition by list (a); create table fk4 partition of fk45 for values in (4); create table fk5 partition of fk45 for values in (5); alter table fk detach partition fk2367; Before the fix, you get constrained table │ constraint │ references ───────────────────┼────────────┼──────────── fk2367 │ fk_a_fkey │ pk (1 fila) which means you need to ALTER TABLE fk2367 DROP CONSTRAINT fk_a_fkey; and then put it back. Maybe it'd be better to have the query emit the commands to drop and reconstruct the FK? -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/