=== modified file 'storage/innobase/handler/ha_innodb.cc'
--- storage/innobase/handler/ha_innodb.cc	2014-11-21 19:20:39 +0000
+++ storage/innobase/handler/ha_innodb.cc	2014-11-22 09:29:38 +0000
@@ -1981,15 +1981,6 @@
 	ut_a(block > 0);
 	ut_a(max_value > 0);
 
-        /*
-          Allow auto_increment to go over max_value up to max ulonglong.
-          This allows us to detect that all values are exhausted.
-          If we don't do this, we will return max_value several times
-          and get duplicate key errors instead of auto increment value
-          out of range.
-        */
-        max_value= (~(ulonglong) 0);
-
 	/* According to MySQL documentation, if the offset is greater than
 	the step then the offset is ignored. */
 	if (offset > block) {
@@ -13605,17 +13596,13 @@
 	/* Not in the middle of a mult-row INSERT. */
 	} else if (prebuilt->autoinc_last_value == 0) {
 		set_if_bigger(*first_value, autoinc);
-	}
+	/* Check for -ve values. */
+	} else if (*first_value > col_max_value && trx->n_autoinc_rows > 0) {
+		/* Set to next logical value. */
+		ut_a(autoinc > trx->n_autoinc_rows);
+		*first_value = (autoinc - trx->n_autoinc_rows) - 1;
+ 	}
 
-        if (*first_value > col_max_value)
-        {
-          	/* Out of range number. Let handler::update_auto_increment()
-                   take care of this */
-                prebuilt->autoinc_last_value = 0;
-                dict_table_autoinc_unlock(prebuilt->table);
-                *nb_reserved_values= 0;
-                return;
-        }
 	*nb_reserved_values = trx->n_autoinc_rows;
 
 	/* With old style AUTOINC locking we only update the table's
@@ -13624,7 +13611,7 @@
 		ulonglong	current;
 		ulonglong	next_value;
 
-		current = *first_value;
+		current = *first_value > col_max_value ? autoinc : *first_value;
 
 		/* If the increment step of the auto increment column
 		decreases then it is not affecting the immediate

=== modified file 'storage/xtradb/handler/ha_innodb.cc'
--- storage/xtradb/handler/ha_innodb.cc	2014-11-21 19:20:39 +0000
+++ storage/xtradb/handler/ha_innodb.cc	2014-11-22 09:29:40 +0000
@@ -2259,15 +2259,6 @@
 	ut_a(block > 0);
 	ut_a(max_value > 0);
 
-        /*
-          Allow auto_increment to go over max_value up to max ulonglong.
-          This allows us to detect that all values are exhausted.
-          If we don't do this, we will return max_value several times
-          and get duplicate key errors instead of auto increment value
-          out of range.
-        */
-        max_value= (~(ulonglong) 0);
-
 	/* According to MySQL documentation, if the offset is greater than
 	the step then the offset is ignored. */
 	if (offset > block) {
@@ -14303,17 +14294,13 @@
 	/* Not in the middle of a mult-row INSERT. */
 	} else if (prebuilt->autoinc_last_value == 0) {
 		set_if_bigger(*first_value, autoinc);
+	/* Check for -ve values. */
+	} else if (*first_value > col_max_value && trx->n_autoinc_rows > 0) {
+		/* Set to next logical value. */
+		ut_a(autoinc > trx->n_autoinc_rows);
+		*first_value = (autoinc - trx->n_autoinc_rows) - 1;
 	}
 
-        if (*first_value > col_max_value)
-        {
-          	/* Out of range number. Let handler::update_auto_increment()
-                   take care of this */
-                prebuilt->autoinc_last_value = 0;
-                dict_table_autoinc_unlock(prebuilt->table);
-                *nb_reserved_values = 0;
-                return;
-        }
 	*nb_reserved_values = trx->n_autoinc_rows;
 
 	/* With old style AUTOINC locking we only update the table's
@@ -14322,7 +14309,7 @@
 		ulonglong	current;
 		ulonglong	next_value;
 
-		current = *first_value;
+		current = *first_value > col_max_value ? autoinc : *first_value;
 
 		/* If the increment step of the auto increment column
 		decreases then it is not affecting the immediate

