00001 /****************************************************** 00002 Query graph 00003 00004 (c) 1996 Innobase Oy 00005 00006 Created 5/27/1996 Heikki Tuuri 00007 *******************************************************/ 00008 00009 #ifndef que0que_h 00010 #define que0que_h 00011 00012 #include "univ.i" 00013 #include "data0data.h" 00014 #include "dict0types.h" 00015 #include "trx0trx.h" 00016 #include "srv0srv.h" 00017 #include "usr0types.h" 00018 #include "que0types.h" 00019 #include "row0types.h" 00020 #include "pars0types.h" 00021 00022 /* If the following flag is set TRUE, the module will print trace info 00023 of SQL execution in the UNIV_SQL_DEBUG version */ 00024 extern ibool que_trace_on; 00025 00026 /*************************************************************************** 00027 Adds a query graph to the session's list of graphs. */ 00028 00029 void 00030 que_graph_publish( 00031 /*==============*/ 00032 que_t* graph, /* in: graph */ 00033 sess_t* sess); /* in: session */ 00034 /*************************************************************************** 00035 Creates a query graph fork node. */ 00036 00037 que_fork_t* 00038 que_fork_create( 00039 /*============*/ 00040 /* out, own: fork node */ 00041 que_t* graph, /* in: graph, if NULL then this 00042 fork node is assumed to be the 00043 graph root */ 00044 que_node_t* parent, /* in: parent node */ 00045 ulint fork_type, /* in: fork type */ 00046 mem_heap_t* heap); /* in: memory heap where created */ 00047 /*************************************************************************** 00048 Gets the first thr in a fork. */ 00049 UNIV_INLINE 00050 que_thr_t* 00051 que_fork_get_first_thr( 00052 /*===================*/ 00053 que_fork_t* fork); /* in: query fork */ 00054 /*************************************************************************** 00055 Gets the child node of the first thr in a fork. */ 00056 UNIV_INLINE 00057 que_node_t* 00058 que_fork_get_child( 00059 /*===============*/ 00060 que_fork_t* fork); /* in: query fork */ 00061 /*************************************************************************** 00062 Sets the parent of a graph node. */ 00063 UNIV_INLINE 00064 void 00065 que_node_set_parent( 00066 /*================*/ 00067 que_node_t* node, /* in: graph node */ 00068 que_node_t* parent);/* in: parent */ 00069 /*************************************************************************** 00070 Creates a query graph thread node. */ 00071 00072 que_thr_t* 00073 que_thr_create( 00074 /*===========*/ 00075 /* out, own: query thread node */ 00076 que_fork_t* parent, /* in: parent node, i.e., a fork node */ 00077 mem_heap_t* heap); /* in: memory heap where created */ 00078 /************************************************************************** 00079 Checks if the query graph is in a state where it should be freed, and 00080 frees it in that case. If the session is in a state where it should be 00081 closed, also this is done. */ 00082 00083 ibool 00084 que_graph_try_free( 00085 /*===============*/ 00086 /* out: TRUE if freed */ 00087 que_t* graph); /* in: query graph */ 00088 /************************************************************************** 00089 Frees a query graph, but not the heap where it was created. Does not free 00090 explicit cursor declarations, they are freed in que_graph_free. */ 00091 00092 void 00093 que_graph_free_recursive( 00094 /*=====================*/ 00095 que_node_t* node); /* in: query graph node */ 00096 /************************************************************************** 00097 Frees a query graph. */ 00098 00099 void 00100 que_graph_free( 00101 /*===========*/ 00102 que_t* graph); /* in: query graph; we assume that the memory 00103 heap where this graph was created is private 00104 to this graph: if not, then use 00105 que_graph_free_recursive and free the heap 00106 afterwards! */ 00107 /************************************************************************** 00108 Stops a query thread if graph or trx is in a state requiring it. The 00109 conditions are tested in the order (1) graph, (2) trx. The kernel mutex has 00110 to be reserved. */ 00111 00112 ibool 00113 que_thr_stop( 00114 /*=========*/ 00115 /* out: TRUE if stopped */ 00116 que_thr_t* thr); /* in: query thread */ 00117 /************************************************************************** 00118 Moves a thread from another state to the QUE_THR_RUNNING state. Increments 00119 the n_active_thrs counters of the query graph and transaction. */ 00120 00121 void 00122 que_thr_move_to_run_state_for_mysql( 00123 /*================================*/ 00124 que_thr_t* thr, /* in: an query thread */ 00125 trx_t* trx); /* in: transaction */ 00126 /************************************************************************** 00127 A patch for MySQL used to 'stop' a dummy query thread used in MySQL 00128 select, when there is no error or lock wait. */ 00129 00130 void 00131 que_thr_stop_for_mysql_no_error( 00132 /*============================*/ 00133 que_thr_t* thr, /* in: query thread */ 00134 trx_t* trx); /* in: transaction */ 00135 /************************************************************************** 00136 A patch for MySQL used to 'stop' a dummy query thread used in MySQL. The 00137 query thread is stopped and made inactive, except in the case where 00138 it was put to the lock wait state in lock0lock.c, but the lock has already 00139 been granted or the transaction chosen as a victim in deadlock resolution. */ 00140 00141 void 00142 que_thr_stop_for_mysql( 00143 /*===================*/ 00144 que_thr_t* thr); /* in: query thread */ 00145 /************************************************************************** 00146 Run a query thread. Handles lock waits. */ 00147 00148 void 00149 que_run_threads( 00150 /*============*/ 00151 que_thr_t* thr); /* in: query thread */ 00152 /************************************************************************** 00153 After signal handling is finished, returns control to a query graph error 00154 handling routine. (Currently, just returns the control to the root of the 00155 graph so that the graph can communicate an error message to the client.) */ 00156 00157 void 00158 que_fork_error_handle( 00159 /*==================*/ 00160 trx_t* trx, /* in: trx */ 00161 que_t* fork); /* in: query graph which was run before signal 00162 handling started, NULL not allowed */ 00163 /************************************************************************** 00164 Moves a suspended query thread to the QUE_THR_RUNNING state and releases 00165 a single worker thread to execute it. This function should be used to end 00166 the wait state of a query thread waiting for a lock or a stored procedure 00167 completion. */ 00168 00169 void 00170 que_thr_end_wait( 00171 /*=============*/ 00172 que_thr_t* thr, /* in: query thread in the 00173 QUE_THR_LOCK_WAIT, 00174 or QUE_THR_PROCEDURE_WAIT, or 00175 QUE_THR_SIG_REPLY_WAIT state */ 00176 que_thr_t** next_thr); /* in/out: next query thread to run; 00177 if the value which is passed in is 00178 a pointer to a NULL pointer, then the 00179 calling function can start running 00180 a new query thread */ 00181 /************************************************************************** 00182 Same as que_thr_end_wait, but no parameter next_thr available. */ 00183 00184 void 00185 que_thr_end_wait_no_next_thr( 00186 /*=========================*/ 00187 que_thr_t* thr); /* in: query thread in the 00188 QUE_THR_LOCK_WAIT, 00189 or QUE_THR_PROCEDURE_WAIT, or 00190 QUE_THR_SIG_REPLY_WAIT state */ 00191 /************************************************************************** 00192 Starts execution of a command in a query fork. Picks a query thread which 00193 is not in the QUE_THR_RUNNING state and moves it to that state. If none 00194 can be chosen, a situation which may arise in parallelized fetches, NULL 00195 is returned. */ 00196 00197 que_thr_t* 00198 que_fork_start_command( 00199 /*===================*/ 00200 /* out: a query thread of the graph moved to 00201 QUE_THR_RUNNING state, or NULL; the query 00202 thread should be executed by que_run_threads 00203 by the caller */ 00204 que_fork_t* fork); /* in: a query fork */ 00205 /*************************************************************************** 00206 Gets the trx of a query thread. */ 00207 UNIV_INLINE 00208 trx_t* 00209 thr_get_trx( 00210 /*========*/ 00211 que_thr_t* thr); /* in: query thread */ 00212 /*************************************************************************** 00213 Gets the type of a graph node. */ 00214 UNIV_INLINE 00215 ulint 00216 que_node_get_type( 00217 /*==============*/ 00218 que_node_t* node); /* in: graph node */ 00219 /*************************************************************************** 00220 Gets pointer to the value data type field of a graph node. */ 00221 UNIV_INLINE 00222 dtype_t* 00223 que_node_get_data_type( 00224 /*===================*/ 00225 que_node_t* node); /* in: graph node */ 00226 /*************************************************************************** 00227 Gets pointer to the value dfield of a graph node. */ 00228 UNIV_INLINE 00229 dfield_t* 00230 que_node_get_val( 00231 /*=============*/ 00232 que_node_t* node); /* in: graph node */ 00233 /*************************************************************************** 00234 Gets the value buffer size of a graph node. */ 00235 UNIV_INLINE 00236 ulint 00237 que_node_get_val_buf_size( 00238 /*======================*/ 00239 /* out: val buffer size, not defined if 00240 val.data == NULL in node */ 00241 que_node_t* node); /* in: graph node */ 00242 /*************************************************************************** 00243 Sets the value buffer size of a graph node. */ 00244 UNIV_INLINE 00245 void 00246 que_node_set_val_buf_size( 00247 /*======================*/ 00248 que_node_t* node, /* in: graph node */ 00249 ulint size); /* in: size */ 00250 /************************************************************************* 00251 Gets the next list node in a list of query graph nodes. */ 00252 UNIV_INLINE 00253 que_node_t* 00254 que_node_get_next( 00255 /*==============*/ 00256 que_node_t* node); /* in: node in a list */ 00257 /************************************************************************* 00258 Gets the parent node of a query graph node. */ 00259 UNIV_INLINE 00260 que_node_t* 00261 que_node_get_parent( 00262 /*================*/ 00263 /* out: parent node or NULL */ 00264 que_node_t* node); /* in: node */ 00265 /******************************************************************** 00266 Get the first containing loop node (e.g. while_node_t or for_node_t) for the 00267 given node, or NULL if the node is not within a loop. */ 00268 00269 que_node_t* 00270 que_node_get_containing_loop_node( 00271 /*==============================*/ 00272 /* out: containing loop node, or NULL. */ 00273 que_node_t* node); /* in: node */ 00274 /************************************************************************* 00275 Catenates a query graph node to a list of them, possible empty list. */ 00276 UNIV_INLINE 00277 que_node_t* 00278 que_node_list_add_last( 00279 /*===================*/ 00280 /* out: one-way list of nodes */ 00281 que_node_t* node_list, /* in: node list, or NULL */ 00282 que_node_t* node); /* in: node */ 00283 /************************************************************************* 00284 Gets a query graph node list length. */ 00285 UNIV_INLINE 00286 ulint 00287 que_node_list_get_len( 00288 /*==================*/ 00289 /* out: length, for NULL list 0 */ 00290 que_node_t* node_list); /* in: node list, or NULL */ 00291 /************************************************************************** 00292 Checks if graph, trx, or session is in a state where the query thread should 00293 be stopped. */ 00294 UNIV_INLINE 00295 ibool 00296 que_thr_peek_stop( 00297 /*==============*/ 00298 /* out: TRUE if should be stopped; NOTE that 00299 if the peek is made without reserving the 00300 kernel mutex, then another peek with the 00301 mutex reserved is necessary before deciding 00302 the actual stopping */ 00303 que_thr_t* thr); /* in: query thread */ 00304 /*************************************************************************** 00305 Returns TRUE if the query graph is for a SELECT statement. */ 00306 UNIV_INLINE 00307 ibool 00308 que_graph_is_select( 00309 /*================*/ 00310 /* out: TRUE if a select */ 00311 que_t* graph); /* in: graph */ 00312 /************************************************************************** 00313 Prints info of an SQL query graph node. */ 00314 00315 void 00316 que_node_print_info( 00317 /*================*/ 00318 que_node_t* node); /* in: query graph node */ 00319 /************************************************************************* 00320 Evaluate the given SQL */ 00321 00322 ulint 00323 que_eval_sql( 00324 /*=========*/ 00325 /* out: error code or DB_SUCCESS */ 00326 pars_info_t* info, /* in: info struct, or NULL */ 00327 const char* sql, /* in: SQL string */ 00328 ibool reserve_dict_mutex, 00329 /* in: if TRUE, acquire/release 00330 dict_sys->mutex around call to pars_sql. */ 00331 trx_t* trx); /* in: trx */ 00332 00333 /* Query graph query thread node: the fields are protected by the kernel 00334 mutex with the exceptions named below */ 00335 00336 struct que_thr_struct{ 00337 que_common_t common; /* type: QUE_NODE_THR */ 00338 ulint magic_n; /* magic number to catch memory 00339 corruption */ 00340 que_node_t* child; /* graph child node */ 00341 que_t* graph; /* graph where this node belongs */ 00342 ibool is_active; /* TRUE if the thread has been set 00343 to the run state in 00344 que_thr_move_to_run_state, but not 00345 deactivated in 00346 que_thr_dec_reference_count */ 00347 ulint state; /* state of the query thread */ 00348 UT_LIST_NODE_T(que_thr_t) 00349 thrs; /* list of thread nodes of the fork 00350 node */ 00351 UT_LIST_NODE_T(que_thr_t) 00352 trx_thrs; /* lists of threads in wait list of 00353 the trx */ 00354 UT_LIST_NODE_T(que_thr_t) 00355 queue; /* list of runnable thread nodes in 00356 the server task queue */ 00357 /*------------------------------*/ 00358 /* The following fields are private to the OS thread executing the 00359 query thread, and are not protected by the kernel mutex: */ 00360 00361 que_node_t* run_node; /* pointer to the node where the 00362 subgraph down from this node is 00363 currently executed */ 00364 que_node_t* prev_node; /* pointer to the node from which 00365 the control came */ 00366 ulint resource; /* resource usage of the query thread 00367 thus far */ 00368 ulint lock_state; /* lock state of thread (table or 00369 row) */ 00370 }; 00371 00372 #define QUE_THR_MAGIC_N 8476583 00373 #define QUE_THR_MAGIC_FREED 123461526 00374 00375 /* Query graph fork node: its fields are protected by the kernel mutex */ 00376 struct que_fork_struct{ 00377 que_common_t common; /* type: QUE_NODE_FORK */ 00378 que_t* graph; /* query graph of this node */ 00379 ulint fork_type; /* fork type */ 00380 ulint n_active_thrs; /* if this is the root of a graph, the 00381 number query threads that have been 00382 started in que_thr_move_to_run_state 00383 but for which que_thr_dec_refer_count 00384 has not yet been called */ 00385 trx_t* trx; /* transaction: this is set only in 00386 the root node */ 00387 ulint state; /* state of the fork node */ 00388 que_thr_t* caller; /* pointer to a possible calling query 00389 thread */ 00390 UT_LIST_BASE_NODE_T(que_thr_t) 00391 thrs; /* list of query threads */ 00392 /*------------------------------*/ 00393 /* The fields in this section are defined only in the root node */ 00394 sym_tab_t* sym_tab; /* symbol table of the query, 00395 generated by the parser, or NULL 00396 if the graph was created 'by hand' */ 00397 pars_info_t* info; /* in: info struct, or NULL */ 00398 /* The following cur_... fields are relevant only in a select graph */ 00399 00400 ulint cur_end; /* QUE_CUR_NOT_DEFINED, QUE_CUR_START, 00401 QUE_CUR_END */ 00402 ulint cur_pos; /* if there are n rows in the result 00403 set, values 0 and n + 1 mean before 00404 first row, or after last row, depending 00405 on cur_end; values 1...n mean a row 00406 index */ 00407 ibool cur_on_row; /* TRUE if cursor is on a row, i.e., 00408 it is not before the first row or 00409 after the last row */ 00410 dulint n_inserts; /* number of rows inserted */ 00411 dulint n_updates; /* number of rows updated */ 00412 dulint n_deletes; /* number of rows deleted */ 00413 sel_node_t* last_sel_node; /* last executed select node, or NULL 00414 if none */ 00415 UT_LIST_NODE_T(que_fork_t) 00416 graphs; /* list of query graphs of a session 00417 or a stored procedure */ 00418 /*------------------------------*/ 00419 mem_heap_t* heap; /* memory heap where the fork was 00420 created */ 00421 00422 }; 00423 00424 /* Query fork (or graph) types */ 00425 #define QUE_FORK_SELECT_NON_SCROLL 1 /* forward-only cursor */ 00426 #define QUE_FORK_SELECT_SCROLL 2 /* scrollable cursor */ 00427 #define QUE_FORK_INSERT 3 00428 #define QUE_FORK_UPDATE 4 00429 #define QUE_FORK_ROLLBACK 5 00430 /* This is really the undo graph used in rollback, 00431 no signal-sending roll_node in this graph */ 00432 #define QUE_FORK_PURGE 6 00433 #define QUE_FORK_EXECUTE 7 00434 #define QUE_FORK_PROCEDURE 8 00435 #define QUE_FORK_PROCEDURE_CALL 9 00436 #define QUE_FORK_MYSQL_INTERFACE 10 00437 #define QUE_FORK_RECOVERY 11 00438 00439 /* Query fork (or graph) states */ 00440 #define QUE_FORK_ACTIVE 1 00441 #define QUE_FORK_COMMAND_WAIT 2 00442 #define QUE_FORK_INVALID 3 00443 #define QUE_FORK_BEING_FREED 4 00444 00445 /* Flag which is ORed to control structure statement node types */ 00446 #define QUE_NODE_CONTROL_STAT 1024 00447 00448 /* Query graph node types */ 00449 #define QUE_NODE_LOCK 1 00450 #define QUE_NODE_INSERT 2 00451 #define QUE_NODE_UPDATE 4 00452 #define QUE_NODE_CURSOR 5 00453 #define QUE_NODE_SELECT 6 00454 #define QUE_NODE_AGGREGATE 7 00455 #define QUE_NODE_FORK 8 00456 #define QUE_NODE_THR 9 00457 #define QUE_NODE_UNDO 10 00458 #define QUE_NODE_COMMIT 11 00459 #define QUE_NODE_ROLLBACK 12 00460 #define QUE_NODE_PURGE 13 00461 #define QUE_NODE_CREATE_TABLE 14 00462 #define QUE_NODE_CREATE_INDEX 15 00463 #define QUE_NODE_SYMBOL 16 00464 #define QUE_NODE_RES_WORD 17 00465 #define QUE_NODE_FUNC 18 00466 #define QUE_NODE_ORDER 19 00467 #define QUE_NODE_PROC (20 + QUE_NODE_CONTROL_STAT) 00468 #define QUE_NODE_IF (21 + QUE_NODE_CONTROL_STAT) 00469 #define QUE_NODE_WHILE (22 + QUE_NODE_CONTROL_STAT) 00470 #define QUE_NODE_ASSIGNMENT 23 00471 #define QUE_NODE_FETCH 24 00472 #define QUE_NODE_OPEN 25 00473 #define QUE_NODE_COL_ASSIGNMENT 26 00474 #define QUE_NODE_FOR (27 + QUE_NODE_CONTROL_STAT) 00475 #define QUE_NODE_RETURN 28 00476 #define QUE_NODE_ROW_PRINTF 29 00477 #define QUE_NODE_ELSIF 30 00478 #define QUE_NODE_CALL 31 00479 #define QUE_NODE_EXIT 32 00480 00481 /* Query thread states */ 00482 #define QUE_THR_RUNNING 1 00483 #define QUE_THR_PROCEDURE_WAIT 2 00484 #define QUE_THR_COMPLETED 3 /* in selects this means that the 00485 thread is at the end of its result set 00486 (or start, in case of a scroll cursor); 00487 in other statements, this means the 00488 thread has done its task */ 00489 #define QUE_THR_COMMAND_WAIT 4 00490 #define QUE_THR_LOCK_WAIT 5 00491 #define QUE_THR_SIG_REPLY_WAIT 6 00492 #define QUE_THR_SUSPENDED 7 00493 #define QUE_THR_ERROR 8 00494 00495 /* Query thread lock states */ 00496 #define QUE_THR_LOCK_NOLOCK 0 00497 #define QUE_THR_LOCK_ROW 1 00498 #define QUE_THR_LOCK_TABLE 2 00499 00500 /* From where the cursor position is counted */ 00501 #define QUE_CUR_NOT_DEFINED 1 00502 #define QUE_CUR_START 2 00503 #define QUE_CUR_END 3 00504 00505 00506 #ifndef UNIV_NONINL 00507 #include "que0que.ic" 00508 #endif 00509 00510 #endif
1.4.7

