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 1sowFp-00Ey5e-J4 for pgsql-hackers@arkaria.postgresql.org; Fri, 13 Sep 2024 02:41:50 +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 1sowFp-007kSz-82 for pgsql-hackers@arkaria.postgresql.org; Fri, 13 Sep 2024 02:41:49 +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 1sowFo-007kSr-Ob for pgsql-hackers@lists.postgresql.org; Fri, 13 Sep 2024 02:41:48 +0000 Received: from mail-ed1-x544.google.com ([2a00:1450:4864:20::544]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sowFl-000taE-BW for pgsql-hackers@lists.postgresql.org; Fri, 13 Sep 2024 02:41:47 +0000 Received: by mail-ed1-x544.google.com with SMTP id 4fb4d7f45d1cf-5c4226a56a8so65502a12.2 for ; Thu, 12 Sep 2024 19:41:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1726195302; x=1726800102; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=1FRXF77xVEN6Q4YPVRceL59VWDeCvx3XxojErfFsgZY=; b=SwzSXzzLz6sh7aJesQ1EskGjwW2D0TqrTnLK1AfwttPmL47Ykb1Me0X/Y1EdkHgCrF X/uR7jr7+EOE8RYr/Ks/9MATSQlJKwPPee0VAG3c0iVgM52NDSUpvUO7zP6VflLPQZG9 Ptnt0Hu8WIEAMtH4DSNs5UxMLi5/qv87QWS6tj8g9HbQPqa6Z+JLugFxegTQ05dBi70z 882kJWedo5poX/lA4R1fgJjzvIRj34xwzanbHvGxffma30H9UrbaPoy3aBmonPbIW5v5 GXu6/wFeB3B6SlOAtLIUViDs8ts/ldAo5zIPt3OJBnvTB7im0nQWI6BWiGU/y+0m9VVr R8YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726195302; x=1726800102; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=1FRXF77xVEN6Q4YPVRceL59VWDeCvx3XxojErfFsgZY=; b=S6bdyeF44mmONxwQamhZiPc8+J7G32aIRlCiSSBC+4l2rK4PYkhEOco+h/DhOp6pQN JutORW7l9q12xDnhlD5/lWYGZkJC7Wf2yIAce0xjjZRQ/qHjeR7qQO9loAMI8vZMKF2t 9ffUVt+aoMu10YHTojm8soiOmUoa/83GrUV6H/B1XjDKYf9AOvgP3QSEAUMa6oYUZes2 BWrXYRi2DkFvmuRxrEjXJXHKwS7qoKEzeUui2UTeCvlpY/bm6gs1b/IQAqVl99U7/6cU N/WR2wMQbsfxufM+6i3Unj49YiAUxkCkvybV2KNaqDPvwG0obQ7qq1GnYtI9Fkhdt7A3 5sEw== X-Forwarded-Encrypted: i=1; AJvYcCXMeoz+yAFqRoG/iQWivm1qDnPabYFqz0ZCnvdpx+rpoSMpCDsIcGY/C3jI9Zs4wXcqpNv5Ygyq8YRpfPOj@lists.postgresql.org X-Gm-Message-State: AOJu0YwR8rCAfxcRwlWC0jpLndoq3C5dCQLoMvUDEChJy4+UbyRbF+vA WYl8P63SykhU3RRHAQNVckatNZyVJPthJdnDlMqsmERrmacgnCbCfoBze1HwbRIGyc6Ct6Fr6WX Y6f+kSA== X-Google-Smtp-Source: AGHT+IGG8XUgTRcZ29XZlGJ0KlgnxgeyTt+LBYyzBtwl4umTGyhpN1CasiO4Y6XBeTD2oShOV6/bmA== X-Received: by 2002:a17:907:94c3:b0:a8d:29b7:ecee with SMTP id a640c23a62f3a-a90296715e6mr416213066b.61.1726195301847; Thu, 12 Sep 2024 19:41:41 -0700 (PDT) Received: from [10.93.0.121] (p50992f7c.dip0.t-ipconnect.de. [80.153.47.124]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8d25952c01sm821125466b.64.2024.09.12.19.41.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 19:41:41 -0700 (PDT) Message-ID: <560b5873c9b8129a4b7b4fe3239ec32363168599.camel@cybertec.at> Subject: Re: Mutable foreign key constraints From: Laurenz Albe To: Tom Lane , pgsql-hackers@lists.postgresql.org Date: Fri, 13 Sep 2024 04:41:40 +0200 In-Reply-To: <4162598.1726176826@sss.pgh.pa.us> References: <4162598.1726176826@sss.pgh.pa.us> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.4 (3.52.4-1.fc40) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2024-09-12 at 17:33 -0400, Tom Lane wrote: > I happened to notice that Postgres will let you do >=20 > regression=3D# create table foo (id timestamp primary key); > CREATE TABLE > regression=3D# create table bar (ts timestamptz references foo); > CREATE TABLE >=20 > 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. >=20 > I'm inclined to propose rejecting FK constraints if the comparison > operator is not immutable. I think that is the only sane thing to do. Consider test=3D> SHOW timezone; TimeZone =20 =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90 Europe/Vienna (1 row) test=3D> INSERT INTO foo VALUES ('2024-09-13 12:00:00'); INSERT 0 1 test=3D> INSERT INTO bar VALUES ('2024-09-13 12:00:00+02'); INSERT 0 1 test=3D> SELECT * FROM foo JOIN bar ON foo.id =3D bar.ts; id =E2=94=82 ts =20 =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=AA=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90 2024-09-13 12:00:00 =E2=94=82 2024-09-13 12:00:00+02 (1 row) test=3D> SET timezone =3D 'Asia/Kolkata'; SET test=3D> SELECT * FROM foo JOIN bar ON foo.id =3D bar.ts; id =E2=94=82 ts=20 =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=AA=E2=95=90=E2=95=90=E2=95=90= =E2=95=90 (0 rows) test=3D> INSERT INTO foo VALUES ('2024-09-14 12:00:00'); INSERT 0 1 test=3D> INSERT INTO bar VALUES ('2024-09-14 12:00:00+02'); ERROR: insert or update on table "bar" violates foreign key constraint "= bar_ts_fkey" DETAIL: Key (ts)=3D(2024-09-14 15:30:00+05:30) is not present in table "= foo". That's very broken and should not be allowed. > 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. I hope that there are few cases of that in the field, and I think it is OK to break them. After all, it can be fixed with a simple ALTER TABLE foo ALTER id TYPE timestamptz; If the session time zone is UTC, that wouldn't even require a rewrite. I agree that it cannot be backpatched. Yours, Laurenz Albe