Index: wp-includes/post.php
===================================================================
--- wp-includes/post.php	(revision 11201)
+++ wp-includes/post.php	(working copy)
@@ -1717,7 +1717,7 @@
 			$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s 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 ( $post_name_check || in_array($slug, $wp_rewrite->feeds) ) {
 			$suffix = 2;
 			do {
@@ -2415,20 +2415,8 @@
 	else
 		$post_name = sanitize_title($post_name);
 
-	// expected_slashed ($post_name)
-	$post_name_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_status = 'inherit' AND ID != %d LIMIT 1", $post_name, $post_ID));
+	$post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
 
-	if ($post_name_check) {
-		$suffix = 2;
-		while ($post_name_check) {
-			$alt_post_name = $post_name . "-$suffix";
-			// expected_slashed ($alt_post_name, $post_name)
-			$post_name_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_status = 'inherit' AND ID != %d LIMIT 1", $alt_post_name, $post_ID, $post_parent));
-			$suffix++;
-		}
-		$post_name = $alt_post_name;
-	}
-
 	if ( empty($post_date) )
 		$post_date = current_time('mysql');
 	if ( empty($post_date_gmt) )
Index: wp-includes/version.php
===================================================================
--- wp-includes/version.php	(revision 11201)
+++ wp-includes/version.php	(working copy)
@@ -15,6 +15,6 @@
  *
  * @global int $wp_db_version
  */
-$wp_db_version = 10850;
+$wp_db_version = 11200;
 
 ?>
Index: wp-admin/includes/upgrade.php
===================================================================
--- wp-admin/includes/upgrade.php	(revision 11201)
+++ wp-admin/includes/upgrade.php	(working copy)
@@ -340,7 +340,7 @@
 	if ( $wp_current_db_version < 8989 )
 		upgrade_270();
 
-	if ( $wp_current_db_version < 10360 )
+	if ( $wp_current_db_version < 11200 )
 		upgrade_280();
 
 	maybe_disable_automattic_widgets();
@@ -980,6 +980,9 @@
 
 	if ( $wp_current_db_version < 10360 )
 		populate_roles_280();
+	
+	if ( $wp_current_db_version < 11200 )
+		fix_attachment_slug_conflicts();
 }
 
 
Index: wp-admin/includes/schema.php
===================================================================
--- wp-admin/includes/schema.php	(revision 11201)
+++ wp-admin/includes/schema.php	(working copy)
@@ -576,4 +576,28 @@
 	}
 }
 
-?>
+/**
+ * Fix duplicate slugs in hierarchical data
+ * See http://core.trac.wordpress.org/ticket/9539
+ *
+ * @since 2.8.0
+ **/
+
+function fix_attachment_slug_conflicts() {
+	global $wpdb;
+	
+	$attachments = $wpdb->get_results("
+		SELECT attachment.*
+		FROM $wpdb->posts as attachment
+		JOIN $wpdb->posts as page
+		ON attachment.post_name = page.post_name
+		WHERE attachment.post_type = 'attachment'
+		AND page.post_type = 'page'
+		AND attachment.post_parent = page.post_parent
+		AND attachment.post_name <> ''
+		");
+	
+	while ( $attachment = array_pop($attachments) )
+		wp_update_post($attachment);
+} # fix_attachment_slug_conflicts()
+?>
\ No newline at end of file
