Re: Allow foreign keys to reference a superset of unique columns

From: Kaiting Chen <ktchen14(at)gmail(dot)com>
To: James Coleman <jtc331(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org, hellopfm(at)gmail(dot)com, Wolfgang Walther <walther(at)technowledgy(dot)de>, Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>, David Rowley <dgrowleyml(at)gmail(dot)com>
Subject: Re: Allow foreign keys to reference a superset of unique columns
Date: 2022-09-27 21:58:31
Message-ID: CA+CLzG97H+2E-Q3aKpV577VrphbHfxS8FSj-vCmGgU8QQ=mHYA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> As I was reading through the email chain I had this thought: could you
> get the same benefit (or 90% of it anyway) by instead allowing the
> creation of a uniqueness constraint that contains more columns than
> the index backing it? So long as the index backing it still guaranteed
> the uniqueness on a subset of columns that would seem to be safe.

> After writing down that idea I noticed Wolfgang Walther had commented
> similarly, but it appears that that idea got lost (or at least not
> responded to).

Is it necessary to have the unique constraint at all? This currently works
in
PostgreSQL:

CREATE TABLE foo (a integer, b integer);

CREATE UNIQUE INDEX ON foo (a, b);

CREATE TABLE bar (
x integer,
y integer,
FOREIGN KEY (x, y) REFERENCES foo(a, b)
);

Where no unique constraint exists on foo (a, b). Forcing the creation of a
unique constraint in this case seems more confusing to me, as a user, than
allowing it without the definition of the unique constraint, given the
existing
behavior.

> I'd be happy to sign up to review an updated patch if you're
> interested in continuing this effort. If so, could you register the
> patch in the CF app (if not there already)?

The patch should already be registered! Though it's still in a state that
needs
a lot of work.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Kaiting Chen 2022-09-27 22:09:33 Re: Allow foreign keys to reference a superset of unique columns
Previous Message Tom Lane 2022-09-27 21:50:07 Re: longfin and tamandua aren't too happy but I'm not sure why