 src/backend/commands/aggregatecmds.c        |   25 +--
 src/backend/commands/alter.c                |  247 +++++++++++++++++++++++----
 src/backend/commands/collationcmds.c        |   20 +--
 src/backend/commands/conversioncmds.c       |   53 ------
 src/backend/commands/event_trigger.c        |   46 -----
 src/backend/commands/foreigncmds.c          |   87 ----------
 src/backend/commands/functioncmds.c         |   26 +--
 src/backend/commands/opclasscmds.c          |   78 +++------
 src/backend/commands/proclang.c             |   42 -----
 src/backend/commands/tablespace.c           |   14 +-
 src/backend/commands/tsearchcmds.c          |  196 ---------------------
 src/backend/parser/gram.y                   |    8 +-
 src/backend/storage/lmgr/lock.c             |   29 +++-
 src/include/commands/alter.h                |    2 +
 src/include/commands/conversioncmds.h       |    1 -
 src/include/commands/defrem.h               |    2 -
 src/include/commands/event_trigger.h        |    1 -
 src/include/commands/proclang.h             |    1 -
 src/test/regress/expected/alter_generic.out |   12 +-
 19 files changed, 295 insertions(+), 595 deletions(-)

diff --git a/src/backend/commands/aggregatecmds.c b/src/backend/commands/aggregatecmds.c
index 1447683..b069040 100644
--- a/src/backend/commands/aggregatecmds.c
+++ b/src/backend/commands/aggregatecmds.c
@@ -29,6 +29,7 @@
 #include "catalog/pg_aggregate.h"
 #include "catalog/pg_proc.h"
 #include "catalog/pg_type.h"
+#include "commands/alter.h"
 #include "commands/defrem.h"
 #include "miscadmin.h"
 #include "parser/parse_func.h"
@@ -240,15 +241,14 @@ RenameAggregate(List *name, List *args, const char *newname)
 	HeapTuple	tup;
 	Form_pg_proc procForm;
 	Relation	rel;
-	AclResult	aclresult;
 
 	rel = heap_open(ProcedureRelationId, RowExclusiveLock);
 
 	/* Look up function and make sure it's an aggregate */
 	procOid = LookupAggNameTypeNames(name, args, false);
 
-	tup = SearchSysCacheCopy1(PROCOID, ObjectIdGetDatum(procOid));
-	if (!HeapTupleIsValid(tup)) /* should not happen */
+	tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(procOid));
+	if (!HeapTupleIsValid(tup))	/* should not happen */
 		elog(ERROR, "cache lookup failed for function %u", procOid);
 	procForm = (Form_pg_proc) GETSTRUCT(tup);
 
@@ -268,24 +268,11 @@ RenameAggregate(List *name, List *args, const char *newname)
 											   procForm->proargtypes.values),
 						get_namespace_name(namespaceOid))));
 
