Mutable foreign key constraints - Mailing list pgsql-hackers

From Tom Lane
Subject Mutable foreign key constraints
Date
Msg-id 4162598.1726176826@sss.pgh.pa.us
Whole thread Raw
Responses Re: Mutable foreign key constraints
Re: Mutable foreign key constraints
Re: Mutable foreign key constraints
List pgsql-hackers
I happened to notice that Postgres will let you do

regression=# create table foo (id timestamp primary key);
CREATE TABLE
regression=# 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=# select amopopr::regoperator from pg_amop join pg_operator o on o.oid = amopopr join pg_proc p on p.oid =
oprcodewhere amopmethod=403 and amopstrategy=3 and provolatile != 'i'; 
                         amopopr
---------------------------------------------------------
 =(date,timestamp with time zone)
 =(timestamp without time zone,timestamp with time zone)
 =(timestamp with time zone,date)
 =(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



pgsql-hackers by date:

Previous
From: David Rowley
Date:
Subject: Re: Add memory/disk usage for WindowAgg nodes in EXPLAIN
Next
From: Nathan Bossart
Date:
Subject: Re: Pgstattuple on Sequences: Seeking Community Feedback on Potential Patch