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.

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