-	/* must be owner */
-	if (!pg_proc_ownercheck(procOid, GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
-					   NameListToString(name));
-
-	/* must have CREATE privilege on namespace */
-	aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
-	if (aclresult != ACLCHECK_OK)
-		aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
-					   get_namespace_name(namespaceOid));
-
-	/* rename */
-	namestrcpy(&(((Form_pg_proc) GETSTRUCT(tup))->proname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
+	/* OK, do the work */
+	AlterObjectRename_internal(rel, procOid, newname);
 
+	ReleaseSysCache(tup);
 	heap_close(rel, NoLock);
-	heap_freetuple(tup);
 
 	return procOid;
 }
diff --git a/src/backend/commands/alter.c b/src/backend/commands/alter.c
index 73138f9..461e61c 100644
--- a/src/backend/commands/alter.c
+++ b/src/backend/commands/alter.c
@@ -19,9 +19,18 @@
 #include "catalog/dependency.h"
 #include "catalog/indexing.h"
 #include "catalog/namespace.h"
+#include "catalog/pg_conversion.h"
+#include "catalog/pg_event_trigger.h"
+#include "catalog/pg_foreign_data_wrapper.h"
+#include "catalog/pg_foreign_server.h"
+#include "catalog/pg_language.h"
 #include "catalog/pg_largeobject.h"
 #include "catalog/pg_largeobject_metadata.h"
 #include "catalog/pg_namespace.h"
+#include "catalog/pg_ts_config.h"
+#include "catalog/pg_ts_dict.h"
+#include "catalog/pg_ts_parser.h"
+#include "catalog/pg_ts_template.h"
 #include "commands/alter.h"
 #include "commands/collationcmds.h"
 #include "commands/conversioncmds.h"
@@ -36,6 +45,7 @@
 #include "commands/trigger.h"
 #include "commands/typecmds.h"
 #include "commands/user.h"
+#include "parser/parse_func.h"
 #include "miscadmin.h"
 #include "tcop/utility.h"
 #include "utils/builtins.h"
@@ -45,6 +55,184 @@
 #include "utils/syscache.h"
 #include "utils/tqual.h"
 
+/*
+ * ereport_on_namespace_conflict
+ *
+ * It raises an error with appropriate message for namespace conflict.
+ */
+static void
+ereport_on_namespace_conflict(Oid classId, Oid namespaceId, const char *name)
+{
+	const char *fmt = NULL;
+
+	switch (classId)
+	{
+		case ConversionRelationId:
+			fmt =  "conversion \"%s\" already exists in schema \"%s\"";
+			break;
+
+		case EventTriggerRelationId:
+			fmt = "event trigger \"%s\" already exists";
+			break;
+
+		case ForeignDataWrapperRelationId:
+			fmt = "foreign-data wrapper \"%s\" already exists";
+			break;
+
+		case ForeignServerRelationId:
+			fmt = "server \"%s\" already exists";
+			break;
+
+		case LanguageRelationId:
+			fmt = "language \"%s\" already exists";
+			break;
+
+		case TSConfigRelationId:
+			fmt = "text search parser \"%s\" already exists";
+			break;
+
+		case TSDictionaryRelationId:
+			fmt = "text search dictionary \"%s\" already exists";
+			break;
+
+		case TSParserRelationId:
+			fmt = "text search parser \"%s\" already exists";
+			break;
+
+		case TSTemplateRelationId:
+			fmt = "text search template \"%s\" already exists";
+			break;
+
+		default:	/* should not happen */
+			elog(ERROR, "unexpected class-id: %u", classId);
+	}
+
+	ereport(ERROR,
+			(errcode(ERRCODE_DUPLICATE_OBJECT),
+			 (OidIsValid(namespaceId) ?
+			  errmsg(fmt, name, get_namespace_name(namespaceId)) :
+			  errmsg(fmt, name))));
+}
+
+/*
+ * AlterObjectRename_internal
+ *
+ * Generic function to rename the given object, for simple cases (won't
+ * work for tables, nor other cases where we need to do more than change
+ * the name column of a single catalog entry).
+ * It checks name duplication only when catalog cache by name as key is
+ * available. Otherwise, the caller should ensure here is no object with
+ * identical name.
+ *
+ * rel: catalog relation containing object (RowExclusiveLock'd by caller)
+ * objectId: OID of object to be renamed
+ * new_name: CString representation of new name
+ */
+void
+AlterObjectRename_internal(Relation rel, Oid objectId, const char *new_name)
+{
+	Oid			classId = RelationGetRelid(rel);
+	int			oidCacheId = get_object_catcache_oid(classId);
+	int			nameCacheId = get_object_catcache_name(classId);
+	AttrNumber	Anum_name = get_object_attnum_name(classId);
+	AttrNumber	Anum_namespace = get_object_attnum_namespace(classId);
+	AttrNumber	Anum_owner = get_object_attnum_owner(classId);
+	AclObjectKind acl_kind = get_object_aclkind(classId);
+	HeapTuple	oldtup;
+	HeapTuple	newtup;
+	Datum		datum;
+	bool		isnull;
+	Oid			namespaceId;
+	Oid			ownerId;
+	char	   *old_name;
+	AclResult	aclresult;
+	Datum	   *values;
+	bool	   *nulls;
+	bool	   *replaces;
+
+	oldtup = SearchSysCache1(oidCacheId, ObjectIdGetDatum(objectId));
+	if (!HeapTupleIsValid(oldtup))
+		elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
+			 objectId, RelationGetRelationName(rel));
+
+	datum = heap_getattr(oldtup, Anum_name,
+						 RelationGetDescr(rel), &isnull);
+	Assert(!isnull);
+	old_name = NameStr(*(DatumGetName(datum)));
+
+	/* Get OID of namespace */
+	if (Anum_namespace > 0)
+	{
+		datum = heap_getattr(oldtup, Anum_namespace,
+							 RelationGetDescr(rel), &isnull);
+		Assert(!isnull);
+		namespaceId = DatumGetObjectId(datum);
+	}
+	else
+		namespaceId = InvalidOid;
+
+	/* Permission checks ... superusers can always do it */
+	if (!superuser())
+	{
+		/* Fail if object does not have an explicit owner */
+		if (Anum_owner <= 0)
+			ereport(ERROR,
+					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+					 (errmsg("must be superuser to set schema of %s",
+							 getObjectDescriptionOids(classId, objectId)))));
+
+		/* Otherwise, must be owner of the existing object */
+		datum = heap_getattr(oldtup, Anum_owner,
+							 RelationGetDescr(rel), &isnull);
+		Assert(!isnull);
+		ownerId = DatumGetObjectId(datum);
+
+		if (!has_privs_of_role(GetUserId(), DatumGetObjectId(ownerId)))
+			aclcheck_error(ACLCHECK_NOT_OWNER, acl_kind, old_name);
+
+		/* User must have CREATE privilege on the namespace */
+		if (OidIsValid(namespaceId))
+		{
+			aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
+											  ACL_CREATE);
+			if (aclresult != ACLCHECK_OK)
+				aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
+							   get_namespace_name(namespaceId));
+		}
+	}
+
+	/*
+     * Check for duplicate name (more friendly than unique-index failure).
+     * Since this is just a friendliness check, we can just skip it in cases
+     * where there isn't a suitable syscache available.
+     */
+    if (nameCacheId >= 0 &&
+		SearchSysCacheExists2(nameCacheId,
+							  CStringGetDatum(new_name),
+							  ObjectIdGetDatum(namespaceId)))
+		ereport_on_namespace_conflict(classId, namespaceId, new_name);
+
+	/* Build modified tuple */
+	values = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(Datum));
+	nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
+	replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
+	values[Anum_name - 1] = PointerGetDatum(new_name);
+	replaces[Anum_name - 1] = true;
+	newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
+							   values, nulls, replaces);
+
+	/* Perform actual update */
+	simple_heap_update(rel, &oldtup->t_self, newtup);
+	CatalogUpdateIndexes(rel, newtup);
+
+	/* Release memory */
+	pfree(values);
+	pfree(nulls);
+	pfree(replaces);
+	heap_freetuple(newtup);
+
+	ReleaseSysCache(oldtup);
+}
 
 /*
  * Executes an ALTER OBJECT / RENAME TO statement.	Based on the object
@@ -64,27 +252,12 @@ ExecRenameStmt(RenameStmt *stmt)
 		case OBJECT_CONSTRAINT:
 			return RenameConstraint(stmt);
 
-		case OBJECT_CONVERSION:
-			return RenameConversion(stmt->object, stmt->newname);
-
 		case OBJECT_DATABASE:
 			return RenameDatabase(stmt->subname, stmt->newname);
 
-		case OBJECT_FDW:
-			return RenameForeignDataWrapper(stmt->subname, stmt->newname);
-
-		case OBJECT_FOREIGN_SERVER:
-			return RenameForeignServer(stmt->subname, stmt->newname);
-
-		case OBJECT_EVENT_TRIGGER:
-			return RenameEventTrigger(stmt->subname, stmt->newname);
-
 		case OBJECT_FUNCTION:
 			return RenameFunction(stmt->object, stmt->objarg, stmt->newname);
 
-		case OBJECT_LANGUAGE:
-			return RenameLanguage(stmt->subname, stmt->newname);
-
 		case OBJECT_OPCLASS:
 			return RenameOpClass(stmt->object, stmt->subname, stmt->newname);
 
@@ -114,21 +287,39 @@ ExecRenameStmt(RenameStmt *stmt)
 		case OBJECT_TRIGGER:
 			return renametrig(stmt);
 
-		case OBJECT_TSPARSER:
-			return RenameTSParser(stmt->object, stmt->newname);
+		case OBJECT_DOMAIN:
+		case OBJECT_TYPE:
+			return RenameType(stmt);
 
+		case OBJECT_CONVERSION:
+		case OBJECT_EVENT_TRIGGER:
+		case OBJECT_FDW:
+		case OBJECT_FOREIGN_SERVER:
+		case OBJECT_LANGUAGE:
+		case OBJECT_TSCONFIGURATION:
 		case OBJECT_TSDICTIONARY:
-			return RenameTSDictionary(stmt->object, stmt->newname);
-
+		case OBJECT_TSPARSER:
 		case OBJECT_TSTEMPLATE:
-			return RenameTSTemplate(stmt->object, stmt->newname);
+			{
+				ObjectAddress	address;
+				Relation		catalog;
+				Relation		relation;
 
-		case OBJECT_TSCONFIGURATION:
-			return RenameTSConfiguration(stmt->object, stmt->newname);
+				address = get_object_address(stmt->renameType,
+											 stmt->object, stmt->objarg,
+											 &relation,
+											 AccessExclusiveLock, false);
+				catalog = heap_open(address.classId, RowExclusiveLock);
+
+				AlterObjectRename_internal(catalog,
+										   address.objectId,
+										   stmt->newname);
+				if (relation)
+					heap_close(relation, NoLock);
+				heap_close(catalog, RowExclusiveLock);
 
-		case OBJECT_DOMAIN:
-		case OBJECT_TYPE:
-			return RenameType(stmt);
+				return address.objectId;
+			}
 
 		default:
 			elog(ERROR, "unrecognized rename stmt type: %d",
@@ -375,11 +566,7 @@ AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
 	 */
 	if (nameCacheId >= 0 &&
 		SearchSysCacheExists2(nameCacheId, name, ObjectIdGetDatum(nspOid)))
