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.

1162 lines
41 KiB

17 years ago
9 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
9 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
9 years ago
9 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
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
9 years ago
9 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
9 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 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, 2016, 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/trx0trx.h
  15. The transaction
  16. Created 3/26/1996 Heikki Tuuri
  17. *******************************************************/
  18. #ifndef trx0trx_h
  19. #define trx0trx_h
  20. #include "univ.i"
  21. #include "trx0types.h"
  22. #include "dict0types.h"
  23. #ifndef UNIV_HOTBACKUP
  24. #include "lock0types.h"
  25. #include "log0log.h"
  26. #include "usr0types.h"
  27. #include "que0types.h"
  28. #include "mem0mem.h"
  29. #include "read0types.h"
  30. #include "trx0xa.h"
  31. #include "ut0vec.h"
  32. #include "fts0fts.h"
  33. /** Dummy session used currently in MySQL interface */
  34. extern sess_t* trx_dummy_sess;
  35. /********************************************************************//**
  36. In XtraDB it is impossible for a transaction to own a search latch outside of
  37. InnoDB code, so there is nothing to release on demand. We keep this function to
  38. simplify maintenance.*/
  39. UNIV_INLINE
  40. void
  41. trx_search_latch_release_if_reserved(
  42. /*=================================*/
  43. trx_t* trx MY_ATTRIBUTE((unused))); /*!< in: transaction */
  44. /******************************************************************//**
  45. Set detailed error message for the transaction. */
  46. UNIV_INTERN
  47. void
  48. trx_set_detailed_error(
  49. /*===================*/
  50. trx_t* trx, /*!< in: transaction struct */
  51. const char* msg); /*!< in: detailed error message */
  52. /*************************************************************//**
  53. Set detailed error message for the transaction from a file. Note that the
  54. file is rewinded before reading from it. */
  55. UNIV_INTERN
  56. void
  57. trx_set_detailed_error_from_file(
  58. /*=============================*/
  59. trx_t* trx, /*!< in: transaction struct */
  60. FILE* file); /*!< in: file to read message from */
  61. /****************************************************************//**
  62. Retrieves the error_info field from a trx.
  63. @return the error info */
  64. UNIV_INLINE
  65. const dict_index_t*
  66. trx_get_error_info(
  67. /*===============*/
  68. const trx_t* trx); /*!< in: trx object */
  69. /********************************************************************//**
  70. Creates a transaction object for MySQL.
  71. @return own: transaction object */
  72. UNIV_INTERN
  73. trx_t*
  74. trx_allocate_for_mysql(void);
  75. /*========================*/
  76. /********************************************************************//**
  77. Creates a transaction object for background operations by the master thread.
  78. @return own: transaction object */
  79. UNIV_INTERN
  80. trx_t*
  81. trx_allocate_for_background(void);
  82. /*=============================*/
  83. /********************************************************************//**
  84. Frees a transaction object of a background operation of the master thread. */
  85. UNIV_INTERN
  86. void
  87. trx_free_for_background(
  88. /*====================*/
  89. trx_t* trx); /*!< in, own: trx object */
  90. /********************************************************************//**
  91. At shutdown, frees a transaction object that is in the PREPARED state. */
  92. UNIV_INTERN
  93. void
  94. trx_free_prepared(
  95. /*==============*/
  96. trx_t* trx) /*!< in, own: trx object */
  97. UNIV_COLD;
  98. /********************************************************************//**
  99. Frees a transaction object for MySQL. */
  100. UNIV_INTERN
  101. void
  102. trx_free_for_mysql(
  103. /*===============*/
  104. trx_t* trx); /*!< in, own: trx object */
  105. /****************************************************************//**
  106. Creates trx objects for transactions and initializes the trx list of
  107. trx_sys at database start. Rollback segment and undo log lists must
  108. already exist when this function is called, because the lists of
  109. transactions to be rolled back or cleaned up are built based on the
  110. undo log lists. */
  111. UNIV_INTERN
  112. void
  113. trx_lists_init_at_db_start(void);
  114. /*============================*/
  115. #ifdef UNIV_DEBUG
  116. #define trx_start_if_not_started_xa(t) \
  117. { \
  118. (t)->start_line = __LINE__; \
  119. (t)->start_file = __FILE__; \
  120. trx_start_if_not_started_xa_low((t)); \
  121. }
  122. #else
  123. #define trx_start_if_not_started_xa(t) \
  124. trx_start_if_not_started_xa_low((t))
  125. #endif /* UNIV_DEBUG */
  126. /*************************************************************//**
  127. Starts the transaction if it is not yet started. */
  128. UNIV_INTERN
  129. void
  130. trx_start_if_not_started_xa_low(
  131. /*============================*/
  132. trx_t* trx); /*!< in: transaction */
  133. /*************************************************************//**
  134. Starts the transaction if it is not yet started. */
  135. UNIV_INTERN
  136. void
  137. trx_start_if_not_started_low(
  138. /*=========================*/
  139. trx_t* trx); /*!< in: transaction */
  140. #ifdef UNIV_DEBUG
  141. #define trx_start_if_not_started(t) \
  142. { \
  143. (t)->start_line = __LINE__; \
  144. (t)->start_file = __FILE__; \
  145. trx_start_if_not_started_low((t)); \
  146. }
  147. #else
  148. #define trx_start_if_not_started(t) \
  149. trx_start_if_not_started_low((t))
  150. #endif /* UNIV_DEBUG */
  151. /*************************************************************//**
  152. Starts the transaction for a DDL operation. */
  153. UNIV_INTERN
  154. void
  155. trx_start_for_ddl_low(
  156. /*==================*/
  157. trx_t* trx, /*!< in/out: transaction */
  158. trx_dict_op_t op) /*!< in: dictionary operation type */
  159. MY_ATTRIBUTE((nonnull));
  160. #ifdef UNIV_DEBUG
  161. #define trx_start_for_ddl(t, o) \
  162. { \
  163. ut_ad((t)->start_file == 0); \
  164. (t)->start_line = __LINE__; \
  165. (t)->start_file = __FILE__; \
  166. trx_start_for_ddl_low((t), (o)); \
  167. }
  168. #else
  169. #define trx_start_for_ddl(t, o) \
  170. trx_start_for_ddl_low((t), (o))
  171. #endif /* UNIV_DEBUG */
  172. /****************************************************************//**
  173. Commits a transaction. */
  174. UNIV_INTERN
  175. void
  176. trx_commit(
  177. /*=======*/
  178. trx_t* trx) /*!< in/out: transaction */
  179. MY_ATTRIBUTE((nonnull));
  180. /****************************************************************//**
  181. Commits a transaction and a mini-transaction. */
  182. UNIV_INTERN
  183. void
  184. trx_commit_low(
  185. /*===========*/
  186. trx_t* trx, /*!< in/out: transaction */
  187. mtr_t* mtr) /*!< in/out: mini-transaction (will be committed),
  188. or NULL if trx made no modifications */
  189. MY_ATTRIBUTE((nonnull(1)));
  190. /****************************************************************//**
  191. Cleans up a transaction at database startup. The cleanup is needed if
  192. the transaction already got to the middle of a commit when the database
  193. crashed, and we cannot roll it back. */
  194. UNIV_INTERN
  195. void
  196. trx_cleanup_at_db_startup(
  197. /*======================*/
  198. trx_t* trx); /*!< in: transaction */
  199. /**********************************************************************//**
  200. Does the transaction commit for MySQL.
  201. @return DB_SUCCESS or error number */
  202. UNIV_INTERN
  203. dberr_t
  204. trx_commit_for_mysql(
  205. /*=================*/
  206. trx_t* trx); /*!< in/out: transaction */
  207. /**********************************************************************//**
  208. Does the transaction prepare for MySQL. */
  209. UNIV_INTERN
  210. void
  211. trx_prepare_for_mysql(
  212. /*==================*/
  213. trx_t* trx); /*!< in/out: trx handle */
  214. /**********************************************************************//**
  215. This function is used to find number of prepared transactions and
  216. their transaction objects for a recovery.
  217. @return number of prepared transactions */
  218. UNIV_INTERN
  219. int
  220. trx_recover_for_mysql(
  221. /*==================*/
  222. XID* xid_list, /*!< in/out: prepared transactions */
  223. ulint len); /*!< in: number of slots in xid_list */
  224. /*******************************************************************//**
  225. This function is used to find one X/Open XA distributed transaction
  226. which is in the prepared state
  227. @return trx or NULL; on match, the trx->xid will be invalidated;
  228. note that the trx may have been committed, unless the caller is
  229. holding lock_sys->mutex */
  230. UNIV_INTERN
  231. trx_t *
  232. trx_get_trx_by_xid(
  233. /*===============*/
  234. const XID* xid); /*!< in: X/Open XA transaction identifier */
  235. /**********************************************************************//**
  236. If required, flushes the log to disk if we called trx_commit_for_mysql()
  237. with trx->flush_log_later == TRUE. */
  238. UNIV_INTERN
  239. void
  240. trx_commit_complete_for_mysql(
  241. /*==========================*/
  242. trx_t* trx) /*!< in/out: transaction */
  243. MY_ATTRIBUTE((nonnull));
  244. /**********************************************************************//**
  245. Marks the latest SQL statement ended. */
  246. UNIV_INTERN
  247. void
  248. trx_mark_sql_stat_end(
  249. /*==================*/
  250. trx_t* trx); /*!< in: trx handle */
  251. /********************************************************************//**
  252. Assigns a read view for a consistent read query. All the consistent reads
  253. within the same transaction will get the same read view, which is created
  254. when this function is first called for a new started transaction.
  255. @return consistent read view */
  256. UNIV_INTERN
  257. read_view_t*
  258. trx_assign_read_view(
  259. /*=================*/
  260. trx_t* trx); /*!< in: active transaction */
  261. /********************************************************************//**
  262. Clones the read view from another transaction. All the consistent reads within
  263. the receiver transaction will get the same read view as the donor transaction
  264. @return read view clone */
  265. UNIV_INTERN
  266. read_view_t*
  267. trx_clone_read_view(
  268. /*================*/
  269. trx_t* trx, /*!< in: receiver transaction */
  270. trx_t* from_trx) /*!< in: donor transaction */
  271. MY_ATTRIBUTE((nonnull, warn_unused_result));
  272. /****************************************************************//**
  273. Prepares a transaction for commit/rollback. */
  274. UNIV_INTERN
  275. void
  276. trx_commit_or_rollback_prepare(
  277. /*===========================*/
  278. trx_t* trx); /*!< in/out: transaction */
  279. /*********************************************************************//**
  280. Creates a commit command node struct.
  281. @return own: commit node struct */
  282. UNIV_INTERN
  283. commit_node_t*
  284. trx_commit_node_create(
  285. /*===================*/
  286. mem_heap_t* heap); /*!< in: mem heap where created */
  287. /***********************************************************//**
  288. Performs an execution step for a commit type node in a query graph.
  289. @return query thread to run next, or NULL */
  290. UNIV_INTERN
  291. que_thr_t*
  292. trx_commit_step(
  293. /*============*/
  294. que_thr_t* thr); /*!< in: query thread */
  295. /**********************************************************************//**
  296. Prints info about a transaction.
  297. Caller must hold trx_sys->mutex. */
  298. UNIV_INTERN
  299. void
  300. trx_print_low(
  301. /*==========*/
  302. FILE* f,
  303. /*!< in: output stream */
  304. const trx_t* trx,
  305. /*!< in: transaction */
  306. ulint max_query_len,
  307. /*!< in: max query length to print,
  308. or 0 to use the default max length */
  309. ulint n_rec_locks,
  310. /*!< in: lock_number_of_rows_locked(&trx->lock) */
  311. ulint n_trx_locks,
  312. /*!< in: length of trx->lock.trx_locks */
  313. ulint heap_size)
  314. /*!< in: mem_heap_get_size(trx->lock.lock_heap) */
  315. MY_ATTRIBUTE((nonnull));
  316. /**********************************************************************//**
  317. Prints info about a transaction.
  318. The caller must hold lock_sys->mutex and trx_sys->mutex.
  319. When possible, use trx_print() instead. */
  320. UNIV_INTERN
  321. void
  322. trx_print_latched(
  323. /*==============*/
  324. FILE* f, /*!< in: output stream */
  325. const trx_t* trx, /*!< in: transaction */
  326. ulint max_query_len) /*!< in: max query length to print,
  327. or 0 to use the default max length */
  328. MY_ATTRIBUTE((nonnull));
  329. /**********************************************************************//**
  330. Prints info about a transaction.
  331. Acquires and releases lock_sys->mutex and trx_sys->mutex. */
  332. UNIV_INTERN
  333. void
  334. trx_print(
  335. /*======*/
  336. FILE* f, /*!< in: output stream */
  337. const trx_t* trx, /*!< in: transaction */
  338. ulint max_query_len) /*!< in: max query length to print,
  339. or 0 to use the default max length */
  340. MY_ATTRIBUTE((nonnull));
  341. /**********************************************************************//**
  342. Determine if a transaction is a dictionary operation.
  343. @return dictionary operation mode */
  344. UNIV_INLINE
  345. enum trx_dict_op_t
  346. trx_get_dict_operation(
  347. /*===================*/
  348. const trx_t* trx) /*!< in: transaction */
  349. MY_ATTRIBUTE((pure));
  350. /**********************************************************************//**
  351. Flag a transaction a dictionary operation. */
  352. UNIV_INLINE
  353. void
  354. trx_set_dict_operation(
  355. /*===================*/
  356. trx_t* trx, /*!< in/out: transaction */
  357. enum trx_dict_op_t op); /*!< in: operation, not
  358. TRX_DICT_OP_NONE */
  359. #ifndef UNIV_HOTBACKUP
  360. /**********************************************************************//**
  361. Determines if a transaction is in the given state.
  362. The caller must hold trx_sys->mutex, or it must be the thread
  363. that is serving a running transaction.
  364. A running transaction must be in trx_sys->ro_trx_list or trx_sys->rw_trx_list
  365. unless it is a non-locking autocommit read only transaction, which is only
  366. in trx_sys->mysql_trx_list.
  367. @return TRUE if trx->state == state */
  368. UNIV_INLINE
  369. ibool
  370. trx_state_eq(
  371. /*=========*/
  372. const trx_t* trx, /*!< in: transaction */
  373. trx_state_t state) /*!< in: state;
  374. if state != TRX_STATE_NOT_STARTED
  375. asserts that
  376. trx->state != TRX_STATE_NOT_STARTED */
  377. MY_ATTRIBUTE((nonnull, warn_unused_result));
  378. # ifdef UNIV_DEBUG
  379. /**********************************************************************//**
  380. Asserts that a transaction has been started.
  381. The caller must hold trx_sys->mutex.
  382. @return TRUE if started */
  383. UNIV_INTERN
  384. ibool
  385. trx_assert_started(
  386. /*===============*/
  387. const trx_t* trx) /*!< in: transaction */
  388. MY_ATTRIBUTE((nonnull, warn_unused_result));
  389. # endif /* UNIV_DEBUG */
  390. /**********************************************************************//**
  391. Determines if the currently running transaction has been interrupted.
  392. @return TRUE if interrupted */
  393. UNIV_INTERN
  394. ibool
  395. trx_is_interrupted(
  396. /*===============*/
  397. const trx_t* trx); /*!< in: transaction */
  398. /**********************************************************************//**
  399. Determines if the currently running transaction is in strict mode.
  400. @return TRUE if strict */
  401. UNIV_INTERN
  402. ibool
  403. trx_is_strict(
  404. /*==========*/
  405. trx_t* trx); /*!< in: transaction */
  406. #else /* !UNIV_HOTBACKUP */
  407. #define trx_is_interrupted(trx) FALSE
  408. #endif /* !UNIV_HOTBACKUP */
  409. /*******************************************************************//**
  410. Calculates the "weight" of a transaction. The weight of one transaction
  411. is estimated as the number of altered rows + the number of locked rows.
  412. @param t transaction
  413. @return transaction weight */
  414. #define TRX_WEIGHT(t) ((t)->undo_no + UT_LIST_GET_LEN((t)->lock.trx_locks))
  415. /*******************************************************************//**
  416. Compares the "weight" (or size) of two transactions. Transactions that
  417. have edited non-transactional tables are considered heavier than ones
  418. that have not.
  419. @return TRUE if weight(a) >= weight(b) */
  420. UNIV_INTERN
  421. ibool
  422. trx_weight_ge(
  423. /*==========*/
  424. const trx_t* a, /*!< in: the first transaction to be compared */
  425. const trx_t* b); /*!< in: the second transaction to be compared */
  426. /* Maximum length of a string that can be returned by
  427. trx_get_que_state_str(). */
  428. #define TRX_QUE_STATE_STR_MAX_LEN 12 /* "ROLLING BACK" */
  429. /*******************************************************************//**
  430. Retrieves transaction's que state in a human readable string. The string
  431. should not be free()'d or modified.
  432. @return string in the data segment */
  433. UNIV_INLINE
  434. const char*
  435. trx_get_que_state_str(
  436. /*==================*/
  437. const trx_t* trx); /*!< in: transaction */
  438. /****************************************************************//**
  439. Assign a read-only transaction a rollback-segment, if it is attempting
  440. to write to a TEMPORARY table. */
  441. UNIV_INTERN
  442. void
  443. trx_assign_rseg(
  444. /*============*/
  445. trx_t* trx); /*!< A read-only transaction that
  446. needs to be assigned a RBS. */
  447. /*************************************************************//**
  448. Callback function for trx_find_descriptor() to compare trx IDs. */
  449. UNIV_INTERN
  450. int
  451. trx_descr_cmp(
  452. /*==========*/
  453. const void *a, /*!< in: pointer to first comparison argument */
  454. const void *b); /*!< in: pointer to second comparison argument */
  455. /*************************************************************//**
  456. Release a slot for a given trx in the global descriptors array. */
  457. UNIV_INTERN
  458. void
  459. trx_release_descriptor(
  460. /*===================*/
  461. trx_t* trx); /*!< in: trx pointer */
  462. /*******************************************************************//**
  463. Transactions that aren't started by the MySQL server don't set
  464. the trx_t::mysql_thd field. For such transactions we set the lock
  465. wait timeout to 0 instead of the user configured value that comes
  466. from innodb_lock_wait_timeout via trx_t::mysql_thd.
  467. @param trx transaction
  468. @return lock wait timeout in seconds */
  469. #define trx_lock_wait_timeout_get(trx) \
  470. ((trx)->mysql_thd != NULL \
  471. ? thd_lock_wait_timeout((trx)->mysql_thd) \
  472. : 0)
  473. /*******************************************************************//**
  474. Determine if the transaction is a non-locking autocommit select
  475. (implied read-only).
  476. @param t transaction
  477. @return true if non-locking autocommit select transaction. */
  478. #define trx_is_autocommit_non_locking(t) \
  479. ((t)->auto_commit && (t)->will_lock == 0)
  480. /*******************************************************************//**
  481. Determine if the transaction is a non-locking autocommit select
  482. with an explicit check for the read-only status.
  483. @param t transaction
  484. @return true if non-locking autocommit read-only transaction. */
  485. #define trx_is_ac_nl_ro(t) \
  486. ((t)->read_only && trx_is_autocommit_non_locking((t)))
  487. /*******************************************************************//**
  488. Assert that the transaction is in the trx_sys_t::rw_trx_list */
  489. #define assert_trx_in_rw_list(t) do { \
  490. ut_ad(!(t)->read_only); \
  491. assert_trx_in_list(t); \
  492. } while (0)
  493. /*******************************************************************//**
  494. Assert that the transaction is either in trx_sys->ro_trx_list or
  495. trx_sys->rw_trx_list but not both and it cannot be an autocommit
  496. non-locking select */
  497. #define assert_trx_in_list(t) do { \
  498. ut_ad((t)->in_ro_trx_list == (t)->read_only); \
  499. ut_ad((t)->in_rw_trx_list == !(t)->read_only); \
  500. ut_ad(!trx_is_autocommit_non_locking((t))); \
  501. switch ((t)->state) { \
  502. case TRX_STATE_PREPARED: \
  503. /* fall through */ \
  504. case TRX_STATE_ACTIVE: \
  505. case TRX_STATE_COMMITTED_IN_MEMORY: \
  506. continue; \
  507. case TRX_STATE_NOT_STARTED: \
  508. break; \
  509. } \
  510. ut_error; \
  511. } while (0)
  512. #ifdef UNIV_DEBUG
  513. /*******************************************************************//**
  514. Assert that an autocommit non-locking select cannot be in the
  515. ro_trx_list nor the rw_trx_list and that it is a read-only transaction.
  516. The tranasction must be in the mysql_trx_list. */
  517. # define assert_trx_nonlocking_or_in_list(t) \
  518. do { \
  519. if (trx_is_autocommit_non_locking(t)) { \
  520. trx_state_t t_state = (t)->state; \
  521. ut_ad((t)->read_only); \
  522. ut_ad(!(t)->is_recovered); \
  523. ut_ad(!(t)->in_ro_trx_list); \
  524. ut_ad(!(t)->in_rw_trx_list); \
  525. ut_ad((t)->in_mysql_trx_list); \
  526. ut_ad(t_state == TRX_STATE_NOT_STARTED \
  527. || t_state == TRX_STATE_ACTIVE); \
  528. } else { \
  529. assert_trx_in_list(t); \
  530. } \
  531. } while (0)
  532. #else /* UNIV_DEBUG */
  533. /*******************************************************************//**
  534. Assert that an autocommit non-locking slect cannot be in the
  535. ro_trx_list nor the rw_trx_list and that it is a read-only transaction.
  536. The tranasction must be in the mysql_trx_list. */
  537. # define assert_trx_nonlocking_or_in_list(trx) ((void)0)
  538. #endif /* UNIV_DEBUG */
  539. /*******************************************************************//**
  540. Latching protocol for trx_lock_t::que_state. trx_lock_t::que_state
  541. captures the state of the query thread during the execution of a query.
  542. This is different from a transaction state. The query state of a transaction
  543. can be updated asynchronously by other threads. The other threads can be
  544. system threads, like the timeout monitor thread or user threads executing
  545. other queries. Another thing to be mindful of is that there is a delay between
  546. when a query thread is put into LOCK_WAIT state and before it actually starts
  547. waiting. Between these two events it is possible that the query thread is
  548. granted the lock it was waiting for, which implies that the state can be changed
  549. asynchronously.
  550. All these operations take place within the context of locking. Therefore state
  551. changes within the locking code must acquire both the lock mutex and the
  552. trx->mutex when changing trx->lock.que_state to TRX_QUE_LOCK_WAIT or
  553. trx->lock.wait_lock to non-NULL but when the lock wait ends it is sufficient
  554. to only acquire the trx->mutex.
  555. To query the state either of the mutexes is sufficient within the locking
  556. code and no mutex is required when the query thread is no longer waiting. */
  557. /** The locks and state of an active transaction. Protected by
  558. lock_sys->mutex, trx->mutex or both. */
  559. struct trx_lock_t {
  560. ulint n_active_thrs; /*!< number of active query threads */
  561. trx_que_t que_state; /*!< valid when trx->state
  562. == TRX_STATE_ACTIVE: TRX_QUE_RUNNING,
  563. TRX_QUE_LOCK_WAIT, ... */
  564. lock_t* wait_lock; /*!< if trx execution state is
  565. TRX_QUE_LOCK_WAIT, this points to
  566. the lock request, otherwise this is
  567. NULL; set to non-NULL when holding
  568. both trx->mutex and lock_sys->mutex;
  569. set to NULL when holding
  570. lock_sys->mutex; readers should
  571. hold lock_sys->mutex, except when
  572. they are holding trx->mutex and
  573. wait_lock==NULL */
  574. ib_uint64_t deadlock_mark; /*!< A mark field that is initialized
  575. to and checked against lock_mark_counter
  576. by lock_deadlock_recursive(). */
  577. ibool was_chosen_as_deadlock_victim;
  578. /*!< when the transaction decides to
  579. wait for a lock, it sets this to FALSE;
  580. if another transaction chooses this
  581. transaction as a victim in deadlock
  582. resolution, it sets this to TRUE.
  583. Protected by trx->mutex. */
  584. time_t wait_started; /*!< lock wait started at this time,
  585. protected only by lock_sys->mutex */
  586. que_thr_t* wait_thr; /*!< query thread belonging to this
  587. trx that is in QUE_THR_LOCK_WAIT
  588. state. For threads suspended in a
  589. lock wait, this is protected by
  590. lock_sys->mutex. Otherwise, this may
  591. only be modified by the thread that is
  592. serving the running transaction. */
  593. mem_heap_t* lock_heap; /*!< memory heap for trx_locks;
  594. protected by lock_sys->mutex */
  595. UT_LIST_BASE_NODE_T(lock_t)
  596. trx_locks; /*!< locks requested
  597. by the transaction;
  598. insertions are protected by trx->mutex
  599. and lock_sys->mutex; removals are
  600. protected by lock_sys->mutex */
  601. ib_vector_t* table_locks; /*!< All table locks requested by this
  602. transaction, including AUTOINC locks */
  603. ibool cancel; /*!< TRUE if the transaction is being
  604. rolled back either via deadlock
  605. detection or due to lock timeout. The
  606. caller has to acquire the trx_t::mutex
  607. in order to cancel the locks. In
  608. lock_trx_table_locks_remove() we
  609. check for this cancel of a transaction's
  610. locks and avoid reacquiring the trx
  611. mutex to prevent recursive deadlocks.
  612. Protected by both the lock sys mutex
  613. and the trx_t::mutex. */
  614. };
  615. #define TRX_MAGIC_N 91118598
  616. /** The transaction handle
  617. Normally, there is a 1:1 relationship between a transaction handle
  618. (trx) and a session (client connection). One session is associated
  619. with exactly one user transaction. There are some exceptions to this:
  620. * For DDL operations, a subtransaction is allocated that modifies the
  621. data dictionary tables. Lock waits and deadlocks are prevented by
  622. acquiring the dict_operation_lock before starting the subtransaction
  623. and releasing it after committing the subtransaction.
  624. * The purge system uses a special transaction that is not associated
  625. with any session.
  626. * If the system crashed or it was quickly shut down while there were
  627. transactions in the ACTIVE or PREPARED state, these transactions would
  628. no longer be associated with a session when the server is restarted.
  629. A session may be served by at most one thread at a time. The serving
  630. thread of a session might change in some MySQL implementations.
  631. Therefore we do not have os_thread_get_curr_id() assertions in the code.
  632. Normally, only the thread that is currently associated with a running
  633. transaction may access (read and modify) the trx object, and it may do
  634. so without holding any mutex. The following are exceptions to this:
  635. * trx_rollback_resurrected() may access resurrected (connectionless)
  636. transactions while the system is already processing new user
  637. transactions. The trx_sys->mutex prevents a race condition between it
  638. and lock_trx_release_locks() [invoked by trx_commit()].
  639. * trx_print_low() may access transactions not associated with the current
  640. thread. The caller must be holding trx_sys->mutex and lock_sys->mutex.
  641. * When a transaction handle is in the trx_sys->mysql_trx_list or
  642. trx_sys->trx_list, some of its fields must not be modified without
  643. holding trx_sys->mutex exclusively.
  644. * The locking code (in particular, lock_deadlock_recursive() and
  645. lock_rec_convert_impl_to_expl()) will access transactions associated
  646. to other connections. The locks of transactions are protected by
  647. lock_sys->mutex and sometimes by trx->mutex. */
  648. struct trx_t{
  649. ulint magic_n;
  650. ib_mutex_t mutex; /*!< Mutex protecting the fields
  651. state and lock
  652. (except some fields of lock, which
  653. are protected by lock_sys->mutex) */
  654. /** State of the trx from the point of view of concurrency control
  655. and the valid state transitions.
  656. Possible states:
  657. TRX_STATE_NOT_STARTED
  658. TRX_STATE_ACTIVE
  659. TRX_STATE_PREPARED
  660. TRX_STATE_COMMITTED_IN_MEMORY (alias below COMMITTED)
  661. Valid state transitions are:
  662. Regular transactions:
  663. * NOT_STARTED -> ACTIVE -> COMMITTED -> NOT_STARTED
  664. Auto-commit non-locking read-only:
  665. * NOT_STARTED -> ACTIVE -> NOT_STARTED
  666. XA (2PC):
  667. * NOT_STARTED -> ACTIVE -> PREPARED -> COMMITTED -> NOT_STARTED
  668. Recovered XA:
  669. * NOT_STARTED -> PREPARED -> COMMITTED -> (freed)
  670. XA (2PC) (shutdown before ROLLBACK or COMMIT):
  671. * NOT_STARTED -> PREPARED -> (freed)
  672. Latching and various transaction lists membership rules:
  673. XA (2PC) transactions are always treated as non-autocommit.
  674. Transitions to ACTIVE or NOT_STARTED occur when
  675. !in_rw_trx_list and !in_ro_trx_list (no trx_sys->mutex needed).
  676. Autocommit non-locking read-only transactions move between states
  677. without holding any mutex. They are !in_rw_trx_list, !in_ro_trx_list.
  678. When a transaction is NOT_STARTED, it can be in_mysql_trx_list if
  679. it is a user transaction. It cannot be in ro_trx_list or rw_trx_list.
  680. ACTIVE->PREPARED->COMMITTED is only possible when trx->in_rw_trx_list.
  681. The transition ACTIVE->PREPARED is protected by trx_sys->mutex.
  682. ACTIVE->COMMITTED is possible when the transaction is in
  683. ro_trx_list or rw_trx_list.
  684. Transitions to COMMITTED are protected by both lock_sys->mutex
  685. and trx->mutex.
  686. NOTE: Some of these state change constraints are an overkill,
  687. currently only required for a consistent view for printing stats.
  688. This unnecessarily adds a huge cost for the general case.
  689. NOTE: In the future we should add read only transactions to the
  690. ro_trx_list the first time they try to acquire a lock ie. by default
  691. we treat all read-only transactions as non-locking. */
  692. trx_state_t state;
  693. trx_lock_t lock; /*!< Information about the transaction
  694. locks and state. Protected by
  695. trx->mutex or lock_sys->mutex
  696. or both */
  697. ulint is_recovered; /*!< 0=normal transaction,
  698. 1=recovered, must be rolled back,
  699. protected by trx_sys->mutex when
  700. trx->in_rw_trx_list holds */
  701. /* These fields are not protected by any mutex. */
  702. const char* op_info; /*!< English text describing the
  703. current operation, or an empty
  704. string */
  705. ulint isolation_level;/*!< TRX_ISO_REPEATABLE_READ, ... */
  706. ulint check_foreigns; /*!< normally TRUE, but if the user
  707. wants to suppress foreign key checks,
  708. (in table imports, for example) we
  709. set this FALSE */
  710. /*------------------------------*/
  711. /* MySQL has a transaction coordinator to coordinate two phase
  712. commit between multiple storage engines and the binary log. When
  713. an engine participates in a transaction, it's responsible for
  714. registering itself using the trans_register_ha() API. */
  715. unsigned is_registered:1;/* This flag is set to 1 after the
  716. transaction has been registered with
  717. the coordinator using the XA API, and
  718. is set to 0 after commit or rollback. */
  719. unsigned owns_prepare_mutex:1;/* 1 if owns prepare mutex, if
  720. this is set to 1 then registered should
  721. also be set to 1. This is used in the
  722. XA code */
  723. /*------------------------------*/
  724. ulint check_unique_secondary;
  725. /*!< normally TRUE, but if the user
  726. wants to speed up inserts by
  727. suppressing unique key checks
  728. for secondary indexes when we decide
  729. if we can use the insert buffer for
  730. them, we set this FALSE */
  731. ulint support_xa; /*!< normally we do the XA two-phase
  732. commit steps, but by setting this to
  733. FALSE, one can save CPU time and about
  734. 150 bytes in the undo log size as then
  735. we skip XA steps */
  736. ulint fake_changes;
  737. ulint flush_log_later;/* In 2PC, we hold the
  738. prepare_commit mutex across
  739. both phases. In that case, we
  740. defer flush of the logs to disk
  741. until after we release the
  742. mutex. */
  743. ulint must_flush_log_later;/*!< this flag is set to TRUE in
  744. trx_commit() if flush_log_later was
  745. TRUE, and there were modifications by
  746. the transaction; in that case we must
  747. flush the log in
  748. trx_commit_complete_for_mysql() */
  749. ulint duplicates; /*!< TRX_DUP_IGNORE | TRX_DUP_REPLACE */
  750. bool has_search_latch;
  751. /*!< true if this trx has latched any
  752. search system latch in S-mode */
  753. ulint search_latch_timeout;
  754. /*!< If we notice that someone is
  755. waiting for our S-lock on the search
  756. latch to be released, we wait in
  757. row0sel.cc for BTR_SEA_TIMEOUT new
  758. searches until we try to keep
  759. the search latch again over
  760. calls from MySQL; this is intended
  761. to reduce contention on the search
  762. latch */
  763. trx_dict_op_t dict_operation; /**< @see enum trx_dict_op */
  764. /* Fields protected by the srv_conc_mutex. */
  765. ulint declared_to_be_inside_innodb;
  766. /*!< this is TRUE if we have declared
  767. this transaction in
  768. srv_conc_enter_innodb to be inside the
  769. InnoDB engine */
  770. ulint n_tickets_to_enter_innodb;
  771. /*!< this can be > 0 only when
  772. declared_to_... is TRUE; when we come
  773. to srv_conc_innodb_enter, if the value
  774. here is > 0, we decrement this by 1 */
  775. ulint dict_operation_lock_mode;
  776. /*!< 0, RW_S_LATCH, or RW_X_LATCH:
  777. the latch mode trx currently holds
  778. on dict_operation_lock. Protected
  779. by dict_operation_lock. */
  780. trx_id_t no; /*!< transaction serialization number:
  781. max trx id shortly before the
  782. transaction is moved to
  783. COMMITTED_IN_MEMORY state.
  784. Protected by trx_sys_t::mutex
  785. when trx->in_rw_trx_list. Initially
  786. set to TRX_ID_MAX. */
  787. time_t start_time; /*!< time the trx state last time became
  788. TRX_STATE_ACTIVE */
  789. trx_id_t id; /*!< transaction id */
  790. XID xid; /*!< X/Open XA transaction
  791. identification to identify a
  792. transaction branch */
  793. lsn_t commit_lsn; /*!< lsn at the time of the commit */
  794. table_id_t table_id; /*!< Table to drop iff dict_operation
  795. == TRX_DICT_OP_TABLE, or 0. */
  796. /*------------------------------*/
  797. THD* mysql_thd; /*!< MySQL thread handle corresponding
  798. to this trx, or NULL */
  799. const char* mysql_log_file_name;
  800. /*!< if MySQL binlog is used, this field
  801. contains a pointer to the latest file
  802. name; this is NULL if binlog is not
  803. used */
  804. ib_int64_t mysql_log_offset;
  805. /*!< if MySQL binlog is used, this
  806. field contains the end offset of the
  807. binlog entry */
  808. time_t idle_start;
  809. ib_int64_t last_stmt_start;
  810. /*------------------------------*/
  811. ulint n_mysql_tables_in_use; /*!< number of Innobase tables
  812. used in the processing of the current
  813. SQL statement in MySQL */
  814. ulint mysql_n_tables_locked;
  815. /*!< how many tables the current SQL
  816. statement uses, except those
  817. in consistent read */
  818. /*------------------------------*/
  819. UT_LIST_NODE_T(trx_t)
  820. trx_list; /*!< list of transactions;
  821. protected by trx_sys->mutex.
  822. The same node is used for both
  823. trx_sys_t::ro_trx_list and
  824. trx_sys_t::rw_trx_list */
  825. #ifdef UNIV_DEBUG
  826. /** The following two fields are mutually exclusive. */
  827. /* @{ */
  828. ibool in_ro_trx_list; /*!< TRUE if in trx_sys->ro_trx_list */
  829. ibool in_rw_trx_list; /*!< TRUE if in trx_sys->rw_trx_list */
  830. /* @} */
  831. #endif /* UNIV_DEBUG */
  832. UT_LIST_NODE_T(trx_t)
  833. mysql_trx_list; /*!< list of transactions created for
  834. MySQL; protected by trx_sys->mutex */
  835. #ifdef UNIV_DEBUG
  836. ibool in_mysql_trx_list;
  837. /*!< TRUE if in
  838. trx_sys->mysql_trx_list */
  839. #endif /* UNIV_DEBUG */
  840. UT_LIST_NODE_T(trx_t)
  841. trx_serial_list;/*!< list node for
  842. trx_sys->trx_serial_list */
  843. bool in_trx_serial_list;
  844. /* Set when transaction is in the
  845. trx_serial_list */
  846. /*------------------------------*/
  847. dberr_t error_state; /*!< 0 if no error, otherwise error
  848. number; NOTE That ONLY the thread
  849. doing the transaction is allowed to
  850. set this field: this is NOT protected
  851. by any mutex */
  852. const dict_index_t*error_info; /*!< if the error number indicates a
  853. duplicate key error, a pointer to
  854. the problematic index is stored here */
  855. ulint error_key_num; /*!< if the index creation fails to a
  856. duplicate key error, a mysql key
  857. number of that index is stored here */
  858. sess_t* sess; /*!< session of the trx, NULL if none */
  859. que_t* graph; /*!< query currently run in the session,
  860. or NULL if none; NOTE that the query
  861. belongs to the session, and it can
  862. survive over a transaction commit, if
  863. it is a stored procedure with a COMMIT
  864. WORK statement, for instance */
  865. read_view_t* global_read_view;
  866. /*!< consistent read view associated
  867. to a transaction or NULL */
  868. read_view_t* read_view; /*!< consistent read view used in the
  869. transaction or NULL, this read view
  870. if defined can be normal read view
  871. associated to a transaction (i.e.
  872. same as global_read_view) or read view
  873. associated to a cursor */
  874. read_view_t* prebuilt_view; /* pre-built view array */
  875. /*------------------------------*/
  876. UT_LIST_BASE_NODE_T(trx_named_savept_t)
  877. trx_savepoints; /*!< savepoints set with SAVEPOINT ...,
  878. oldest first */
  879. /*------------------------------*/
  880. ib_mutex_t undo_mutex; /*!< mutex protecting the fields in this
  881. section (down to undo_no_arr), EXCEPT
  882. last_sql_stat_start, which can be
  883. accessed only when we know that there
  884. cannot be any activity in the undo
  885. logs! */
  886. undo_no_t undo_no; /*!< next undo log record number to
  887. assign; since the undo log is
  888. private for a transaction, this
  889. is a simple ascending sequence
  890. with no gaps; thus it represents
  891. the number of modified/inserted
  892. rows in a transaction */
  893. trx_savept_t last_sql_stat_start;
  894. /*!< undo_no when the last sql statement
  895. was started: in case of an error, trx
  896. is rolled back down to this undo
  897. number; see note at undo_mutex! */
  898. trx_rseg_t* rseg; /*!< rollback segment assigned to the
  899. transaction, or NULL if not assigned
  900. yet */
  901. trx_undo_t* insert_undo; /*!< pointer to the insert undo log, or
  902. NULL if no inserts performed yet */
  903. trx_undo_t* update_undo; /*!< pointer to the update undo log, or
  904. NULL if no update performed yet */
  905. undo_no_t roll_limit; /*!< least undo number to undo during
  906. a rollback */
  907. ulint pages_undone; /*!< number of undo log pages undone
  908. since the last undo log truncation */
  909. trx_undo_arr_t* undo_no_arr; /*!< array of undo numbers of undo log
  910. records which are currently processed
  911. by a rollback operation */
  912. /*------------------------------*/
  913. ulint n_autoinc_rows; /*!< no. of AUTO-INC rows required for
  914. an SQL statement. This is useful for
  915. multi-row INSERTs */
  916. ib_vector_t* autoinc_locks; /* AUTOINC locks held by this
  917. transaction. Note that these are
  918. also in the lock list trx_locks. This
  919. vector needs to be freed explicitly
  920. when the trx instance is destroyed.
  921. Protected by lock_sys->mutex. */
  922. /*------------------------------*/
  923. ibool read_only; /*!< TRUE if transaction is flagged
  924. as a READ-ONLY transaction.
  925. if !auto_commit || will_lock > 0
  926. then it will added to the list
  927. trx_sys_t::ro_trx_list. A read only
  928. transaction will not be assigned an
  929. UNDO log. Non-locking auto-commit
  930. read-only transaction will not be on
  931. either list. */
  932. ibool auto_commit; /*!< TRUE if it is an autocommit */
  933. ulint will_lock; /*!< Will acquire some locks. Increment
  934. each time we determine that a lock will
  935. be acquired by the MySQL layer. */
  936. bool ddl; /*!< true if it is a transaction that
  937. is being started for a DDL operation */
  938. /*------------------------------*/
  939. fts_trx_t* fts_trx; /*!< FTS information, or NULL if
  940. transaction hasn't modified tables
  941. with FTS indexes (yet). */
  942. doc_id_t fts_next_doc_id;/* The document id used for updates */
  943. /*------------------------------*/
  944. ulint flush_tables; /*!< if "covering" the FLUSH TABLES",
  945. count of tables being flushed. */
  946. /*------------------------------*/
  947. #ifdef UNIV_DEBUG
  948. ulint start_line; /*!< Track where it was started from */
  949. const char* start_file; /*!< Filename where it was started */
  950. #endif /* UNIV_DEBUG */
  951. /*------------------------------*/
  952. bool api_trx; /*!< trx started by InnoDB API */
  953. bool api_auto_commit;/*!< automatic commit */
  954. bool read_write; /*!< if read and write operation */
  955. /*------------------------------*/
  956. char detailed_error[256]; /*!< detailed error message for last
  957. error, or empty. */
  958. /*------------------------------*/
  959. ulint io_reads;
  960. ib_uint64_t io_read;
  961. ulint io_reads_wait_timer;
  962. ib_uint64_t lock_que_wait_ustarted;
  963. ulint lock_que_wait_timer;
  964. ulint innodb_que_wait_timer;
  965. ulint distinct_page_access;
  966. #define DPAH_SIZE 8192
  967. byte* distinct_page_access_hash;
  968. ibool take_stats;
  969. };
  970. /* Transaction isolation levels (trx->isolation_level) */
  971. #define TRX_ISO_READ_UNCOMMITTED 0 /* dirty read: non-locking
  972. SELECTs are performed so that
  973. we do not look at a possible
  974. earlier version of a record;
  975. thus they are not 'consistent'
  976. reads under this isolation
  977. level; otherwise like level
  978. 2 */
  979. #define TRX_ISO_READ_COMMITTED 1 /* somewhat Oracle-like
  980. isolation, except that in
  981. range UPDATE and DELETE we
  982. must block phantom rows
  983. with next-key locks;
  984. SELECT ... FOR UPDATE and ...
  985. LOCK IN SHARE MODE only lock
  986. the index records, NOT the
  987. gaps before them, and thus
  988. allow free inserting;
  989. each consistent read reads its
  990. own snapshot */
  991. #define TRX_ISO_REPEATABLE_READ 2 /* this is the default;
  992. all consistent reads in the
  993. same trx read the same
  994. snapshot;
  995. full next-key locking used
  996. in locking reads to block
  997. insertions into gaps */
  998. #define TRX_ISO_SERIALIZABLE 3 /* all plain SELECTs are
  999. converted to LOCK IN SHARE
  1000. MODE reads */
  1001. /* Treatment of duplicate values (trx->duplicates; for example, in inserts).
  1002. Multiple flags can be combined with bitwise OR. */
  1003. #define TRX_DUP_IGNORE 1 /* duplicate rows are to be updated */
  1004. #define TRX_DUP_REPLACE 2 /* duplicate rows are to be replaced */
  1005. /* Types of a trx signal */
  1006. #define TRX_SIG_NO_SIGNAL 0
  1007. #define TRX_SIG_TOTAL_ROLLBACK 1
  1008. #define TRX_SIG_ROLLBACK_TO_SAVEPT 2
  1009. #define TRX_SIG_COMMIT 3
  1010. #define TRX_SIG_BREAK_EXECUTION 5
  1011. /* Sender types of a signal */
  1012. #define TRX_SIG_SELF 0 /* sent by the session itself, or
  1013. by an error occurring within this
  1014. session */
  1015. #define TRX_SIG_OTHER_SESS 1 /* sent by another session (which
  1016. must hold rights to this) */
  1017. /** Commit node states */
  1018. enum commit_node_state {
  1019. COMMIT_NODE_SEND = 1, /*!< about to send a commit signal to
  1020. the transaction */
  1021. COMMIT_NODE_WAIT /*!< commit signal sent to the transaction,
  1022. waiting for completion */
  1023. };
  1024. /** Commit command node in a query graph */
  1025. struct commit_node_t{
  1026. que_common_t common; /*!< node type: QUE_NODE_COMMIT */
  1027. enum commit_node_state
  1028. state; /*!< node execution state */
  1029. };
  1030. /** Test if trx->mutex is owned. */
  1031. #define trx_mutex_own(t) mutex_own(&t->mutex)
  1032. /** Acquire the trx->mutex. */
  1033. #define trx_mutex_enter(t) do { \
  1034. mutex_enter(&t->mutex); \
  1035. } while (0)
  1036. /** Release the trx->mutex. */
  1037. #define trx_mutex_exit(t) do { \
  1038. mutex_exit(&t->mutex); \
  1039. } while (0)
  1040. /** @brief The latch protecting the adaptive search system
  1041. This latch protects the
  1042. (1) hash index;
  1043. (2) columns of a record to which we have a pointer in the hash index;
  1044. but does NOT protect:
  1045. (3) next record offset field in a record;
  1046. (4) next or previous records on the same page.
  1047. Bear in mind (3) and (4) when using the hash index.
  1048. */
  1049. extern prio_rw_lock_t* btr_search_latch_arr;
  1050. #ifndef UNIV_NONINL
  1051. #include "trx0trx.ic"
  1052. #endif
  1053. #endif /* !UNIV_HOTBACKUP */
  1054. #endif