*** a/src/backend/commands/alter.c
--- b/src/backend/commands/alter.c
***************
*** 261,280 **** AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid)
  		case OCLASS_CLASS:
  			{
  				Relation	rel;
- 				Relation	classRel;
  
  				rel = relation_open(objid, AccessExclusiveLock);
  				oldNspOid = RelationGetNamespace(rel);
  
! 				classRel = heap_open(RelationRelationId, RowExclusiveLock);
! 
! 				AlterRelationNamespaceInternal(classRel,
! 											   objid,
! 											   oldNspOid,
! 											   nspOid,
! 											   true);
! 
! 				heap_close(classRel, RowExclusiveLock);
  
  				relation_close(rel, NoLock);
  				break;
--- 261,271 ----
  		case OCLASS_CLASS:
  			{
  				Relation	rel;
  
  				rel = relation_open(objid, AccessExclusiveLock);
  				oldNspOid = RelationGetNamespace(rel);
  
! 				AlterTableNamespaceInternal(rel, oldNspOid, nspOid);
  
  				relation_close(rel, NoLock);
  				break;
*** a/src/backend/commands/tablecmds.c
--- b/src/backend/commands/tablecmds.c
***************
*** 263,270 **** static int	findAttrByName(const char *attributeName, List *schema);
  static void AlterIndexNamespaces(Relation classRel, Relation rel,
  					 Oid oldNspOid, Oid newNspOid);
  static void AlterSeqNamespaces(Relation classRel, Relation rel,
! 				   Oid oldNspOid, Oid newNspOid,
! 				   const char *newNspName, LOCKMODE lockmode);
  static void ATExecValidateConstraint(Relation rel, char *constrName,
  						 bool recurse, bool recursing, LOCKMODE lockmode);
  static int transformColumnNameList(Oid relId, List *colList,
--- 263,269 ----
  static void AlterIndexNamespaces(Relation classRel, Relation rel,
  					 Oid oldNspOid, Oid newNspOid);
  static void AlterSeqNamespaces(Relation classRel, Relation rel,
! 				   Oid oldNspOid, Oid newNspOid, LOCKMODE lockmode);
  static void ATExecValidateConstraint(Relation rel, char *constrName,
  						 bool recurse, bool recursing, LOCKMODE lockmode);
  static int transformColumnNameList(Oid relId, List *colList,
***************
*** 9710,9716 **** AlterTableNamespace(AlterObjectSchemaStmt *stmt)
  	Oid			relid;
  	Oid			oldNspOid;
  	Oid			nspOid;
- 	Relation	classRel;
  	RangeVar   *newrv;
  
  	relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
--- 9709,9714 ----
***************
*** 9752,9761 **** AlterTableNamespace(AlterObjectSchemaStmt *stmt)
  	/* common checks on switching namespaces */
  	CheckSetNamespace(oldNspOid, nspOid, RelationRelationId, relid);
  
  	/* OK, modify the pg_class row and pg_depend entry */
  	classRel = heap_open(RelationRelationId, RowExclusiveLock);
  
! 	AlterRelationNamespaceInternal(classRel, relid, oldNspOid, nspOid, true);
  
  	/* Fix the table's row type too */
  	AlterTypeNamespaceInternal(rel->rd_rel->reltype, nspOid, false, false);
--- 9750,9771 ----
  	/* common checks on switching namespaces */
  	CheckSetNamespace(oldNspOid, nspOid, RelationRelationId, relid);
  
+ 	AlterTableNamespaceInternal(rel, oldNspOid, nspOid);
+ 
+ 	/* close rel, but keep lock until commit */
+ 	relation_close(rel, NoLock);
+ }
+ 
+ void
+ AlterTableNamespaceInternal(Relation rel, Oid oldNspOid, Oid nspOid)
+ {
+ 	Relation	classRel;
+ 
  	/* OK, modify the pg_class row and pg_depend entry */
  	classRel = heap_open(RelationRelationId, RowExclusiveLock);
  
! 	AlterRelationNamespaceInternal(classRel, RelationGetRelid(rel), oldNspOid,
! 								   nspOid, true);
  
  	/* Fix the table's row type too */
  	AlterTypeNamespaceInternal(rel->rd_rel->reltype, nspOid, false, false);
***************
*** 9764,9778 **** AlterTableNamespace(AlterObjectSchemaStmt *stmt)
  	if (rel->rd_rel->relkind == RELKIND_RELATION)
  	{
  		AlterIndexNamespaces(classRel, rel, oldNspOid, nspOid);
! 		AlterSeqNamespaces(classRel, rel, oldNspOid, nspOid, stmt->newschema,
  						   AccessExclusiveLock);
! 		AlterConstraintNamespaces(relid, oldNspOid, nspOid, false);
  	}
  
  	heap_close(classRel, RowExclusiveLock);
- 
- 	/* close rel, but keep lock until commit */
- 	relation_close(rel, NoLock);
  }
  
  /*
--- 9774,9786 ----
  	if (rel->rd_rel->relkind == RELKIND_RELATION)
  	{
  		AlterIndexNamespaces(classRel, rel, oldNspOid, nspOid);
! 		AlterSeqNamespaces(classRel, rel, oldNspOid, nspOid,
  						   AccessExclusiveLock);
! 		AlterConstraintNamespaces(RelationGetRelid(rel), oldNspOid, nspOid,
! 								  false);
  	}
  
  	heap_close(classRel, RowExclusiveLock);
  }
  
  /*
***************
*** 9861,9867 **** AlterIndexNamespaces(Relation classRel, Relation rel,
   */
  static void
  AlterSeqNamespaces(Relation classRel, Relation rel,
! 	 Oid oldNspOid, Oid newNspOid, const char *newNspName, LOCKMODE lockmode)
  {
  	Relation	depRel;
  	SysScanDesc scan;
--- 9869,9875 ----
   */
  static void
  AlterSeqNamespaces(Relation classRel, Relation rel,
! 				   Oid oldNspOid, Oid newNspOid, LOCKMODE lockmode)
  {
  	Relation	depRel;
  	SysScanDesc scan;
*** a/src/include/commands/tablecmds.h
--- b/src/include/commands/tablecmds.h
***************
*** 36,41 **** extern void AlterTableInternal(Oid relid, List *cmds, bool recurse);
--- 36,44 ----
  
  extern void AlterTableNamespace(AlterObjectSchemaStmt *stmt);
  
+ extern void AlterTableNamespaceInternal(Relation rel, Oid oldNspOid,
+ 							Oid nspOid);
+ 
  extern void AlterRelationNamespaceInternal(Relation classRel, Oid relOid,
  							   Oid oldNspOid, Oid newNspOid,
  							   bool hasDependEntry);
