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 1sorRm-00ELjF-E8 for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Sep 2024 21:33:51 +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 1sorRm-000ikJ-36 for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Sep 2024 21:33:50 +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 1sorRl-000ik1-PS for pgsql-hackers@lists.postgresql.org; Thu, 12 Sep 2024 21:33:49 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sorRi-000sXd-6S for pgsql-hackers@lists.postgresql.org; Thu, 12 Sep 2024 21:33:49 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 48CLXkTR4162599 for ; Thu, 12 Sep 2024 17:33:46 -0400 From: Tom Lane To: pgsql-hackers@lists.postgresql.org Subject: Mutable foreign key constraints MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4162597.1726176826.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 12 Sep 2024 17:33:46 -0400 Message-ID: <4162598.1726176826@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I happened to notice that Postgres will let you do regression=3D# create table foo (id timestamp primary key); CREATE TABLE regression=3D# create table bar (ts timestamptz references foo); CREATE TABLE This strikes me as a pretty bad idea, because whether a particular timestamp is equal to a particular timestamptz depends on your timezone setting. Thus the constraint could appear to be violated after a timezone change. I'm inclined to propose rejecting FK constraints if the comparison operator is not immutable. Among the built-in opclasses, the only instances of non-immutable btree equality operators are regression=3D# select amopopr::regoperator from pg_amop join pg_operator o= on o.oid =3D amopopr join pg_proc p on p.oid =3D oprcode where amopmethod= =3D403 and amopstrategy=3D3 and provolatile !=3D 'i'; amopopr = --------------------------------------------------------- =3D(date,timestamp with time zone) =3D(timestamp without time zone,timestamp with time zone) =3D(timestamp with time zone,date) =3D(timestamp with time zone,timestamp without time zone) (4 rows) A possible objection is that if anybody has such a setup and hasn't noticed a problem because they never change their timezone setting, they might not appreciate us breaking it. So I certainly wouldn't propose back-patching this. But maybe we should add it as a foot-gun defense going forward. Thoughts? regards, tom lane