88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/catalog/pg_constraint.c,v 1.7 2002/09/22 00:37:09 tgl Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/catalog/pg_constraint.c,v 1.8 2002/11/15 02:50:05 momjian Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -190,6 +190,19 @@ CreateConstraintEntry(const char *constraintName,
190190 }
191191 }
192192
193+ if (OidIsValid (domainId ))
194+ {
195+ /*
196+ * Register auto dependency from constraint to owning domain
197+ */
198+ ObjectAddress domobject ;
199+
200+ domobject .classId = RelOid_pg_type ;
201+ domobject .objectId = domainId ;
202+
203+ recordDependencyOn (& conobject , & domobject , DEPENDENCY_AUTO );
204+ }
205+
193206 if (OidIsValid (foreignRelId ))
194207 {
195208 /*
@@ -262,7 +275,7 @@ CreateConstraintEntry(const char *constraintName,
262275 * this test is not very meaningful.
263276 */
264277bool
265- ConstraintNameIsUsed (Oid relId , Oid relNamespace , const char * cname )
278+ ConstraintNameIsUsed (CONSTRAINTCATEGORY conCat , Oid objId , Oid objNamespace , const char * cname )
266279{
267280 bool found ;
268281 Relation conDesc ;
@@ -280,7 +293,7 @@ ConstraintNameIsUsed(Oid relId, Oid relNamespace, const char *cname)
280293
281294 ScanKeyEntryInitialize (& skey [1 ], 0x0 ,
282295 Anum_pg_constraint_connamespace , F_OIDEQ ,
283- ObjectIdGetDatum (relNamespace ));
296+ ObjectIdGetDatum (objNamespace ));
284297
285298 conscan = systable_beginscan (conDesc , ConstraintNameNspIndex , true,
286299 SnapshotNow , 2 , skey );
@@ -289,7 +302,12 @@ ConstraintNameIsUsed(Oid relId, Oid relNamespace, const char *cname)
289302 {
290303 Form_pg_constraint con = (Form_pg_constraint ) GETSTRUCT (tup );
291304
292- if (con -> conrelid == relId )
305+ if (conCat == CONSTRAINT_RELATION && con -> conrelid == objId )
306+ {
307+ found = true;
308+ break ;
309+ }
310+ else if (conCat == CONSTRAINT_DOMAIN && con -> contypid == objId )
293311 {
294312 found = true;
295313 break ;
@@ -314,7 +332,7 @@ ConstraintNameIsUsed(Oid relId, Oid relNamespace, const char *cname)
314332 * someone else might choose the same name concurrently!
315333 */
316334char *
317- GenerateConstraintName (Oid relId , Oid relNamespace , int * counter )
335+ GenerateConstraintName (CONSTRAINTCATEGORY conCat , Oid objId , Oid objNamespace , int * counter )
318336{
319337 bool found ;
320338 Relation conDesc ;
@@ -347,7 +365,7 @@ GenerateConstraintName(Oid relId, Oid relNamespace, int *counter)
347365
348366 ScanKeyEntryInitialize (& skey [1 ], 0x0 ,
349367 Anum_pg_constraint_connamespace , F_OIDEQ ,
350- ObjectIdGetDatum (relNamespace ));
368+ ObjectIdGetDatum (objNamespace ));
351369
352370 conscan = systable_beginscan (conDesc , ConstraintNameNspIndex , true,
353371 SnapshotNow , 2 , skey );
@@ -356,7 +374,12 @@ GenerateConstraintName(Oid relId, Oid relNamespace, int *counter)
356374 {
357375 Form_pg_constraint con = (Form_pg_constraint ) GETSTRUCT (tup );
358376
359- if (con -> conrelid == relId )
377+ if (conCat == CONSTRAINT_RELATION && con -> conrelid == objId )
378+ {
379+ found = true;
380+ break ;
381+ }
382+ else if (conCat == CONSTRAINT_DOMAIN && con -> contypid == objId )
360383 {
361384 found = true;
362385 break ;
@@ -415,10 +438,13 @@ RemoveConstraintById(Oid conId)
415438 con = (Form_pg_constraint ) GETSTRUCT (tup );
416439
417440 /*
418- * If the constraint is for a relation, open and exclusive-lock the
419- * relation it's for.
441+ * If the constraint is for a relation, open and exclusive-lock
442+ * the relation it's for.
443+ *
444+ * If the constraint is for a domain, open and lock the pg_type entry
445+ * tye constraint is used on.
420446 *
421- * XXX not clear what we should lock, if anything, for other constraints.
447+ * XXX not clear what we should lock, if anything, for assert constraints.
422448 */
423449 if (OidIsValid (con -> conrelid ))
424450 {
@@ -463,6 +489,34 @@ RemoveConstraintById(Oid conId)
463489 /* Keep lock on constraint's rel until end of xact */
464490 heap_close (rel , NoLock );
465491 }
492+ /* Lock the domain row in pg_type */
493+ else if (OidIsValid (con -> contypid ))
494+ {
495+ Relation typRel ;
496+ HeapTuple typTup ;
497+ ScanKeyData typKey [1 ];
498+ SysScanDesc typScan ;
499+
500+ typRel = heap_openr (TypeRelationName , RowExclusiveLock );
501+
502+ ScanKeyEntryInitialize (& typKey [0 ], 0x0 ,
503+ Anum_pg_constraint_contypid , F_OIDEQ ,
504+ ObjectIdGetDatum (con -> contypid ));
505+
506+ typScan = systable_beginscan (typRel , TypeOidIndex , true,
507+ SnapshotNow , 1 , typKey );
508+
509+ typTup = systable_getnext (typScan );
510+
511+ if (!HeapTupleIsValid (typTup ))
512+ elog (ERROR , "RemoveConstraintById: Type %d does not exist" ,
513+ con -> contypid );
514+
515+ systable_endscan (typScan );
516+
517+ /* Keep lock on domain type until end of xact */
518+ heap_close (typRel , NoLock );
519+ }
466520
467521 /* Fry the constraint itself */
468522 simple_heap_delete (conDesc , & tup -> t_self );
0 commit comments