diff --git a/mysql-test/mysql-test-run.pl b/mysql-test/mysql-test-run.pl
index f3b733a..6c8cd74 100755
--- a/mysql-test/mysql-test-run.pl
+++ b/mysql-test/mysql-test-run.pl
@@ -4848,6 +4848,7 @@ sub extract_warning_lines ($$) {
      qr|InnoDB: Setting thread \d+ nice to \d+ failed, current nice \d+, errno 13|, # setpriority() fails under valgrind
      qr|Failed to setup SSL|,
      qr|SSL error: Failed to set ciphers to use|,
+     qr|DEBUG|,
      qr/Plugin 'InnoDB' will be forced to shutdown/,
     );
 
diff --git a/sql/opt_range.cc b/sql/opt_range.cc
index e0ca43e..c754a8b 100644
--- a/sql/opt_range.cc
+++ b/sql/opt_range.cc
@@ -121,6 +121,8 @@
 #include "sql_statistics.h"
 #include "filesort.h"         // filesort_free_buffers
 
+#include "debug_sync.h"
+
 #ifndef EXTRA_DEBUG
 #define test_rb_tree(A,B) {}
 #define test_use_count(A) {}
@@ -2987,6 +2989,31 @@ static int fill_used_fields_bitmap(PARAM *param)
     1 if found usable ranges and quick select has been successfully created.
 */
 
+#define DBUG_WRITE_MESSAGE(THD,TBL,MSG) \
+{ \
+    if ((THD)->lex->sql_command == SQLCOM_UPDATE || \
+        (THD)->lex->sql_command == SQLCOM_UPDATE) \
+    { \
+      longlong tmp_records= (TBL)->file->stats.records;     \
+      (TBL)->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);   \
+      sql_print_warning("DEBUG: Full table scan on table %s.  Reason: '%s'  Records: %lld  Records2: %lld  Records3: %lld Optimizer_flag: %llu  Query: %1000s", \
+                    (TBL)->s->table_name.str, \
+		    MSG, \
+                    (longlong) (TBL)->stat_records(), \
+                    (longlong) tmp_records, \
+                    (longlong) (TBL)->file->stats.records,     \
+                    ((THD)->variables.optimizer_switch), \
+                    (THD)->query());                 \
+    } \
+}
+
+#define DBUG_RETURN_MESSAGE(RET,THD,TBL,MSG)   \
+{ \
+  DBUG_WRITE_MESSAGE(THD,TBL,MSG); \
+  DBUG_RETURN(RET); \
+ } \
+
+
 int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
 				  table_map prev_tables,
 				  ha_rows limit, bool force_quick_range, 
@@ -3004,8 +3031,10 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
   needed_reg.clear_all();
   quick_keys.clear_all();
   DBUG_ASSERT(!head->is_filled_at_execution());
-  if (keys_to_use.is_clear_all() || head->is_filled_at_execution())
-    DBUG_RETURN(0);
+  if (keys_to_use.is_clear_all())
+    DBUG_RETURN_MESSAGE(0, thd, head, ("keys_to_use.is_clear_all() is true"));
+  if (head->is_filled_at_execution())
+    DBUG_RETURN_MESSAGE(0, thd, head, ("head->is_filled_at_execution is true"));
   records= head->stat_records();
   if (!records)
     records++;					/* purecov: inspected */
@@ -3031,7 +3060,7 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
     PARAM param;
 
     if (check_stack_overrun(thd, 2*STACK_MIN_SIZE + sizeof(PARAM), buff))
-      DBUG_RETURN(0);                           // Fatal error flag is set
+      DBUG_RETURN_MESSAGE(0, thd, head, ("check_stack_overrun is true"));
 
     /* set up parameter that is passed to all functions */
     param.thd= thd;
