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 1ro4my-00GwW9-Hk for pgsql-hackers@arkaria.postgresql.org; Sat, 23 Mar 2024 17:04:13 +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 1ro4mw-00ARpL-3a for pgsql-hackers@arkaria.postgresql.org; Sat, 23 Mar 2024 17:04:10 +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 1ro4mv-00ARpD-MW for pgsql-hackers@lists.postgresql.org; Sat, 23 Mar 2024 17:04:10 +0000 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1ro4mr-0063Ic-GV for pgsql-hackers@lists.postgresql.org; Sat, 23 Mar 2024 17:04:08 +0000 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-29de4e12d12so2112826a91.3 for ; Sat, 23 Mar 2024 10:04:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20230601.gappssmtp.com; s=20230601; t=1711213445; x=1711818245; darn=lists.postgresql.org; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=lb8zDjJ70B1u4Z0cOrAferAEhESZVvBaFTyihlKDLZo=; b=CnCW7nvNVj+Wn7t+6hi1pIijSFFI4y3yUyN8A1zae1THpen9EucKy79/VeqTQjL5zj 7c0svkDFSDkbVdTwkJAm9P8GiQ/orV9Fp6l9EOnE+jLRaimvsSpL2JTT4vK62uKR5LbX bVp/Jark6Qov+Bdr7ClaML8+kIp2/+0vvIiqsx83YIN3YuRTa+ITFrT5xmfgPWRILXxj 72E0TVvquun172oyf/WU1snsI1NBB/zzsIsGSEe8bagswxFsP/xNayh2GvEo25YOMh8B OlAD8PW/I/1K4bKyEPQV5AHsBFsb1xmkEEYRTUPrOM2A8GIfvLbBdM7gELq7PXHRm916 O49A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711213445; x=1711818245; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=lb8zDjJ70B1u4Z0cOrAferAEhESZVvBaFTyihlKDLZo=; b=hg2N5FPfM7x77MTTKReX/SihCj2s2cuRV8VS37RSb2JrIq/AHOWRysubcDhE3tC0/x miK04pM8LhO3T5r1qX4/eQ03W8PuMSUmSyEYEIv0lBOci83rFInhgJRBK/nNan7nsd7q 7wdfm+dflP+VaHKwSpW9RQmoaDMntTKkHiv/SJ2oUwbMI13gaACn2t+ekf5prASpBFyZ TynJLmLV0yBeL9m09s9t8wNWUkAZZt0MGBTk4Kqogs3n8aOXbBwKJTdIIGjBoFi8l+SP QK430aSwLejgMLrTCutDjudYvponXDDGScyWcuZeyy2izz/K4eg6v0h2USTpyc+DJLFS Q29Q== X-Gm-Message-State: AOJu0Yz1JIJOs/8B5xrb4UZqwytJEXfKvsU6xKLhgJBkPLrfqXpPtCEU s6Yt5oFhT93H2r7bqkgtluJz5RUIbH/mV5TCh9Y2BtdOema4dfB6EW09lSSma617jhz11vHZyZs Y X-Google-Smtp-Source: AGHT+IFKfrvvqMBGo8eAzjPiob8RrjXgWe2cqqn/wbsQBvgBltXMIknLsSrsQiy6/0H6KARkMDdYrw== X-Received: by 2002:a17:90b:2290:b0:29b:c17c:5fa6 with SMTP id kx16-20020a17090b229000b0029bc17c5fa6mr2130300pjb.33.1711213445095; Sat, 23 Mar 2024 10:04:05 -0700 (PDT) Received: from [192.168.2.139] ([50.39.255.79]) by smtp.googlemail.com with ESMTPSA id nd12-20020a17090b4ccc00b002a028030b0esm3813269pjb.39.2024.03.23.10.04.04 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 23 Mar 2024 10:04:04 -0700 (PDT) Message-ID: <78d824e0-b21e-480d-a252-e4b84bc2c24b@illuminatedcomputing.com> Date: Sat, 23 Mar 2024 10:04:04 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: PostgreSQL Hackers From: Paul Jungwirth Subject: altering a column's collation leaves an invalid foreign key Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dear hackers, I was looking at how foreign keys deal with collations, and I came across this comment about not re-checking a foreign key if the column type changes in a compatible way: * Since we require that all collations share the same notion of * equality (which they do, because texteq reduces to bitwise * equality), we don't compare collation here. But now that we have nondeterministic collations, isn't that out of date? For instance I could make this foreign key: paul=# create collation itext (provider = 'icu', locale = 'und-u-ks-level1', deterministic = false); CREATE COLLATION paul=# create table t1 (id text collate itext primary key); CREATE TABLE paul=# create table t2 (id text, parent_id text references t1); CREATE TABLE And then: paul=# insert into t1 values ('a'); INSERT 0 1 paul=# insert into t2 values ('.', 'A'); INSERT 0 1 So far that behavior seems correct, because the user told us 'a' and 'A' were equivalent, but now I can change the collation on the referenced table and the FK doesn't complain: paul=# alter table t1 alter column id type text collate "C"; ALTER TABLE The constraint claims to be valid, but I can't drop & add it: paul=# alter table t2 drop constraint t2_parent_id_fkey; ALTER TABLE paul=# alter table t2 add constraint t2_parent_id_fkey foreign key (parent_id) references t1; ERROR: insert or update on table "t2" violates foreign key constraint "t2_parent_id_fkey" DETAIL: Key (parent_id)=(A) is not present in table "t1". Isn't that a problem? Perhaps if the previous collation was nondeterministic we should force a re-check. (Tested on 17devel 697f8d266c and also 16.) Yours, -- Paul ~{:-) pj@illuminatedcomputing.com