-		ereport(ERROR,
-				(errcode(ERRCODE_DUPLICATE_OBJECT),
-				 errmsg("%s already exists in schema \"%s\"",
-						getObjectDescriptionOids(classId, objid),
-						get_namespace_name(nspOid))));
+		ereport_on_namespace_conflict(classId, nspOid, DatumGetCString(name));
 
 	/* Build modified tuple */
 	values = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(Datum));
diff --git a/src/backend/commands/collationcmds.c b/src/backend/commands/collationcmds.c
index b763f15..f0933b4 100644
--- a/src/backend/commands/collationcmds.c
+++ b/src/backend/commands/collationcmds.c
@@ -154,7 +154,6 @@ RenameCollation(List *name, const char *newname)
 	Oid			namespaceOid;
 	HeapTuple	tup;
 	Relation	rel;
-	AclResult	aclresult;
 
 	rel = heap_open(CollationRelationId, RowExclusiveLock);
 
@@ -189,23 +188,8 @@ RenameCollation(List *name, const char *newname)
 						newname,
 						get_namespace_name(namespaceOid))));
 
-	/* must be owner */
-	if (!pg_collation_ownercheck(collationOid, GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_COLLATION,
-					   NameListToString(name));
-
-	/* must have CREATE privilege on namespace */
-	aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
-	if (aclresult != ACLCHECK_OK)
-		aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
-					   get_namespace_name(namespaceOid));
-
-	/* rename */
-	namestrcpy(&(((Form_pg_collation) GETSTRUCT(tup))->collname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
-
-	heap_freetuple(tup);
+	/* ok, do the task */
+	AlterObjectRename_internal(rel, collationOid, newname);
 
 	heap_close(rel, RowExclusiveLock);
 
diff --git a/src/backend/commands/conversioncmds.c b/src/backend/commands/conversioncmds.c
index 35d4d08..84ab434 100644
--- a/src/backend/commands/conversioncmds.c
+++ b/src/backend/commands/conversioncmds.c
@@ -114,56 +114,3 @@ CreateConversionCommand(CreateConversionStmt *stmt)
 	return ConversionCreate(conversion_name, namespaceId, GetUserId(),
 							from_encoding, to_encoding, funcoid, stmt->def);
 }
-
-/*
- * Rename conversion
- */
-Oid
-RenameConversion(List *name, const char *newname)
-{
-	Oid			conversionOid;
-	Oid			namespaceOid;
-	HeapTuple	tup;
-	Relation	rel;
-	AclResult	aclresult;
-
-	rel = heap_open(ConversionRelationId, RowExclusiveLock);
-
-	conversionOid = get_conversion_oid(name, false);
-
-	tup = SearchSysCacheCopy1(CONVOID, ObjectIdGetDatum(conversionOid));
-	if (!HeapTupleIsValid(tup)) /* should not happen */
-		elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
-
-	namespaceOid = ((Form_pg_conversion) GETSTRUCT(tup))->connamespace;
-
-	/* make sure the new name doesn't exist */
-	if (SearchSysCacheExists2(CONNAMENSP,
-							  CStringGetDatum(newname),
-							  ObjectIdGetDatum(namespaceOid)))
-		ereport(ERROR,
-				(errcode(ERRCODE_DUPLICATE_OBJECT),
-				 errmsg("conversion \"%s\" already exists in schema \"%s\"",
-						newname, get_namespace_name(namespaceOid))));
-
-	/* must be owner */
-	if (!pg_conversion_ownercheck(conversionOid, GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
-					   NameListToString(name));
-
-	/* must have CREATE privilege on namespace */
-	aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
-	if (aclresult != ACLCHECK_OK)
-		aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
-					   get_namespace_name(namespaceOid));
-
-	/* rename */
-	namestrcpy(&(((Form_pg_conversion) GETSTRUCT(tup))->conname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
-
-	heap_close(rel, NoLock);
-	heap_freetuple(tup);
-
-	return conversionOid;
-}
diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c
index 99b17a0..9063187 100644
--- a/src/backend/commands/event_trigger.c
+++ b/src/backend/commands/event_trigger.c
@@ -417,52 +417,6 @@ AlterEventTrigger(AlterEventTrigStmt *stmt)
 	return trigoid;
 }
 
-
-/*
- * Rename event trigger
- */
-Oid
-RenameEventTrigger(const char *trigname, const char *newname)
-{
-	Oid			evtId;
-	HeapTuple	tup;
-	Relation	rel;
-	Form_pg_event_trigger evtForm;
-
-	rel = heap_open(EventTriggerRelationId, RowExclusiveLock);
-
-	/* newname must be available */
-	if (SearchSysCacheExists1(EVENTTRIGGERNAME, CStringGetDatum(newname)))
-		ereport(ERROR,
-				(errcode(ERRCODE_DUPLICATE_OBJECT),
-				 errmsg("event trigger \"%s\" already exists", newname)));
-
-	/* trigname must exists */
-	tup = SearchSysCacheCopy1(EVENTTRIGGERNAME, CStringGetDatum(trigname));
-	if (!HeapTupleIsValid(tup))
-		ereport(ERROR,
-				(errcode(ERRCODE_UNDEFINED_OBJECT),
-				 errmsg("event trigger \"%s\" does not exist", trigname)));
-	if (!pg_event_trigger_ownercheck(HeapTupleGetOid(tup), GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_EVENT_TRIGGER,
-					   trigname);
-
-	evtId = HeapTupleGetOid(tup);
-
-	evtForm = (Form_pg_event_trigger) GETSTRUCT(tup);
-
-	/* tuple is a copy, so we can rename it now */
-	namestrcpy(&(evtForm->evtname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
-
-	heap_freetuple(tup);
-	heap_close(rel, RowExclusiveLock);
-
-	return evtId;
-}
-
-
 /*
  * Change event trigger's owner -- by name
  */
diff --git a/src/backend/commands/foreigncmds.c b/src/backend/commands/foreigncmds.c
index d3af8dc..7700e91 100644
--- a/src/backend/commands/foreigncmds.c
+++ b/src/backend/commands/foreigncmds.c
@@ -200,93 +200,6 @@ GetUserOidFromMapping(const char *username, bool missing_ok)
 	return get_role_oid(username, missing_ok);
 }
 
-
-/*
- * Rename foreign-data wrapper
- */
-Oid
-RenameForeignDataWrapper(const char *oldname, const char *newname)
-{
-	Oid			fdwId;
-	HeapTuple	tup;
-	Relation	rel;
-
-	rel = heap_open(ForeignDataWrapperRelationId, RowExclusiveLock);
-
-	tup = SearchSysCacheCopy1(FOREIGNDATAWRAPPERNAME, CStringGetDatum(oldname));
-	if (!HeapTupleIsValid(tup))
-		ereport(ERROR,
-				(errcode(ERRCODE_UNDEFINED_OBJECT),
-			 errmsg("foreign-data wrapper \"%s\" does not exist", oldname)));
-
-	fdwId = HeapTupleGetOid(tup);
-
-	/* make sure the new name doesn't exist */
-	if (SearchSysCacheExists1(FOREIGNDATAWRAPPERNAME, CStringGetDatum(newname)))
-		ereport(ERROR,
-				(errcode(ERRCODE_DUPLICATE_OBJECT),
-			 errmsg("foreign-data wrapper \"%s\" already exists", newname)));
-
-	/* must be owner of FDW */
-	if (!pg_foreign_data_wrapper_ownercheck(HeapTupleGetOid(tup), GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_FDW,
-					   oldname);
-
-	/* rename */
-	namestrcpy(&(((Form_pg_foreign_data_wrapper) GETSTRUCT(tup))->fdwname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
-
-	heap_close(rel, NoLock);
-	heap_freetuple(tup);
-
-	return fdwId;
-}
-
-
-/*
- * Rename foreign server
- */
-Oid
-RenameForeignServer(const char *oldname, const char *newname)
-{
-	Oid			srvId;
-	HeapTuple	tup;
-	Relation	rel;
-
-	rel = heap_open(ForeignServerRelationId, RowExclusiveLock);
-
-	tup = SearchSysCacheCopy1(FOREIGNSERVERNAME, CStringGetDatum(oldname));
-	if (!HeapTupleIsValid(tup))
-		ereport(ERROR,
-				(errcode(ERRCODE_UNDEFINED_OBJECT),
-				 errmsg("server \"%s\" does not exist", oldname)));
-
-	srvId = HeapTupleGetOid(tup);
-
-	/* make sure the new name doesn't exist */
-	if (SearchSysCacheExists1(FOREIGNSERVERNAME, CStringGetDatum(newname)))
-		ereport(ERROR,
-				(errcode(ERRCODE_DUPLICATE_OBJECT),
-				 errmsg("server \"%s\" already exists", newname)));
-
-	/* must be owner of server */
-	if (!pg_foreign_server_ownercheck(HeapTupleGetOid(tup), GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_FOREIGN_SERVER,
-					   oldname);
-
-	/* rename */
-	namestrcpy(&(((Form_pg_foreign_server) GETSTRUCT(tup))->srvname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
-
-	heap_close(rel, NoLock);
-	heap_freetuple(tup);
-
-	return srvId;
-}
-
-
 /*
  * Internal workhorse for changing a data wrapper's owner.
  *
diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c
index c858511..cbfd737 100644
--- a/src/backend/commands/functioncmds.c
+++ b/src/backend/commands/functioncmds.c
@@ -1048,14 +1048,13 @@ RenameFunction(List *name, List *argtypes, const char *newname)
 	HeapTuple	tup;
 	Form_pg_proc procForm;
 	Relation	rel;
-	AclResult	aclresult;
 
 	rel = heap_open(ProcedureRelationId, RowExclusiveLock);
 
 	procOid = LookupFuncNameTypeNames(name, argtypes, false);
 
-	tup = SearchSysCacheCopy1(PROCOID, ObjectIdGetDatum(procOid));
-	if (!HeapTupleIsValid(tup)) /* should not happen */
+	tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(procOid));
+	if (!HeapTupleIsValid(tup))	/* should not happen */
 		elog(ERROR, "cache lookup failed for function %u", procOid);
 	procForm = (Form_pg_proc) GETSTRUCT(tup);
 
@@ -1080,28 +1079,15 @@ RenameFunction(List *name, List *argtypes, const char *newname)
 						funcname_signature_string(newname,
 												  procForm->pronargs,
 												  NIL,
-											   procForm->proargtypes.values),
+												procForm->proargtypes.values),
 						get_namespace_name(namespaceOid))));
 	}
 
-	/* must be owner */
-	if (!pg_proc_ownercheck(procOid, GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
-					   NameListToString(name));
-
-	/* must have CREATE privilege on namespace */
-	aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
-	if (aclresult != ACLCHECK_OK)
-		aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
-					   get_namespace_name(namespaceOid));
-
-	/* rename */
-	namestrcpy(&(procForm->proname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
+	/* ok, do the work */
+	AlterObjectRename_internal(rel, procOid, newname);
 
+	ReleaseSysCache(tup);
 	heap_close(rel, NoLock);
-	heap_freetuple(tup);
 
 	return procOid;
 }
diff --git a/src/backend/commands/opclasscmds.c b/src/backend/commands/opclasscmds.c
index 4b57e3d..d12149a 100644
--- a/src/backend/commands/opclasscmds.c
+++ b/src/backend/commands/opclasscmds.c
@@ -1670,19 +1670,16 @@ RenameOpClass(List *name, const char *access_method, const char *newname)
 	Oid			opcOid;
 	Oid			amOid;
 	Oid			namespaceOid;
-	HeapTuple	origtup;
 	HeapTuple	tup;
 	Relation	rel;
-	AclResult	aclresult;
 
 	amOid = get_am_oid(access_method, false);
 
 	rel = heap_open(OperatorClassRelationId, RowExclusiveLock);
 
 	/* Look up the opclass. */
-	origtup = OpClassCacheLookup(amOid, name, false);
-	tup = heap_copytuple(origtup);
-	ReleaseSysCache(origtup);
+	tup = OpClassCacheLookup(amOid, name, false);
+
 	opcOid = HeapTupleGetOid(tup);
 	namespaceOid = ((Form_pg_opclass) GETSTRUCT(tup))->opcnamespace;
 
@@ -1698,25 +1695,11 @@ RenameOpClass(List *name, const char *access_method, const char *newname)
 						newname, access_method,
 						get_namespace_name(namespaceOid))));
 	}
-
-	/* must be owner */
-	if (!pg_opclass_ownercheck(opcOid, GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_OPCLASS,
-					   NameListToString(name));
-
-	/* must have CREATE privilege on namespace */
-	aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
-	if (aclresult != ACLCHECK_OK)
-		aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
-					   get_namespace_name(namespaceOid));
-
-	/* rename */
-	namestrcpy(&(((Form_pg_opclass) GETSTRUCT(tup))->opcname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
+	/* ok, do the work */
+	AlterObjectRename_internal(rel, opcOid, newname);
 
 	heap_close(rel, NoLock);
-	heap_freetuple(tup);
+	ReleaseSysCache(tup);
 
 	return opcOid;
 }
@@ -1727,14 +1710,13 @@ RenameOpClass(List *name, const char *access_method, const char *newname)
 Oid
 RenameOpFamily(List *name, const char *access_method, const char *newname)
 {
-	Oid			opfOid;
-	Oid			amOid;
-	Oid			namespaceOid;
-	char	   *schemaname;
-	char	   *opfname;
-	HeapTuple	tup;
-	Relation	rel;
-	AclResult	aclresult;
+	Oid         opfOid;
+	Oid         amOid;
+	Oid         namespaceOid;
+	char       *schemaname;
+	char       *opfname;
+	HeapTuple   tup;
+	Relation    rel;
 
 	amOid = get_am_oid(access_method, false);
 
@@ -1749,10 +1731,10 @@ RenameOpFamily(List *name, const char *access_method, const char *newname)
 	{
 		namespaceOid = LookupExplicitNamespace(schemaname);
 
-		tup = SearchSysCacheCopy3(OPFAMILYAMNAMENSP,
-								  ObjectIdGetDatum(amOid),
-								  PointerGetDatum(opfname),
-								  ObjectIdGetDatum(namespaceOid));
+		tup = SearchSysCache3(OPFAMILYAMNAMENSP,
+							  ObjectIdGetDatum(amOid),
+							  PointerGetDatum(opfname),
+							  ObjectIdGetDatum(namespaceOid));
 		if (!HeapTupleIsValid(tup))
 			ereport(ERROR,
 					(errcode(ERRCODE_UNDEFINED_OBJECT),
@@ -1760,6 +1742,8 @@ RenameOpFamily(List *name, const char *access_method, const char *newname)
 							opfname, access_method)));
 
 		opfOid = HeapTupleGetOid(tup);
+
+		ReleaseSysCache(tup);
 	}
 	else
 	{
@@ -1770,11 +1754,13 @@ RenameOpFamily(List *name, const char *access_method, const char *newname)
 					 errmsg("operator family \"%s\" does not exist for access method \"%s\"",
 							opfname, access_method)));
 
-		tup = SearchSysCacheCopy1(OPFAMILYOID, ObjectIdGetDatum(opfOid));
-		if (!HeapTupleIsValid(tup))		/* should not happen */
-			elog(ERROR, "cache lookup failed for opfamily %u", opfOid);
+		tup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfOid));
+		if (!HeapTupleIsValid(tup))     /* should not happen */
+	        elog(ERROR, "cache lookup failed for opfamily %u", opfOid);
 
 		namespaceOid = ((Form_pg_opfamily) GETSTRUCT(tup))->opfnamespace;
