diff --git a/.bzrignore b/.bzrignore
index 278c3de2ee540be15e84f06f5de3958c4b697d67..978926c8c370d141c1774db1fb828f65402b3eaf 100644
--- a/.bzrignore
+++ b/.bzrignore
@@ -557,3 +557,8 @@ vio/test-ssl
 vio/test-sslclient
 vio/test-sslserver
 vio/viotest-ssl
+bkpull.log
+bkpull.log.2
+bkpull.log.3
+build.log
+sql/safe_to_cache_query.txt
diff --git a/sql/item_create.cc b/sql/item_create.cc
index e4c9a160686e0b1096c1d1dff700fc886f85f522..ad9058c1691e210ef704279628263f53f59d877f 100644
--- a/sql/item_create.cc
+++ b/sql/item_create.cc
@@ -76,7 +76,7 @@ Item *create_func_ceiling(Item* a)
 Item *create_func_connection_id(void)
 {
   THD *thd=current_thd;
-  thd->safe_to_cache_query=0;
+  thd->lex.safe_to_cache_query=0;
   return new Item_int("CONNECTION_ID()",(longlong) thd->thread_id,10);
 }
 
@@ -149,7 +149,7 @@ Item *create_func_floor(Item* a)
 Item *create_func_found_rows(void)
 {
   THD *thd=current_thd;
-  thd->safe_to_cache_query=0;
+  thd->lex.safe_to_cache_query=0;
   return new Item_int("FOUND_ROWS()",(longlong) thd->found_rows(),21);
 }
 
@@ -160,7 +160,7 @@ Item *create_func_from_days(Item* a)
 
 Item *create_func_get_lock(Item* a, Item *b)
 {
-  current_thd->safe_to_cache_query=0;
+  current_thd->lex.safe_to_cache_query=0;
   return new Item_func_get_lock(a, b);
 }
 
@@ -308,7 +308,7 @@ Item *create_func_radians(Item *a)
 
 Item *create_func_release_lock(Item* a)
 {
-  current_thd->safe_to_cache_query=0;
+  current_thd->lex.safe_to_cache_query=0;
   return new Item_func_release_lock(a);
 }
 
@@ -416,13 +416,13 @@ Item *create_func_year(Item* a)
 
 Item *create_load_file(Item* a)
 {
-  current_thd->safe_to_cache_query=0;
+  current_thd->lex.safe_to_cache_query=0;
   return new Item_load_file(a);
 }
 
 Item *create_wait_for_master_pos(Item* a, Item* b)
 {
-  current_thd->safe_to_cache_query=0;
+  current_thd->lex.safe_to_cache_query=0;
   return new Item_master_pos_wait(a, b);
 }
 
@@ -443,7 +443,7 @@ Item *create_func_cast(Item *a, Item_cast cast_type)
 
 Item *create_func_is_free_lock(Item* a)
 {
-  current_thd->safe_to_cache_query=0;
+  current_thd->lex.safe_to_cache_query=0;
   return new Item_func_is_free_lock(a);
 }
 
diff --git a/sql/item_func.cc b/sql/item_func.cc
index 7ae6e9830aa1a724023a07b4ee260186346faa9e..2af26e2fd9a648fa81a4e0411034447cc6ab6c7b 100644
--- a/sql/item_func.cc
+++ b/sql/item_func.cc
@@ -2471,7 +2471,7 @@ Item *get_system_var(enum_var_type var_type, LEX_STRING name)
   }
   if (!(item=var->item(thd, var_type)))
     return 0;					// Impossible
-  thd->safe_to_cache_query=0;
+  thd->lex.safe_to_cache_query=0;
   buff[0]='@';
   buff[1]='@';
   pos=buff+2;
@@ -2497,7 +2497,7 @@ Item *get_system_var(enum_var_type var_type, const char *var_name, uint length,
   DBUG_ASSERT(var != 0);
   if (!(item=var->item(thd, var_type)))
     return 0;					// Impossible
-  thd->safe_to_cache_query=0;
+  thd->lex.safe_to_cache_query=0;
   item->set_name(item_name);		// Will use original name
   return item;
 }
diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc
index ef584f4364efd834fc0c05e40155bcdb11b66669..aa0f5824b4e9e393b6995cfa2a9d2de7ba6c5b1e 100644
--- a/sql/sql_cache.cc
+++ b/sql/sql_cache.cc
@@ -289,7 +289,7 @@ TODO list:
 
       if (thd->temp_tables || global_merge_table_count)
 