@@ -3061,7 +3090,7 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
     {
       thd->no_errors=0;
       free_root(&alloc,MYF(0));			// Return memory & allocator
-      DBUG_RETURN(0);				// Can't use range
+      DBUG_RETURN_MESSAGE(0, thd, head, ("alloc of key_parts failed"));
     }
     key_parts= param.key_parts;
 
@@ -3112,7 +3141,7 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
     {
       thd->no_errors=0;
       free_root(&alloc,MYF(0));			// Return memory & allocator
-      DBUG_RETURN(0);				// Can't use range
+      DBUG_RETURN_MESSAGE(0, thd, head, ("alloc of min and max keys failed"));
     }
 
     thd->mem_root= &alloc;
@@ -3140,6 +3169,7 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
         {
           records=0L;                      /* Return -1 from this function. */
           read_time= (double) HA_POS_ERROR;
+          DBUG_WRITE_MESSAGE(thd, head, ("tree->type == IMPOSSIBLE"));
           goto free_mem;
         }
         /*
@@ -3149,6 +3179,14 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
         if (tree->type != SEL_TREE::KEY && tree->type != SEL_TREE::KEY_SMALLER)
           tree= NULL;
       }
+      else
+      {
+        DBUG_WRITE_MESSAGE(thd, head, ("get_mm_tree returned 0"));
+      }
+    }
+    else
+    {
+      DBUG_WRITE_MESSAGE(thd, head, ("cond == 0"));
     }
 
     /*
@@ -3269,10 +3307,17 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
     if (best_trp)
     {
       records= best_trp->records;
-      if (!(quick= best_trp->make_quick(&param, TRUE)) || quick->init())
+      if (!(quick= best_trp->make_quick(&param, TRUE)))
+      {
+        delete quick;
+        quick= NULL;
+        DBUG_WRITE_MESSAGE(thd, head, ("make_quick() failed"));
+      }
+      else if (quick->init())
       {
         delete quick;
         quick= NULL;
+        DBUG_WRITE_MESSAGE(thd, head, ("quick()->init failed"));
       }
     }
     possible_keys= param.possible_keys;
@@ -3285,11 +3330,17 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
 
   DBUG_EXECUTE("info", print_quick(quick, &needed_reg););
 
+  DEBUG_SYNC(thd,"opt_range_wait_for_innodb_stats");
+
   /*
     Assume that if the user is using 'limit' we will only need to scan
     limit rows if we are using a key
   */
-  DBUG_RETURN(records ? MY_TEST(quick) : -1);
+  if (!records)
+    DBUG_RETURN_MESSAGE(-1, thd, head, ("records == 0"));
+  if (!quick)
+    DBUG_RETURN_MESSAGE(0, thd, head, ("quick == 0"));
+  DBUG_RETURN(1);
 }
 
 /****************************************************************************
diff --git a/storage/xtradb/dict/dict0stats.cc b/storage/xtradb/dict/dict0stats.cc
index b073398..14b2f6e 100644
--- a/storage/xtradb/dict/dict0stats.cc
+++ b/storage/xtradb/dict/dict0stats.cc
@@ -49,6 +49,7 @@ Created Jan 06, 2010 Vasil Dimov
 #include <algorithm>
 #include <map>
 #include <vector>
+#include <execinfo.h>
 
 /* Sampling algorithm description @{
 
@@ -3246,6 +3247,17 @@ dict_stats_update(
 			have less indexes than the real object 'table'. */
 			dict_stats_empty_table(table);
 
+                        DBUG_EXECUTE_IF("innodb_wait_with_empty_stats", 
+                        {
+                          fprintf(stderr, "AAA empty  stats at:\n");
+                          void *mybuf123[100];
+                          int res123= backtrace(mybuf123,100);
+                          backtrace_symbols_fd(mybuf123, res123, fileno(stderr));
+                          //my_sleep(1200ull*1000ULL*1000);
+                          my_sleep(30ull*1000ULL*1000);
+                        }
+                        );
+
 			dict_stats_copy(table, t);
 
 			dict_stats_assert_initialized(table);
