diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 943662f8f8b2665a454bcd30d8fe3923657c50ef..d79e73f59d8c29c4afb5f86517d34041bb3e219f 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.161 2007/07/17 05:02:00 neilc Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.162 2007/08/25 19:08:19 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -428,8 +428,6 @@ DefineIndex(RangeVar *heapRelation,
 					  relationId, accessMethodName, accessMethodId,
 					  amcanorder, isconstraint);
 
-	heap_close(rel, NoLock);
-
 	/*
 	 * Report index creation if appropriate (delay this till after most of the
 	 * error checks)
@@ -441,6 +439,10 @@ DefineIndex(RangeVar *heapRelation,
 				  primary ? "PRIMARY KEY" : "UNIQUE",
 				  indexRelationName, RelationGetRelationName(rel))));
 
+	/* save lockrelid for below, then close rel */
+	heaprelid = rel->rd_lockInfo.lockRelId;
+	heap_close(rel, NoLock);
+
 	indexRelationId =
 		index_create(relationId, indexRelationName, indexRelationId,
 					 indexInfo, accessMethodId, tablespaceId, classObjectId,
@@ -468,7 +470,6 @@ DefineIndex(RangeVar *heapRelation,
 	 * because there are no operations that could change its state while we
 	 * hold lock on the parent table.  This might need to change later.
 	 */
-	heaprelid = rel->rd_lockInfo.lockRelId;
 	LockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock);
 
 	CommitTransactionCommand();