Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1c8336e

Browse files
committed
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
1 parent 1b924a8 commit 1c8336e

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

doc/src/sgml/ddl.sgml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,16 +1181,16 @@ CREATE TABLE posts (
11811181

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

11961196
<para>

doc/src/sgml/ref/create_table.sgml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,10 +1137,11 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
11371137
column(s) of some row of the referenced table. If the <replaceable
11381138
class="parameter">refcolumn</replaceable> list is omitted, the
11391139
primary key of the <replaceable class="parameter">reftable</replaceable>
1140-
is used. The referenced columns must be the columns of a non-deferrable
1141-
unique or primary key constraint in the referenced table. The user
1142-
must have <literal>REFERENCES</literal> permission on the referenced table
1143-
(either the whole table, or the specific referenced columns). The
1140+
is used. Otherwise, the <replaceable class="parameter">refcolumn</replaceable>
1141+
list must refer to the columns of a non-deferrable unique or primary key
1142+
constraint or be the columns of a non-partial unique index. The user
1143+
must have <literal>REFERENCES</literal> permission on the referenced
1144+
table (either the whole table, or the specific referenced columns). The
11441145
addition of a foreign key constraint requires a
11451146
<literal>SHARE ROW EXCLUSIVE</literal> lock on the referenced table.
11461147
Note that foreign key constraints cannot be defined between temporary
@@ -2277,13 +2278,19 @@ CREATE TABLE cities_partdef
22772278
</refsect2>
22782279

22792280
<refsect2>
2280-
<title>Foreign-Key Constraint Actions</title>
2281+
<title>Foreign Key Constraints</title>
22812282

22822283
<para>
2283-
The ability to specify column lists in the foreign-key actions
2284+
The ability to specify column lists in the foreign key actions
22842285
<literal>SET DEFAULT</literal> and <literal>SET NULL</literal> is a
22852286
<productname>PostgreSQL</productname> extension.
22862287
</para>
2288+
2289+
<para>
2290+
It is a <productname>PostgreSQL</productname> extension that a
2291+
foreign key constraint may reference columns of a unique index instead of
2292+
columns of a primary key or unique constraint.
2293+
</para>
22872294
</refsect2>
22882295

22892296
<refsect2>

src/backend/commands/tablecmds.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11443,15 +11443,19 @@ transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid,
1144311443
/*
1144411444
* transformFkeyCheckAttrs -
1144511445
*
11446-
* Make sure that the attributes of a referenced table belong to a unique
11447-
* (or primary key) constraint. Return the OID of the index supporting
11448-
* the constraint, as well as the opclasses associated with the index
11449-
* columns.
11446+
* Validate that the 'attnums' columns in the 'pkrel' relation are valid to
11447+
* reference as part of a foreign key constraint.
11448+
*
11449+
* Returns the OID of the unique index supporting the constraint and
11450+
* populates the caller-provided 'opclasses' array with the opclasses
11451+
* associated with the index columns.
11452+
*
11453+
* Raises an ERROR on validation failure.
1145011454
*/
1145111455
static Oid
1145211456
transformFkeyCheckAttrs(Relation pkrel,
1145311457
int numattrs, int16 *attnums,
11454-
Oid *opclasses) /* output parameter */
11458+
Oid *opclasses)
1145511459
{
1145611460
Oid indexoid = InvalidOid;
1145711461
bool found = false;

0 commit comments

Comments
 (0)