Index: src/wp-includes/post.php
===================================================================
--- src/wp-includes/post.php	(revision 31269)
+++ src/wp-includes/post.php	(working copy)
@@ -3678,16 +3678,18 @@
  * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
  */
 function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {
-	if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) )
+	if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) ) {
 		return $slug;
+	}
 
 	global $wpdb, $wp_rewrite;
 
 	$original_slug = $slug;
 
 	$feeds = $wp_rewrite->feeds;
-	if ( ! is_array( $feeds ) )
+	if ( ! is_array( $feeds ) ) {
 		$feeds = array();
+	}
 
 	if ( 'attachment' == $post_type ) {
 		// Attachment slugs must be unique across all types.
@@ -3712,15 +3714,17 @@
 			$slug = $alt_post_name;
 		}
 	} elseif ( is_post_type_hierarchical( $post_type ) ) {
-		if ( 'nav_menu_item' == $post_type )
+		if ( 'nav_menu_item' == $post_type ) {
 			return $slug;
+		}
 
-		/*
-		 * Page slugs must be unique within their own trees. Pages are in a separate
-		 * namespace than posts so page slugs are allowed to overlap post slugs.
-		 */
-		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
-		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
+		if( '/%postname%/' == $wp_rewrite->permalink_structure && $post_parent === 0 ) {
+			$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d AND post_parent = %d LIMIT 1";
+			$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );
+		} else {
+			$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
+			$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
+		}
 
 		/**
 		 * Filter whether the post slug would make a bad hierarchical post slug.
@@ -3746,6 +3750,10 @@
 		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
 		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
 
+		if ( ! $post_name_check && '/%postname%/' == $wp_rewrite->permalink_structure ) {
+			$post_name_check = get_page_by_path( $slug );
+		}
+
 		/**
 		 * Filter whether the post slug would be bad as a flat slug.
 		 *
@@ -3760,6 +3768,11 @@
 			do {
 				$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
 				$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
+				
+				if ( ! $post_name_check && '/%postname%/' == $wp_rewrite->permalink_structure ) { 
+					$post_name_check = get_page_by_path( $alt_post_name );
+				}
+
 				$suffix++;
 			} while ( $post_name_check );
 			$slug = $alt_post_name;