+
+		ReleaseSysCache(tup);
 	}
 
 	/* make sure the new name doesn't exist */
@@ -1790,24 +1776,10 @@ RenameOpFamily(List *name, const char *access_method, const char *newname)
 						get_namespace_name(namespaceOid))));
 	}
 
-	/* must be owner */
-	if (!pg_opfamily_ownercheck(opfOid, GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_OPFAMILY,
-					   NameListToString(name));
-
-	/* must have CREATE privilege on namespace */
-	aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
-	if (aclresult != ACLCHECK_OK)
-		aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
-					   get_namespace_name(namespaceOid));
-
-	/* rename */
-	namestrcpy(&(((Form_pg_opfamily) GETSTRUCT(tup))->opfname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
+	/* ok, do the work */
+	AlterObjectRename_internal(rel, opfOid, newname);
 
 	heap_close(rel, NoLock);
-	heap_freetuple(tup);
 
 	return opfOid;
 }
diff --git a/src/backend/commands/proclang.c b/src/backend/commands/proclang.c
index 718b711..e6c6d4e 100644
--- a/src/backend/commands/proclang.c
+++ b/src/backend/commands/proclang.c
@@ -537,48 +537,6 @@ DropProceduralLanguageById(Oid langOid)
 }
 
 /*
- * Rename language
- */
-Oid
-RenameLanguage(const char *oldname, const char *newname)
-{
-	Oid			lanId;
-	HeapTuple	tup;
-	Relation	rel;
-
-	rel = heap_open(LanguageRelationId, RowExclusiveLock);
-
-	tup = SearchSysCacheCopy1(LANGNAME, CStringGetDatum(oldname));
-	if (!HeapTupleIsValid(tup))
-		ereport(ERROR,
-				(errcode(ERRCODE_UNDEFINED_OBJECT),
-				 errmsg("language \"%s\" does not exist", oldname)));
-
-	lanId = HeapTupleGetOid(tup);
-
-	/* make sure the new name doesn't exist */
-	if (SearchSysCacheExists1(LANGNAME, CStringGetDatum(newname)))
-		ereport(ERROR,
-				(errcode(ERRCODE_DUPLICATE_OBJECT),
-				 errmsg("language \"%s\" already exists", newname)));
-
-	/* must be owner of PL */
-	if (!pg_language_ownercheck(HeapTupleGetOid(tup), GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_LANGUAGE,
-					   oldname);
-
-	/* rename */
-	namestrcpy(&(((Form_pg_language) GETSTRUCT(tup))->lanname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
-
-	heap_close(rel, NoLock);
-	heap_freetuple(tup);
-
-	return lanId;
-}
-
-/*
  * get_language_oid - given a language name, look up the OID
  *
  * If missing_ok is false, throw an error if language name not found.  If
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 9bba75b..f099789 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -61,6 +61,7 @@
 #include "catalog/indexing.h"
 #include "catalog/objectaccess.h"
 #include "catalog/pg_tablespace.h"
+#include "commands/alter.h"
 #include "commands/comment.h"
 #include "commands/seclabel.h"
 #include "commands/tablespace.h"
@@ -828,8 +829,6 @@ RenameTableSpace(const char *oldname, const char *newname)
 	ScanKeyData entry[1];
 	HeapScanDesc scan;
 	HeapTuple	tup;
-	HeapTuple	newtuple;
-	Form_pg_tablespace newform;
 
 	/* Search pg_tablespace */
 	rel = heap_open(TableSpaceRelationId, RowExclusiveLock);
@@ -847,15 +846,9 @@ RenameTableSpace(const char *oldname, const char *newname)
 						oldname)));
 
 	tspId = HeapTupleGetOid(tup);
