Index: src/backend/commands/dbcommands.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v
retrieving revision 1.235
diff -c -c -r1.235 dbcommands.c
*** src/backend/commands/dbcommands.c	26 Feb 2010 02:00:38 -0000	1.235
--- src/backend/commands/dbcommands.c	20 Jul 2010 18:11:06 -0000
***************
*** 1908,1913 ****
--- 1908,1914 ----
  		if (stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode))
  		{
  			if (!rmtree(dst_path, true))
+ 				/* If this failed, copydir() below is going to error. */
  				ereport(WARNING,
  						(errmsg("some useless files may be left behind in old database directory \"%s\"",
  								dst_path)));
Index: src/backend/commands/tablespace.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/commands/tablespace.c,v
retrieving revision 1.77
diff -c -c -r1.77 tablespace.c
*** src/backend/commands/tablespace.c	18 Jul 2010 04:47:46 -0000	1.77
--- src/backend/commands/tablespace.c	20 Jul 2010 18:11:07 -0000
***************
*** 562,567 ****
--- 562,586 ----
  						 location)));
  	}
  
+ 	if (InRecovery)
+ 	{
+ 		struct stat st;
+ 
+ 		/*
+ 		 * Our theory for replaying a CREATE is to forcibly drop the target
+ 		 * subdirectory if present, and then recreate it. This may be
+ 		 * more work than needed, but it is simple to implement.
+ 		 */
+ 		if (stat(location_with_version_dir, &st) == 0 && S_ISDIR(st.st_mode))
+ 		{
+ 			if (!rmtree(location_with_version_dir, true))
+ 				/* If this failed, mkdir() below is going to error. */
+ 				ereport(WARNING,
+ 						(errmsg("some useless files may be left behind in old database directory \"%s\"",
+ 								location_with_version_dir)));
+ 		}
+ 	}
+ 
  	/*
  	 * The creation of the version directory prevents more than one tablespace
  	 * in a single location.
***************
*** 580,585 ****
--- 599,614 ----
  							location_with_version_dir)));
  	}
  
+ 	/* Remove old symlink in recovery, in case it points to the wrong place */
+ 	if (InRecovery)
+ 	{
+ 		if (unlink(linkloc) < 0 && errno != ENOENT)
+ 			ereport(ERROR,
+ 					(errcode_for_file_access(),
+ 					 errmsg("could not remove symbolic link \"%s\": %m",
+ 							linkloc)));
+ 	}
+ 	
  	/*
  	 * Create the symlink under PGDATA
  	 */
