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 1slQ2a-007YZo-3T for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Sep 2024 09:41:37 +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 1slQ2Y-009c4c-ML for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Sep 2024 09:41:34 +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 1slQ2X-009c4T-Aj for pgsql-hackers@lists.postgresql.org; Tue, 03 Sep 2024 09:41:34 +0000 Received: from fout3-smtp.messagingengine.com ([103.168.172.146] helo=pfout3-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1slQ2Q-000RBq-0j for pgsql-hackers@lists.postgresql.org; Tue, 03 Sep 2024 09:41:32 +0000 Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfout.phl.internal (Postfix) with ESMTP id 1FE2A138039B; Tue, 3 Sep 2024 05:41:23 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-01.internal (MEProxy); Tue, 03 Sep 2024 05:41:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eisentraut.org; h=cc:cc:content-transfer-encoding:content-type:content-type :date:date:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:subject:subject:to:to; s=fm3; t=1725356483; x=1725442883; bh=PX1UpJXeyYCEm3kAxkXl3giw75OrZr4L JcCENmTskkY=; b=hfgvMoGIkmPWojkosAcgrW4Bp3id1X89ulfk5Kz7tuasGs/J ypql7k+tvfyQiOsP32/sB27ZftR+W4qOZRryorrwyBW4tTRSnP45/x4ewpQX8118 V6QUEuXplfUJ0GWL/hmmx7C+Zt9kiZN7FMnqv06ZClXff0tuLFJeZjj5A+t0GSPF 24jj4b3DQh50EWuExF0a7Sv+WhvDldGnv9KiSfL1EvrKNE0oJ8jndtXb1j2ZTIUY N7L7WDicGavdrbeCPLgJiAr1d3gKd4YQ81tDZOoQ59lNRK5vjXPUozsGiwfaRaiL rJohl0KR1FVqtlePeJldcDQbt7+RaH9wQsLyow== 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 :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1725356483; x= 1725442883; bh=PX1UpJXeyYCEm3kAxkXl3giw75OrZr4LJcCENmTskkY=; b=U f8sLsfLSf3R3HISCKM6wydONXm02hMKijMZtyfc8jO8/BCsoPMpcNslDvVv1sf1d GB9POHHJQiQnpU4VjmhFvK9CvP7FAz3aerJd/1q92rZPh9OZxC5cH6fi6JxKTSga V8P4iwD+6R46tGbe8pm6qxWe0JYjw56EfgK0RekXqcG4pOpCZnqjxjxofSEhfmxo 71sdbZASn1DB53MarBuyiegD9wRFeTRPlPaXkCLZgj0qlZsr8XFDIOYA4+iygkj/ CjxV5z8BtQA1YnUTjRqzADuLDpNf3LZo3jn48Yfj8y7tvA3lWlByyg/A0hmh1zNK iAADa3gFHXU8Mji+NHvYg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudehhedgudekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepkfffgggfuffvvehfhfgjtgfgsehtkeertddtvdej necuhfhrohhmpefrvghtvghrucfgihhsvghnthhrrghuthcuoehpvghtvghrsegvihhsvg hnthhrrghuthdrohhrgheqnecuggftrfgrthhtvghrnhepjefhveehtdetgfffhffhfeef gffghffflefgieeuueekhedvhedvfeehffdvfeeunecuvehluhhsthgvrhfuihiivgeptd enucfrrghrrghmpehmrghilhhfrhhomhepphgvthgvrhesvghishgvnhhtrhgruhhtrdho rhhgpdhnsggprhgtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhope hjihgrnhdruhhnihhvvghrshgrlhhithihsehgmhgrihhlrdgtohhmpdhrtghpthhtohep phhjsehilhhluhhmihhnrghtvggutghomhhpuhhtihhnghdrtghomhdprhgtphhtthhope hpghhsqhhlqdhhrggtkhgvrhhssehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: ie0a040ee:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 3 Sep 2024 05:41:22 -0400 (EDT) Message-ID: Date: Tue, 3 Sep 2024 11:41:20 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: altering a column's collation leaves an invalid foreign key To: jian he , Paul Jungwirth Cc: PostgreSQL Hackers References: <78d824e0-b21e-480d-a252-e4b84bc2c24b@illuminatedcomputing.com> <69f0a887-a4ed-49cf-9d63-923891006658@illuminatedcomputing.com> Content-Language: en-US From: Peter Eisentraut In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 07.06.24 08:39, jian he wrote: > On Sat, Apr 13, 2024 at 9:13 PM jian he wrote: >> >>>>> Here is a patch implementing this. It was a bit more fuss than I expected, so maybe someone has a >>>>> better way. >> I think I found a simple way. >> >> the logic is: >> * ATExecAlterColumnType changes one column once at a time. >> * one column can only have one collation. so we don't need to store a >> list of collation oid. >> * ATExecAlterColumnType we can get the new collation (targetcollid) >> and original collation info. >> * RememberAllDependentForRebuilding will check the column dependent, >> whether this column is referenced by a foreign key or not information >> is recorded. >> so AlteredTableInfo->changedConstraintOids have the primary key and >> foreign key oids. >> * ATRewriteCatalogs will call ATPostAlterTypeCleanup (see the comments >> in ATRewriteCatalogs) >> * for tab->changedConstraintOids (foreign key, primary key) will call >> ATPostAlterTypeParse, so >> for foreign key (con->contype == CONSTR_FOREIGN) will call TryReuseForeignKey. >> * in TryReuseForeignKey, we can pass the information that our primary >> key old collation is nondeterministic >> and old collation != new collation to the foreign key constraint. >> so foreign key can act accordingly at ATAddForeignKeyConstraint (old_check_ok). >> >> >> based on the above logic, I add one bool in struct AlteredTableInfo, >> one bool in struct Constraint. >> bool in AlteredTableInfo is for storing it, later passing it to struct >> Constraint. >> we need bool in Constraint because ATAddForeignKeyConstraint needs it. > > I refactored the comments. > also added some extra tests hoping to make it more bullet proof, maybe > it's redundant. I like this patch version (v4). It's the simplest, probably also easiest to backpatch. It has a flaw: It will also trigger a FK recheck if you alter the collation of the referencing column (foreign key column), even though that is not necessary. (Note that your tests and the examples in this thread only discuss altering the PK column collation, because that is what is actually used during the foreign key checks.) Maybe there is an easy way to avoid that, but I couldn't see one in that patch structure. Maybe that is ok as a compromise. If, in the future, we make it a requirement that the collations on the PK and FK side have to be the same if either collation is nondeterministic, then this case can no longer happen anyway. And so building more infrastructure for this check might be wasted.