1 /***************************************************************************** 2 3 Copyright (c) 1996, 2012, Oracle and/or its affiliates. All Rights Reserved. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License, version 2.0, 7 as published by the Free Software Foundation. 8 9 This program is also distributed with certain software (including 10 but not limited to OpenSSL) that is licensed under separate terms, 11 as designated in a particular file or component or in included license 12 documentation. The authors of MySQL hereby grant you an additional 13 permission to link the program and your derivative works with the 14 separately licensed software that they have included with MySQL. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License, version 2.0, for more details. 20 21 You should have received a copy of the GNU General Public License along with 22 this program; if not, write to the Free Software Foundation, Inc., 23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA 24 25 *****************************************************************************/ 26 27 /**************************************************//** 28 @file include/que0que.h 29 Query graph 30 31 Created 5/27/1996 Heikki Tuuri 32 *******************************************************/ 33 34 #ifndef que0que_h 35 #define que0que_h 36 37 #include "univ.i" 38 #include "data0data.h" 39 #include "dict0types.h" 40 #include "trx0trx.h" 41 #include "trx0roll.h" 42 #include "srv0srv.h" 43 #include "usr0types.h" 44 #include "que0types.h" 45 #include "row0types.h" 46 #include "pars0types.h" 47 48 /* If the following flag is set TRUE, the module will print trace info 49 of SQL execution in the UNIV_SQL_DEBUG version */ 50 extern ibool que_trace_on; 51 52 /** Mutex protecting the query threads. */ 53 extern ib_mutex_t que_thr_mutex; 54 55 /***********************************************************************//** 56 Creates a query graph fork node. 57 @return own: fork node */ 58 UNIV_INTERN 59 que_fork_t* 60 que_fork_create( 61 /*============*/ 62 que_t* graph, /*!< in: graph, if NULL then this 63 fork node is assumed to be the 64 graph root */ 65 que_node_t* parent, /*!< in: parent node */ 66 ulint fork_type, /*!< in: fork type */ 67 mem_heap_t* heap); /*!< in: memory heap where created */ 68 /***********************************************************************//** 69 Gets the first thr in a fork. */ 70 UNIV_INLINE 71 que_thr_t* 72 que_fork_get_first_thr( 73 /*===================*/ 74 que_fork_t* fork); /*!< in: query fork */ 75 /***********************************************************************//** 76 Gets the child node of the first thr in a fork. */ 77 UNIV_INLINE 78 que_node_t* 79 que_fork_get_child( 80 /*===============*/ 81 que_fork_t* fork); /*!< in: query fork */ 82 /***********************************************************************//** 83 Sets the parent of a graph node. */ 84 UNIV_INLINE 85 void 86 que_node_set_parent( 87 /*================*/ 88 que_node_t* node, /*!< in: graph node */ 89 que_node_t* parent);/*!< in: parent */ 90 /***********************************************************************//** 91 Creates a query graph thread node. 92 @return own: query thread node */ 93 UNIV_INTERN 94 que_thr_t* 95 que_thr_create( 96 /*===========*/ 97 que_fork_t* parent, /*!< in: parent node, i.e., a fork node */ 98 mem_heap_t* heap); /*!< in: memory heap where created */ 99 /**********************************************************************//** 100 Frees a query graph, but not the heap where it was created. Does not free 101 explicit cursor declarations, they are freed in que_graph_free. */ 102 UNIV_INTERN 103 void 104 que_graph_free_recursive( 105 /*=====================*/ 106 que_node_t* node); /*!< in: query graph node */ 107 /**********************************************************************//** 108 Frees a query graph. */ 109 UNIV_INTERN 110 void 111 que_graph_free( 112 /*===========*/ 113 que_t* graph); /*!< in: query graph; we assume that the memory 114 heap where this graph was created is private 115 to this graph: if not, then use 116 que_graph_free_recursive and free the heap 117 afterwards! */ 118 /**********************************************************************//** 119 Stops a query thread if graph or trx is in a state requiring it. The 120 conditions are tested in the order (1) graph, (2) trx. The lock_sys_t::mutex 121 has to be reserved. 122 @return TRUE if stopped */ 123 UNIV_INTERN 124 ibool 125 que_thr_stop( 126 /*=========*/ 127 que_thr_t* thr); /*!< in: query thread */ 128 /**********************************************************************//** 129 Moves a thread from another state to the QUE_THR_RUNNING state. Increments 130 the n_active_thrs counters of the query graph and transaction. */ 131 UNIV_INTERN 132 void 133 que_thr_move_to_run_state_for_mysql( 134 /*================================*/ 135 que_thr_t* thr, /*!< in: an query thread */ 136 trx_t* trx); /*!< in: transaction */ 137 /**********************************************************************//** 138 A patch for MySQL used to 'stop' a dummy query thread used in MySQL 139 select, when there is no error or lock wait. */ 140 UNIV_INTERN 141 void 142 que_thr_stop_for_mysql_no_error( 143 /*============================*/ 144 que_thr_t* thr, /*!< in: query thread */ 145 trx_t* trx); /*!< in: transaction */ 146 /**********************************************************************//** 147 A patch for MySQL used to 'stop' a dummy query thread used in MySQL. The 148 query thread is stopped and made inactive, except in the case where 149 it was put to the lock wait state in lock0lock.cc, but the lock has already 150 been granted or the transaction chosen as a victim in deadlock resolution. */ 151 UNIV_INTERN 152 void 153 que_thr_stop_for_mysql( 154 /*===================*/ 155 que_thr_t* thr); /*!< in: query thread */ 156 /**********************************************************************//** 157 Run a query thread. Handles lock waits. */ 158 UNIV_INTERN 159 void 160 que_run_threads( 161 /*============*/ 162 que_thr_t* thr); /*!< in: query thread */ 163 /**********************************************************************//** 164 Moves a suspended query thread to the QUE_THR_RUNNING state and release 165 a worker thread to execute it. This function should be used to end 166 the wait state of a query thread waiting for a lock or a stored procedure 167 completion. 168 @return query thread instance of thread to wakeup or NULL */ 169 UNIV_INTERN 170 que_thr_t* 171 que_thr_end_lock_wait( 172 /*==================*/ 173 trx_t* trx); /*!< in: transaction in the 174 QUE_THR_LOCK_WAIT state */ 175 /**********************************************************************//** 176 Starts execution of a command in a query fork. Picks a query thread which 177 is not in the QUE_THR_RUNNING state and moves it to that state. If none 178 can be chosen, a situation which may arise in parallelized fetches, NULL 179 is returned. 180 @return a query thread of the graph moved to QUE_THR_RUNNING state, or 181 NULL; the query thread should be executed by que_run_threads by the 182 caller */ 183 UNIV_INTERN 184 que_thr_t* 185 que_fork_start_command( 186 /*===================*/ 187 que_fork_t* fork); /*!< in: a query fork */ 188 /***********************************************************************//** 189 Gets the trx of a query thread. */ 190 UNIV_INLINE 191 trx_t* 192 thr_get_trx( 193 /*========*/ 194 que_thr_t* thr); /*!< in: query thread */ 195 /*******************************************************************//** 196 Determines if this thread is rolling back an incomplete transaction 197 in crash recovery. 198 @return TRUE if thr is rolling back an incomplete transaction in crash 199 recovery */ 200 UNIV_INLINE 201 ibool 202 thr_is_recv( 203 /*========*/ 204 const que_thr_t* thr); /*!< in: query thread */ 205 /***********************************************************************//** 206 Gets the type of a graph node. */ 207 UNIV_INLINE 208 ulint 209 que_node_get_type( 210 /*==============*/ 211 que_node_t* node); /*!< in: graph node */ 212 /***********************************************************************//** 213 Gets pointer to the value data type field of a graph node. */ 214 UNIV_INLINE 215 dtype_t* 216 que_node_get_data_type( 217 /*===================*/ 218 que_node_t* node); /*!< in: graph node */ 219 /***********************************************************************//** 220 Gets pointer to the value dfield of a graph node. */ 221 UNIV_INLINE 222 dfield_t* 223 que_node_get_val( 224 /*=============*/ 225 que_node_t* node); /*!< in: graph node */ 226 /***********************************************************************//** 227 Gets the value buffer size of a graph node. 228 @return val buffer size, not defined if val.data == NULL in node */ 229 UNIV_INLINE 230 ulint 231 que_node_get_val_buf_size( 232 /*======================*/ 233 que_node_t* node); /*!< in: graph node */ 234 /***********************************************************************//** 235 Sets the value buffer size of a graph node. */ 236 UNIV_INLINE 237 void 238 que_node_set_val_buf_size( 239 /*======================*/ 240 que_node_t* node, /*!< in: graph node */ 241 ulint size); /*!< in: size */ 242 /*********************************************************************//** 243 Gets the next list node in a list of query graph nodes. */ 244 UNIV_INLINE 245 que_node_t* 246 que_node_get_next( 247 /*==============*/ 248 que_node_t* node); /*!< in: node in a list */ 249 /*********************************************************************//** 250 Gets the parent node of a query graph node. 251 @return parent node or NULL */ 252 UNIV_INLINE 253 que_node_t* 254 que_node_get_parent( 255 /*================*/ 256 que_node_t* node); /*!< in: node */ 257 /****************************************************************//** 258 Get the first containing loop node (e.g. while_node_t or for_node_t) for the 259 given node, or NULL if the node is not within a loop. 260 @return containing loop node, or NULL. */ 261 UNIV_INTERN 262 que_node_t* 263 que_node_get_containing_loop_node( 264 /*==============================*/ 265 que_node_t* node); /*!< in: node */ 266 /*********************************************************************//** 267 Catenates a query graph node to a list of them, possible empty list. 268 @return one-way list of nodes */ 269 UNIV_INLINE 270 que_node_t* 271 que_node_list_add_last( 272 /*===================*/ 273 que_node_t* node_list, /*!< in: node list, or NULL */ 274 que_node_t* node); /*!< in: node */ 275 /************************************************************************* 276 Get the last node from the list.*/ 277 UNIV_INLINE 278 que_node_t* 279 que_node_list_get_last( 280 /*===================*/ 281 /* out: node last node from list.*/ 282 que_node_t* node_list); /* in: node list, or NULL */ 283 /*********************************************************************//** 284 Gets a query graph node list length. 285 @return length, for NULL list 0 */ 286 UNIV_INLINE 287 ulint 288 que_node_list_get_len( 289 /*==================*/ 290 que_node_t* node_list); /*!< in: node list, or NULL */ 291 /**********************************************************************//** 292 Checks if graph, trx, or session is in a state where the query thread should 293 be stopped. 294 @return TRUE if should be stopped; NOTE that if the peek is made 295 without reserving the trx_t::mutex, then another peek with the mutex 296 reserved is necessary before deciding the actual stopping */ 297 UNIV_INLINE 298 ibool 299 que_thr_peek_stop( 300 /*==============*/ 301 que_thr_t* thr); /*!< in: query thread */ 302 /***********************************************************************//** 303 Returns TRUE if the query graph is for a SELECT statement. 304 @return TRUE if a select */ 305 UNIV_INLINE 306 ibool 307 que_graph_is_select( 308 /*================*/ 309 que_t* graph); /*!< in: graph */ 310 /**********************************************************************//** 311 Prints info of an SQL query graph node. */ 312 UNIV_INTERN 313 void 314 que_node_print_info( 315 /*================*/ 316 que_node_t* node); /*!< in: query graph node */ 317 /*********************************************************************//** 318 Evaluate the given SQL 319 @return error code or DB_SUCCESS */ 320 UNIV_INTERN 321 dberr_t 322 que_eval_sql( 323 /*=========*/ 324 pars_info_t* info, /*!< in: info struct, or NULL */ 325 const char* sql, /*!< in: SQL string */ 326 ibool reserve_dict_mutex, 327 /*!< in: if TRUE, acquire/release 328 dict_sys->mutex around call to pars_sql. */ 329 trx_t* trx); /*!< in: trx */ 330 331 /**********************************************************************//** 332 Round robin scheduler. 333 @return a query thread of the graph moved to QUE_THR_RUNNING state, or 334 NULL; the query thread should be executed by que_run_threads by the 335 caller */ 336 UNIV_INTERN 337 que_thr_t* 338 que_fork_scheduler_round_robin( 339 /*===========================*/ 340 que_fork_t* fork, /*!< in: a query fork */ 341 que_thr_t* thr); /*!< in: current pos */ 342 343 /*********************************************************************//** 344 Initialise the query sub-system. */ 345 UNIV_INTERN 346 void 347 que_init(void); 348 /*==========*/ 349 350 /*********************************************************************//** 351 Close the query sub-system. */ 352 UNIV_INTERN 353 void 354 que_close(void); 355 /*===========*/ 356 357 /* Query graph query thread node: the fields are protected by the 358 trx_t::mutex with the exceptions named below */ 359 360 struct que_thr_t{ 361 que_common_t common; /*!< type: QUE_NODE_THR */ 362 ulint magic_n; /*!< magic number to catch memory 363 corruption */ 364 que_node_t* child; /*!< graph child node */ 365 que_t* graph; /*!< graph where this node belongs */ 366 ulint state; /*!< state of the query thread */ 367 ibool is_active; /*!< TRUE if the thread has been set 368 to the run state in 369 que_thr_move_to_run_state, but not 370 deactivated in 371 que_thr_dec_reference_count */ 372 /*------------------------------*/ 373 /* The following fields are private to the OS thread executing the 374 query thread, and are not protected by any mutex: */ 375 376 que_node_t* run_node; /*!< pointer to the node where the 377 subgraph down from this node is 378 currently executed */ 379 que_node_t* prev_node; /*!< pointer to the node from which 380 the control came */ 381 ulint resource; /*!< resource usage of the query thread 382 thus far */ 383 ulint lock_state; /*!< lock state of thread (table or 384 row) */ 385 struct srv_slot_t* 386 slot; /* The thread slot in the wait 387 array in srv_sys_t */ 388 /*------------------------------*/ 389 /* The following fields are links for the various lists that 390 this type can be on. */ 391 UT_LIST_NODE_T(que_thr_t) 392 thrs; /*!< list of thread nodes of the fork 393 node */ 394 UT_LIST_NODE_T(que_thr_t) 395 trx_thrs; /*!< lists of threads in wait list of 396 the trx */ 397 UT_LIST_NODE_T(que_thr_t) 398 queue; /*!< list of runnable thread nodes in 399 the server task queue */ 400 ulint fk_cascade_depth; /*!< maximum cascading call depth 401 supported for foreign key constraint 402 related delete/updates */ 403 }; 404 405 #define QUE_THR_MAGIC_N 8476583 406 #define QUE_THR_MAGIC_FREED 123461526 407 408 /* Query graph fork node: its fields are protected by the query thread mutex */ 409 struct que_fork_t{ 410 que_common_t common; /*!< type: QUE_NODE_FORK */ 411 que_t* graph; /*!< query graph of this node */ 412 ulint fork_type; /*!< fork type */ 413 ulint n_active_thrs; /*!< if this is the root of a graph, the 414 number query threads that have been 415 started in que_thr_move_to_run_state 416 but for which que_thr_dec_refer_count 417 has not yet been called */ 418 trx_t* trx; /*!< transaction: this is set only in 419 the root node */ 420 ulint state; /*!< state of the fork node */ 421 que_thr_t* caller; /*!< pointer to a possible calling query 422 thread */ 423 UT_LIST_BASE_NODE_T(que_thr_t) 424 thrs; /*!< list of query threads */ 425 /*------------------------------*/ 426 /* The fields in this section are defined only in the root node */ 427 sym_tab_t* sym_tab; /*!< symbol table of the query, 428 generated by the parser, or NULL 429 if the graph was created 'by hand' */ 430 pars_info_t* info; /*!< info struct, or NULL */ 431 /* The following cur_... fields are relevant only in a select graph */ 432 433 ulint cur_end; /*!< QUE_CUR_NOT_DEFINED, QUE_CUR_START, 434 QUE_CUR_END */ 435 ulint cur_pos; /*!< if there are n rows in the result 436 set, values 0 and n + 1 mean before 437 first row, or after last row, depending 438 on cur_end; values 1...n mean a row 439 index */ 440 ibool cur_on_row; /*!< TRUE if cursor is on a row, i.e., 441 it is not before the first row or 442 after the last row */ 443 sel_node_t* last_sel_node; /*!< last executed select node, or NULL 444 if none */ 445 UT_LIST_NODE_T(que_fork_t) 446 graphs; /*!< list of query graphs of a session 447 or a stored procedure */ 448 /*------------------------------*/ 449 mem_heap_t* heap; /*!< memory heap where the fork was 450 created */ 451 452 }; 453 454 /* Query fork (or graph) types */ 455 #define QUE_FORK_SELECT_NON_SCROLL 1 /* forward-only cursor */ 456 #define QUE_FORK_SELECT_SCROLL 2 /* scrollable cursor */ 457 #define QUE_FORK_INSERT 3 458 #define QUE_FORK_UPDATE 4 459 #define QUE_FORK_ROLLBACK 5 460 /* This is really the undo graph used in rollback, 461 no signal-sending roll_node in this graph */ 462 #define QUE_FORK_PURGE 6 463 #define QUE_FORK_EXECUTE 7 464 #define QUE_FORK_PROCEDURE 8 465 #define QUE_FORK_PROCEDURE_CALL 9 466 #define QUE_FORK_MYSQL_INTERFACE 10 467 #define QUE_FORK_RECOVERY 11 468 469 /* Query fork (or graph) states */ 470 #define QUE_FORK_ACTIVE 1 471 #define QUE_FORK_COMMAND_WAIT 2 472 #define QUE_FORK_INVALID 3 473 #define QUE_FORK_BEING_FREED 4 474 475 /* Flag which is ORed to control structure statement node types */ 476 #define QUE_NODE_CONTROL_STAT 1024 477 478 /* Query graph node types */ 479 #define QUE_NODE_LOCK 1 480 #define QUE_NODE_INSERT 2 481 #define QUE_NODE_UPDATE 4 482 #define QUE_NODE_CURSOR 5 483 #define QUE_NODE_SELECT 6 484 #define QUE_NODE_AGGREGATE 7 485 #define QUE_NODE_FORK 8 486 #define QUE_NODE_THR 9 487 #define QUE_NODE_UNDO 10 488 #define QUE_NODE_COMMIT 11 489 #define QUE_NODE_ROLLBACK 12 490 #define QUE_NODE_PURGE 13 491 #define QUE_NODE_CREATE_TABLE 14 492 #define QUE_NODE_CREATE_INDEX 15 493 #define QUE_NODE_SYMBOL 16 494 #define QUE_NODE_RES_WORD 17 495 #define QUE_NODE_FUNC 18 496 #define QUE_NODE_ORDER 19 497 #define QUE_NODE_PROC (20 + QUE_NODE_CONTROL_STAT) 498 #define QUE_NODE_IF (21 + QUE_NODE_CONTROL_STAT) 499 #define QUE_NODE_WHILE (22 + QUE_NODE_CONTROL_STAT) 500 #define QUE_NODE_ASSIGNMENT 23 501 #define QUE_NODE_FETCH 24 502 #define QUE_NODE_OPEN 25 503 #define QUE_NODE_COL_ASSIGNMENT 26 504 #define QUE_NODE_FOR (27 + QUE_NODE_CONTROL_STAT) 505 #define QUE_NODE_RETURN 28 506 #define QUE_NODE_ROW_PRINTF 29 507 #define QUE_NODE_ELSIF 30 508 #define QUE_NODE_CALL 31 509 #define QUE_NODE_EXIT 32 510 511 /* Query thread states */ 512 #define QUE_THR_RUNNING 1 513 #define QUE_THR_PROCEDURE_WAIT 2 514 #define QUE_THR_COMPLETED 3 /* in selects this means that the 515 thread is at the end of its result set 516 (or start, in case of a scroll cursor); 517 in other statements, this means the 518 thread has done its task */ 519 #define QUE_THR_COMMAND_WAIT 4 520 #define QUE_THR_LOCK_WAIT 5 521 #define QUE_THR_SUSPENDED 7 522 #define QUE_THR_ERROR 8 523 524 /* Query thread lock states */ 525 #define QUE_THR_LOCK_NOLOCK 0 526 #define QUE_THR_LOCK_ROW 1 527 #define QUE_THR_LOCK_TABLE 2 528 529 /* From where the cursor position is counted */ 530 #define QUE_CUR_NOT_DEFINED 1 531 #define QUE_CUR_START 2 532 #define QUE_CUR_END 3 533 534 #ifndef UNIV_NONINL 535 #include "que0que.ic" 536 #endif 537 538 #endif 539