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.

377 lines
11 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1997, 2009, Innobase Oy. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file row/row0undo.c
  15. Row undo
  16. Created 1/8/1997 Heikki Tuuri
  17. *******************************************************/
  18. #include "row0undo.h"
  19. #ifdef UNIV_NONINL
  20. #include "row0undo.ic"
  21. #endif
  22. #include "fsp0fsp.h"
  23. #include "mach0data.h"
  24. #include "trx0rseg.h"
  25. #include "trx0trx.h"
  26. #include "trx0roll.h"
  27. #include "trx0undo.h"
  28. #include "trx0purge.h"
  29. #include "trx0rec.h"
  30. #include "que0que.h"
  31. #include "row0row.h"
  32. #include "row0uins.h"
  33. #include "row0umod.h"
  34. #include "row0upd.h"
  35. #include "row0mysql.h"
  36. #include "srv0srv.h"
  37. /* How to undo row operations?
  38. (1) For an insert, we have stored a prefix of the clustered index record
  39. in the undo log. Using it, we look for the clustered record, and using
  40. that we look for the records in the secondary indexes. The insert operation
  41. may have been left incomplete, if the database crashed, for example.
  42. We may have look at the trx id and roll ptr to make sure the record in the
  43. clustered index is really the one for which the undo log record was
  44. written. We can use the framework we get from the original insert op.
  45. (2) Delete marking: We can use the framework we get from the original
  46. delete mark op. We only have to check the trx id.
  47. (3) Update: This may be the most complicated. We have to use the framework
  48. we get from the original update op.
  49. What if the same trx repeatedly deletes and inserts an identical row.
  50. Then the row id changes and also roll ptr. What if the row id was not
  51. part of the ordering fields in the clustered index? Maybe we have to write
  52. it to undo log. Well, maybe not, because if we order the row id and trx id
  53. in descending order, then the only undeleted copy is the first in the
  54. index. Our searches in row operations always position the cursor before
  55. the first record in the result set. But, if there is no key defined for
  56. a table, then it would be desirable that row id is in ascending order.
  57. So, lets store row id in descending order only if it is not an ordering
  58. field in the clustered index.
  59. NOTE: Deletes and inserts may lead to situation where there are identical
  60. records in a secondary index. Is that a problem in the B-tree? Yes.
  61. Also updates can lead to this, unless trx id and roll ptr are included in
  62. ord fields.
  63. (1) Fix in clustered indexes: include row id, trx id, and roll ptr
  64. in node pointers of B-tree.
  65. (2) Fix in secondary indexes: include all fields in node pointers, and
  66. if an entry is inserted, check if it is equal to the right neighbor,
  67. in which case update the right neighbor: the neighbor must be delete
  68. marked, set it unmarked and write the trx id of the current transaction.
  69. What if the same trx repeatedly updates the same row, updating a secondary
  70. index field or not? Updating a clustered index ordering field?
  71. (1) If it does not update the secondary index and not the clustered index
  72. ord field. Then the secondary index record stays unchanged, but the
  73. trx id in the secondary index record may be smaller than in the clustered
  74. index record. This is no problem?
  75. (2) If it updates secondary index ord field but not clustered: then in
  76. secondary index there are delete marked records, which differ in an
  77. ord field. No problem.
  78. (3) Updates clustered ord field but not secondary, and secondary index
  79. is unique. Then the record in secondary index is just updated at the
  80. clustered ord field.
  81. (4)
  82. Problem with duplicate records:
  83. Fix 1: Add a trx op no field to all indexes. A problem: if a trx with a
  84. bigger trx id has inserted and delete marked a similar row, our trx inserts
  85. again a similar row, and a trx with an even bigger id delete marks it. Then
  86. the position of the row should change in the index if the trx id affects
  87. the alphabetical ordering.
  88. Fix 2: If an insert encounters a similar row marked deleted, we turn the
  89. insert into an 'update' of the row marked deleted. Then we must write undo
  90. info on the update. A problem: what if a purge operation tries to remove
  91. the delete marked row?
  92. We can think of the database row versions as a linked list which starts
  93. from the record in the clustered index, and is linked by roll ptrs
  94. through undo logs. The secondary index records are references which tell
  95. what kinds of records can be found in this linked list for a record
  96. in the clustered index.
  97. How to do the purge? A record can be removed from the clustered index
  98. if its linked list becomes empty, i.e., the row has been marked deleted
  99. and its roll ptr points to the record in the undo log we are going through,
  100. doing the purge. Similarly, during a rollback, a record can be removed
  101. if the stored roll ptr in the undo log points to a trx already (being) purged,
  102. or if the roll ptr is NULL, i.e., it was a fresh insert. */
  103. /********************************************************************//**
  104. Creates a row undo node to a query graph.
  105. @return own: undo node */
  106. UNIV_INTERN
  107. undo_node_t*
  108. row_undo_node_create(
  109. /*=================*/
  110. trx_t* trx, /*!< in: transaction */
  111. que_thr_t* parent, /*!< in: parent node, i.e., a thr node */
  112. mem_heap_t* heap) /*!< in: memory heap where created */
  113. {
  114. undo_node_t* undo;
  115. ut_ad(trx && parent && heap);
  116. undo = mem_heap_alloc(heap, sizeof(undo_node_t));
  117. undo->common.type = QUE_NODE_UNDO;
  118. undo->common.parent = parent;
  119. undo->state = UNDO_NODE_FETCH_NEXT;
  120. undo->trx = trx;
  121. btr_pcur_init(&(undo->pcur));
  122. undo->heap = mem_heap_create(256);
  123. return(undo);
  124. }
  125. /***********************************************************//**
  126. Looks for the clustered index record when node has the row reference.
  127. The pcur in node is used in the search. If found, stores the row to node,
  128. and stores the position of pcur, and detaches it. The pcur must be closed
  129. by the caller in any case.
  130. @return TRUE if found; NOTE the node->pcur must be closed by the
  131. caller, regardless of the return value */
  132. UNIV_INTERN
  133. ibool
  134. row_undo_search_clust_to_pcur(
  135. /*==========================*/
  136. undo_node_t* node) /*!< in: row undo node */
  137. {
  138. dict_index_t* clust_index;
  139. ibool found;
  140. mtr_t mtr;
  141. ibool ret;
  142. rec_t* rec;
  143. mem_heap_t* heap = NULL;
  144. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  145. ulint* offsets = offsets_;
  146. rec_offs_init(offsets_);
  147. mtr_start(&mtr);
  148. clust_index = dict_table_get_first_index(node->table);
  149. found = row_search_on_row_ref(&(node->pcur), BTR_MODIFY_LEAF,
  150. node->table, node->ref, &mtr);
  151. rec = btr_pcur_get_rec(&(node->pcur));
  152. offsets = rec_get_offsets(rec, clust_index, offsets,
  153. ULINT_UNDEFINED, &heap);
  154. if (!found || 0 != ut_dulint_cmp(node->roll_ptr,
  155. row_get_rec_roll_ptr(rec, clust_index,
  156. offsets))) {
  157. /* We must remove the reservation on the undo log record
  158. BEFORE releasing the latch on the clustered index page: this
  159. is to make sure that some thread will eventually undo the
  160. modification corresponding to node->roll_ptr. */
  161. /* fputs("--------------------undoing a previous version\n",
  162. stderr); */
  163. ret = FALSE;
  164. } else {
  165. node->row = row_build(ROW_COPY_DATA, clust_index, rec,
  166. offsets, NULL, &node->ext, node->heap);
  167. if (node->update) {
  168. node->undo_row = dtuple_copy(node->row, node->heap);
  169. row_upd_replace(node->undo_row, &node->undo_ext,
  170. clust_index, node->update, node->heap);
  171. } else {
  172. node->undo_row = NULL;
  173. node->undo_ext = NULL;
  174. }
  175. btr_pcur_store_position(&(node->pcur), &mtr);
  176. ret = TRUE;
  177. }
  178. btr_pcur_commit_specify_mtr(&(node->pcur), &mtr);
  179. if (UNIV_LIKELY_NULL(heap)) {
  180. mem_heap_free(heap);
  181. }
  182. return(ret);
  183. }
  184. /***********************************************************//**
  185. Fetches an undo log record and does the undo for the recorded operation.
  186. If none left, or a partial rollback completed, returns control to the
  187. parent node, which is always a query thread node.
  188. @return DB_SUCCESS if operation successfully completed, else error code */
  189. static
  190. ulint
  191. row_undo(
  192. /*=====*/
  193. undo_node_t* node, /*!< in: row undo node */
  194. que_thr_t* thr) /*!< in: query thread */
  195. {
  196. ulint err;
  197. trx_t* trx;
  198. roll_ptr_t roll_ptr;
  199. ibool locked_data_dict;
  200. ut_ad(node && thr);
  201. trx = node->trx;
  202. if (node->state == UNDO_NODE_FETCH_NEXT) {
  203. node->undo_rec = trx_roll_pop_top_rec_of_trx(trx,
  204. trx->roll_limit,
  205. &roll_ptr,
  206. node->heap);
  207. if (!node->undo_rec) {
  208. /* Rollback completed for this query thread */
  209. thr->run_node = que_node_get_parent(node);
  210. return(DB_SUCCESS);
  211. }
  212. node->roll_ptr = roll_ptr;
  213. node->undo_no = trx_undo_rec_get_undo_no(node->undo_rec);
  214. if (trx_undo_roll_ptr_is_insert(roll_ptr)) {
  215. node->state = UNDO_NODE_INSERT;
  216. } else {
  217. node->state = UNDO_NODE_MODIFY;
  218. }
  219. } else if (node->state == UNDO_NODE_PREV_VERS) {
  220. /* Undo should be done to the same clustered index record
  221. again in this same rollback, restoring the previous version */
  222. roll_ptr = node->new_roll_ptr;
  223. node->undo_rec = trx_undo_get_undo_rec_low(roll_ptr,
  224. node->heap);
  225. node->roll_ptr = roll_ptr;
  226. node->undo_no = trx_undo_rec_get_undo_no(node->undo_rec);
  227. if (trx_undo_roll_ptr_is_insert(roll_ptr)) {
  228. node->state = UNDO_NODE_INSERT;
  229. } else {
  230. node->state = UNDO_NODE_MODIFY;
  231. }
  232. }
  233. /* Prevent DROP TABLE etc. while we are rolling back this row.
  234. If we are doing a TABLE CREATE or some other dictionary operation,
  235. then we already have dict_operation_lock locked in x-mode. Do not
  236. try to lock again, because that would cause a hang. */
  237. locked_data_dict = (trx->dict_operation_lock_mode == 0);
  238. if (locked_data_dict) {
  239. row_mysql_lock_data_dictionary(trx);
  240. }
  241. if (node->state == UNDO_NODE_INSERT) {
  242. err = row_undo_ins(node);
  243. node->state = UNDO_NODE_FETCH_NEXT;
  244. } else {
  245. ut_ad(node->state == UNDO_NODE_MODIFY);
  246. err = row_undo_mod(node, thr);
  247. }
  248. if (locked_data_dict) {
  249. row_mysql_unlock_data_dictionary(trx);
  250. }
  251. /* Do some cleanup */
  252. btr_pcur_close(&(node->pcur));
  253. mem_heap_empty(node->heap);
  254. thr->run_node = node;
  255. return(err);
  256. }
  257. /***********************************************************//**
  258. Undoes a row operation in a table. This is a high-level function used
  259. in SQL execution graphs.
  260. @return query thread to run next or NULL */
  261. UNIV_INTERN
  262. que_thr_t*
  263. row_undo_step(
  264. /*==========*/
  265. que_thr_t* thr) /*!< in: query thread */
  266. {
  267. ulint err;
  268. undo_node_t* node;
  269. trx_t* trx;
  270. ut_ad(thr);
  271. srv_activity_count++;
  272. trx = thr_get_trx(thr);
  273. node = thr->run_node;
  274. ut_ad(que_node_get_type(node) == QUE_NODE_UNDO);
  275. err = row_undo(node, thr);
  276. trx->error_state = err;
  277. if (err != DB_SUCCESS) {
  278. /* SQL error detected */
  279. fprintf(stderr, "InnoDB: Fatal error %lu in rollback.\n",
  280. (ulong) err);
  281. if (err == DB_OUT_OF_FILE_SPACE) {
  282. fprintf(stderr,
  283. "InnoDB: Error 13 means out of tablespace.\n"
  284. "InnoDB: Consider increasing"
  285. " your tablespace.\n");
  286. exit(1);
  287. }
  288. ut_error;
  289. return(NULL);
  290. }
  291. return(thr);
  292. }