Skip to content

Commit ac355d5

Browse files
committed
Move most of the error checking for foreign-key constraints out of
parse analysis and into the execution code (in tablecmds.c). This eliminates a lot of unreasonably complex code that needed to have two or more execution paths in case it was dealing with a not-yet-created table column vs. an already-existing one. The execution code is always dealing with already-created tables and so needs only one case. This also eliminates some potential race conditions (the table wasn't locked between parse analysis and execution), makes it easy to fix the gripe about wrong referenced-column names generating a misleading error message, and lets us easily add a dependency from the foreign-key constraint to the unique index that it requires the referenced table to have. (Cf. complaint from Kris Jurka 12-Sep-2002 on pgsql-bugs.) Also, third try at building a deletion mechanism that is not sensitive to the order in which pg_depend entries are visited. Adding the above- mentioned dependency exposed the folly of what dependency.c had been doing: it failed for cases where B depends on C while both auto-depend on A. Dropping A should succeed in this case, but was failing if C happened to be visited before B. It appears the only solution is two separate walks over the dependency tree.
1 parent e303a2d commit ac355d5

File tree

10 files changed

+761
-804
lines changed

10 files changed

+761
-804
lines changed

src/backend/catalog/dependency.c

Lines changed: 180 additions & 128 deletions
Large diffs are not rendered by default.

src/backend/catalog/heap.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.229 2002/09/19 23:40:56 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.230 2002/09/22 00:37:09 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -1342,6 +1342,7 @@ StoreRelCheck(Relation rel, char *ccname, char *ccbin)
13421342
' ',
13431343
' ',
13441344
' ',
1345+
InvalidOid, /* no associated index */
13451346
expr, /* Tree form check constraint */
13461347
ccbin, /* Binary form check constraint */
13471348
ccsrc); /* Source form check constraint */

src/backend/catalog/index.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.196 2002/09/04 20:31:14 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.197 2002/09/22 00:37:09 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -693,6 +693,7 @@ index_create(Oid heapRelationId,
693693
' ',
694694
' ',
695695
' ',
696+
InvalidOid, /* no associated index */
696697
NULL, /* no check constraint */
697698
NULL,
698699
NULL);

src/backend/catalog/pg_constraint.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_constraint.c,v 1.6 2002/09/04 20:31:14 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_constraint.c,v 1.7 2002/09/22 00:37:09 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -53,6 +53,7 @@ CreateConstraintEntry(const char *constraintName,
5353
char foreignUpdateType,
5454
char foreignDeleteType,
5555
char foreignMatchType,
56+
Oid indexRelId,
5657
Node *conExpr,
5758
const char *conBin,
5859
const char *conSrc)
@@ -216,6 +217,21 @@ CreateConstraintEntry(const char *constraintName,
216217
}
217218
}
218219

220+
if (OidIsValid(indexRelId))
221+
{
222+
/*
223+
* Register normal dependency on the unique index that supports
224+
* a foreign-key constraint.
225+
*/
226+
ObjectAddress relobject;
227+
228+
relobject.classId = RelOid_pg_class;
229+
relobject.objectId = indexRelId;
230+
relobject.objectSubId = 0;
231+
232+
recordDependencyOn(&conobject, &relobject, DEPENDENCY_NORMAL);
233+
}
234+
219235
if (conExpr != NULL)
220236
{
221237
/*

0 commit comments

Comments
 (0)