-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathsql_class.cc
8885 lines (7656 loc) · 265 KB
/
sql_class.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2000, 2015, Oracle and/or its affiliates.
Copyright (c) 2008, 2024, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA
*/
/*****************************************************************************
**
** This file implements classes defined in sql_class.h
** Especially the classes to handle a result from a select
**
*****************************************************************************/
#include "mariadb.h"
#include "sql_priv.h"
#include "sql_class.h"
#include "sql_cache.h" // query_cache_abort
#include "sql_base.h" // close_thread_tables
#include "sql_time.h" // date_time_format_copy
#include "tztime.h" // MYSQL_TIME <-> my_time_t
#include "sql_acl.h" // NO_ACL,
// acl_getroot_no_password
#include "sql_base.h"
#include "sql_handler.h" // mysql_ha_cleanup
#include "rpl_rli.h"
#include "rpl_filter.h"
#include "rpl_record.h"
#include "slave.h"
#include <my_bitmap.h>
#include "log_event.h"
#include "sql_audit.h"
#include <m_ctype.h>
#include <sys/stat.h>
#include <mysys_err.h>
#include <limits.h>
#include "sp_head.h"
#include "sp_rcontext.h"
#include "sp_cache.h"
#include "sql_show.h" // append_identifier
#include "sql_db.h" // get_default_db_collation
#include "transaction.h"
#include "sql_select.h" /* declares create_tmp_table() */
#include "debug_sync.h"
#include "sql_parse.h" // is_update_query
#include "sql_callback.h"
#include "lock.h"
#include "wsrep_mysqld.h"
#include "sql_connect.h"
#ifdef WITH_WSREP
#include "wsrep_thd.h"
#include "wsrep_trans_observer.h"
#include "wsrep_server_state.h"
#endif /* WITH_WSREP */
#include "opt_trace.h"
#include <mysql/psi/mysql_transaction.h>
#ifdef HAVE_SYS_SYSCALL_H
#include <sys/syscall.h>
#endif
/*
The following is used to initialise Table_ident with a internal
table name
*/
char internal_table_name[2]= "*";
char empty_c_string[1]= {0}; /* used for not defined db */
/****************************************************************************
** User variables
****************************************************************************/
extern "C" const uchar *get_var_key(const void *entry_, size_t *length,
my_bool)
{
auto entry= static_cast<const user_var_entry *>(entry_);
*length= entry->name.length;
return reinterpret_cast<const uchar *>(entry->name.str);
}
extern "C" void free_user_var(void *entry_)
{
user_var_entry *entry= static_cast<user_var_entry *>(entry_);
char *pos= (char*) entry+ALIGN_SIZE(sizeof(*entry));
if (entry->value && entry->value != pos)
my_free(entry->value);
my_free(entry);
}
/* Functions for last-value-from-sequence hash */
extern "C" const uchar *get_sequence_last_key(const void *entry_,
size_t *length, my_bool)
{
auto *entry= static_cast<const SEQUENCE_LAST_VALUE *>(entry_);
*length= entry->length;
return entry->key;
}
extern "C" void free_sequence_last(void *entry)
{
delete static_cast<SEQUENCE_LAST_VALUE *>(entry);
}
bool Key_part_spec::operator==(const Key_part_spec& other) const
{
return length == other.length &&
field_name.streq(other.field_name);
}
bool Key_part_spec::check_key_for_blob(const handler *file) const
{
if (!(file->ha_table_flags() & HA_CAN_INDEX_BLOBS))
{
my_error(ER_BLOB_USED_AS_KEY, MYF(0), field_name.str, file->table_type());
return true;
}
return false;
}
bool Key_part_spec::check_key_length_for_blob() const
{
if (!length)
{
my_error(ER_BLOB_KEY_WITHOUT_LENGTH, MYF(0), field_name.str);
return true;
}
return false;
}
bool Key_part_spec::init_multiple_key_for_blob(const handler *file)
{
if (check_key_for_blob(file))
return true;
if (!length)
length= file->max_key_length() + 1;
return false;
}
/**
Construct an (almost) deep copy of this key. Only those
elements that are known to never change are not copied.
If out of memory, a partial copy is returned and an error is set
in THD.
*/
Key::Key(const Key &rhs, MEM_ROOT *mem_root)
:DDL_options(rhs),type(rhs.type),
key_create_info(rhs.key_create_info),
columns(rhs.columns, mem_root),
name(rhs.name),
option_list(rhs.option_list),
generated(rhs.generated), invisible(false),
without_overlaps(rhs.without_overlaps), old(rhs.old), length(rhs.length),
period(rhs.period)
{
list_copy_and_replace_each_value(columns, mem_root);
}
/**
Construct an (almost) deep copy of this foreign key. Only those
elements that are known to never change are not copied.
If out of memory, a partial copy is returned and an error is set
in THD.
*/
Foreign_key::Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root)
:Key(rhs,mem_root),
constraint_name(rhs.constraint_name),
ref_db(rhs.ref_db),
ref_table(rhs.ref_table),
ref_columns(rhs.ref_columns,mem_root),
delete_opt(rhs.delete_opt),
update_opt(rhs.update_opt),
match_opt(rhs.match_opt)
{
list_copy_and_replace_each_value(ref_columns, mem_root);
}
/*
Test if a foreign key (= generated key) is a prefix of the given key
(ignoring key name and type, but minding the algorithm)
NOTES:
This is only used to test if an index for a FOREIGN KEY exists
IMPLEMENTATION
We only compare field names
RETURN
true Generated key is a prefix of other key
false Not a prefix
*/
bool is_foreign_key_prefix(Key *a, Key *b)
{
ha_key_alg a_alg= a->key_create_info.algorithm;
ha_key_alg b_alg= b->key_create_info.algorithm;
// The real algorithm in InnoDB will be BTREE if none was given by user.
a_alg= a_alg == HA_KEY_ALG_UNDEF ? HA_KEY_ALG_BTREE : a_alg;
b_alg= b_alg == HA_KEY_ALG_UNDEF ? HA_KEY_ALG_BTREE : b_alg;
if (a_alg != b_alg)
return false;
/* Ensure that 'a' is the generated key */
if (a->generated)
{
if (b->generated && a->columns.elements > b->columns.elements)
swap_variables(Key*, a, b); // Put shorter key in 'a'
}
else
{
if (!b->generated)
return false; // No foreign key
swap_variables(Key*, a, b); // Put generated key in 'a'
}
/* Test if 'a' is a prefix of 'b' */
if (a->columns.elements > b->columns.elements)
return false; // Can't be prefix
List_iterator<Key_part_spec> col_it1(a->columns);
List_iterator<Key_part_spec> col_it2(b->columns);
const Key_part_spec *col1, *col2;
#ifdef ENABLE_WHEN_INNODB_CAN_HANDLE_SWAPED_FOREIGN_KEY_COLUMNS
while ((col1= col_it1++))
{
bool found= 0;
col_it2.rewind();
while ((col2= col_it2++))
{
if (*col1 == *col2)
{
found= TRUE;
break;
}
}
if (!found)
return false; // Error
}
return true; // Is prefix
#else
while ((col1= col_it1++))
{
col2= col_it2++;
if (!(*col1 == *col2))
return false;
}
return true; // Is prefix
#endif
}
/*
@brief
Check if the foreign key options are compatible with the specification
of the columns on which the key is created
@retval
FALSE The foreign key options are compatible with key columns
@retval
TRUE Otherwise
*/
bool Foreign_key::validate(List<Create_field> &table_fields)
{
Create_field *sql_field;
Key_part_spec *column;
List_iterator<Key_part_spec> cols(columns);
List_iterator<Create_field> it(table_fields);
DBUG_ENTER("Foreign_key::validate");
if (old)
DBUG_RETURN(FALSE); // must be good
while ((column= cols++))
{
it.rewind();
while ((sql_field= it++) &&
!sql_field->field_name.streq(column->field_name))
{ }
if (!sql_field)
{
my_error(ER_KEY_COLUMN_DOES_NOT_EXIST, MYF(0), column->field_name.str);
DBUG_RETURN(TRUE);
}
if (type == Key::FOREIGN_KEY && sql_field->vcol_info)
{
if (delete_opt == FK_OPTION_SET_NULL)
{
my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
"ON DELETE SET NULL");
DBUG_RETURN(TRUE);
}
if (update_opt == FK_OPTION_SET_NULL)
{
my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
"ON UPDATE SET NULL");
DBUG_RETURN(TRUE);
}
if (update_opt == FK_OPTION_CASCADE)
{
my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
"ON UPDATE CASCADE");
DBUG_RETURN(TRUE);
}
}
}
DBUG_RETURN(FALSE);
}
/****************************************************************************
** Thread specific functions
****************************************************************************/
extern "C" unsigned long long thd_query_id(const MYSQL_THD thd)
{
return((unsigned long long)thd->query_id);
}
/**
Get thread attributes for connection threads
@retval Reference to thread attribute for connection threads
*/
pthread_attr_t *get_connection_attrib(void)
{
return &connection_attrib;
}
/**
Get max number of connections
@retval Max number of connections for MySQL Server
*/
ulong get_max_connections(void)
{
return max_connections;
}
/*
The following functions form part of the C plugin API
*/
extern "C" int mysql_tmpfile(const char *prefix)
{
char filename[FN_REFLEN];
File fd= create_temp_file(filename, mysql_tmpdir, prefix,
O_BINARY | O_SEQUENTIAL,
MYF(MY_WME | MY_TEMPORARY));
return fd;
}
extern "C"
int thd_in_lock_tables(const THD *thd)
{
return MY_TEST(thd->in_lock_tables);
}
extern "C"
int thd_tablespace_op(const THD *thd)
{
return MY_TEST(thd->tablespace_op);
}
extern "C"
const char *set_thd_proc_info(THD *thd_arg, const char *info,
const char *calling_function,
const char *calling_file,
const unsigned int calling_line)
{
PSI_stage_info old_stage;
PSI_stage_info new_stage;
new_stage.m_key= 0;
new_stage.m_name= info;
set_thd_stage_info(thd_arg, & new_stage, & old_stage,
calling_function, calling_file, calling_line);
return old_stage.m_name;
}
extern "C"
void set_thd_stage_info(void *thd_arg,
const PSI_stage_info *new_stage,
PSI_stage_info *old_stage,
const char *calling_func,
const char *calling_file,
const unsigned int calling_line)
{
THD *thd= (THD*) thd_arg;
if (thd == NULL)
thd= current_thd;
if (old_stage)
thd->backup_stage(old_stage);
if (new_stage)
thd->enter_stage(new_stage, calling_func, calling_file, calling_line);
}
void thd_enter_cond(MYSQL_THD thd, mysql_cond_t *cond, mysql_mutex_t *mutex,
const PSI_stage_info *stage, PSI_stage_info *old_stage,
const char *src_function, const char *src_file,
int src_line)
{
if (!thd)
thd= current_thd;
return thd->enter_cond(cond, mutex, stage, old_stage, src_function, src_file,
src_line);
}
void thd_exit_cond(MYSQL_THD thd, const PSI_stage_info *stage,
const char *src_function, const char *src_file,
int src_line)
{
if (!thd)
thd= current_thd;
thd->exit_cond(stage, src_function, src_file, src_line);
return;
}
extern "C"
void thd_storage_lock_wait(THD *thd, long long value)
{
thd->utime_after_lock+= value;
}
/**
Provide a handler data getter to simplify coding
*/
extern "C"
void *thd_get_ha_data(const THD *thd, const struct transaction_participant *hton)
{
DBUG_ASSERT(thd == current_thd || mysql_mutex_is_owner(&thd->LOCK_thd_data));
return thd->ha_data[hton->slot].ha_ptr;
}
/**
Provide a handler data setter to simplify coding
@see thd_set_ha_data() definition in plugin.h
*/
extern "C"
void thd_set_ha_data(THD *thd, const struct transaction_participant *hton,
const void *ha_data)
{
plugin_ref *lock= &thd->ha_data[hton->slot].lock;
mysql_mutex_lock(&thd->LOCK_thd_data);
thd->ha_data[hton->slot].ha_ptr= const_cast<void*>(ha_data);
mysql_mutex_unlock(&thd->LOCK_thd_data);
if (ha_data && !*lock)
*lock= ha_lock_engine(NULL, (handlerton*) hton);
else if (!ha_data && *lock)
{
plugin_unlock(NULL, *lock);
*lock= NULL;
}
}
/**
Allow storage engine to wakeup commits waiting in THD::wait_for_prior_commit.
@see thd_wakeup_subsequent_commits() definition in plugin.h
*/
extern "C"
void thd_wakeup_subsequent_commits(THD *thd, int wakeup_error)
{
thd->wakeup_subsequent_commits(wakeup_error);
}
extern "C"
long long thd_test_options(const THD *thd, long long test_options)
{
return thd->variables.option_bits & test_options;
}
extern "C"
int thd_sql_command(const THD *thd)
{
return (int) thd->lex->sql_command;
}
/*
Returns options used with DDL's, like IF EXISTS etc...
Will returns 'nonsense' if the command was not a DDL.
*/
extern "C"
struct DDL_options_st *thd_ddl_options(const THD *thd)
{
return &thd->lex->create_info;
}
extern "C"
int thd_tx_isolation(const THD *thd)
{
return (int) thd->tx_isolation;
}
extern "C"
int thd_tx_is_read_only(const THD *thd)
{
return (int) thd->tx_read_only;
}
extern "C"
{ /* Functions for thd_error_context_service */
const char *thd_get_error_message(const THD *thd)
{
return thd->get_stmt_da()->message();
}
uint thd_get_error_number(const THD *thd)
{
return thd->get_stmt_da()->sql_errno();
}
ulong thd_get_error_row(const THD *thd)
{
return thd->get_stmt_da()->current_row_for_warning();
}
void thd_inc_error_row(THD *thd)
{
thd->get_stmt_da()->inc_current_row_for_warning();
}
}
#if MARIA_PLUGIN_INTERFACE_VERSION < 0x0200
/**
TODO: This function is for API compatibility, remove it eventually.
All engines should switch to use thd_get_error_context_description()
plugin service function.
*/
extern "C"
char *thd_security_context(THD *thd,
char *buffer, unsigned int length,
unsigned int max_query_len)
{
return thd_get_error_context_description(thd, buffer, length, max_query_len);
}
#endif
/**
Implementation of Drop_table_error_handler::handle_condition().
The reason in having this implementation is to silence technical low-level
warnings during DROP TABLE operation. Currently we don't want to expose
the following warnings during DROP TABLE:
- Some of table files are missed or invalid (the table is going to be
deleted anyway, so why bother that something was missed);
- A trigger associated with the table does not have DEFINER (One of the
MySQL specifics now is that triggers are loaded for the table being
dropped. So, we may have a warning that trigger does not have DEFINER
attribute during DROP TABLE operation).
@return TRUE if the condition is handled.
*/
bool Drop_table_error_handler::handle_condition(THD *thd,
uint sql_errno,
const char* sqlstate,
Sql_condition::enum_warning_level *level,
const char* msg,
Sql_condition ** cond_hdl)
{
*cond_hdl= NULL;
return ((sql_errno == EE_DELETE && my_errno == ENOENT) ||
sql_errno == ER_TRG_NO_DEFINER);
}
/**
Handle an error from MDL_context::upgrade_lock() and mysql_lock_tables().
Ignore ER_LOCK_ABORTED and ER_LOCK_DEADLOCK errors.
*/
bool
MDL_deadlock_and_lock_abort_error_handler::
handle_condition(THD *thd,
uint sql_errno,
const char *sqlstate,
Sql_condition::enum_warning_level *level,
const char* msg,
Sql_condition **cond_hdl)
{
*cond_hdl= NULL;
if (sql_errno == ER_LOCK_ABORTED || sql_errno == ER_LOCK_DEADLOCK)
m_need_reopen= true;
return m_need_reopen;
}
/**
Send timeout to thread.
Note that this is always safe as the thread will always remove it's
timeouts at end of query (and thus before THD is destroyed)
*/
extern "C" void thd_kill_timeout(void *thd_)
{
THD *thd= static_cast<THD *>(thd_);
thd->status_var.max_statement_time_exceeded++;
/* Kill queries that can't cause data corruptions */
thd->awake(KILL_TIMEOUT);
}
const char *thd_where(THD *thd)
{
switch(thd->where) {
case THD_WHERE::CHECKING_TRANSFORMED_SUBQUERY:
case THD_WHERE::IN_ALL_ANY_SUBQUERY:
return "IN/ALL/ANY";
case THD_WHERE::JSON_TABLE_ARGUMENT:
return "JSON_TABLE";
case THD_WHERE::DEFAULT_WHERE:
return "SELECT";
case THD_WHERE::FIELD_LIST:
case THD_WHERE::PARTITION_FUNCTION:
return "PARTITION BY";
case THD_WHERE::FROM_CLAUSE:
return "FROM";
case THD_WHERE::ON_CLAUSE:
return "ON";
case THD_WHERE::WHERE_CLAUSE:
return "WHERE";
case THD_WHERE::SET_LIST:
return "SET";
case THD_WHERE::INSERT_LIST:
return "INSERT INTO";
case THD_WHERE::RETURNING:
return "RETURNING";
case THD_WHERE::UPDATE_CLAUSE:
return "UPDATE";
case THD_WHERE::VALUES_CLAUSE:
return "VALUES";
case THD_WHERE::FOR_SYSTEM_TIME:
return "FOR SYSTEM_TIME";
case THD_WHERE::ORDER_CLAUSE:
return "ORDER BY";
case THD_WHERE::HAVING_CLAUSE:
return "HAVING";
case THD_WHERE::GROUP_STATEMENT:
return "GROUP BY";
case THD_WHERE::PROCEDURE_LIST:
return "PROCEDURE";
case THD_WHERE::CHECK_OPTION:
return "CHECK OPTION";
case THD_WHERE::DO_STATEMENT:
return "DO";
case THD_WHERE::HANDLER_STATEMENT:
return "HANDLER ... READ";
case THD_WHERE::USE_WHERE_STRING:
return thd->where_str;
default:
break; // "fall-through" to default return below
};
DBUG_ASSERT(false);
return "UNKNOWN";
}
THD::THD(my_thread_id id, bool is_wsrep_applier)
:Statement(&main_lex, &main_mem_root, STMT_CONVENTIONAL_EXECUTION,
/* statement id */ 0),
rli_fake(0), rgi_fake(0), rgi_slave(NULL),
protocol_text(this), protocol_binary(this), initial_status_var(0),
m_current_stage_key(0), m_psi(0),
in_sub_stmt(0), log_all_errors(0),
binlog_unsafe_warning_flags(0),
current_stmt_binlog_format(BINLOG_FORMAT_MIXED),
bulk_param(0),
table_map_for_update(0),
m_sent_row_count(0),
sent_row_count_for_statement(0),
m_examined_row_count(0),
examined_row_count_for_statement(0),
accessed_rows_and_keys(0),
m_digest(NULL),
m_statement_psi(NULL),
m_transaction_psi(NULL),
m_idle_psi(NULL),
col_access(NO_ACL),
thread_id(id),
thread_dbug_id(id),
os_thread_id(0),
global_disable_checkpoint(0),
current_backup_stage(BACKUP_FINISHED),
failed_com_change_user(0),
is_fatal_error(0),
transaction_rollback_request(0),
is_fatal_sub_stmt_error(false),
in_lock_tables(0),
bootstrap(0),
derived_tables_processing(FALSE),
waiting_on_group_commit(FALSE), has_waiter(FALSE),
last_sql_command(SQLCOM_END), spcont(NULL),
m_parser_state(NULL),
#ifndef EMBEDDED_LIBRARY
audit_plugin_version(-1),
#endif
#if defined(ENABLED_DEBUG_SYNC)
debug_sync_control(0),
#endif /* defined(ENABLED_DEBUG_SYNC) */
wait_for_commit_ptr(0),
m_internal_handler(0),
main_da(0, false, false),
m_stmt_da(&main_da),
tdc_hash_pins(0),
xid_hash_pins(0),
m_tmp_tables_locked(false),
async_state()
#ifdef HAVE_REPLICATION
,
current_linfo(0),
slave_info(0)
#endif
#ifdef WITH_WSREP
,
wsrep_applier(is_wsrep_applier),
wsrep_applier_closing(false),
wsrep_client_thread(false),
wsrep_retry_counter(0),
wsrep_PA_safe(true),
wsrep_retry_query(NULL),
wsrep_retry_query_len(0),
wsrep_retry_command(COM_CONNECT),
wsrep_consistency_check(NO_CONSISTENCY_CHECK),
wsrep_mysql_replicated(0),
wsrep_TOI_pre_query(NULL),
wsrep_TOI_pre_query_len(0),
wsrep_po_handle(WSREP_PO_INITIALIZER),
wsrep_po_cnt(0),
wsrep_apply_format(0),
wsrep_rbr_buf(NULL),
wsrep_sync_wait_gtid(WSREP_GTID_UNDEFINED),
wsrep_last_written_gtid_seqno(0),
wsrep_current_gtid_seqno(0),
wsrep_affected_rows(0),
wsrep_has_ignored_error(false),
wsrep_was_on(false),
wsrep_ignore_table(false),
wsrep_aborter(0),
wsrep_delayed_BF_abort(false),
wsrep_ctas(false),
/* wsrep-lib */
m_wsrep_next_trx_id(WSREP_UNDEFINED_TRX_ID),
m_wsrep_mutex(&LOCK_thd_data),
m_wsrep_cond(&COND_wsrep_thd),
m_wsrep_client_service(this, m_wsrep_client_state),
m_wsrep_client_state(this,
m_wsrep_mutex,
m_wsrep_cond,
Wsrep_server_state::instance(),
m_wsrep_client_service,
wsrep::client_id(thread_id)),
wsrep_applier_service(NULL),
wsrep_wfc()
#endif /*WITH_WSREP */
{
bzero(&variables, sizeof(variables));
/*
We set THR_THD to temporally point to this THD to register all the
variables that allocates memory for this THD
*/
THD *old_THR_THD= current_thd;
set_current_thd(this);
status_var.local_memory_used= sizeof(THD);
status_var.max_local_memory_used= status_var.local_memory_used;
status_var.global_memory_used= 0;
variables.pseudo_thread_id= thread_id;
variables.max_mem_used= global_system_variables.max_mem_used;
main_da.init();
mdl_context.init(this);
mdl_backup_lock= 0;
/*
Pass nominal parameters to init_alloc_root only to ensure that
the destructor works OK in case of an error. The main_mem_root
will be re-initialized in init_for_queries().
The base one will mainly be use to allocate memory during authentication.
*/
init_sql_alloc(key_memory_thd_main_mem_root,
&main_mem_root, DEFAULT_ROOT_BLOCK_SIZE, 0,
MYF(MY_THREAD_SPECIFIC));
/*
Allocation of user variables for binary logging is always done with main
mem root
*/
user_var_events_alloc= mem_root;
stmt_arena= this;
thread_stack= 0;
scheduler= thread_scheduler; // Will be fixed later
event_scheduler.data= 0;
skip_wait_timeout= false;
catalog= (char*)"std"; // the only catalog we have for now
main_security_ctx.init();
security_ctx= &main_security_ctx;
no_errors= 0;
password= 0;
count_cuted_fields= CHECK_FIELD_IGNORE;
killed= NOT_KILLED;
killed_err= 0;
is_slave_error= FALSE;
my_hash_clear(&handler_tables_hash);
my_hash_clear(&ull_hash);
tmp_table=0;
cuted_fields= 0L;
limit_found_rows= 0;
m_row_count_func= -1;
statement_id_counter= 0UL;
// Must be reset to handle error with THD's created for init of mysqld
lex->current_select= 0;
start_utime= utime_after_query= 0;
system_time.start.val= system_time.sec= system_time.sec_part= 0;
utime_after_lock= 0L;
progress.arena= 0;
progress.report_to_client= 0;
progress.max_counter= 0;
slave_thread = 0;
connection_name.str= 0;
connection_name.length= 0;
file_id = 0;
query_id= 0;
query_name_consts= 0;
semisync_info= 0;
#ifndef DBUG_OFF
expected_semi_sync_offs= 0;
#endif
db_charset= global_system_variables.collation_database;
bzero((void*) ha_data, sizeof(ha_data));
mysys_var=0;
binlog_evt_union.do_union= FALSE;
binlog_table_maps= FALSE;
binlog_xid= 0;
enable_slow_log= 0;
durability_property= HA_REGULAR_DURABILITY;
#ifdef DBUG_ASSERT_EXISTS
dbug_sentry=THD_SENTRY_MAGIC;
#endif
mysql_audit_init_thd(this);
net.vio=0;
net.buff= 0;
net.reading_or_writing= 0;
client_capabilities= 0; // minimalistic client
system_thread= NON_SYSTEM_THREAD;
shared_thd= 0;
cleanup_done= free_connection_done= abort_on_warning= got_warning= 0;
peer_port= 0; // For SHOW PROCESSLIST
transaction= &default_transaction;
transaction->m_pending_rows_event= 0;
transaction->on= 1;
wt_thd_lazy_init(&transaction->wt,
&variables.wt_deadlock_search_depth_short,
&variables.wt_timeout_short,
&variables.wt_deadlock_search_depth_long,
&variables.wt_timeout_long);
#ifdef SIGNAL_WITH_VIO_CLOSE
active_vio = 0;
#endif
mysql_mutex_init(key_LOCK_thd_data, &LOCK_thd_data, MY_MUTEX_INIT_FAST);
mysql_mutex_init(key_LOCK_wakeup_ready, &LOCK_wakeup_ready, MY_MUTEX_INIT_FAST);
mysql_mutex_init(key_LOCK_thd_kill, &LOCK_thd_kill, MY_MUTEX_INIT_FAST);
mysql_cond_init(key_COND_wakeup_ready, &COND_wakeup_ready, 0);
mysql_mutex_record_order(&LOCK_thd_kill, &LOCK_thd_data);
/* Variables with default values */
proc_info="login";
where= THD_WHERE::DEFAULT_WHERE;
slave_net = 0;
m_command=COM_CONNECT;
*scramble= '\0';
#ifdef WITH_WSREP
mysql_cond_init(key_COND_wsrep_thd, &COND_wsrep_thd, NULL);
wsrep_info[sizeof(wsrep_info) - 1] = '\0'; /* make sure it is 0-terminated */
#endif
/* Call to init() below requires fully initialized Open_tables_state. */
reset_open_tables_state();
init();
debug_sync_init_thread(this);
#if defined(ENABLED_PROFILING)
profiling.set_thd(this);
#endif
user_connect=(USER_CONN *)0;
my_hash_init(key_memory_user_var_entry, &user_vars,
Lex_ident_user_var::charset_info(),
USER_VARS_HASH_SIZE, 0, 0, get_var_key, free_user_var,
HASH_THREAD_SPECIFIC);
my_hash_init(PSI_INSTRUMENT_ME, &sequences, Lex_ident_fs::charset_info(),
SEQUENCES_HASH_SIZE, 0, 0, get_sequence_last_key,
free_sequence_last, HASH_THREAD_SPECIFIC);
/* For user vars replication*/
if (opt_bin_log)
my_init_dynamic_array(key_memory_user_var_entry, &user_var_events,
sizeof(BINLOG_USER_VAR_EVENT *), 16, 16, MYF(0));
else
bzero((char*) &user_var_events, sizeof(user_var_events));
/* Protocol */
protocol= &protocol_text; // Default protocol
protocol_text.init(this);
protocol_binary.init(this);
thr_timer_init(&query_timer, (void (*)(void*)) thd_kill_timeout, this);
tablespace_op=FALSE;
substitute_null_with_insert_id = FALSE;
lock_info.mysql_thd= (void *)this;
m_token_array= NULL;
if (max_digest_length > 0)
{
m_token_array= (unsigned char*) my_malloc(PSI_INSTRUMENT_ME,
max_digest_length,
MYF(MY_WME|MY_THREAD_SPECIFIC));
}
m_binlog_invoker= INVOKER_NONE;
invoker.init();
prepare_derived_at_open= FALSE;
create_tmp_table_for_derived= FALSE;
save_prep_leaf_list= FALSE;
reset_sp_cache= false;
org_charset= 0;
/* Restore THR_THD */
set_current_thd(old_THR_THD);
}
void THD::push_internal_handler(Internal_error_handler *handler)
{
DBUG_ENTER("THD::push_internal_handler");
if (m_internal_handler)
{
handler->m_prev_internal_handler= m_internal_handler;
m_internal_handler= handler;
}
else
{
m_internal_handler= handler;
}
DBUG_VOID_RETURN;
}
bool THD::handle_condition(uint sql_errno,
const char* sqlstate,
Sql_condition::enum_warning_level *level,
const char* msg,
Sql_condition ** cond_hdl)
{
if (!m_internal_handler)
{
*cond_hdl= NULL;
return FALSE;
}
for (Internal_error_handler *error_handler= m_internal_handler;
error_handler;
error_handler= error_handler->m_prev_internal_handler)