You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

531 lines
18 KiB

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