-    - Another option would be to set thd->safe_to_cache_query to 0
+    - Another option would be to set thd->lex.safe_to_cache_query to 0
       in 'get_lock_data' if any of the tables was a tmp table or a
       MRG_ISAM table.
       (This could be done with almost no speed penalty)
@@ -900,7 +900,7 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length)
   /* Check that we haven't forgot to reset the query cache variables */
   DBUG_ASSERT(thd->net.query_cache_query == 0);
 
-  if (!thd->safe_to_cache_query)
+  if (!thd->lex.safe_to_cache_query)
   {
     DBUG_PRINT("qcache", ("SELECT is non-cacheable"));
     goto err;
@@ -994,7 +994,7 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length)
 		  table_list.db, table_list.alias));
       refused++;				// This is actually a hit
       STRUCT_UNLOCK(&structure_guard_mutex);
-      thd->safe_to_cache_query=0;		// Don't try to cache this
+      thd->lex.safe_to_cache_query=0;		// Don't try to cache this
       BLOCK_UNLOCK_RD(query_block);
       DBUG_RETURN(-1);				// Privilege error
     }
@@ -1003,7 +1003,7 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length)
       DBUG_PRINT("qcache", ("Need to check column privileges for %s.%s",
 			    table_list.db, table_list.alias));
       BLOCK_UNLOCK_RD(query_block);
-      thd->safe_to_cache_query=0;		// Don't try to cache this
+      thd->lex.safe_to_cache_query=0;		// Don't try to cache this
       goto err_unlock;				// Parse query
     }
   }
