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 1uKFrL-00HOPT-JP for pgsql-hackers@arkaria.postgresql.org; Wed, 28 May 2025 12:26:15 +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 1uKFrJ-00H6lz-Rp for pgsql-hackers@arkaria.postgresql.org; Wed, 28 May 2025 12:26:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uKFrJ-00H6lr-Gs for pgsql-hackers@lists.postgresql.org; Wed, 28 May 2025 12:26:13 +0000 Received: from fout-b2-smtp.messagingengine.com ([202.12.124.145]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uKFrG-000NRC-0E for pgsql-hackers@lists.postgresql.org; Wed, 28 May 2025 12:26:12 +0000 Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfout.stl.internal (Postfix) with ESMTP id D4B49114016E; Wed, 28 May 2025 08:26:08 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Wed, 28 May 2025 08:26:08 -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-sender :x-me-sender:x-sasl-enc; s=fm1; t=1748435168; x=1748521568; bh=0 bpRib3RKBsAx/AldQ5Ub7h06cz2Phz4U1sl01puvT8=; b=DEwxN6o+39Tap5jNV zlb5a+fsaI1xW0aHsiK63KeBQJhYyYP0I104hVd4PN/k3oxtN0uxF2xuNQgN3lMi oTarJR6dycG4g82GK6sMyN9K9XVwL3kGLhkhCO99U0KTdOATif1IjpFdrCw8/aan 5fEr+RVEWz0hl7K89UTfRc0oZuSZ2cDige6q83nyc6wCY/H3OCLHN7bSjB9x0sid gcDocRf5wcVOFK4VpmhaqnZXtYJ+Wxlvatx3KqEOoCMEJa7Y6ULRCvR3toE0hxTr rHZygyO4avKw4rwlJ5b7e5Lfh1Zg+2JEiRwrb/9nwXMrqO/+hqLejev0TJ8KPe4n c7tcA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtddtgddvfedvieculddtuddrgeefvddrtd dtmdcutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggft fghnshhusghstghrihgsvgdpuffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftd dtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpeffhffvvefukfgg tggugfgjsehmkeerredttdejnecuhfhrohhmpeetlhhvrghrohcujfgvrhhrvghrrgcuoe grlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqeenucggtffrrghtthgvrhhn peduleekkefgtddttedtkefguddvieffleetgeejiefhteehkeevfeettdduvdfhueenuc ffohhmrghinhepvghnthgvrhhprhhishgvuggsrdgtohhmnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnh hoqdhiphdrohhrghdpnhgspghrtghpthhtohepgedpmhhouggvpehsmhhtphhouhhtpdhr tghpthhtoheprghhsegthigsvghrthgvtgdrrghtpdhrtghpthhtohepshhulhgrmhhulh esghhmrghilhdrtghomhdprhgtphhtthhopehtnhgurhifrghnghesghhmrghilhdrtgho mhdprhgtphhtthhopehpghhsqhhlqdhhrggtkhgvrhhssehlihhsthhsrdhpohhsthhgrh gvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 28 May 2025 08:26:08 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1748435166; bh=m9fEmgT+qfDrjkAaFZ+M3FWCdDXXDwfRJWfmszLg7Hc=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=IB9zy9JWBznT36cPvBKZbxVu7wWozDgsAAC5THWXnmmdPvMy17d3wG6aEE3rbL5qG CvLW7RAKJNwyLvmneAnxZplrZ3dt1IeFyugbzOoJrrRXGwO9DenbWYb9tF6P4aWcZv 2H8qrJshMKZ6+jAoM0Gg3KzZd3wF3pWRBk4NkydwjERAAKanLXhtGAv13/zpU0gUsQ 831bmU0fxuzdams2RJgHXUcnLLpVfuMOT8k5/SD8WhBRoGxOQgzXZp5GZOECApBrKX 2uqUWfGf6dVbgWfaAd68wTXAk+evWmVWN0Xk4CpAOXIXnaY45cZ5efDlI4HQJVqdnw f9PM/uPY+8nrQ== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id A618D90; Wed, 28 May 2025 08:26:06 -0400 (EDT) Date: Wed, 28 May 2025 14:26:06 +0200 From: Alvaro Herrera To: Tender Wang Cc: pgsql-hackers@lists.postgresql.org, Antonin Houska , Amul Sul Subject: Re: Foreign key validation failure in 18beta1 Message-ID: <202505281226.ohdyrr2jrw6r@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2wgq6flqmqnvq5nc" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --2wgq6flqmqnvq5nc Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2025-May-28, Tender Wang wrote: > I dided the codes, in QueueFKConstraintValidation(), we add three > newconstraint for the > fk rel, because the pk rel is partition table. > > During phase 3 of AlterTable, in ATRewriteTables(), > call validateForeignKeyConstraint() three times. > The first time the pk rel is pk, and it's ok. > The second time the pk rel is only pk_1, and the type(1) is not in pk_1, so > an error is reported. > > In this case, the two children newconstraint should not be added to the > queue. Yeah, I reached the same conclusion and this is the preliminary fix I had written for it. I don't like that I had to duplicate a few lines of code, but maybe it's not too bad. Also the comments need to be clarified a bit more. Clearly b663b9436e75 lacked tests. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "Sallah, I said NO camels! That's FIVE camels; can't you count?" (Indiana Jones) --2wgq6flqmqnvq5nc Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-Don-t-validate-referenced-side-FK-subconstraints.patch" From 4c11da193991b95de8baf7689a9938ca9bb171aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Herrera?= Date: Wed, 28 May 2025 14:22:58 +0200 Subject: [PATCH] Don't validate referenced-side FK subconstraints Silly bug in b663b9436e75. Reported-by: Antonin Houska Discussion: https://postgr.es/m/26983.1748418675@localhost --- src/backend/commands/tablecmds.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 54ad38247aa..581c59f117b 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -12988,8 +12988,10 @@ QueueFKConstraintValidation(List **wqueue, Relation conrel, Relation rel, } /* - * If the table at either end of the constraint is partitioned, we need to - * recurse and handle every constraint that is a child of this constraint. + * If the referencing table is partitioned, we need to recurse and handle + * every constraint that is a child of this constraint. Also update the + * sub-constraints on the referenced side of the constraint to appear as + * validated; no actual validation is needed for them. */ if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE || get_rel_relkind(con->confrelid) == RELKIND_PARTITIONED_TABLE) @@ -13021,6 +13023,24 @@ QueueFKConstraintValidation(List **wqueue, Relation conrel, Relation rel, if (childcon->convalidated) continue; + /* + * For child constraints in the referenced side of the constraint, + * we don't need to do any actual validation, but we do need to + * update the status to validated. + */ + if (childcon->confrelid != con->confrelid) + { + copyTuple = heap_copytuple(childtup); + copy_con = (Form_pg_constraint) GETSTRUCT(copyTuple); + copy_con->convalidated = true; + CatalogTupleUpdate(conrel, ©Tuple->t_self, copyTuple); + + InvokeObjectPostAlterHook(ConstraintRelationId, con->oid, 0); + + heap_freetuple(copyTuple); + continue; + } + childrel = table_open(childcon->conrelid, lockmode); QueueFKConstraintValidation(wqueue, conrel, childrel, childtup, -- 2.39.5 --2wgq6flqmqnvq5nc--