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