@@ -2457,7 +2457,7 @@ TABLE_COUNTER_TYPE Query_cache::is_cacheable(THD *thd, uint32 query_len,
       (thd->variables.query_cache_type == 1 ||
        (thd->variables.query_cache_type == 2 && (lex->select_lex.options &
 						 OPTION_TO_QUERY_CACHE))) &&
-      thd->safe_to_cache_query)
+      lex->safe_to_cache_query)
   {
     my_bool has_transactions = 0;
     DBUG_PRINT("qcache", ("options %lx %lx, type %u",
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index 4141211ad927429a3e72fa92c56570b1d46bb4e4..9bca7245cba7c5a54f6c528acfd1275dbfa7c837 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -87,7 +87,7 @@ THD::THD():user_time(0), fatal_error(0),
   host=user=priv_user=db=query=ip=0;
   host_or_ip="unknown ip";
   locked=killed=count_cuted_fields=some_tables_deleted=no_errors=password=
-    query_start_used=safe_to_cache_query=prepare_command=0;
+    query_start_used=prepare_command=0;
   db_length=query_length=col_access=0;
   query_error=0;
   next_insert_id=last_insert_id=0;
diff --git a/sql/sql_class.h b/sql/sql_class.h
index 599a0572a8ad16d3d20a301fa9fc601d8a926ec4..a521da9a33fddac6fa4575a419b8ac5c4e3368f3 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -507,7 +507,6 @@ public:
   bool	     query_start_used,last_insert_id_used,insert_id_used,rand_used;
   bool	     system_thread,in_lock_tables,global_read_lock;
   bool       query_error, bootstrap, cleanup_done;
-  bool	     safe_to_cache_query;
   bool	     volatile killed;
   bool       prepare_command;
 
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index 9ed66aede6f18873f55cce1ca13a69c3fd6e9feb..e3ffe2a81208ebadde8f291e2f695f405cd3b994 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -154,6 +154,7 @@ LEX *lex_start(THD *thd, uchar *buf,uint length)
   lex->ignore_space=test(thd->sql_mode & MODE_IGNORE_SPACE);
   lex->slave_thd_opt=0;
   lex->sql_command=SQLCOM_END;
+  lex->safe_to_cache_query= 1;
   bzero(&lex->mi,sizeof(lex->mi));
   return lex;
 }
@@ -182,7 +183,7 @@ static int find_keyword(LEX *lex, uint len, bool function)
   udf_func *udf;
   if (function && using_udf_functions && (udf=find_udf((char*) tok, len)))
   {
-    lex->thd->safe_to_cache_query=0;
+    lex->safe_to_cache_query=0;
     lex->yylval->udf=udf;
     switch (udf->returns) {
     case STRING_RESULT:
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index 53bd0c0489a98deea35e4f0b85ab9f45487351ff..ce3acb4f4506b158d9cf5c1d8ae65773a98efab4 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -441,6 +441,7 @@ typedef struct st_lex
   bool drop_primary, drop_if_exists, drop_temporary, local_file;
   bool in_comment, ignore_space, verbose, simple_alter;
   bool derived_tables, describe, olap;
+  bool safe_to_cache_query;
   uint slave_thd_opt;
   CHARSET_INFO *charset;
   char *help_arg;
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 263ac50120dcd6db7552780a7c01167bdc2502ff..4e5f0019ad6dfce853528eef5a387e9e6fc48d96 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -2875,7 +2875,6 @@ mysql_init_query(THD *thd)
   thd->last_insert_id_used= thd->query_start_used= thd->insert_id_used=0;
   thd->sent_row_count= thd->examined_row_count= 0;
   thd->fatal_error= thd->rand_used= 0;
-  thd->safe_to_cache_query= 1;
   thd->possible_loops= 0;
   DBUG_VOID_RETURN;
 }
diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index 3ec12177778232e33b3596f63bb71a8ac564fe31..a3a1f93a8299cd9168163a3c5f4f38fedd51d230 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -601,10 +601,10 @@ static bool parse_prepare_query(PREP_STMT *stmt,
   mysql_log.write(thd,COM_PREPARE,"%s",packet);       
   mysql_init_query(thd);   
   thd->prepare_command=true; 
-  thd->safe_to_cache_query= 0;
   thd->lex.param_count=0;
 
   LEX *lex=lex_start(thd, (uchar*) packet, length);
+  lex->safe_to_cache_query= 0;
   if (!yyparse() && !thd->fatal_error) 
     error= send_prepare_results(stmt);
   lex_end(lex);
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index eaae24d0310a8a65c9339e7a2c03c44008c5aa71..5dcb4f09951c519e26dd94367b8372e7a2b27850 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -1676,7 +1676,7 @@ select_option:
 	      YYABORT;
 	    Select->options|= OPTION_FOUND_ROWS;
 	  }
-	| SQL_NO_CACHE_SYM { current_thd->safe_to_cache_query=0; }
+	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
 	| SQL_CACHE_SYM    { Select->options|= OPTION_TO_QUERY_CACHE; }
 	| ALL		{}
 	;
@@ -1689,7 +1689,7 @@ select_lock_type:
 	    if (check_simple_select())
 	      YYABORT;	
 	    lex->lock_option= TL_WRITE;
-	    lex->thd->safe_to_cache_query=0;
+	    lex->safe_to_cache_query=0;
 	  }
 	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
 	  {
@@ -1697,7 +1697,7 @@ select_lock_type:
 	    if (check_simple_select())
 	      YYABORT;	
 	    lex->lock_option= TL_READ_WITH_SHARED_LOCKS;
-	    lex->thd->safe_to_cache_query=0;
+	    lex->safe_to_cache_query=0;
 	  }
 	;
 
@@ -1885,12 +1885,12 @@ simple_expr:
 	| '@' ident_or_text SET_VAR expr
 	  {
 	    $$= new Item_func_set_user_var($2,$4);
-	    current_thd->safe_to_cache_query=0;
+	    Lex->safe_to_cache_query=0;
 	  }
 	| '@' ident_or_text	 
 	  {
 	    $$= new Item_func_get_user_var($2);
-	    current_thd->safe_to_cache_query=0;
+	    Lex->safe_to_cache_query=0;
 	  }
 	| '@' '@' opt_var_ident_type ident_or_text
 	  {
@@ -1944,13 +1944,13 @@ simple_expr:
 	| CONCAT_WS '(' expr ',' expr_list ')'
 	  { $$= new Item_func_concat_ws($3, *$5); }
 	| CURDATE optional_braces
-	  { $$= new Item_func_curdate(); current_thd->safe_to_cache_query=0; }
+	  { $$= new Item_func_curdate(); Lex->safe_to_cache_query=0; }
 	| CURTIME optional_braces
-	  { $$= new Item_func_curtime(); current_thd->safe_to_cache_query=0; }
+	  { $$= new Item_func_curtime(); Lex->safe_to_cache_query=0; }
 	| CURTIME '(' expr ')'
 	  { 
 	    $$= new Item_func_curtime($3); 
-	    current_thd->safe_to_cache_query=0;
+	    Lex->safe_to_cache_query=0;
 	  }
 	| DATE_ADD_INTERVAL '(' expr ',' INTERVAL_SYM expr interval ')'
 	  { $$= new Item_date_add_interval($3,$6,$7,0); }
@@ -1959,7 +1959,7 @@ simple_expr:
 	| DATABASE '(' ')'
 	  { 
 	    $$= new Item_func_database();
-            current_thd->safe_to_cache_query=0; 
+            Lex->safe_to_cache_query=0; 
 	  }
 	| ELT_FUNC '(' expr ',' expr_list ')'
 	  { $$= new Item_func_elt($3, *$5); }
@@ -1968,7 +1968,7 @@ simple_expr:
 	| ENCRYPT '(' expr ')'
 	  {
 	    $$= new Item_func_encrypt($3);
-	    current_thd->safe_to_cache_query=0; 
+	    Lex->safe_to_cache_query=0; 
 	  }
 	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
 	| DECODE_SYM '(' expr ',' TEXT_STRING ')'
@@ -2028,7 +2028,7 @@ simple_expr:
 	| LAST_INSERT_ID '(' expr ')'
 	  {
 	    $$= new Item_func_set_last_insert_id($3);
-	    current_thd->safe_to_cache_query=0;
+	    Lex->safe_to_cache_query=0;
 	  }
 	| LEFT '(' expr ',' expr ')'
 	  { $$= new Item_func_left($3,$5); }
@@ -2083,9 +2083,9 @@ simple_expr:
  	  { $$= new Item_func_spatial_collection(* $3, 
                        Geometry::wkbMultiPolygon, Geometry::wkbPolygon ); }
 	| NOW_SYM optional_braces
-	  { $$= new Item_func_now(); current_thd->safe_to_cache_query=0;}
+	  { $$= new Item_func_now(); Lex->safe_to_cache_query=0;}
 	| NOW_SYM '(' expr ')'
-	  { $$= new Item_func_now($3); current_thd->safe_to_cache_query=0;}
+	  { $$= new Item_func_now($3); Lex->safe_to_cache_query=0;}
 	| PASSWORD '(' expr ')'
 	  {
 	    $$= new Item_func_password($3);
@@ -2104,9 +2104,9 @@ simple_expr:
 	| POSITION_SYM '(' no_in_expr IN_SYM expr ')'
 	  { $$ = new Item_func_locate($5,$3); }
 	| RAND '(' expr ')'
-	  { $$= new Item_func_rand($3); current_thd->safe_to_cache_query=0;}
+	  { $$= new Item_func_rand($3); Lex->safe_to_cache_query=0;}
 	| RAND '(' ')'
-	  { $$= new Item_func_rand(); current_thd->safe_to_cache_query=0;}
+	  { $$= new Item_func_rand(); Lex->safe_to_cache_query=0;}
 	| REPLACE '(' expr ',' expr ',' expr ')'
 	  { $$= new Item_func_replace($3,$5,$7); }
 	| RIGHT '(' expr ',' expr ')'
@@ -2189,12 +2189,12 @@ simple_expr:
 	| UNIX_TIMESTAMP '(' ')'
 	  {
 	    $$= new Item_func_unix_timestamp();
-	    current_thd->safe_to_cache_query=0;
+	    Lex->safe_to_cache_query=0;
 	  }
 	| UNIX_TIMESTAMP '(' expr ')'
 	  { $$= new Item_func_unix_timestamp($3); }
 	| USER '(' ')'
-	  { $$= new Item_func_user(); current_thd->safe_to_cache_query=0; }
+	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
 	| WEEK_SYM '(' expr ')'
 	  { $$= new Item_func_week($3,new Item_int((char*) "0",0,1)); }
 	| WEEK_SYM '(' expr ',' expr ')'
@@ -2208,7 +2208,7 @@ simple_expr:
 	| BENCHMARK_SYM '(' ULONG_NUM ',' expr ')'
 	  { 
 	    $$=new Item_func_benchmark($3,$5);
-	    current_thd->safe_to_cache_query=0;
+	    Lex->safe_to_cache_query=0;
 	  }
 	| EXTRACT_SYM '(' interval FROM expr ')'
 	{ $$=new Item_extract( $3, $5); };
@@ -2667,7 +2667,7 @@ procedure_clause:
 	    lex->proc_list.next= (byte**) &lex->proc_list.first;
 	    if (add_proc_to_list(new Item_field(NULL,NULL,$2.str)))
 	      YYABORT;
-	    current_thd->safe_to_cache_query=0;
+	    Lex->safe_to_cache_query=0;
 	  }
 	  '(' procedure_list ')';
 
@@ -2739,7 +2739,7 @@ opt_into:
 	}
         | INTO select_var_list_init
 	{
-	  current_thd->safe_to_cache_query=0;
+	  Lex->safe_to_cache_query=0;
 	}
         ;