============================================================
*** src/backend/commands/indexcmds.c	61a8b3774b682554e8670624583ab4cf4b9dbdb9
--- src/backend/commands/indexcmds.c	dc6fc2a3fbce90748bcf4cd7a60ea2ea887bc97f
*************** static Oid GetIndexOpClass(List *opclass
*** 64,70 ****
  				  bool isconstraint);
  static Oid GetIndexOpClass(List *opclass, Oid attrType,
  				char *accessMethodName, Oid accessMethodId);
- static bool relationHasPrimaryKey(Relation rel);
  
  
  /*
--- 64,69 ----
*************** DefineIndex(RangeVar *heapRelation,
*** 324,330 ****
  		 * it's no problem either.
  		 */
  		if (is_alter_table &&
! 			relationHasPrimaryKey(rel))
  		{
  			ereport(ERROR,
  					(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
--- 323,329 ----
  		 * it's no problem either.
  		 */
  		if (is_alter_table &&
! 			(GetPrimaryKeyIndexOid(rel) != InvalidOid))
  		{
  			ereport(ERROR,
  					(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
*************** ChooseRelationName(const char *name1, co
*** 1216,1229 ****
  }
  
  /*
!  * relationHasPrimaryKey -
   *
!  *	See whether an existing relation has a primary key.
   */
! static bool
! relationHasPrimaryKey(Relation rel)
  {
! 	bool		result = false;
  	List	   *indexoidlist;
  	ListCell   *indexoidscan;
  
--- 1215,1229 ----
  }
  
  /*
!  * GetPrimaryKeyIndexOid
   *
!  * Returns the oid of the primary key index of the relation, if any,
!  * otherwise InvalidOid is returned.
   */
! Oid
! GetPrimaryKeyIndexOid(Relation rel)
  {
! 	Oid			result = InvalidOid;
  	List	   *indexoidlist;
  	ListCell   *indexoidscan;
  
*************** relationHasPrimaryKey(Relation rel)
*** 1244,1257 ****
  									0, 0, 0);
  		if (!HeapTupleIsValid(indexTuple))		/* should not happen */
  			elog(ERROR, "cache lookup failed for index %u", indexoid);
! 		result = ((Form_pg_index) GETSTRUCT(indexTuple))->indisprimary;
  		ReleaseSysCache(indexTuple);
! 		if (result)
  			break;
  	}
  
  	list_free(indexoidlist);
- 
  	return result;
  }
  
--- 1244,1260 ----
  									0, 0, 0);
  		if (!HeapTupleIsValid(indexTuple))		/* should not happen */
  			elog(ERROR, "cache lookup failed for index %u", indexoid);
! 
! 		if (((Form_pg_index) GETSTRUCT(indexTuple))->indisprimary)
! 			result = indexoid;
! 
  		ReleaseSysCache(indexTuple);
! 
! 		if (result != InvalidOid)
  			break;
  	}
  
  	list_free(indexoidlist);
  	return result;
  }
  
============================================================
*** src/include/commands/defrem.h	e2384af33d917bff68234bbe407ea16e3ec43123
--- src/include/commands/defrem.h	58bb763402c9bef8ead035a3524505ad8fe58de5
***************
*** 15,22 ****
  #define DEFREM_H
  
  #include "nodes/parsenodes.h"
  
- 
  /* commands/indexcmds.c */
  extern void DefineIndex(RangeVar *heapRelation,
  			char *indexRelationName,
--- 15,22 ----
  #define DEFREM_H
  
  #include "nodes/parsenodes.h"
+ #include "utils/relcache.h"
  
  /* commands/indexcmds.c */
  extern void DefineIndex(RangeVar *heapRelation,
  			char *indexRelationName,
*************** extern Oid	GetDefaultOpClass(Oid type_id
*** 43,48 ****
--- 43,49 ----
  extern char *ChooseRelationName(const char *name1, const char *name2,
  				   const char *label, Oid namespace);
  extern Oid	GetDefaultOpClass(Oid type_id, Oid am_id);
+ extern Oid	GetPrimaryKeyIndexOid(Relation rel);
  
  /* commands/functioncmds.c */
  extern void CreateFunction(CreateFunctionStmt *stmt);
