88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.30 2003/02/03 21:15:43 tgl Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.31 2003/02/19 04:02:53 momjian Exp $
1212 *
1313 * DESCRIPTION
1414 * The "DefineFoo" routines take the parse tree and pick out the
@@ -353,6 +353,12 @@ RemoveType(List *names, DropBehavior behavior)
353353 elog (ERROR , "Type \"%s\" does not exist" ,
354354 TypeNameToString (typename ));
355355
356+ /*
357+ * Grab an exclusive lock on the type id, the SearchSysCache confirms
358+ * the type still exists after locking
359+ */
360+ LockObject (typeoid , RelOid_pg_type , AccessExclusiveLock );
361+
356362 tup = SearchSysCache (TYPEOID ,
357363 ObjectIdGetDatum (typeoid ),
358364 0 , 0 , 0 );
@@ -376,6 +382,9 @@ RemoveType(List *names, DropBehavior behavior)
376382 object .objectSubId = 0 ;
377383
378384 performDeletion (& object , behavior );
385+
386+ /* Hold the lock until the end of the transaction */
387+ UnlockObject (typeoid , RelOid_pg_type , NoLock );
379388}
380389
381390
680689RemoveDomain (List * names , DropBehavior behavior )
681690{
682691 TypeName * typename ;
683- Oid typeoid ;
692+ Oid domainoid ;
684693 HeapTuple tup ;
685694 char typtype ;
686695 ObjectAddress object ;
@@ -692,20 +701,26 @@ RemoveDomain(List *names, DropBehavior behavior)
692701 typename -> arrayBounds = NIL ;
693702
694703 /* Use LookupTypeName here so that shell types can be removed. */
695- typeoid = LookupTypeName (typename );
696- if (!OidIsValid (typeoid ))
704+ domainoid = LookupTypeName (typename );
705+ if (!OidIsValid (domainoid ))
697706 elog (ERROR , "Type \"%s\" does not exist" ,
698707 TypeNameToString (typename ));
699708
709+ /*
710+ * Lock the domain. The SearchSysCache confirms the domain still exists
711+ * after locking
712+ */
713+ LockObject (domainoid , RelOid_pg_type , AccessExclusiveLock );
714+
700715 tup = SearchSysCache (TYPEOID ,
701- ObjectIdGetDatum (typeoid ),
716+ ObjectIdGetDatum (domainoid ),
702717 0 , 0 , 0 );
703718 if (!HeapTupleIsValid (tup ))
704719 elog (ERROR , "RemoveDomain: type \"%s\" does not exist" ,
705720 TypeNameToString (typename ));
706721
707722 /* Permission check: must own type or its namespace */
708- if (!pg_type_ownercheck (typeoid , GetUserId ()) &&
723+ if (!pg_type_ownercheck (domainoid , GetUserId ()) &&
709724 !pg_namespace_ownercheck (((Form_pg_type ) GETSTRUCT (tup ))-> typnamespace ,
710725 GetUserId ()))
711726 aclcheck_error (ACLCHECK_NOT_OWNER , TypeNameToString (typename ));
@@ -723,10 +738,13 @@ RemoveDomain(List *names, DropBehavior behavior)
723738 * Do the deletion
724739 */
725740 object .classId = RelOid_pg_type ;
726- object .objectId = typeoid ;
741+ object .objectId = domainoid ;
727742 object .objectSubId = 0 ;
728743
729744 performDeletion (& object , behavior );
745+
746+ /* Hold the lock until the end of the transaction */
747+ UnlockObject (domainoid , RelOid_pg_type , NoLock );
730748}
731749
732750
@@ -941,6 +959,12 @@ AlterDomainDefault(List *names, Node *defaultRaw)
941959 elog (ERROR , "Type \"%s\" does not exist" ,
942960 TypeNameToString (typename ));
943961
962+ /*
963+ * Lock the domain. The SearchSysCacheCopy confirms the type
964+ * still exists after locking
965+ */
966+ LockObject (domainoid , RelOid_pg_type , AccessExclusiveLock );
967+
944968 tup = SearchSysCacheCopy (TYPEOID ,
945969 ObjectIdGetDatum (domainoid ),
946970 0 , 0 , 0 );
@@ -1025,6 +1049,7 @@ AlterDomainDefault(List *names, Node *defaultRaw)
10251049 /* Clean up */
10261050 heap_close (rel , NoLock );
10271051 heap_freetuple (newtuple );
1052+ UnlockObject (domainoid , RelOid_pg_type , NoLock );
10281053};
10291054
10301055/*
@@ -1056,6 +1081,12 @@ AlterDomainNotNull(List *names, bool notNull)
10561081 elog (ERROR , "Type \"%s\" does not exist" ,
10571082 TypeNameToString (typename ));
10581083
1084+ /*
1085+ * Lock the domain. The SearchSysCacheCopy confirms the domain
1086+ * still exists after locking
1087+ */
1088+ LockObject (domainoid , RelOid_pg_type , AccessExclusiveLock );
1089+
10591090 tup = SearchSysCacheCopy (TYPEOID ,
10601091 ObjectIdGetDatum (domainoid ),
10611092 0 , 0 , 0 );
@@ -1137,6 +1168,7 @@ AlterDomainNotNull(List *names, bool notNull)
11371168 /* Clean up */
11381169 heap_freetuple (tup );
11391170 heap_close (typrel , RowExclusiveLock );
1171+ UnlockObject (domainoid , RelOid_pg_type , NoLock );
11401172}
11411173
11421174/*
@@ -1172,6 +1204,12 @@ AlterDomainDropConstraint(List *names, const char *constrName, DropBehavior beha
11721204 elog (ERROR , "Type \"%s\" does not exist" ,
11731205 TypeNameToString (typename ));
11741206
1207+ /*
1208+ * Lock the domain. The SearchSysCacheCopy confirms the type still
1209+ * exists after locking.
1210+ */
1211+ LockObject (domainoid , RelOid_pg_type , AccessExclusiveLock );
1212+
11751213 tup = SearchSysCacheCopy (TYPEOID ,
11761214 ObjectIdGetDatum (domainoid ),
11771215 0 , 0 , 0 );
@@ -1219,6 +1257,7 @@ AlterDomainDropConstraint(List *names, const char *constrName, DropBehavior beha
12191257 heap_close (conrel , RowExclusiveLock );
12201258
12211259 heap_close (rel , NoLock );
1260+ UnlockObject (domainoid , RelOid_pg_type , NoLock );
12221261};
12231262
12241263/*
@@ -1259,6 +1298,12 @@ AlterDomainAddConstraint(List *names, Node *newConstraint)
12591298 elog (ERROR , "Type \"%s\" does not exist" ,
12601299 TypeNameToString (typename ));
12611300
1301+ /*
1302+ * Lock the domain. The SearchSysCacheCopy confirms the domain
1303+ * still exists after locking.
1304+ */
1305+ LockObject (domainoid , RelOid_pg_type , AccessExclusiveLock );
1306+
12621307 tup = SearchSysCacheCopy (TYPEOID ,
12631308 ObjectIdGetDatum (domainoid ),
12641309 0 , 0 , 0 );
@@ -1393,6 +1438,7 @@ AlterDomainAddConstraint(List *names, Node *newConstraint)
13931438
13941439 /* Clean up */
13951440 heap_close (typrel , RowExclusiveLock );
1441+ UnlockObject (domainoid , RelOid_pg_type , NoLock );
13961442}
13971443
13981444/*
@@ -1696,7 +1742,10 @@ GetDomainConstraints(Oid typeOid)
16961742 Form_pg_type typTup ;
16971743 ScanKeyData key [1 ];
16981744 SysScanDesc scan ;
1699-
1745+
1746+ /* Lock the domain */
1747+ LockObject (typeOid , RelOid_pg_type , AccessShareLock );
1748+
17001749 tup = SearchSysCache (TYPEOID ,
17011750 ObjectIdGetDatum (typeOid ),
17021751 0 , 0 , 0 );
@@ -1824,6 +1873,12 @@ AlterTypeOwner(List *names, AclId newOwnerSysId)
18241873 elog (ERROR , "Type \"%s\" does not exist" ,
18251874 TypeNameToString (typename ));
18261875
1876+ /*
1877+ * Lock the type. The SearchSysCacheCopy serves to confirm the
1878+ * domain still exists after locking
1879+ */
1880+ LockObject (typeOid , RelOid_pg_type , AccessExclusiveLock );
1881+
18271882 tup = SearchSysCacheCopy (TYPEOID ,
18281883 ObjectIdGetDatum (typeOid ),
18291884 0 , 0 , 0 );
@@ -1846,4 +1901,5 @@ AlterTypeOwner(List *names, AclId newOwnerSysId)
18461901
18471902 /* Clean up */
18481903 heap_close (rel , RowExclusiveLock );
1904+ UnlockObject (typeOid , RelOid_pg_type , NoLock );
18491905}
0 commit comments