From: Laurenz Albe <laurenz.albe@cybertec.at>
To: David Rowley <dgrowleyml@gmail.com>
Cc: gparc@free.fr, pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Subject: Re: BUG #18295: In PostgreSQL a unique index on targeted columns is sufficient to support a foreign key
Date: Fri, 26 Jan 2024 14:53:41 +0100
Message-ID: <578afb7d3a88969b4d77f49280cd30247ee4b687.camel@cybertec.at> (raw)
In-Reply-To: <CAApHDvpMps521Pih_ccrffzyv0SFtC0TuB6vULqhrrxzfW+o3w@mail.gmail.com>
References: <18295-0ed0fac5c9f7b17b@postgresql.org>
<1142099713.208806083.1706091077854.JavaMail.zimbra@free.fr>
<51c67d44ee2c5c888d9842110a27b9f9c329cb5e.camel@cybertec.at>
<1963986314.213748462.1706112064903.JavaMail.zimbra@free.fr>
<1197950598.222716079.1706196971222.JavaMail.zimbra@free.fr>
<87bde9f9c7fe5467617c303e63a380a17f0b2600.camel@cybertec.at>
<CAApHDvoaMq1oGxJbfVK4Miwp=0DbhuTJhkUuYOw-Q=WkOmH-qQ@mail.gmail.com>
<763b0bc3828415afa5f815753100722611faa1e9.camel@cybertec.at>
<CAApHDvpMps521Pih_ccrffzyv0SFtC0TuB6vULqhrrxzfW+o3w@mail.gmail.com>
On Sat, 2024-01-27 at 02:19 +1300, David Rowley wrote:
> On Sat, 27 Jan 2024 at 01:14, Laurenz Albe <laurenz.albe@cybertec.at> wrote:
>
> I'd only adjust the following addition to be a new paragraph:
Check.
> and drop the "also" at the same time.
Done.
> I also noticed that, generally, we're not that consistent if we spell
> it "foreign-key" or "foreign key". You're introducing "foreign key"
> in a location where there are a couple of "foreign-key"s. Maybe it's
> better to be consistent in at least that location?
Yes, you are right. I noticed that everywhere else on the page the
form "foreign key" is used, so that's what I did in the attached patch.
Yours,
Laurenz Albe
Attachments:v3-0001-Doc-foreign-keys-can-reference-unique-indexes.patch (text/x-patch, 3.9K) download
From 85f062595b297f19ff978acec752b0b39c9c16b7 Mon Sep 17 00:00:00 2001
From: Laurenz Albe <laurenz.albe@cybertec.at>
Date: Fri, 26 Jan 2024 14:49:17 +0100
Subject: [PATCH v3] Doc: foreign keys can reference unique indexes
It has always been like that, so we should document it.
The alternative would have been to restrict foreign keys
to only reference primary key or unique constraints as
the standard decrees, but that would create a compatibility
break for no real benefit. Per complaint by Gilles Parc.
Author: Laurenz Albe
Discussion: https://postgr.es/m/18295-0ed0fac5c9f7b17b%40postgresql.org
---
doc/src/sgml/ddl.sgml | 5 +++--
doc/src/sgml/ref/create_table.sgml | 12 +++++++++---
src/backend/commands/tablecmds.c | 5 ++---
3 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgmlindex fc03a349f0..88661a585e 100644--- a/doc/src/sgml/ddl.sgml+++ b/doc/src/sgml/ddl.sgml@@ -1317,8 +1317,9 @@ CREATE TABLE posts (
</para>
<para>
- A foreign key must reference columns that either are a primary key or- form a unique constraint. This means that the referenced columns always+ A foreign key should reference columns that either are a primary key or+ form a unique constraint (<productname>PostgreSQL</productname> only+ requires a unique index on the columns). This means that the referenced columns always
have an index (the one underlying the primary key or unique constraint);
so checks on whether a referencing row has a match will be efficient.
Since a <command>DELETE</command> of a row from the referenced table
diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgmlindex 079af9126a..aea9666806 100644--- a/doc/src/sgml/ref/create_table.sgml+++ b/doc/src/sgml/ref/create_table.sgml@@ -1166,7 +1166,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
class="parameter">refcolumn</replaceable> list is omitted, the
primary key of the <replaceable class="parameter">reftable</replaceable>
is used. The referenced columns must be the columns of a non-deferrable
- unique or primary key constraint in the referenced table. The user+ unique or primary key constraint or a unique index in the referenced table. The user
must have <literal>REFERENCES</literal> permission on the referenced table
(either the whole table, or the specific referenced columns). The
addition of a foreign key constraint requires a
@@ -2305,13 +2305,19 @@ CREATE TABLE cities_partdef
</refsect2>
<refsect2>
- <title>Foreign-Key Constraint Actions</title>+ <title>Foreign Key Constraints</title>
<para>
- The ability to specify column lists in the foreign-key actions+ The ability to specify column lists in the foreign key actions
<literal>SET DEFAULT</literal> and <literal>SET NULL</literal> is a
<productname>PostgreSQL</productname> extension.
</para>
++ <para>+ It is a <productname>PostgreSQL</productname> extension that a+ foreign key constraint may reference a unique index instead of a+ primary key or unique constraint.+ </para>
</refsect2>
<refsect2>
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.cindex a062ec4055..74683014e3 100644--- a/src/backend/commands/tablecmds.c+++ b/src/backend/commands/tablecmds.c@@ -12090,9 +12090,8 @@ transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid,
* transformFkeyCheckAttrs -
*
* Make sure that the attributes of a referenced table belong to a unique
- * (or primary key) constraint. Return the OID of the index supporting- * the constraint, as well as the opclasses associated with the index- * columns.+ * index. Return the OID of the index supporting the constraint, as well+ * as the opclasses associated with the index columns.
*/
static Oid
transformFkeyCheckAttrs(Relation pkrel,
--
2.43.0
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: laurenz.albe@cybertec.at, dgrowleyml@gmail.com
Subject: Re: BUG #18295: In PostgreSQL a unique index on targeted columns is sufficient to support a foreign key
In-Reply-To: <578afb7d3a88969b4d77f49280cd30247ee4b687.camel@cybertec.at>
References: <18295-0ed0fac5c9f7b17b@postgresql.org> <1142099713.208806083.1706091077854.JavaMail.zimbra@free.fr> <51c67d44ee2c5c888d9842110a27b9f9c329cb5e.camel@cybertec.at> <1963986314.213748462.1706112064903.JavaMail.zimbra@free.fr> <1197950598.222716079.1706196971222.JavaMail.zimbra@free.fr> <87bde9f9c7fe5467617c303e63a380a17f0b2600.camel@cybertec.at> <CAApHDvoaMq1oGxJbfVK4Miwp=0DbhuTJhkUuYOw-Q=WkOmH-qQ@mail.gmail.com> <763b0bc3828415afa5f815753100722611faa1e9.camel@cybertec.at> <CAApHDvpMps521Pih_ccrffzyv0SFtC0TuB6vULqhrrxzfW+o3w@mail.gmail.com> <578afb7d3a88969b4d77f49280cd30247ee4b687.camel@cybertec.at>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox