Doc: mention foreign keys can reference unique indexes

We seem to have only documented a foreign key can reference the columns of
a primary key or unique constraint.  Here we adjust the documentation
to mention columns in a non-partial unique index can be mentioned too.

The header comment for transformFkeyCheckAttrs() also didn't mention
unique indexes, so fix that too.  In passing make that header comment
reflect reality in the various other aspects where it deviated from it.

Bug: 18295
Reported-by: Gilles PARC
Author: Laurenz Albe, David Rowley
Discussion: https://www.postgresql.org/message-id/18295-0ed0fac5c9f7b17b%40postgresql.org
Backpatch-through: 12
This commit is contained in:
David Rowley 2024-01-30 10:17:31 +13:00
parent b2fd1dab90
commit dff1756c39
3 changed files with 34 additions and 19 deletions

View File

@ -1083,16 +1083,16 @@ CREATE TABLE order_items (
<para> <para>
A foreign key must reference columns that either are a primary key or A foreign key must reference columns that either are a primary key or
form a unique constraint. This means that the referenced columns always form a unique constraint, or are columns from a non-partial unique index.
have an index (the one underlying the primary key or unique constraint); This means that the referenced columns always have an index to allow
so checks on whether a referencing row has a match will be efficient. efficient lookups on whether a referencing row has a match. Since a
Since a <command>DELETE</command> of a row from the referenced table <command>DELETE</command> of a row from the referenced table or an
or an <command>UPDATE</command> of a referenced column will require <command>UPDATE</command> of a referenced column will require a scan of
a scan of the referencing table for rows matching the old value, it the referencing table for rows matching the old value, it is often a good
is often a good idea to index the referencing columns too. Because this idea to index the referencing columns too. Because this is not always
is not always needed, and there are many choices available on how needed, and there are many choices available on how to index, the
to index, declaration of a foreign key constraint does not declaration of a foreign key constraint does not automatically create an
automatically create an index on the referencing columns. index on the referencing columns.
</para> </para>
<para> <para>

View File

@ -1038,10 +1038,11 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
column(s) of some row of the referenced table. If the <replaceable column(s) of some row of the referenced table. If the <replaceable
class="parameter">refcolumn</replaceable> list is omitted, the class="parameter">refcolumn</replaceable> list is omitted, the
primary key of the <replaceable class="parameter">reftable</replaceable> primary key of the <replaceable class="parameter">reftable</replaceable>
is used. The referenced columns must be the columns of a non-deferrable is used. Otherwise, the <replaceable class="parameter">refcolumn</replaceable>
unique or primary key constraint in the referenced table. The user list must refer to the columns of a non-deferrable unique or primary key
must have <literal>REFERENCES</literal> permission on the referenced table constraint or be the columns of a non-partial unique index. The user
(either the whole table, or the specific referenced columns). The 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 addition of a foreign key constraint requires a
<literal>SHARE ROW EXCLUSIVE</literal> lock on the referenced table. <literal>SHARE ROW EXCLUSIVE</literal> lock on the referenced table.
Note that foreign key constraints cannot be defined between temporary Note that foreign key constraints cannot be defined between temporary
@ -2137,6 +2138,16 @@ CREATE TABLE cities_partdef
</para> </para>
</refsect2> </refsect2>
<refsect2>
<title>Foreign Key Constraints</title>
<para>
It is a <productname>PostgreSQL</productname> extension that a
foreign key constraint may reference columns of a unique index instead of
columns of a primary key or unique constraint.
</para>
</refsect2>
<refsect2> <refsect2>
<title><literal>NULL</literal> <quote>Constraint</quote></title> <title><literal>NULL</literal> <quote>Constraint</quote></title>

View File

@ -10107,15 +10107,19 @@ transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid,
/* /*
* transformFkeyCheckAttrs - * transformFkeyCheckAttrs -
* *
* Make sure that the attributes of a referenced table belong to a unique * Validate that the 'attnums' columns in the 'pkrel' relation are valid to
* (or primary key) constraint. Return the OID of the index supporting * reference as part of a foreign key constraint.
* the constraint, as well as the opclasses associated with the index *
* columns. * Returns the OID of the unique index supporting the constraint and
* populates the caller-provided 'opclasses' array with the opclasses
* associated with the index columns.
*
* Raises an ERROR on validation failure.
*/ */
static Oid static Oid
transformFkeyCheckAttrs(Relation pkrel, transformFkeyCheckAttrs(Relation pkrel,
int numattrs, int16 *attnums, int numattrs, int16 *attnums,
Oid *opclasses) /* output parameter */ Oid *opclasses)
{ {
Oid indexoid = InvalidOid; Oid indexoid = InvalidOid;
bool found = false; bool found = false;