@@ -450,17 +450,28 @@ static void getRelationIdentity(StringInfo buffer, Oid relid);
450450 * sub-object is looked up, the parent object will be locked instead.
451451 *
452452 * If the object is a relation or a child object of a relation (e.g. an
453- * attribute or contraint ), the relation is also opened and *relp receives
453+ * attribute or constraint ), the relation is also opened and *relp receives
454454 * the open relcache entry pointer; otherwise, *relp is set to NULL. This
455455 * is a bit grotty but it makes life simpler, since the caller will
456456 * typically need the relcache entry too. Caller must close the relcache
457457 * entry when done with it. The relation is locked with the specified lockmode
458458 * if the target object is the relation itself or an attribute, but for other
459459 * child objects, only AccessShareLock is acquired on the relation.
460460 *
461+ * If the object is not found, an error is thrown, unless missing_ok is
462+ * true. In this case, no lock is acquired, relp is set to NULL, and the
463+ * returned address has objectId set to InvalidOid.
464+ *
461465 * We don't currently provide a function to release the locks acquired here;
462466 * typically, the lock must be held until commit to guard against a concurrent
463467 * drop operation.
468+ *
469+ * Note: If the object is not found, we don't give any indication of the
470+ * reason. (It might have been a missing schema if the name was qualified, or
471+ * an inexistant type name in case of a cast, function or operator; etc).
472+ * Currently there is only one caller that might be interested in such info, so
473+ * we don't spend much effort here. If more callers start to care, it might be
474+ * better to add some support for that in this function.
464475 */
465476ObjectAddress
466477get_object_address (ObjectType objtype , List * objname , List * objargs ,
@@ -580,9 +591,11 @@ get_object_address(ObjectType objtype, List *objname, List *objargs,
580591 {
581592 TypeName * sourcetype = (TypeName * ) linitial (objname );
582593 TypeName * targettype = (TypeName * ) linitial (objargs );
583- Oid sourcetypeid = typenameTypeId ( NULL , sourcetype ) ;
584- Oid targettypeid = typenameTypeId ( NULL , targettype ) ;
594+ Oid sourcetypeid ;
595+ Oid targettypeid ;
585596
597+ sourcetypeid = LookupTypeNameOid (NULL , sourcetype , missing_ok );
598+ targettypeid = LookupTypeNameOid (NULL , targettype , missing_ok );
586599 address .classId = CastRelationId ;
587600 address .objectId =
588601 get_cast_oid (sourcetypeid , targettypeid , missing_ok );
@@ -942,26 +955,31 @@ get_object_address_relobject(ObjectType objtype, List *objname,
942955
943956 /* Extract relation name and open relation. */
944957 relname = list_truncate (list_copy (objname ), nnames - 1 );
945- relation = heap_openrv (makeRangeVarFromNameList (relname ),
946- AccessShareLock );
947- reloid = RelationGetRelid (relation );
958+ relation = heap_openrv_extended (makeRangeVarFromNameList (relname ),
959+ AccessShareLock ,
960+ missing_ok );
961+
962+ reloid = relation ? RelationGetRelid (relation ) : InvalidOid ;
948963
949964 switch (objtype )
950965 {
951966 case OBJECT_RULE :
952967 address .classId = RewriteRelationId ;
953- address .objectId = get_rewrite_oid (reloid , depname , missing_ok );
968+ address .objectId = relation ?
969+ get_rewrite_oid (reloid , depname , missing_ok ) : InvalidOid ;
954970 address .objectSubId = 0 ;
955971 break ;
956972 case OBJECT_TRIGGER :
957973 address .classId = TriggerRelationId ;
958- address .objectId = get_trigger_oid (reloid , depname , missing_ok );
974+ address .objectId = relation ?
975+ get_trigger_oid (reloid , depname , missing_ok ) : InvalidOid ;
959976 address .objectSubId = 0 ;
960977 break ;
961978 case OBJECT_CONSTRAINT :
962979 address .classId = ConstraintRelationId ;
963- address .objectId =
964- get_relation_constraint_oid (reloid , depname , missing_ok );
980+ address .objectId = relation ?
981+ get_relation_constraint_oid (reloid , depname , missing_ok ) :
982+ InvalidOid ;
965983 address .objectSubId = 0 ;
966984 break ;
967985 default :
@@ -975,7 +993,9 @@ get_object_address_relobject(ObjectType objtype, List *objname,
975993 /* Avoid relcache leak when object not found. */
976994 if (!OidIsValid (address .objectId ))
977995 {
978- heap_close (relation , AccessShareLock );
996+ if (relation != NULL )
997+ heap_close (relation , AccessShareLock );
998+
979999 relation = NULL ; /* department of accident prevention */
9801000 return address ;
9811001 }
@@ -1008,6 +1028,7 @@ get_object_address_attribute(ObjectType objtype, List *objname,
10081028 errmsg ("column name must be qualified" )));
10091029 attname = strVal (lfirst (list_tail (objname )));
10101030 relname = list_truncate (list_copy (objname ), list_length (objname ) - 1 );
1031+ /* XXX no missing_ok support here */
10111032 relation = relation_openrv (makeRangeVarFromNameList (relname ), lockmode );
10121033 reloid = RelationGetRelid (relation );
10131034
@@ -1053,7 +1074,7 @@ get_object_address_type(ObjectType objtype,
10531074 address .objectId = InvalidOid ;
10541075 address .objectSubId = 0 ;
10551076
1056- tup = LookupTypeName (NULL , typename , NULL );
1077+ tup = LookupTypeName (NULL , typename , NULL , missing_ok );
10571078 if (!HeapTupleIsValid (tup ))
10581079 {
10591080 if (!missing_ok )
@@ -1090,6 +1111,7 @@ get_object_address_opcf(ObjectType objtype,
10901111 ObjectAddress address ;
10911112
10921113 Assert (list_length (objargs ) == 1 );
1114+ /* XXX no missing_ok support here */
10931115 amoid = get_am_oid (strVal (linitial (objargs )), false);
10941116
10951117 switch (objtype )
0 commit comments