Index: src/backend/parser/analyze.c
===================================================================
RCS file: /home/hlinnaka/pgcvsrepository/pgsql/src/backend/parser/analyze.c,v
retrieving revision 1.353
diff -c -r1.353 analyze.c
*** src/backend/parser/analyze.c	11 Oct 2006 16:42:59 -0000	1.353
--- src/backend/parser/analyze.c	22 Jun 2007 10:16:20 -0000
***************
*** 69,74 ****
--- 69,75 ----
  {
  	const char *stmtType;		/* "CREATE TABLE" or "ALTER TABLE" */
  	RangeVar   *relation;		/* relation to create */
+ 	Oid			namespace;		/* namespace of relation to alter */
  	List	   *inhRelations;	/* relations to inherit from */
  	bool		hasoids;		/* does relation have an OID column? */
  	bool		isalter;		/* true if altering existing table */
***************
*** 945,950 ****
--- 946,952 ----
  
  	cxt.stmtType = "CREATE TABLE";
  	cxt.relation = stmt->relation;
+ 	cxt.namespace = InvalidOid;
  	cxt.inhRelations = stmt->inhRelations;
  	cxt.isalter = false;
  	cxt.columns = NIL;
***************
*** 1087,1093 ****
  		 * quite unlikely to be a problem, especially since few people would
  		 * need two serial columns in one table.
  		 */
! 		snamespaceid = RangeVarGetCreationNamespace(cxt->relation);
  		snamespace = get_namespace_name(snamespaceid);
  		sname = ChooseRelationName(cxt->relation->relname,
  								   column->colname,
--- 1089,1098 ----
  		 * quite unlikely to be a problem, especially since few people would
  		 * need two serial columns in one table.
  		 */
! 		if (OidIsValid(cxt->namespace))
! 		   snamespaceid = cxt->namespace;
! 		else
! 		   snamespaceid = RangeVarGetCreationNamespace(cxt->relation);
  		snamespace = get_namespace_name(snamespaceid);
  		sname = ChooseRelationName(cxt->relation->relname,
  								   column->colname,
***************
*** 3010,3015 ****
--- 3015,3021 ----
  	List	   *newcmds = NIL;
  	bool		skipValidation = true;
  	AlterTableCmd *newcmd;
+ 	Relation	relation;
  
  	cxt.stmtType = "ALTER TABLE";
  	cxt.relation = stmt->relation;
***************
*** 3024,3029 ****
--- 3030,3045 ----
  	cxt.alist = NIL;
  	cxt.pkey = NULL;
  
+ 	relation = heap_openrv(stmt->relation, AccessShareLock);
+ 	if (relation->rd_rel->relkind != RELKIND_RELATION)
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ 				 errmsg("relation \"%s\" is not a table",
+ 						stmt->relation->relname)));
+ 
+ 	cxt.namespace = RelationGetNamespace(relation);
+ 	
+ 
  	/*
  	 * The only subtypes that currently require parse transformation handling
  	 * are ADD COLUMN and ADD CONSTRAINT.  These largely re-use code from
***************
*** 3166,3171 ****
--- 3182,3194 ----
  	*extras_before = list_concat(*extras_before, cxt.blist);
  	*extras_after = list_concat(cxt.alist, *extras_after);
  
+ 	/*
+ 	 * Close the parent rel, but keep our AccessShareLock on it until xact
+ 	 * commit.	That will prevent someone else from deleting or ALTERing the
+ 	 * table before we get to execute the changes.
+ 	 */
+ 	heap_close(relation, NoLock);
+ 
  	return qry;
  }
  