-	newtuple = heap_copytuple(tup);
-	newform = (Form_pg_tablespace) GETSTRUCT(newtuple);
 
 	heap_endscan(scan);
 
-	/* Must be owner */
-	if (!pg_tablespace_ownercheck(HeapTupleGetOid(newtuple), GetUserId()))
-		aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_TABLESPACE, oldname);
-
 	/* Validate new name */
 	if (!allowSystemTableMods && IsReservedName(newname))
 		ereport(ERROR,
@@ -879,10 +872,7 @@ RenameTableSpace(const char *oldname, const char *newname)
 	heap_endscan(scan);
 
 	/* OK, update the entry */
-	namestrcpy(&(newform->spcname), newname);
-
-	simple_heap_update(rel, &newtuple->t_self, newtuple);
-	CatalogUpdateIndexes(rel, newtuple);
+	AlterObjectRename_internal(rel, tspId, newname);
 
 	heap_close(rel, NoLock);
 
diff --git a/src/backend/commands/tsearchcmds.c b/src/backend/commands/tsearchcmds.c
index aea9fd1..95377ac 100644
--- a/src/backend/commands/tsearchcmds.c
+++ b/src/backend/commands/tsearchcmds.c
@@ -305,51 +305,6 @@ RemoveTSParserById(Oid prsId)
 	heap_close(relation, RowExclusiveLock);
 }
 
