125
193
sql_command_flags[SQLCOM_DROP_INDEX]= CF_CHANGES_DATA;
127
195
sql_command_flags[SQLCOM_UPDATE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
196
sql_command_flags[SQLCOM_UPDATE_MULTI]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
128
197
sql_command_flags[SQLCOM_INSERT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
129
198
sql_command_flags[SQLCOM_INSERT_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
130
199
sql_command_flags[SQLCOM_DELETE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
200
sql_command_flags[SQLCOM_DELETE_MULTI]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
131
201
sql_command_flags[SQLCOM_REPLACE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
132
202
sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
204
sql_command_flags[SQLCOM_SHOW_STATUS]= CF_STATUS_COMMAND;
205
sql_command_flags[SQLCOM_SHOW_DATABASES]= CF_STATUS_COMMAND;
206
sql_command_flags[SQLCOM_SHOW_OPEN_TABLES]= CF_STATUS_COMMAND;
207
sql_command_flags[SQLCOM_SHOW_FIELDS]= CF_STATUS_COMMAND;
208
sql_command_flags[SQLCOM_SHOW_KEYS]= CF_STATUS_COMMAND;
209
sql_command_flags[SQLCOM_SHOW_VARIABLES]= CF_STATUS_COMMAND;
210
sql_command_flags[SQLCOM_SHOW_CHARSETS]= CF_STATUS_COMMAND;
211
sql_command_flags[SQLCOM_SHOW_COLLATIONS]= CF_STATUS_COMMAND;
212
sql_command_flags[SQLCOM_SHOW_BINLOGS]= CF_STATUS_COMMAND;
213
sql_command_flags[SQLCOM_SHOW_SLAVE_HOSTS]= CF_STATUS_COMMAND;
214
sql_command_flags[SQLCOM_SHOW_BINLOG_EVENTS]= CF_STATUS_COMMAND;
134
215
sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
135
216
sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
217
sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
218
sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
136
219
sql_command_flags[SQLCOM_SHOW_CREATE_DB]= CF_STATUS_COMMAND;
137
220
sql_command_flags[SQLCOM_SHOW_CREATE]= CF_STATUS_COMMAND;
221
sql_command_flags[SQLCOM_SHOW_MASTER_STAT]= CF_STATUS_COMMAND;
222
sql_command_flags[SQLCOM_SHOW_SLAVE_STAT]= CF_STATUS_COMMAND;
224
sql_command_flags[SQLCOM_SHOW_TABLES]= (CF_STATUS_COMMAND |
225
CF_SHOW_TABLE_COMMAND);
226
sql_command_flags[SQLCOM_SHOW_TABLE_STATUS]= (CF_STATUS_COMMAND |
227
CF_SHOW_TABLE_COMMAND);
140
229
The following admin table operations are allowed
232
sql_command_flags[SQLCOM_REPAIR]= CF_WRITE_LOGS_COMMAND;
233
sql_command_flags[SQLCOM_OPTIMIZE]= CF_WRITE_LOGS_COMMAND;
143
234
sql_command_flags[SQLCOM_ANALYZE]= CF_WRITE_LOGS_COMMAND;
238
bool is_update_query(enum enum_sql_command command)
240
DBUG_ASSERT(command >= 0 && command <= SQLCOM_END);
241
return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
244
void execute_init_command(THD *thd, sys_var_str *init_command_var,
245
rw_lock_t *var_mutex)
248
ulong save_client_capabilities;
250
thd_proc_info(thd, "Execution of init_command");
252
We need to lock init_command_var because
253
during execution of init_command_var query
254
values of init_command_var can't be changed
256
rw_rdlock(var_mutex);
257
save_client_capabilities= thd->client_capabilities;
258
thd->client_capabilities|= CLIENT_MULTI_QUERIES;
260
We don't need return result of execution to client side.
261
To forbid this we should set thd->net.vio to 0.
263
save_vio= thd->net.vio;
265
dispatch_command(COM_QUERY, thd,
266
init_command_var->value,
267
init_command_var->value_length);
268
rw_unlock(var_mutex);
269
thd->client_capabilities= save_client_capabilities;
270
thd->net.vio= save_vio;
274
Ends the current transaction and (maybe) begin the next.
276
@param thd Current thread
277
@param completion Completion type
283
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
287
DBUG_ENTER("end_trans");
289
if (unlikely(thd->in_sub_stmt))
291
my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
294
if (thd->transaction.xid_state.xa_state != XA_NOTR)
296
my_error(ER_XAER_RMFAIL, MYF(0),
297
xa_state_names[thd->transaction.xid_state.xa_state]);
300
switch (completion) {
303
We don't use end_active_trans() here to ensure that this works
304
even if there is a problem with the OPTION_AUTO_COMMIT flag
305
(Which of course should never happen...)
307
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
309
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
310
thd->transaction.all.modified_non_trans_table= false;
313
do_release= 1; /* fall through */
314
case COMMIT_AND_CHAIN:
315
res= end_active_trans(thd);
316
if (!res && completion == COMMIT_AND_CHAIN)
317
res= begin_trans(thd);
319
case ROLLBACK_RELEASE:
320
do_release= 1; /* fall through */
322
case ROLLBACK_AND_CHAIN:
324
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
325
if (ha_rollback(thd))
327
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
328
thd->transaction.all.modified_non_trans_table= false;
329
if (!res && (completion == ROLLBACK_AND_CHAIN))
330
res= begin_trans(thd);
335
my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
340
my_error(thd->killed_errno(), MYF(0));
341
else if ((res == 0) && do_release)
342
thd->killed= THD::KILL_CONNECTION;
349
Read one command from connection and execute it (query or simple command).
350
This function is called in loop from thread function.
352
For profiling to work, it must never be called recursively.
357
1 request of thread shutdown (see dispatch_command() description)
360
bool do_command(THD *thd)
366
enum enum_server_command command;
367
DBUG_ENTER("do_command");
370
indicator of uninitialized lex => normal flow of errors handling
373
thd->lex->current_select= 0;
376
This thread will do a blocking read from the client which
377
will be interrupted when the next command is received from
378
the client, the connection is closed or "net_wait_timeout"
379
number of seconds has passed
381
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
384
XXX: this code is here only to clear possible errors of init_connect.
385
Consider moving to init_connect() instead.
387
thd->clear_error(); // Clear error message
388
thd->main_da.reset_diagnostics_area();
390
net_new_transaction(net);
392
packet_length= my_net_read(net);
393
if (packet_length == packet_error)
395
DBUG_PRINT("info",("Got error %d reading command from socket %s",
397
vio_description(net->vio)));
399
/* Check if we can continue without closing the connection */
401
/* The error must be set. */
402
DBUG_ASSERT(thd->is_error());
403
net_end_statement(thd);
407
return_value= true; // We have to close it.
416
packet= (char*) net->read_pos;
418
'packet_length' contains length of data, as it was stored in packet
419
header. In case of malformed header, my_net_read returns zero.
420
If packet_length is not zero, my_net_read ensures that the returned
421
number of bytes was actually read from network.
422
There is also an extra safety measure in my_net_read:
423
it sets packet[packet_length]= 0, but only for non-zero packets.
425
if (packet_length == 0) /* safety */
427
/* Initialize with COM_SLEEP packet */
428
packet[0]= (uchar) COM_SLEEP;
431
/* Do not rely on my_net_read, extra safety against programming errors. */
432
packet[packet_length]= '\0'; /* safety */
434
command= (enum enum_server_command) (uchar) packet[0];
436
if (command >= COM_END)
437
command= COM_END; // Wrong command
439
DBUG_PRINT("info",("Command on %s = %d (%s)",
440
vio_description(net->vio), command,
441
command_name[command].str));
443
/* Restore read timeout value */
444
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
446
DBUG_ASSERT(packet_length);
447
return_value= dispatch_command(command, thd, packet+1, (uint) (packet_length-1));
450
DBUG_RETURN(return_value);
454
Determine if an attempt to update a non-temporary table while the
455
read-only option was enabled has been made.
457
This is a helper function to mysql_execute_command.
459
@note SQLCOM_MULTI_UPDATE is an exception and dealt with elsewhere.
461
@see mysql_execute_command
464
@retval true The statement should be denied.
465
@retval false The statement isn't updating any relevant tables.
468
static my_bool deny_updates_if_read_only_option(THD *thd,
469
TABLE_LIST *all_tables)
471
DBUG_ENTER("deny_updates_if_read_only_option");
478
if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
481
/* Multi update is an exception and is dealt with later. */
482
if (lex->sql_command == SQLCOM_UPDATE_MULTI)
485
const my_bool create_temp_tables=
486
(lex->sql_command == SQLCOM_CREATE_TABLE) &&
487
(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE);
489
const my_bool drop_temp_tables=
490
(lex->sql_command == SQLCOM_DROP_TABLE) &&
493
const my_bool update_real_tables=
494
some_non_temp_table_to_be_updated(thd, all_tables) &&
495
!(create_temp_tables || drop_temp_tables);
498
const my_bool create_or_drop_databases=
499
(lex->sql_command == SQLCOM_CREATE_DB) ||
500
(lex->sql_command == SQLCOM_DROP_DB);
502
if (update_real_tables || create_or_drop_databases)
505
An attempt was made to modify one or more non-temporary tables.
511
/* Assuming that only temporary tables are modified. */
147
516
Perform one connection-level (COM_XXXX) command.
149
518
@param command type of command to perform
150
@param session connection handle
519
@param thd connection handle
151
520
@param packet data for the command, packet is always null-terminated
152
521
@param packet_length length of packet + 1 (to show that data is
153
522
null-terminated) except for COM_SLEEP, where it
157
set session->lex->sql_command to SQLCOM_END here.
526
set thd->lex->sql_command to SQLCOM_END here.
159
528
The following has to be changed to an 8 byte integer
164
533
1 request of thread shutdown, i. e. if command is
165
534
COM_QUIT/COM_SHUTDOWN
167
bool dispatch_command(enum enum_server_command command, Session *session,
168
char* packet, uint32_t packet_length)
536
bool dispatch_command(enum enum_server_command command, THD *thd,
537
char* packet, uint packet_length)
171
Query_id &query_id= Query_id::get_query_id();
173
DRIZZLE_COMMAND_START(session->thread_id, command);
175
session->command= command;
176
session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
178
session->setQueryId(query_id.value());
541
DBUG_ENTER("dispatch_command");
542
DBUG_PRINT("info",("packet: '%*.s'; command: %d", packet_length, packet, command));
544
thd->command=command;
546
Commands which always take a long time are logged into
547
the slow log only if opt_log_slow_admin_statements is set.
549
thd->enable_slow_log= true;
550
thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
552
VOID(pthread_mutex_lock(&LOCK_thread_count));
553
thd->query_id= global_query_id;
180
555
switch( command ) {
181
556
/* Ignore these statements. */
560
/* Only increase id on these statements but don't count them. */
561
case COM_STMT_PREPARE:
184
566
/* Increase id and count all other statements. */
186
session->status_var.questions++;
190
/* TODO: set session->lex->sql_command to SQLCOM_END here */
192
plugin::Logging::preDo(session);
193
if (unlikely(plugin::EventObserver::beforeStatement(*session)))
195
// We should do something about an error...
198
session->server_status&=
568
statistic_increment(thd->status_var.questions, &LOCK_status);
573
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
574
VOID(pthread_mutex_unlock(&LOCK_thread_count));
199
577
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
200
578
switch (command) {
201
579
case COM_INIT_DB:
203
if (packet_length == 0)
205
my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
209
string tmp(packet, packet_length);
211
SchemaIdentifier identifier(tmp);
213
if (not mysql_change_db(session, identifier))
582
status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
583
thd->convert_string(&tmp, system_charset_info,
584
packet, packet_length, thd->charset());
585
if (!mysql_change_db(thd, &tmp, false))
587
general_log_write(thd, command, thd->db, thd->db_length);
592
case COM_REGISTER_SLAVE:
594
if (!register_slave(thd, (uchar*)packet, packet_length))
598
case COM_CHANGE_USER:
600
status_var_increment(thd->status_var.com_other);
601
char *user= (char*) packet, *packet_end= packet + packet_length;
602
/* Safe because there is always a trailing \0 at the end of the packet */
603
char *passwd= strend(user)+1;
605
thd->clear_error(); // if errors from rollback
608
Old clients send null-terminated string ('\0' for empty string) for
609
password. New clients send the size (1 byte) + string (not null
610
terminated, so also '\0' for empty string).
612
Cast *passwd to an unsigned char, so that it doesn't extend the sign
613
for *passwd > 127 and become 2**32-127 after casting to uint.
615
char db_buff[NAME_LEN+1]; // buffer to store db in utf8
619
If there is no password supplied, the packet must contain '\0',
620
in any type of handshake (4.1 or pre-4.1).
622
if (passwd >= packet_end)
624
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
627
uint passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
628
(uchar)(*passwd++) : strlen(passwd));
629
uint dummy_errors, save_db_length, db_length;
631
Security_context save_security_ctx= *thd->security_ctx;
632
USER_CONN *save_user_connect;
636
Database name is always NUL-terminated, so in case of empty database
637
the packet must contain at least the trailing '\0'.
639
if (db >= packet_end)
641
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
644
db_length= strlen(db);
646
char *ptr= db + db_length + 1;
649
if (ptr < packet_end)
651
if (ptr + 2 > packet_end)
653
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
657
cs_number= uint2korr(ptr);
660
/* Convert database name to utf8 */
661
db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
662
system_charset_info, db, db_length,
663
thd->charset(), &dummy_errors)]= 0;
666
/* Save user and privileges */
667
save_db_length= thd->db_length;
669
save_user_connect= thd->user_connect;
671
if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
673
thd->security_ctx->user= save_security_ctx.user;
674
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
678
/* Clear variables that are allocated */
679
thd->user_connect= 0;
680
thd->security_ctx->priv_user= thd->security_ctx->user;
681
res= check_user(thd, COM_CHANGE_USER, passwd, passwd_len, db, false);
685
x_free(thd->security_ctx->user);
686
*thd->security_ctx= save_security_ctx;
687
thd->user_connect= save_user_connect;
689
thd->db_length= save_db_length;
694
x_free(save_security_ctx.user);
698
thd_init_client_charset(thd, cs_number);
699
thd->update_charset();
704
case COM_STMT_EXECUTE:
706
case COM_STMT_SEND_LONG_DATA:
707
case COM_STMT_PREPARE:
711
/* We should toss an error here */
221
if (not session->readAndStoreQuery(packet, packet_length))
716
if (alloc_query(thd, packet, packet_length))
222
717
break; // fatal error is set
223
DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
225
const_cast<const char *>(session->schema()->c_str()));
227
mysql_parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
718
char *packet_end= thd->query + thd->query_length;
719
const char* end_of_stmt= NULL;
721
general_log_write(thd, command, thd->query, thd->query_length);
722
DBUG_PRINT("query",("%-.4096s",thd->query));
724
if (!(specialflag & SPECIAL_NO_PRIOR))
726
struct sched_param tmp_sched_param;
728
memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
729
tmp_sched_param.sched_priority= QUERY_PRIOR;
730
(void)pthread_setschedparam(pthread_self(), SCHED_OTHER, &tmp_sched_param);
733
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
735
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
737
char *beginning_of_next_stmt= (char*) end_of_stmt;
739
net_end_statement(thd);
741
Multiple queries exits, execute them individually
743
close_thread_tables(thd);
744
ulong length= (ulong)(packet_end - beginning_of_next_stmt);
746
log_slow_statement(thd);
748
/* Remove garbage at start of query */
749
while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
751
beginning_of_next_stmt++;
755
VOID(pthread_mutex_lock(&LOCK_thread_count));
756
thd->query_length= length;
757
thd->query= beginning_of_next_stmt;
759
Count each statement from the client.
761
statistic_increment(thd->status_var.questions, &LOCK_status);
762
thd->query_id= next_query_id();
763
thd->set_time(); /* Reset the query start time. */
764
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
765
VOID(pthread_mutex_unlock(&LOCK_thread_count));
767
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
770
if (!(specialflag & SPECIAL_NO_PRIOR))
772
struct sched_param tmp_sched_param;
774
memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
775
tmp_sched_param.sched_priority= WAIT_PRIOR;
776
(void)pthread_setschedparam(pthread_self(), SCHED_OTHER, &tmp_sched_param);
778
DBUG_PRINT("info",("query ready"));
781
case COM_FIELD_LIST: // This isn't actually needed
783
char *fields, *packet_end= packet + packet_length, *arg_end;
784
/* Locked closure of all tables */
785
TABLE_LIST table_list;
786
LEX_STRING conv_name;
788
/* used as fields initializator */
791
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
792
bzero((char*) &table_list,sizeof(table_list));
793
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
796
We have name + wildcard in packet, separated by endzero
798
arg_end= strend(packet);
799
thd->convert_string(&conv_name, system_charset_info,
800
packet, (uint) (arg_end - packet), thd->charset());
801
table_list.alias= table_list.table_name= conv_name.str;
804
if (!my_strcasecmp(system_charset_info, table_list.db,
805
INFORMATION_SCHEMA_NAME.str))
807
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
809
table_list.schema_table= schema_table;
812
thd->query_length= (uint) (packet_end - packet); // Don't count end \0
813
if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
815
general_log_print(thd, command, "%s %s", table_list.table_name, fields);
816
if (lower_case_table_names)
817
my_casedn_str(files_charset_info, table_list.table_name);
819
/* init structures for VIEW processing */
820
table_list.select_lex= &(thd->lex->select_lex);
823
mysql_reset_thd_for_next_command(thd);
826
select_lex.table_list.link_in_list((uchar*) &table_list,
827
(uchar**) &table_list.next_local);
828
thd->lex->add_to_query_tables(&table_list);
830
/* switch on VIEW optimisation: do not fill temporary tables */
831
thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
832
mysqld_list_fields(thd,&table_list,fields);
833
thd->lex->unit.cleanup();
834
thd->cleanup_after_query();
232
838
/* We don't calculate statistics for this command */
233
session->main_da.disable_status(); // Don't send anything back
839
general_log_print(thd, command, NullS);
840
net->error=0; // Don't give 'abort' message
841
thd->main_da.disable_status(); // Don't send anything back
234
842
error=true; // End server
844
case COM_BINLOG_DUMP:
848
uint32 slave_server_id;
850
status_var_increment(thd->status_var.com_other);
851
thd->enable_slow_log= opt_log_slow_admin_statements;
852
/* TODO: The following has to be changed to an 8 byte integer */
853
pos = uint4korr(packet);
854
flags = uint2korr(packet + 4);
855
thd->server_id=0; /* avoid suicide */
856
if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
857
kill_zombie_dump_threads(slave_server_id);
858
thd->server_id = slave_server_id;
860
general_log_print(thd, command, "Log: '%s' Pos: %ld", packet+10,
862
mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
863
unregister_slave(thd,1,1);
864
/* fake COM_QUIT -- if we get here, the thread needs to terminate */
236
868
case COM_SHUTDOWN:
238
session->status_var.com_other++;
240
session->close_thread_tables(); // Free before kill
870
status_var_increment(thd->status_var.com_other);
872
If the client is < 4.1.3, it is going to send us no argument; then
873
packet_length is 0, packet[0] is the end 0 of the packet. Note that
874
SHUTDOWN_DEFAULT is 0. If client is >= 4.1.3, the shutdown level is in
877
enum mysql_enum_shutdown_level level=
878
(enum mysql_enum_shutdown_level) (uchar) packet[0];
879
if (level == SHUTDOWN_DEFAULT)
880
level= SHUTDOWN_WAIT_ALL_BUFFERS; // soon default will be configurable
881
else if (level != SHUTDOWN_WAIT_ALL_BUFFERS)
883
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "this shutdown level");
886
DBUG_PRINT("quit",("Got shutdown command for level %u", level));
887
general_log_print(thd, command, NullS);
889
close_thread_tables(thd); // Free before kill
896
STATUS_VAR current_global_status_var;
899
ulonglong queries_per_second1000;
901
uint buff_len= sizeof(buff);
903
general_log_print(thd, command, NullS);
904
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_STATUS]);
905
calc_sum_of_all_status(¤t_global_status_var);
906
if (!(uptime= (ulong) (thd->start_time - server_start_time)))
907
queries_per_second1000= 0;
909
queries_per_second1000= thd->query_id * 1000LL / uptime;
911
length= snprintf((char*) buff, buff_len - 1,
912
"Uptime: %lu Threads: %d Questions: %lu "
913
"Slow queries: %lu Opens: %lu Flush tables: %lu "
914
"Open tables: %u Queries per second avg: %u.%u",
916
(int) thread_count, (ulong) thd->query_id,
917
current_global_status_var.long_query_count,
918
current_global_status_var.opened_tables,
920
cached_open_tables(),
921
(uint) (queries_per_second1000 / 1000),
922
(uint) (queries_per_second1000 % 1000));
923
/* Store the buffer in permanent memory */
924
my_ok(thd, 0, 0, buff);
925
VOID(my_net_write(net, (uchar*) buff, length));
926
VOID(net_flush(net));
927
thd->main_da.disable_status();
246
session->status_var.com_other++;
247
session->my_ok(); // Tell client we are alive
931
status_var_increment(thd->status_var.com_other);
932
my_ok(thd); // Tell client we are alive
934
case COM_PROCESS_INFO:
935
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
936
general_log_print(thd, command, NullS);
937
mysqld_list_processes(thd, NullS, 0);
939
case COM_PROCESS_KILL:
941
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
942
ulong id=(ulong) uint4korr(packet);
943
sql_kill(thd,id,false);
948
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
949
uint opt_command= uint2korr(packet);
951
switch (opt_command) {
952
case (int) MYSQL_OPTION_MULTI_STATEMENTS_ON:
953
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
956
case (int) MYSQL_OPTION_MULTI_STATEMENTS_OFF:
957
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
961
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
967
status_var_increment(thd->status_var.com_other);
968
mysql_print_status();
969
general_log_print(thd, command, NullS);
250
973
case COM_CONNECT: // Impossible here
974
case COM_TIME: // Impossible from client
253
977
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
468
1296
variables, but for now this is probably good enough.
469
1297
Don't reset warnings when executing a stored routine.
471
if (all_tables || ! lex->is_single_level_stmt())
473
drizzle_reset_errors(session, 0);
476
assert(session->transaction.stmt.hasModifiedNonTransData() == false);
478
/* now we are ready to execute the statement */
479
res= lex->statement->execute();
480
session->set_proc_info("query end");
1299
if (all_tables || !lex->is_single_level_stmt())
1300
mysql_reset_errors(thd, 0);
1302
if (unlikely(thd->slave_thread))
1305
Check if statment should be skipped because of slave filtering
1309
- UPDATE MULTI: For this statement, we want to check the filtering
1310
rules later in the code
1311
- SET: we always execute it (Not that many SET commands exists in
1312
the binary log anyway -- only 4.1 masters write SET statements,
1313
in 5.0 there are no SET statements in the binary log)
1314
- DROP TEMPORARY TABLE IF EXISTS: we always execute it (otherwise we
1315
have stale files on slave caused by exclusion of one tmp table).
1317
if (!(lex->sql_command == SQLCOM_UPDATE_MULTI) &&
1318
!(lex->sql_command == SQLCOM_SET_OPTION) &&
1319
!(lex->sql_command == SQLCOM_DROP_TABLE &&
1320
lex->drop_temporary && lex->drop_if_exists) &&
1321
all_tables_not_ok(thd, all_tables))
1323
/* we warn the slave SQL thread */
1324
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1325
if (thd->one_shot_set)
1328
It's ok to check thd->one_shot_set here:
1330
The charsets in a MySQL 5.0 slave can change by both a binlogged
1331
SET ONE_SHOT statement and the event-internal charset setting,
1332
and these two ways to change charsets do not seems to work
1335
At least there seems to be problems in the rli cache for
1336
charsets if we are using ONE_SHOT. Note that this is normally no
1337
problem because either the >= 5.0 slave reads a 4.1 binlog (with
1338
ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1340
reset_one_shot_variables(thd);
1348
When option readonly is set deny operations which change non-temporary
1349
tables. Except for the replication thread and the 'super' users.
1351
if (deny_updates_if_read_only_option(thd, all_tables))
1353
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1356
} /* endif unlikely slave */
1357
status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1359
DBUG_ASSERT(thd->transaction.stmt.modified_non_trans_table == false);
1361
switch (lex->sql_command) {
1362
case SQLCOM_SHOW_STATUS:
1364
system_status_var old_status_var= thd->status_var;
1365
thd->initial_status_var= &old_status_var;
1366
res= execute_sqlcom_select(thd, all_tables);
1367
/* Don't log SHOW STATUS commands to slow query log */
1368
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1369
SERVER_QUERY_NO_GOOD_INDEX_USED);
1371
restore status variables, as we don't want 'show status' to cause
1374
pthread_mutex_lock(&LOCK_status);
1375
add_diff_to_status(&global_status_var, &thd->status_var,
1377
thd->status_var= old_status_var;
1378
pthread_mutex_unlock(&LOCK_status);
1381
case SQLCOM_SHOW_DATABASES:
1382
case SQLCOM_SHOW_TABLES:
1383
case SQLCOM_SHOW_TABLE_STATUS:
1384
case SQLCOM_SHOW_OPEN_TABLES:
1385
case SQLCOM_SHOW_FIELDS:
1386
case SQLCOM_SHOW_KEYS:
1387
case SQLCOM_SHOW_VARIABLES:
1388
case SQLCOM_SHOW_CHARSETS:
1389
case SQLCOM_SHOW_COLLATIONS:
1392
thd->status_var.last_query_cost= 0.0;
1393
res= execute_sqlcom_select(thd, all_tables);
1396
case SQLCOM_EMPTY_QUERY:
1402
res = purge_master_logs(thd, lex->to_log);
1405
case SQLCOM_PURGE_BEFORE:
1409
/* PURGE MASTER LOGS BEFORE 'data' */
1410
it= (Item *)lex->value_list.head();
1411
if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
1414
my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1417
it= new Item_func_unix_timestamp(it);
1419
it is OK only emulate fix_fieds, because we need only
1422
it->quick_fix_field();
1423
res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1426
case SQLCOM_SHOW_WARNS:
1428
res= mysqld_show_warnings(thd, (ulong)
1429
((1L << (uint) MYSQL_ERROR::WARN_LEVEL_NOTE) |
1430
(1L << (uint) MYSQL_ERROR::WARN_LEVEL_WARN) |
1431
(1L << (uint) MYSQL_ERROR::WARN_LEVEL_ERROR)
1435
case SQLCOM_SHOW_ERRORS:
1437
res= mysqld_show_warnings(thd, (ulong)
1438
(1L << (uint) MYSQL_ERROR::WARN_LEVEL_ERROR));
1441
case SQLCOM_SHOW_SLAVE_HOSTS:
1443
res = show_slave_hosts(thd);
1446
case SQLCOM_SHOW_BINLOG_EVENTS:
1448
res = mysql_show_binlog_events(thd);
1452
case SQLCOM_ASSIGN_TO_KEYCACHE:
1454
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1455
res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1458
case SQLCOM_CHANGE_MASTER:
1460
pthread_mutex_lock(&LOCK_active_mi);
1461
res = change_master(thd,active_mi);
1462
pthread_mutex_unlock(&LOCK_active_mi);
1465
case SQLCOM_SHOW_SLAVE_STAT:
1467
pthread_mutex_lock(&LOCK_active_mi);
1468
if (active_mi != NULL)
1470
res = show_master_info(thd, active_mi);
1474
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1475
"the master info structure does not exist");
1478
pthread_mutex_unlock(&LOCK_active_mi);
1481
case SQLCOM_SHOW_MASTER_STAT:
1483
res = show_binlog_info(thd);
1487
case SQLCOM_SHOW_ENGINE_STATUS:
1489
res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
1492
case SQLCOM_CREATE_TABLE:
1494
/* If CREATE TABLE of non-temporary table, do implicit commit */
1495
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
1497
if (end_active_trans(thd))
1503
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1505
// Skip first table, which is the table we are creating
1506
TABLE_LIST *create_table= lex->unlink_first_table(&link_to_local);
1507
TABLE_LIST *select_tables= lex->query_tables;
1509
Code below (especially in mysql_create_table() and select_create
1510
methods) may modify HA_CREATE_INFO structure in LEX, so we have to
1511
use a copy of this structure to make execution prepared statement-
1512
safe. A shallow copy is enough as this code won't modify any memory
1513
referenced from this structure.
1515
HA_CREATE_INFO create_info(lex->create_info);
1517
We need to copy alter_info for the same reasons of re-execution
1518
safety, only in case of Alter_info we have to do (almost) a deep
1521
Alter_info alter_info(lex->alter_info, thd->mem_root);
1523
if (thd->is_fatal_error)
1525
/* If out of memory when creating a copy of alter_info. */
1527
goto end_with_restore_list;
1530
if ((res= create_table_precheck(thd, select_tables, create_table)))
1531
goto end_with_restore_list;
1533
/* Might have been updated in create_table_precheck */
1534
create_info.alias= create_table->alias;
1536
#ifdef HAVE_READLINK
1537
/* Fix names if symlinked tables */
1538
if (append_file_to_dir(thd, &create_info.data_file_name,
1539
create_table->table_name) ||
1540
append_file_to_dir(thd, &create_info.index_file_name,
1541
create_table->table_name))
1542
goto end_with_restore_list;
1545
If we are using SET CHARSET without DEFAULT, add an implicit
1546
DEFAULT to not confuse old users. (This may change).
1548
if ((create_info.used_fields &
1549
(HA_CREATE_USED_DEFAULT_CHARSET | HA_CREATE_USED_CHARSET)) ==
1550
HA_CREATE_USED_CHARSET)
1552
create_info.used_fields&= ~HA_CREATE_USED_CHARSET;
1553
create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
1554
create_info.default_table_charset= create_info.table_charset;
1555
create_info.table_charset= 0;
1558
The create-select command will open and read-lock the select table
1559
and then create, open and write-lock the new table. If a global
1560
read lock steps in, we get a deadlock. The write lock waits for
1561
the global read lock, while the global read lock waits for the
1562
select table to be closed. So we wait until the global readlock is
1563
gone before starting both steps. Note that
1564
wait_if_global_read_lock() sets a protection against a new global
1565
read lock when it succeeds. This needs to be released by
1566
start_waiting_global_read_lock(). We protect the normal CREATE
1567
TABLE in the same way. That way we avoid that a new table is
1568
created during a gobal read lock.
1570
if (!thd->locked_tables &&
1571
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1574
goto end_with_restore_list;
1576
if (select_lex->item_list.elements) // With select
1578
select_result *result;
1580
select_lex->options|= SELECT_NO_UNLOCK;
1581
unit->set_limit(select_lex);
1584
Disable non-empty MERGE tables with CREATE...SELECT. Too
1585
complicated. See Bug #26379. Empty MERGE tables are read-only
1586
and don't allow CREATE...SELECT anyway.
1588
if (create_info.used_fields & HA_CREATE_USED_UNION)
1590
my_error(ER_WRONG_OBJECT, MYF(0), create_table->db,
1591
create_table->table_name, "BASE TABLE");
1593
goto end_with_restore_list;
1596
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1598
lex->link_first_table_back(create_table, link_to_local);
1599
create_table->create= true;
1602
if (!(res= open_and_lock_tables(thd, lex->query_tables)))
1605
Is table which we are changing used somewhere in other parts
1608
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1610
TABLE_LIST *duplicate;
1611
create_table= lex->unlink_first_table(&link_to_local);
1612
if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
1614
update_non_unique_table_error(create_table, "CREATE", duplicate);
1616
goto end_with_restore_list;
1619
/* If we create merge table, we have to test tables in merge, too */
1620
if (create_info.used_fields & HA_CREATE_USED_UNION)
1623
for (tab= (TABLE_LIST*) create_info.merge_list.first;
1625
tab= tab->next_local)
1627
TABLE_LIST *duplicate;
1628
if ((duplicate= unique_table(thd, tab, select_tables, 0)))
1630
update_non_unique_table_error(tab, "CREATE", duplicate);
1632
goto end_with_restore_list;
1638
select_create is currently not re-execution friendly and
1639
needs to be created for every execution of a PS/SP.
1641
if ((result= new select_create(create_table,
1644
select_lex->item_list,
1650
CREATE from SELECT give its SELECT_LEX for SELECT,
1651
and item_list belong to SELECT
1653
res= handle_select(thd, lex, result, 0);
1657
else if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1658
create_table= lex->unlink_first_table(&link_to_local);
1663
/* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1664
if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1665
thd->options|= OPTION_KEEP_LOG;
1666
/* regular create */
1667
if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1668
res= mysql_create_like_table(thd, create_table, select_tables,
1672
res= mysql_create_table(thd, create_table->db,
1673
create_table->table_name, &create_info,
1680
/* put tables back for PS rexecuting */
1681
end_with_restore_list:
1682
lex->link_first_table_back(create_table, link_to_local);
1685
case SQLCOM_CREATE_INDEX:
1687
case SQLCOM_DROP_INDEX:
1689
CREATE INDEX and DROP INDEX are implemented by calling ALTER
1690
TABLE with proper arguments.
1692
In the future ALTER TABLE will notice that the request is to
1693
only add indexes and create these one by one for the existing
1694
table without having to do a full rebuild.
1697
/* Prepare stack copies to be re-execution safe */
1698
HA_CREATE_INFO create_info;
1699
Alter_info alter_info(lex->alter_info, thd->mem_root);
1701
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1704
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1705
if (end_active_trans(thd))
1708
Currently CREATE INDEX or DROP INDEX cause a full table rebuild
1709
and thus classify as slow administrative statements just like
1712
thd->enable_slow_log= opt_log_slow_admin_statements;
1714
bzero((char*) &create_info, sizeof(create_info));
1715
create_info.db_type= 0;
1716
create_info.row_type= ROW_TYPE_NOT_USED;
1717
create_info.default_table_charset= thd->variables.collation_database;
1719
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1720
&create_info, first_table, &alter_info,
1724
#ifdef HAVE_REPLICATION
1725
case SQLCOM_SLAVE_START:
1727
pthread_mutex_lock(&LOCK_active_mi);
1728
start_slave(thd,active_mi,1 /* net report*/);
1729
pthread_mutex_unlock(&LOCK_active_mi);
1732
case SQLCOM_SLAVE_STOP:
1734
If the client thread has locked tables, a deadlock is possible.
1736
- the client thread does LOCK TABLE t READ.
1737
- then the master updates t.
1738
- then the SQL slave thread wants to update t,
1739
so it waits for the client thread because t is locked by it.
1740
- then the client thread does SLAVE STOP.
1741
SLAVE STOP waits for the SQL slave thread to terminate its
1742
update t, which waits for the client thread because t is locked by it.
1743
To prevent that, refuse SLAVE STOP if the
1744
client thread has locked tables
1746
if (thd->locked_tables || thd->active_transaction() || thd->global_read_lock)
1748
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1749
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1753
pthread_mutex_lock(&LOCK_active_mi);
1754
stop_slave(thd,active_mi,1/* net report*/);
1755
pthread_mutex_unlock(&LOCK_active_mi);
1758
#endif /* HAVE_REPLICATION */
1760
case SQLCOM_ALTER_TABLE:
1761
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1764
Code in mysql_alter_table() may modify its HA_CREATE_INFO argument,
1765
so we have to use a copy of this structure to make execution
1766
prepared statement- safe. A shallow copy is enough as no memory
1767
referenced from this structure will be modified.
1769
HA_CREATE_INFO create_info(lex->create_info);
1770
Alter_info alter_info(lex->alter_info, thd->mem_root);
1772
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1777
/* Must be set in the parser */
1778
DBUG_ASSERT(select_lex->db);
1780
{ // Rename of table
1781
TABLE_LIST tmp_table;
1782
bzero((char*) &tmp_table,sizeof(tmp_table));
1783
tmp_table.table_name= lex->name.str;
1784
tmp_table.db=select_lex->db;
1787
/* Don't yet allow changing of symlinks with ALTER TABLE */
1788
if (create_info.data_file_name)
1789
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1790
"DATA DIRECTORY option ignored");
1791
if (create_info.index_file_name)
1792
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1793
"INDEX DIRECTORY option ignored");
1794
create_info.data_file_name= create_info.index_file_name= NULL;
1795
/* ALTER TABLE ends previous transaction */
1796
if (end_active_trans(thd))
1799
if (!thd->locked_tables &&
1800
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1806
thd->enable_slow_log= opt_log_slow_admin_statements;
1807
res= mysql_alter_table(thd, select_lex->db, lex->name.str,
1811
select_lex->order_list.elements,
1812
(ORDER *) select_lex->order_list.first,
1816
case SQLCOM_RENAME_TABLE:
1818
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1820
for (table= first_table; table; table= table->next_local->next_local)
1822
TABLE_LIST old_list, new_list;
1824
we do not need initialize old_list and new_list because we will
1825
come table[0] and table->next[0] there
1828
new_list= table->next_local[0];
1831
if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
1837
case SQLCOM_SHOW_BINLOGS:
1839
res = show_binlogs(thd);
1842
case SQLCOM_SHOW_CREATE:
1843
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1845
res= mysqld_show_create(thd, first_table);
1848
case SQLCOM_CHECKSUM:
1850
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1851
res = mysql_checksum_table(thd, first_table, &lex->check_opt);
1856
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1857
thd->enable_slow_log= opt_log_slow_admin_statements;
1858
res= mysql_repair_table(thd, first_table, &lex->check_opt);
1859
/* ! we write after unlocking the table */
1860
if (!res && !lex->no_write_to_binlog)
1863
Presumably, REPAIR and binlog writing doesn't require synchronization
1865
write_bin_log(thd, true, thd->query, thd->query_length);
1867
select_lex->table_list.first= (uchar*) first_table;
1868
lex->query_tables=all_tables;
1873
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1874
thd->enable_slow_log= opt_log_slow_admin_statements;
1875
res = mysql_check_table(thd, first_table, &lex->check_opt);
1876
select_lex->table_list.first= (uchar*) first_table;
1877
lex->query_tables=all_tables;
1880
case SQLCOM_ANALYZE:
1882
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1883
thd->enable_slow_log= opt_log_slow_admin_statements;
1884
res= mysql_analyze_table(thd, first_table, &lex->check_opt);
1885
/* ! we write after unlocking the table */
1886
if (!res && !lex->no_write_to_binlog)
1889
Presumably, ANALYZE and binlog writing doesn't require synchronization
1891
write_bin_log(thd, true, thd->query, thd->query_length);
1893
select_lex->table_list.first= (uchar*) first_table;
1894
lex->query_tables=all_tables;
1898
case SQLCOM_OPTIMIZE:
1900
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1901
thd->enable_slow_log= opt_log_slow_admin_statements;
1902
res= (specialflag & (SPECIAL_SAFE_MODE | SPECIAL_NO_NEW_FUNC)) ?
1903
mysql_recreate_table(thd, first_table) :
1904
mysql_optimize_table(thd, first_table, &lex->check_opt);
1905
/* ! we write after unlocking the table */
1906
if (!res && !lex->no_write_to_binlog)
1909
Presumably, OPTIMIZE and binlog writing doesn't require synchronization
1911
write_bin_log(thd, true, thd->query, thd->query_length);
1913
select_lex->table_list.first= (uchar*) first_table;
1914
lex->query_tables=all_tables;
1918
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1919
if (update_precheck(thd, all_tables))
1921
DBUG_ASSERT(select_lex->offset_limit == 0);
1922
unit->set_limit(select_lex);
1923
res= (up_result= mysql_update(thd, all_tables,
1924
select_lex->item_list,
1927
select_lex->order_list.elements,
1928
(ORDER *) select_lex->order_list.first,
1929
unit->select_limit_cnt,
1930
lex->duplicates, lex->ignore));
1931
/* mysql_update return 2 if we need to switch to multi-update */
1935
case SQLCOM_UPDATE_MULTI:
1937
DBUG_ASSERT(first_table == all_tables && first_table != 0);
1938
/* if we switched from normal update, rights are checked */
1941
if ((res= multi_update_precheck(thd, all_tables)))
1947
res= mysql_multi_update_prepare(thd);
1949
#ifdef HAVE_REPLICATION
1950
/* Check slave filtering rules */
1951
if (unlikely(thd->slave_thread))
1953
if (all_tables_not_ok(thd, all_tables))
1957
res= 0; /* don't care of prev failure */
1958
thd->clear_error(); /* filters are of highest prior */
1960
/* we warn the slave SQL thread */
1961
my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1969
#endif /* HAVE_REPLICATION */
1973
some_non_temp_table_to_be_updated(thd, all_tables))
1975
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1978
#ifdef HAVE_REPLICATION
1982
res= mysql_multi_update(thd, all_tables,
1983
&select_lex->item_list,
1986
select_lex->options,
1987
lex->duplicates, lex->ignore, unit, select_lex);
1990
case SQLCOM_REPLACE:
1992
if (mysql_bin_log.is_open())
1995
Generate an incident log event before writing the real event
1996
to the binary log. We put this event is before the statement
1997
since that makes it simpler to check that the statement was
1998
not executed on the slave (since incidents usually stop the
2001
Observe that any row events that are generated will be
2004
This is only for testing purposes and will not be present in a
2008
Incident incident= INCIDENT_NONE;
2009
DBUG_PRINT("debug", ("Just before generate_incident()"));
2010
DBUG_EXECUTE_IF("incident_database_resync_on_replace",
2011
incident= INCIDENT_LOST_EVENTS;);
2014
Incident_log_event ev(thd, incident);
2015
mysql_bin_log.write(&ev);
2016
mysql_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
2018
DBUG_PRINT("debug", ("Just after generate_incident()"));
2023
DBUG_ASSERT(first_table == all_tables && first_table != 0);
2024
if ((res= insert_precheck(thd, all_tables)))
2027
if (!thd->locked_tables &&
2028
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
2034
res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
2035
lex->update_list, lex->value_list,
2036
lex->duplicates, lex->ignore);
2040
case SQLCOM_REPLACE_SELECT:
2041
case SQLCOM_INSERT_SELECT:
2043
select_result *sel_result;
2044
DBUG_ASSERT(first_table == all_tables && first_table != 0);
2045
if ((res= insert_precheck(thd, all_tables)))
2048
/* Fix lock for first table */
2049
if (first_table->lock_type == TL_WRITE_DELAYED)
2050
first_table->lock_type= TL_WRITE;
2052
/* Don't unlock tables until command is written to binary log */
2053
select_lex->options|= SELECT_NO_UNLOCK;
2055
unit->set_limit(select_lex);
2057
if (! thd->locked_tables &&
2058
! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
2064
if (!(res= open_and_lock_tables(thd, all_tables)))
2066
/* Skip first table, which is the table we are inserting in */
2067
TABLE_LIST *second_table= first_table->next_local;
2068
select_lex->table_list.first= (uchar*) second_table;
2069
select_lex->context.table_list=
2070
select_lex->context.first_name_resolution_table= second_table;
2071
res= mysql_insert_select_prepare(thd);
2072
if (!res && (sel_result= new select_insert(first_table,
2080
res= handle_select(thd, lex, sel_result, OPTION_SETUP_TABLES_DONE);
2082
Invalidate the table in the query cache if something changed
2083
after unlocking when changes become visible.
2084
TODO: this is workaround. right way will be move invalidating in
2085
the unlock procedure.
2087
if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT &&
2090
/* INSERT ... SELECT should invalidate only the very first table */
2091
TABLE_LIST *save_table= first_table->next_local;
2092
first_table->next_local= 0;
2093
first_table->next_local= save_table;
2097
/* revert changes for SP */
2098
select_lex->table_list.first= (uchar*) first_table;
2103
case SQLCOM_TRUNCATE:
2104
if (end_active_trans(thd))
2109
DBUG_ASSERT(first_table == all_tables && first_table != 0);
2111
Don't allow this within a transaction because we want to use
2114
if (thd->locked_tables || thd->active_transaction())
2116
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2117
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2121
res= mysql_truncate(thd, first_table, 0);
2126
DBUG_ASSERT(first_table == all_tables && first_table != 0);
2127
DBUG_ASSERT(select_lex->offset_limit == 0);
2128
unit->set_limit(select_lex);
2130
if (!thd->locked_tables &&
2131
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
2137
res = mysql_delete(thd, all_tables, select_lex->where,
2138
&select_lex->order_list,
2139
unit->select_limit_cnt, select_lex->options,
2143
case SQLCOM_DELETE_MULTI:
2145
DBUG_ASSERT(first_table == all_tables && first_table != 0);
2146
TABLE_LIST *aux_tables=
2147
(TABLE_LIST *)thd->lex->auxiliary_table_list.first;
2148
multi_delete *del_result;
2150
if (!thd->locked_tables &&
2151
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
2157
if ((res= multi_delete_precheck(thd, all_tables)))
2160
/* condition will be true on SP re-excuting */
2161
if (select_lex->item_list.elements != 0)
2162
select_lex->item_list.empty();
2163
if (add_item_to_list(thd, new Item_null()))
2166
thd_proc_info(thd, "init");
2167
if ((res= open_and_lock_tables(thd, all_tables)))
2170
if ((res= mysql_multi_delete_prepare(thd)))
2173
if (!thd->is_fatal_error &&
2174
(del_result= new multi_delete(aux_tables, lex->table_count)))
2176
res= mysql_select(thd, &select_lex->ref_pointer_array,
2177
select_lex->get_table_list(),
2178
select_lex->with_wild,
2179
select_lex->item_list,
2181
0, (ORDER *)NULL, (ORDER *)NULL, (Item *)NULL,
2183
select_lex->options | thd->options |
2184
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
2185
OPTION_SETUP_TABLES_DONE,
2186
del_result, unit, select_lex);
2187
res|= thd->is_error();
2189
del_result->abort();
2196
case SQLCOM_DROP_TABLE:
2198
DBUG_ASSERT(first_table == all_tables && first_table != 0);
2199
if (!lex->drop_temporary)
2201
if (end_active_trans(thd))
2207
If this is a slave thread, we may sometimes execute some
2208
DROP / * 40005 TEMPORARY * / TABLE
2209
that come from parts of binlogs (likely if we use RESET SLAVE or CHANGE
2210
MASTER TO), while the temporary table has already been dropped.
2211
To not generate such irrelevant "table does not exist errors",
2212
we silently add IF EXISTS if TEMPORARY was used.
2214
if (thd->slave_thread)
2215
lex->drop_if_exists= 1;
2217
/* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
2218
thd->options|= OPTION_KEEP_LOG;
2220
/* DDL and binlog write order protected by LOCK_open */
2221
res= mysql_rm_table(thd, first_table, lex->drop_if_exists,
2222
lex->drop_temporary);
2225
case SQLCOM_SHOW_PROCESSLIST:
2226
mysqld_list_processes(thd, NullS, lex->verbose);
2228
case SQLCOM_SHOW_ENGINE_LOGS:
2230
res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
2233
case SQLCOM_CHANGE_DB:
2235
LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
2237
if (!mysql_change_db(thd, &db_str, false))
2245
DBUG_ASSERT(first_table == all_tables && first_table != 0);
2246
if (lex->local_file)
2248
if (!(thd->client_capabilities & CLIENT_LOCAL_FILES) ||
2251
my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
2256
res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
2257
lex->update_list, lex->value_list, lex->duplicates,
2258
lex->ignore, (bool) lex->local_file);
2262
case SQLCOM_SET_OPTION:
2264
List<set_var_base> *lex_var_list= &lex->var_list;
2266
if (lex->autocommit && end_active_trans(thd))
2269
if (open_and_lock_tables(thd, all_tables))
2271
if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
2273
my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
2276
if (!(res= sql_set_variables(thd, lex_var_list)))
2279
If the previous command was a SET ONE_SHOT, we don't want to forget
2280
about the ONE_SHOT property of that SET. So we use a |= instead of = .
2282
thd->one_shot_set|= lex->one_shot_set;
2288
We encountered some sort of error, but no message was sent.
2289
Send something semi-generic here since we don't know which
2290
assignment in the list caused the error.
2292
if (!thd->is_error())
2293
my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
2300
case SQLCOM_UNLOCK_TABLES:
2302
It is critical for mysqldump --single-transaction --master-data that
2303
UNLOCK TABLES does not implicitely commit a connection which has only
2304
done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2305
false, mysqldump will not work.
2307
unlock_locked_tables(thd);
2308
if (thd->options & OPTION_TABLE_LOCK)
2310
end_active_trans(thd);
2311
thd->options&= ~(OPTION_TABLE_LOCK);
2313
if (thd->global_read_lock)
2314
unlock_global_read_lock(thd);
2317
case SQLCOM_LOCK_TABLES:
2319
We try to take transactional locks if
2320
- only transactional locks are requested (lex->lock_transactional) and
2321
- no non-transactional locks exist (!thd->locked_tables).
2323
DBUG_PRINT("lock_info", ("lex->lock_transactional: %d "
2324
"thd->locked_tables: 0x%lx",
2325
lex->lock_transactional,
2326
(long) thd->locked_tables));
2327
if (lex->lock_transactional && !thd->locked_tables)
2331
All requested locks are transactional and no non-transactional
2334
if ((rc= try_transactional_lock(thd, all_tables)) == -1)
2342
Non-transactional locking has been requested or
2343
non-transactional locks exist already or transactional locks are
2344
not supported by all storage engines. Take non-transactional
2349
One or more requested locks are non-transactional and/or
2350
non-transactional locks exist or a storage engine does not support
2351
transactional locks. Check if at least one transactional lock is
2352
requested. If yes, warn about the conversion to non-transactional
2353
locks or abort in strict mode.
2355
if (check_transactional_lock(thd, all_tables))
2357
unlock_locked_tables(thd);
2358
/* we must end the trasaction first, regardless of anything */
2359
if (end_active_trans(thd))
2361
thd->in_lock_tables=1;
2362
thd->options|= OPTION_TABLE_LOCK;
2364
if (!(res= simple_open_n_lock_tables(thd, all_tables)))
2366
thd->locked_tables=thd->lock;
2368
(void) set_handler_table_locks(thd, all_tables, false);
2369
DBUG_PRINT("lock_info", ("thd->locked_tables: 0x%lx",
2370
(long) thd->locked_tables));
2376
Need to end the current transaction, so the storage engine (InnoDB)
2377
can free its locks if LOCK TABLES locked some tables before finding
2378
that it can't lock a table in its list
2380
ha_autocommit_or_rollback(thd, 1);
2381
end_active_trans(thd);
2382
thd->options&= ~(OPTION_TABLE_LOCK);
2384
thd->in_lock_tables=0;
2386
case SQLCOM_CREATE_DB:
2389
As mysql_create_db() may modify HA_CREATE_INFO structure passed to
2390
it, we need to use a copy of LEX::create_info to make execution
2391
prepared statement- safe.
2393
HA_CREATE_INFO create_info(lex->create_info);
2394
if (end_active_trans(thd))
2400
if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
2401
check_db_name(&lex->name))
2403
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2407
If in a slave thread :
2408
CREATE DATABASE DB was certainly not preceded by USE DB.
2409
For that reason, db_ok() in sql/slave.cc did not check the
2410
do_db/ignore_db. And as this query involves no tables, tables_ok()
2411
above was not called. So we have to check rules again here.
2413
if (thd->slave_thread &&
2414
(!rpl_filter->db_ok(lex->name.str) ||
2415
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2417
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2420
res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
2421
lex->name.str), &create_info, 0);
2424
case SQLCOM_DROP_DB:
2426
if (end_active_trans(thd))
2431
if (check_db_name(&lex->name))
2433
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2437
If in a slave thread :
2438
DROP DATABASE DB may not be preceded by USE DB.
2439
For that reason, maybe db_ok() in sql/slave.cc did not check the
2440
do_db/ignore_db. And as this query involves no tables, tables_ok()
2441
above was not called. So we have to check rules again here.
2443
#ifdef HAVE_REPLICATION
2444
if (thd->slave_thread &&
2445
(!rpl_filter->db_ok(lex->name.str) ||
2446
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2448
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2452
if (thd->locked_tables || thd->active_transaction())
2454
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2455
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2458
res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
2461
case SQLCOM_ALTER_DB_UPGRADE:
2463
LEX_STRING *db= & lex->name;
2464
if (end_active_trans(thd))
2469
#ifdef HAVE_REPLICATION
2470
if (thd->slave_thread &&
2471
(!rpl_filter->db_ok(db->str) ||
2472
!rpl_filter->db_ok_with_wild_table(db->str)))
2475
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2479
if (check_db_name(db))
2481
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2484
if (thd->locked_tables || thd->active_transaction())
2487
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2488
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2492
res= mysql_upgrade_db(thd, db);
2497
case SQLCOM_ALTER_DB:
2499
LEX_STRING *db= &lex->name;
2500
HA_CREATE_INFO create_info(lex->create_info);
2501
if (check_db_name(db))
2503
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2507
If in a slave thread :
2508
ALTER DATABASE DB may not be preceded by USE DB.
2509
For that reason, maybe db_ok() in sql/slave.cc did not check the
2510
do_db/ignore_db. And as this query involves no tables, tables_ok()
2511
above was not called. So we have to check rules again here.
2513
#ifdef HAVE_REPLICATION
2514
if (thd->slave_thread &&
2515
(!rpl_filter->db_ok(db->str) ||
2516
!rpl_filter->db_ok_with_wild_table(db->str)))
2518
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2522
if (thd->locked_tables || thd->active_transaction())
2524
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2525
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2528
res= mysql_alter_db(thd, db->str, &create_info);
2531
case SQLCOM_SHOW_CREATE_DB:
2533
DBUG_EXECUTE_IF("4x_server_emul",
2534
my_error(ER_UNKNOWN_ERROR, MYF(0)); goto error;);
2535
if (check_db_name(&lex->name))
2537
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2540
res= mysqld_show_create_db(thd, lex->name.str, &lex->create_info);
2545
RESET commands are never written to the binary log, so we have to
2546
initialize this variable because RESET shares the same code as FLUSH
2548
lex->no_write_to_binlog= 1;
2551
bool write_to_binlog;
2554
reload_cache() will tell us if we are allowed to write to the
2557
if (!reload_cache(thd, lex->type, first_table, &write_to_binlog))
2560
We WANT to write and we CAN write.
2561
! we write after unlocking the table.
2564
Presumably, RESET and binlog writing doesn't require synchronization
2566
if (!lex->no_write_to_binlog && write_to_binlog)
2568
write_bin_log(thd, false, thd->query, thd->query_length);
2577
Item *it= (Item *)lex->value_list.head();
2579
if (lex->table_or_sp_used())
2581
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Usage of subqueries or stored "
2582
"function calls as part of this statement");
2586
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
2588
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2592
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2596
if (thd->transaction.xid_state.xa_state != XA_NOTR)
2598
my_error(ER_XAER_RMFAIL, MYF(0),
2599
xa_state_names[thd->transaction.xid_state.xa_state]);
2603
Breakpoints for backup testing.
2605
if (begin_trans(thd))
2610
if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
2611
lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2615
case SQLCOM_ROLLBACK:
2616
if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
2617
lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2621
case SQLCOM_RELEASE_SAVEPOINT:
2624
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2626
if (my_strnncoll(system_charset_info,
2627
(uchar *)lex->ident.str, lex->ident.length,
2628
(uchar *)sv->name, sv->length) == 0)
2633
if (ha_release_savepoint(thd, sv))
2634
res= true; // cannot happen
2637
thd->transaction.savepoints=sv->prev;
2640
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2643
case SQLCOM_ROLLBACK_TO_SAVEPOINT:
2646
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2648
if (my_strnncoll(system_charset_info,
2649
(uchar *)lex->ident.str, lex->ident.length,
2650
(uchar *)sv->name, sv->length) == 0)
2655
if (ha_rollback_to_savepoint(thd, sv))
2656
res= true; // cannot happen
2659
if (((thd->options & OPTION_KEEP_LOG) ||
2660
thd->transaction.all.modified_non_trans_table) &&
2662
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2663
ER_WARNING_NOT_COMPLETE_ROLLBACK,
2664
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2667
thd->transaction.savepoints=sv;
2670
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2673
case SQLCOM_SAVEPOINT:
2674
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) ||
2675
thd->in_sub_stmt) || !opt_using_transactions)
2679
SAVEPOINT **sv, *newsv;
2680
for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
2682
if (my_strnncoll(system_charset_info,
2683
(uchar *)lex->ident.str, lex->ident.length,
2684
(uchar *)(*sv)->name, (*sv)->length) == 0)
2687
if (*sv) /* old savepoint of the same name exists */
2690
ha_release_savepoint(thd, *sv); // it cannot fail
2693
else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
2694
savepoint_alloc_size)) == 0)
2696
my_error(ER_OUT_OF_RESOURCES, MYF(0));
2699
newsv->name=strmake_root(&thd->transaction.mem_root,
2700
lex->ident.str, lex->ident.length);
2701
newsv->length=lex->ident.length;
2703
if we'll get an error here, don't add new savepoint to the list.
2704
we'll lose a little bit of memory in transaction mem_root, but it'll
2705
be free'd when transaction ends anyway
2707
if (ha_savepoint(thd, newsv))
2711
newsv->prev=thd->transaction.savepoints;
2712
thd->transaction.savepoints=newsv;
2717
case SQLCOM_BINLOG_BASE64_EVENT:
2719
mysql_client_binlog_statement(thd);
2723
DBUG_ASSERT(0); /* Impossible */
2727
thd_proc_info(thd, "query end");
2730
Binlog-related cleanup:
2731
Reset system variables temporarily modified by SET ONE SHOT.
2733
Exception: If this is a SET, do nothing. This is to allow
2734
mysqlbinlog to print many SET commands (in this case we want the
2735
charset temp setting to live until the real query). This is also
2736
needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2739
if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2740
reset_one_shot_variables(thd);
482
2743
The return value for ROW_COUNT() is "implementation dependent" if the
483
2744
statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
484
2745
wants. We also keep the last value in case of SQLCOM_CALL or
487
if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
2748
if (!(sql_command_flags[lex->sql_command] & CF_HAS_ROW_COUNT))
2749
thd->row_count_func= -1;
2757
if (need_start_waiting)
489
session->row_count_func= -1;
2760
Release the protection against the global read lock and wake
2761
everyone, who might want to set a global read lock.
2763
start_waiting_global_read_lock(thd);
492
return (res || session->is_error());
2765
DBUG_RETURN(res || thd->is_error());
494
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2769
static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables)
496
LEX *lex= session->lex;
497
2772
select_result *result=lex->result;
499
2774
/* assign global limit variable if limit is not given */
501
Select_Lex *param= lex->unit.global_parameters;
2776
SELECT_LEX *param= lex->unit.global_parameters;
502
2777
if (!param->explicit_limit)
503
2778
param->select_limit=
504
new Item_int((uint64_t) session->variables.select_limit);
2779
new Item_int((ulonglong) thd->variables.select_limit);
506
if (not (res= session->openTablesLock(all_tables)))
2781
if (!(res= open_and_lock_tables(thd, all_tables)))
508
2783
if (lex->describe)