pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
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 13:14:06 +0100
Message-ID: <763b0bc3828415afa5f815753100722611faa1e9.camel@cybertec.at> (raw)
In-Reply-To: <CAApHDvoaMq1oGxJbfVK4Miwp=0DbhuTJhkUuYOw-Q=WkOmH-qQ@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>

On Sat, 2024-01-27 at 00:33 +1300, David Rowley wrote:
> On Fri, 26 Jan 2024 at 23:55, Laurenz Albe <laurenz.albe@cybertec.at> wrote:
> > Attached is my suggested patch.
> 
> Why did you choose to remove the mention of primary key and unique
> constraints in:
> 
> -    A foreign key must reference columns that either are a primary key or
> -    form a unique constraint.  This means that the referenced columns always
> -    have an index (the one underlying the primary key or unique constraint);
> +    A foreign key must reference columns on which a unique index in defined,
> 
> but choose to keep them here:
> 
> -      unique or primary key constraint in the referenced table.  The user
> +      unique or primary key constraint or a unique index in the
> 
> I'd rather we continue to mention primary keys and unique constraints
> in ddl.sgml.  It just seems good practice to me to define a constraint
> and it seems better if people continue to do that to increase the
> likelihood that their schema is compatible with another RDBMS.

I removed the mention of constraints for simplicity, but I agree that the
documentation should encourage users to reference constraints.

Attached is a modified patch.

Yours,
Laurenz Albe

Attachments:

v2-0001-Doc-foreign-keys-can-reference-unique-indexes.patch (text/x-patch, 3.8K) download From ad9a9acd27b3624d0c35885523d459365a477f0f Mon Sep 17 00:00:00 2001 From: Laurenz Albe <laurenz.albe@cybertec.at> Date: Fri, 26 Jan 2024 13:11:08 +0100 Subject: [PATCH v2] 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 | 7 +++++-- src/backend/commands/tablecmds.c | 5 ++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgml index 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.sgml index 079af9126a..9ee9ec2736 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,12 +2305,15 @@ 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 <literal>SET DEFAULT</literal> and <literal>SET NULL</literal> is a <productname>PostgreSQL</productname> extension. + It is also 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> diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 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

view thread (15+ messages)  latest in thread

Message-ID: <763b0bc3828415afa5f815753100722611faa1e9.camel@cybertec.at>
Permalink:  ../763b0bc3828415afa5f815753100722611faa1e9.camel@cybertec.at/
Also on:    postgresql.org/message-id/763b0bc3828415afa5f815753100722611faa1e9.camel@cybertec.at

 ·  · 

reply

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: <763b0bc3828415afa5f815753100722611faa1e9.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>

* 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