-/*
- * ALTER TEXT SEARCH PARSER RENAME
- */
-Oid
-RenameTSParser(List *oldname, const char *newname)
-{
-	HeapTuple	tup;
-	Relation	rel;
-	Oid			prsId;
-	Oid			namespaceOid;
-
-	if (!superuser())
-		ereport(ERROR,
-				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
-				 errmsg("must be superuser to rename text search parsers")));
-
-	rel = heap_open(TSParserRelationId, RowExclusiveLock);
-
-	prsId = get_ts_parser_oid(oldname, false);
-
-	tup = SearchSysCacheCopy1(TSPARSEROID, ObjectIdGetDatum(prsId));
-
-	if (!HeapTupleIsValid(tup)) /* should not happen */
-		elog(ERROR, "cache lookup failed for text search parser %u", prsId);
-
-	namespaceOid = ((Form_pg_ts_parser) GETSTRUCT(tup))->prsnamespace;
-
-	if (SearchSysCacheExists2(TSPARSERNAMENSP,
-							  PointerGetDatum(newname),
-							  ObjectIdGetDatum(namespaceOid)))
-		ereport(ERROR,
-				(errcode(ERRCODE_DUPLICATE_OBJECT),
-				 errmsg("text search parser \"%s\" already exists",
-						newname)));
-
-	namestrcpy(&(((Form_pg_ts_parser) GETSTRUCT(tup))->prsname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
-
-	heap_close(rel, NoLock);
-	heap_freetuple(tup);
-
-	return prsId;
-}
-
 /* ---------------------- TS Dictionary commands -----------------------*/
 
 /*
@@ -535,59 +490,6 @@ DefineTSDictionary(List *names, List *parameters)
 }
 
 /*
- * ALTER TEXT SEARCH DICTIONARY RENAME
- */
-Oid
-RenameTSDictionary(List *oldname, const char *newname)
-{
-	HeapTuple	tup;
-	Relation	rel;
-	Oid			dictId;
-	Oid			namespaceOid;
-	AclResult	aclresult;
-
-	rel = heap_open(TSDictionaryRelationId, RowExclusiveLock);
-
-	dictId = get_ts_dict_oid(oldname, false);
-
-	tup = SearchSysCacheCopy1(TSDICTOID, ObjectIdGetDatum(dictId));
-
-	if (!HeapTupleIsValid(tup)) /* should not happen */
-		elog(ERROR, "cache lookup failed for text search dictionary %u",
-			 dictId);
-
-	namespaceOid = ((Form_pg_ts_dict) GETSTRUCT(tup))->dictnamespace;
-
-	if (SearchSysCacheExists2(TSDICTNAMENSP,
-							  PointerGetDatum(newname),
-							  ObjectIdGetDatum(namespaceOid)))
-		ereport(ERROR,
-				(errcode(ERRCODE_DUPLICATE_OBJECT),
-				 errmsg("text search dictionary \"%s\" already exists",
-						newname)));
-
-	/* must be owner */
-	if (!pg_ts_dict_ownercheck(dictId, GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TSDICTIONARY,
-					   NameListToString(oldname));
-
-	/* must have CREATE privilege on namespace */
-	aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
-	if (aclresult != ACLCHECK_OK)
-		aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
-					   get_namespace_name(namespaceOid));
-
-	namestrcpy(&(((Form_pg_ts_dict) GETSTRUCT(tup))->dictname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
-
-	heap_close(rel, NoLock);
-	heap_freetuple(tup);
-
-	return dictId;
-}
-
-/*
  * Guts of TS dictionary deletion.
  */
 void
@@ -905,52 +807,6 @@ DefineTSTemplate(List *names, List *parameters)
 }
 
 /*
- * ALTER TEXT SEARCH TEMPLATE RENAME
- */
-Oid
-RenameTSTemplate(List *oldname, const char *newname)
-{
-	HeapTuple	tup;
-	Relation	rel;
-	Oid			tmplId;
-	Oid			namespaceOid;
-
-	if (!superuser())
-		ereport(ERROR,
-				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
-			   errmsg("must be superuser to rename text search templates")));
-
-	rel = heap_open(TSTemplateRelationId, RowExclusiveLock);
-
-	tmplId = get_ts_template_oid(oldname, false);
-
-	tup = SearchSysCacheCopy1(TSTEMPLATEOID, ObjectIdGetDatum(tmplId));
-
-	if (!HeapTupleIsValid(tup)) /* should not happen */
-		elog(ERROR, "cache lookup failed for text search template %u",
-			 tmplId);
-
-	namespaceOid = ((Form_pg_ts_template) GETSTRUCT(tup))->tmplnamespace;
-
-	if (SearchSysCacheExists2(TSTEMPLATENAMENSP,
-							  PointerGetDatum(newname),
-							  ObjectIdGetDatum(namespaceOid)))
-		ereport(ERROR,
-				(errcode(ERRCODE_DUPLICATE_OBJECT),
-				 errmsg("text search template \"%s\" already exists",
-						newname)));
-
-	namestrcpy(&(((Form_pg_ts_template) GETSTRUCT(tup))->tmplname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
-
-	heap_close(rel, NoLock);
-	heap_freetuple(tup);
-
-	return tmplId;
-}
-
-/*
  * Guts of TS template deletion.
  */
 void
@@ -1249,58 +1105,6 @@ DefineTSConfiguration(List *names, List *parameters)
 }
 
 /*
- * ALTER TEXT SEARCH CONFIGURATION RENAME
- */
-Oid
-RenameTSConfiguration(List *oldname, const char *newname)
-{
-	HeapTuple	tup;
-	Relation	rel;
-	Oid			cfgId;
-	AclResult	aclresult;
-	Oid			namespaceOid;
-
-	rel = heap_open(TSConfigRelationId, RowExclusiveLock);
-
-	cfgId = get_ts_config_oid(oldname, false);
-
-	tup = SearchSysCacheCopy1(TSCONFIGOID, ObjectIdGetDatum(cfgId));
-
-	if (!HeapTupleIsValid(tup)) /* should not happen */
-		elog(ERROR, "cache lookup failed for text search configuration %u",
-			 cfgId);
-
-	namespaceOid = ((Form_pg_ts_config) GETSTRUCT(tup))->cfgnamespace;
-
-	if (SearchSysCacheExists2(TSCONFIGNAMENSP,
-							  PointerGetDatum(newname),
-							  ObjectIdGetDatum(namespaceOid)))
-		ereport(ERROR,
-				(errcode(ERRCODE_DUPLICATE_OBJECT),
-				 errmsg("text search configuration \"%s\" already exists",
-						newname)));
-
-	/* must be owner */
-	if (!pg_ts_config_ownercheck(cfgId, GetUserId()))
-		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TSCONFIGURATION,
-					   NameListToString(oldname));
-
-	/* must have CREATE privilege on namespace */
-	aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
-	aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
-				   get_namespace_name(namespaceOid));
-
-	namestrcpy(&(((Form_pg_ts_config) GETSTRUCT(tup))->cfgname), newname);
-	simple_heap_update(rel, &tup->t_self, tup);
-	CatalogUpdateIndexes(rel, tup);
-
-	heap_close(rel, NoLock);
-	heap_freetuple(tup);
-
-	return cfgId;
-}
-
-/*
  * Guts of TS configuration deletion.
  */
 void
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 76ef11e..f62cd3d 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -6776,7 +6776,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
 				{
 					RenameStmt *n = makeNode(RenameStmt);
 					n->renameType = OBJECT_FDW;
-					n->subname = $5;
+					n->object = list_make1(makeString($5));
 					n->newname = $8;
 					n->missing_ok = false;
 					$$ = (Node *)n;
@@ -6804,7 +6804,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
 				{
 					RenameStmt *n = makeNode(RenameStmt);
 					n->renameType = OBJECT_LANGUAGE;
-					n->subname = $4;
+					n->object = list_make1(makeString($4));
 					n->newname = $7;
 					n->missing_ok = false;
 					$$ = (Node *)n;
@@ -6842,7 +6842,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
 				{
 					RenameStmt *n = makeNode(RenameStmt);
 					n->renameType = OBJECT_FOREIGN_SERVER;
-					n->subname = $3;
+					n->object = list_make1(makeString($3));
 					n->newname = $6;
 					n->missing_ok = false;
 					$$ = (Node *)n;
@@ -7015,7 +7015,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
 				{
 					RenameStmt *n = makeNode(RenameStmt);
 					n->renameType = OBJECT_EVENT_TRIGGER;
-					n->subname = $4;
+					n->object = list_make1(makeString($4));
 					n->newname = $7;
 					$$ = (Node *)n;
 				}
diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index 49dadd1..5f2239f 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -1017,6 +1017,12 @@ LockAcquireExtended(const LOCKTAG *locktag,
 /*
  * Find or create LOCK and PROCLOCK objects as needed for a new lock
  * request.
+ *
+ * Returns the PROCLOCK object, or NULL if we failed to create the objects
+ * for lack of shared memory.
+ *
+ * The appropriate partition lock must be held at entry, and will be
+ * held at exit.
  */
 static PROCLOCK *
 SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc,
@@ -2414,6 +2420,8 @@ FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode)
  * FastPathTransferRelationLocks
  *		Transfer locks matching the given lock tag from per-backend fast-path
  *		arrays to the shared hash table.
+ *
+ * Returns true if successful, false if ran out of shared memory.
  */
 static bool
 FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag,
@@ -2529,6 +2537,7 @@ FastPathGetRelationLockEntry(LOCALLOCK *locallock)
 									locallock->hashcode, lockmode);
 		if (!proclock)
 		{
+			LWLockRelease(partitionLock);
 			ereport(ERROR,
 					(errcode(ERRCODE_OUT_OF_MEMORY),
 					 errmsg("out of shared memory"),
@@ -3422,9 +3431,6 @@ GetRunningTransactionLocks(int *nlocks)
 	for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
 		LWLockAcquire(FirstLockMgrLock + i, LW_SHARED);
 
-	/* Now scan the tables to copy the data */
-	hash_seq_init(&seqstat, LockMethodProcLockHash);
-
 	/* Now we can safely count the number of proclocks */
 	els = hash_get_num_entries(LockMethodProcLockHash);
 
@@ -3434,6 +3440,9 @@ GetRunningTransactionLocks(int *nlocks)
 	 */
 	accessExclusiveLocks = palloc(els * sizeof(xl_standby_lock));
 
+	/* Now scan the tables to copy the data */
+	hash_seq_init(&seqstat, LockMethodProcLockHash);
+
 	/*
 	 * If lock is a currently granted AccessExclusiveLock then it will have
 	 * just one proclock holder, so locks are never accessed twice in this
@@ -3978,22 +3987,34 @@ VirtualXactLock(VirtualTransactionId vxid, bool wait)
 
 	/*
 	 * OK, we're going to need to sleep on the VXID.  But first, we must set
-	 * up the primary lock table entry, if needed.
+	 * up the primary lock table entry, if needed (ie, convert the proc's
+	 * fast-path lock on its VXID to a regular lock).
 	 */
 	if (proc->fpVXIDLock)
 	{
 		PROCLOCK   *proclock;
 		uint32		hashcode;
+		LWLockId	partitionLock;
 
 		hashcode = LockTagHashCode(&tag);
+
+		partitionLock = LockHashPartitionLock(hashcode);
+		LWLockAcquire(partitionLock, LW_EXCLUSIVE);
+
 		proclock = SetupLockInTable(LockMethods[DEFAULT_LOCKMETHOD], proc,
 									&tag, hashcode, ExclusiveLock);
 		if (!proclock)
+		{
+			LWLockRelease(partitionLock);
 			ereport(ERROR,
 					(errcode(ERRCODE_OUT_OF_MEMORY),
 					 errmsg("out of shared memory"),
 					 errhint("You might need to increase max_locks_per_transaction.")));
+		}
 		GrantLock(proclock->tag.myLock, proclock, ExclusiveLock);
+
+		LWLockRelease(partitionLock);
+
 		proc->fpVXIDLock = false;
 	}
 
diff --git a/src/include/commands/alter.h b/src/include/commands/alter.h
index 37e095e..cf93790 100644
--- a/src/include/commands/alter.h
+++ b/src/include/commands/alter.h
@@ -19,6 +19,8 @@
 #include "utils/relcache.h"
 
 extern Oid ExecRenameStmt(RenameStmt *stmt);
+extern void	AlterObjectRename_internal(Relation rel, Oid objectId,
+									   const char *new_name);
 
 extern Oid ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt);
 extern Oid	AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid,
diff --git a/src/include/commands/conversioncmds.h b/src/include/commands/conversioncmds.h
index 3995628..75e42f5 100644
--- a/src/include/commands/conversioncmds.h
+++ b/src/include/commands/conversioncmds.h
@@ -18,6 +18,5 @@
 #include "nodes/parsenodes.h"
 
 extern Oid CreateConversionCommand(CreateConversionStmt *parsetree);
-extern Oid RenameConversion(List *name, const char *newname);
 
 #endif   /* CONVERSIONCMDS_H */
diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h
index c327136..7a80f41 100644
--- a/src/include/commands/defrem.h
+++ b/src/include/commands/defrem.h
@@ -102,8 +102,6 @@ extern text *serialize_deflist(List *deflist);
 extern List *deserialize_deflist(Datum txt);
 
 /* commands/foreigncmds.c */
-extern Oid RenameForeignServer(const char *oldname, const char *newname);
-extern Oid RenameForeignDataWrapper(const char *oldname, const char *newname);
 extern Oid AlterForeignServerOwner(const char *name, Oid newOwnerId);
 extern void AlterForeignServerOwner_oid(Oid, Oid newOwnerId);
 extern Oid AlterForeignDataWrapperOwner(const char *name, Oid newOwnerId);
diff --git a/src/include/commands/event_trigger.h b/src/include/commands/event_trigger.h
index 8b86b70..6d84b15 100644
--- a/src/include/commands/event_trigger.h
+++ b/src/include/commands/event_trigger.h
@@ -36,7 +36,6 @@ extern void RemoveEventTriggerById(Oid ctrigOid);
 extern Oid	get_event_trigger_oid(const char *trigname, bool missing_ok);
 
 extern Oid AlterEventTrigger(AlterEventTrigStmt *stmt);
-extern Oid RenameEventTrigger(const char* trigname, const char *newname);
 extern Oid AlterEventTriggerOwner(const char *name, Oid newOwnerId);
 extern void AlterEventTriggerOwner_oid(Oid, Oid newOwnerId);
 
diff --git a/src/include/commands/proclang.h b/src/include/commands/proclang.h
index 1f17ae4..f93b62c 100644
--- a/src/include/commands/proclang.h
+++ b/src/include/commands/proclang.h
@@ -16,7 +16,6 @@
 
 extern Oid CreateProceduralLanguage(CreatePLangStmt *stmt);
 extern void DropProceduralLanguageById(Oid langOid);
-extern Oid RenameLanguage(const char *oldname, const char *newname);
 extern bool PLTemplateExists(const char *languageName);
 extern Oid	get_language_oid(const char *langname, bool missing_ok);
 
diff --git a/src/test/regress/expected/alter_generic.out b/src/test/regress/expected/alter_generic.out
index 3beddd7..faed327 100644
--- a/src/test/regress/expected/alter_generic.out
+++ b/src/test/regress/expected/alter_generic.out
@@ -129,7 +129,7 @@ ERROR:  must be member of role "regtest_alter_user3"
 ALTER CONVERSION alt_conv3 SET SCHEMA alt_nsp2;  -- failed (not owner)
 ERROR:  must be owner of conversion alt_conv3
 ALTER CONVERSION alt_conv2 SET SCHEMA alt_nsp2;  -- failed (name conflict)
-ERROR:  conversion alt_conv2 already exists in schema "alt_nsp2"
+ERROR:  conversion "alt_conv2" already exists in schema "alt_nsp2"
 RESET SESSION AUTHORIZATION;
 SELECT n.nspname, c.conname, a.rolname
   FROM pg_conversion c, pg_namespace n, pg_authid a
@@ -346,7 +346,7 @@ ERROR:  must be member of role "regtest_alter_user3"
 ALTER TEXT SEARCH DICTIONARY alt_ts_dict3 SET SCHEMA alt_nsp2;  -- failed (not owner)
 ERROR:  must be owner of text search dictionary alt_ts_dict3
 ALTER TEXT SEARCH DICTIONARY alt_ts_dict2 SET SCHEMA alt_nsp2;  -- failed (name conflict)
-ERROR:  text search dictionary alt_ts_dict2 already exists in schema "alt_nsp2"
+ERROR:  text search dictionary "alt_ts_dict2" already exists
 RESET SESSION AUTHORIZATION;
 SELECT nspname, dictname, rolname
   FROM pg_ts_dict t, pg_namespace n, pg_authid a
@@ -368,7 +368,7 @@ SET SESSION AUTHORIZATION regtest_alter_user1;
 CREATE TEXT SEARCH CONFIGURATION alt_ts_conf1 (copy=english);
 CREATE TEXT SEARCH CONFIGURATION alt_ts_conf2 (copy=english);
 ALTER TEXT SEARCH CONFIGURATION alt_ts_conf1 RENAME TO alt_ts_conf2;  -- failed (name conflict)
-ERROR:  text search configuration "alt_ts_conf2" already exists
+ERROR:  text search parser "alt_ts_conf2" already exists
 ALTER TEXT SEARCH CONFIGURATION alt_ts_conf1 RENAME TO alt_ts_conf3;  -- OK
 ALTER TEXT SEARCH CONFIGURATION alt_ts_conf2 OWNER TO regtest_alter_user2;  -- failed (no role membership)
 ERROR:  must be member of role "regtest_alter_user2"
@@ -387,7 +387,7 @@ ERROR:  must be member of role "regtest_alter_user3"
 ALTER TEXT SEARCH CONFIGURATION alt_ts_conf3 SET SCHEMA alt_nsp2;  -- failed (not owner)
 ERROR:  must be owner of text search configuration alt_ts_conf3
 ALTER TEXT SEARCH CONFIGURATION alt_ts_conf2 SET SCHEMA alt_nsp2;  -- failed (name conflict)
-ERROR:  text search configuration alt_ts_conf2 already exists in schema "alt_nsp2"
+ERROR:  text search parser "alt_ts_conf2" already exists
 RESET SESSION AUTHORIZATION;
 SELECT nspname, cfgname, rolname
   FROM pg_ts_config t, pg_namespace n, pg_authid a
@@ -413,7 +413,7 @@ ALTER TEXT SEARCH TEMPLATE alt_ts_temp1 RENAME TO alt_ts_temp3; -- OK
 ALTER TEXT SEARCH TEMPLATE alt_ts_temp2 SET SCHEMA alt_nsp2;    -- OK
 CREATE TEXT SEARCH TEMPLATE alt_ts_temp2 (lexize=dsimple_lexize);
 ALTER TEXT SEARCH TEMPLATE alt_ts_temp2 SET SCHEMA alt_nsp2;    -- failed (name conflict)
-ERROR:  text search template alt_ts_temp2 already exists in schema "alt_nsp2"
+ERROR:  text search template "alt_ts_temp2" already exists
 SELECT nspname, tmplname
   FROM pg_ts_template t, pg_namespace n
   WHERE t.tmplnamespace = n.oid AND nspname like 'alt_nsp%'
@@ -439,7 +439,7 @@ ALTER TEXT SEARCH PARSER alt_ts_prs2 SET SCHEMA alt_nsp2;   -- OK
 CREATE TEXT SEARCH PARSER alt_ts_prs2
     (start = prsd_start, gettoken = prsd_nexttoken, end = prsd_end, lextypes = prsd_lextype);
 ALTER TEXT SEARCH PARSER alt_ts_prs2 SET SCHEMA alt_nsp2;   -- failed (name conflict)
-ERROR:  text search parser alt_ts_prs2 already exists in schema "alt_nsp2"
+ERROR:  text search parser "alt_ts_prs2" already exists
 SELECT nspname, prsname
   FROM pg_ts_parser t, pg_namespace n
   WHERE t.prsnamespace = n.oid AND nspname like 'alt_nsp%'
