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.

513 lines
18 KiB

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