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 1tA7zq-00DsKM-Pk for pgsql-hackers@arkaria.postgresql.org; Sun, 10 Nov 2024 13:28:54 +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 1tA7zn-006w9A-6s for pgsql-hackers@arkaria.postgresql.org; Sun, 10 Nov 2024 13:28:51 +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 1tA7zm-006w92-IZ for pgsql-hackers@lists.postgresql.org; Sun, 10 Nov 2024 13:28:51 +0000 Received: from fhigh-a6-smtp.messagingengine.com ([103.168.172.157]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tA7zi-001CYl-Vw for pgsql-hackers@postgresql.org; Sun, 10 Nov 2024 13:28:50 +0000 Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfhigh.phl.internal (Postfix) with ESMTP id 864DB1140131; Sun, 10 Nov 2024 08:28:45 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-11.internal (MEProxy); Sun, 10 Nov 2024 08:28:45 -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=1731245325; x=1731331725; bh=l vpHI3MliTxqIp75SMJRVoTlcIBvcCxS6YBEjgGVXsw=; b=cf7u0a6O4bi9Lit3A yBks36r1GlVZgvJQ/fkqNiE6iVGsV83q89b2O25EXgFKJyYYSRihgwyWwER9Qq4G wc6bU8dMIYUS5aau6oKy9Qp5ZQtcCcUR2UA+gzkWNwjXlmCATUlTQsR3U/mt+OVs eODNeYZ+t2x9aODeDAeJpV+zK4vabGk++87DqPvh9JxhVrEW7OlSUQ3K31UuZxb6 bZZytAQM7PzvAeBUF2BDmm/uOsO66tkFI0RlCQ9Tb2aAaRyTNivgOgbKRtzoCErw t3a6YpFZpHrav0mUTcE9bj5mOnWMAXAbuPthYkP9Nu/mpjS43SvDFUFDr/VqNhWK LzgtA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddruddtgdehudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpeffhffvvefukfggtggugfgjsehtkeertddttdejnecu hfhrohhmpeetlhhvrghrohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrd hnohdqihhprdhorhhgqeenucggtffrrghtthgvrhhnpedvkedtffduffdtffffheffhfej jefhgfeiueeukeejkeffgfdufffhudffffeuveenucffohhmrghinhepvghnthgvrhhprh hishgvuggsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghi lhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrghdpnhgspghrtg hpthhtohepiedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepjhhgughrsegurghl ihgsohdrtghomhdprhgtphhtthhopegvgigtlhhushhiohhnsehgmhgrihhlrdgtohhmpd hrtghpthhtohepthhnughrfigrnhhgsehgmhgrihhlrdgtohhmpdhrtghpthhtohepghhu ihhllhgruhhmvgeslhgvlhgrrhhgvgdrihhnfhhopdhrtghpthhtohepphhgshhqlhdqhh grtghkvghrshesphhoshhtghhrvghsqhhlrdhorhhgpdhrtghpthhtohepthhglhesshhs shdrphhghhdrphgrrdhush X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 10 Nov 2024 08:28:44 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1731245320; bh=q3pTYWIeqdAw6QtkmyVxpvqh5i8FqJ9CfO6HqpyfjE0=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=B9or+Dx3JdQs2WwXRYmKWGj2UmqiT4MKcKo7UyHXmYp92nF8lH+cUgnQA8rt67D+n pMH1KkguUEngyEO/LY0aD9ljdWLAwPmzbUxA0iduNWBlm8EGHDCpi0G+JvUyIQl4uH NOPKpYviKpJoxySvdeEKh/RZUU3B7ieLsLa4+34256YWLAju9Z0hGnYM7BZNn0msb5 LMGyf1o5HJM7BbGOEhoDoFRKN+Bwzt/wRgqoP32IcWuz8f5Tfg6c3mF3ojmrfgnmmJ C/WlZ1QwlrvoxdZVppPb1XRNTpGfSmK7CLBO48q00Etc1O+p7v/+fO1dXpo8Sv6UIg iX5vpCUAbGgYA== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id E56E0EE; Sun, 10 Nov 2024 14:28:40 +0100 (CET) Date: Sun, 10 Nov 2024 14:28:40 +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: <202411101328.qo4lzsmw2ixl@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <3645003.1731080579@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-Nov-08, Tom Lane wrote: > Hmm ... interestingly, if I run this in HEAD's regression database, > I get > > constrained table | constraint | references > -------------------+---------------+------------- > clstr_tst | clstr_tst_con | clstr_tst_s > (1 row) Eeek. > So it looks like this query needs a guard to make it ignore > constraints on traditional-inheritance tables. Hmm, looks tricky, the only thing I found was to only consider rows in pg_inherit if there's a corresponding one in pg_partitioned_table. This should do it. I added the DROP/ADD commands. I also added some pg_catalog schema quals, though that may be kinda useless. Anyway, this reports empty in the regression database. SELECT conrelid::pg_catalog.regclass AS "constrained table", conname AS constraint, confrelid::pg_catalog.regclass AS "references", pg_catalog.format('ALTER TABLE %s DROP CONSTRAINT %I;', conrelid::regclass, conname), pg_catalog.format('ALTER TABLE %s ADD CONSTRAINT %I %s;', conrelid::regclass, conname, pg_catalog.pg_get_constraintdef(oid)) FROM pg_catalog.pg_constraint WHERE contype = 'f' and conparentid = 0 AND (SELECT count(*) FROM pg_catalog.pg_constraint p2 WHERE conparentid = pg_constraint.oid) <> (SELECT count(*) FROM pg_catalog.pg_inherits WHERE EXISTS (SELECT 1 FROM pg_catalog.pg_partitioned_table WHERE partrelid = inhparent) AND inhparent = pg_constraint.conrelid OR inhparent = pg_constraint.confrelid) ; I would have loved to be able to add the constraint as NOT VALID followed by a separate VALIDATE command, because if there are any RI violations, the constraint would now be in place to prevent future ones. However, =# ALTER TABLE fk2367 ADD CONSTRAINT fk_a_fkey FOREIGN KEY (a) REFERENCES pk(a) NOT VALID; ERROR: cannot add NOT VALID foreign key on partitioned table "fk2367" referencing relation "pk" DETAIL: This feature is not yet supported on partitioned tables. So it looks like we should suggest to save the output of the query, execute each DROP followed by each ADD, and if the latter fails, fix the violations and retry the ADD. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "Java is clearly an example of money oriented programming" (A. Stepanov)