MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
row0undo.cc
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1997, 2012, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation; version 2 of the License.
8 
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16 
17 *****************************************************************************/
18 
19 /**************************************************/
26 #include "row0undo.h"
27 
28 #ifdef UNIV_NONINL
29 #include "row0undo.ic"
30 #endif
31 
32 #include "fsp0fsp.h"
33 #include "mach0data.h"
34 #include "trx0rseg.h"
35 #include "trx0trx.h"
36 #include "trx0roll.h"
37 #include "trx0undo.h"
38 #include "trx0purge.h"
39 #include "trx0rec.h"
40 #include "que0que.h"
41 #include "row0row.h"
42 #include "row0uins.h"
43 #include "row0umod.h"
44 #include "row0upd.h"
45 #include "row0mysql.h"
46 #include "srv0srv.h"
47 
48 /* How to undo row operations?
49 (1) For an insert, we have stored a prefix of the clustered index record
50 in the undo log. Using it, we look for the clustered record, and using
51 that we look for the records in the secondary indexes. The insert operation
52 may have been left incomplete, if the database crashed, for example.
53 We may have look at the trx id and roll ptr to make sure the record in the
54 clustered index is really the one for which the undo log record was
55 written. We can use the framework we get from the original insert op.
56 (2) Delete marking: We can use the framework we get from the original
57 delete mark op. We only have to check the trx id.
58 (3) Update: This may be the most complicated. We have to use the framework
59 we get from the original update op.
60 
61 What if the same trx repeatedly deletes and inserts an identical row.
62 Then the row id changes and also roll ptr. What if the row id was not
63 part of the ordering fields in the clustered index? Maybe we have to write
64 it to undo log. Well, maybe not, because if we order the row id and trx id
65 in descending order, then the only undeleted copy is the first in the
66 index. Our searches in row operations always position the cursor before
67 the first record in the result set. But, if there is no key defined for
68 a table, then it would be desirable that row id is in ascending order.
69 So, lets store row id in descending order only if it is not an ordering
70 field in the clustered index.
71 
72 NOTE: Deletes and inserts may lead to situation where there are identical
73 records in a secondary index. Is that a problem in the B-tree? Yes.
74 Also updates can lead to this, unless trx id and roll ptr are included in
75 ord fields.
76 (1) Fix in clustered indexes: include row id, trx id, and roll ptr
77 in node pointers of B-tree.
78 (2) Fix in secondary indexes: include all fields in node pointers, and
79 if an entry is inserted, check if it is equal to the right neighbor,
80 in which case update the right neighbor: the neighbor must be delete
81 marked, set it unmarked and write the trx id of the current transaction.
82 
83 What if the same trx repeatedly updates the same row, updating a secondary
84 index field or not? Updating a clustered index ordering field?
85 
86 (1) If it does not update the secondary index and not the clustered index
87 ord field. Then the secondary index record stays unchanged, but the
88 trx id in the secondary index record may be smaller than in the clustered
89 index record. This is no problem?
90 (2) If it updates secondary index ord field but not clustered: then in
91 secondary index there are delete marked records, which differ in an
92 ord field. No problem.
93 (3) Updates clustered ord field but not secondary, and secondary index
94 is unique. Then the record in secondary index is just updated at the
95 clustered ord field.
96 (4)
97 
98 Problem with duplicate records:
99 Fix 1: Add a trx op no field to all indexes. A problem: if a trx with a
100 bigger trx id has inserted and delete marked a similar row, our trx inserts
101 again a similar row, and a trx with an even bigger id delete marks it. Then
102 the position of the row should change in the index if the trx id affects
103 the alphabetical ordering.
104 
105 Fix 2: If an insert encounters a similar row marked deleted, we turn the
106 insert into an 'update' of the row marked deleted. Then we must write undo
107 info on the update. A problem: what if a purge operation tries to remove
108 the delete marked row?
109 
110 We can think of the database row versions as a linked list which starts
111 from the record in the clustered index, and is linked by roll ptrs
112 through undo logs. The secondary index records are references which tell
113 what kinds of records can be found in this linked list for a record
114 in the clustered index.
115 
116 How to do the purge? A record can be removed from the clustered index
117 if its linked list becomes empty, i.e., the row has been marked deleted
118 and its roll ptr points to the record in the undo log we are going through,
119 doing the purge. Similarly, during a rollback, a record can be removed
120 if the stored roll ptr in the undo log points to a trx already (being) purged,
121 or if the roll ptr is NULL, i.e., it was a fresh insert. */
122 
123 /********************************************************************/
126 UNIV_INTERN
129 /*=================*/
130  trx_t* trx,
131  que_thr_t* parent,
132  mem_heap_t* heap)
133 {
134  undo_node_t* undo;
135 
136  ut_ad(trx && parent && heap);
137 
138  undo = static_cast<undo_node_t*>(
139  mem_heap_alloc(heap, sizeof(undo_node_t)));
140 
141  undo->common.type = QUE_NODE_UNDO;
142  undo->common.parent = parent;
143 
144  undo->state = UNDO_NODE_FETCH_NEXT;
145  undo->trx = trx;
146 
147  btr_pcur_init(&(undo->pcur));
148 
149  undo->heap = mem_heap_create(256);
150 
151  return(undo);
152 }
153 
154 /***********************************************************/
161 UNIV_INTERN
162 ibool
164 /*==========================*/
165  undo_node_t* node)
166 {
167  dict_index_t* clust_index;
168  ibool found;
169  mtr_t mtr;
170  ibool ret;
171  rec_t* rec;
172  mem_heap_t* heap = NULL;
173  ulint offsets_[REC_OFFS_NORMAL_SIZE];
174  ulint* offsets = offsets_;
175  rec_offs_init(offsets_);
176 
177  mtr_start(&mtr);
178 
179  clust_index = dict_table_get_first_index(node->table);
180 
181  found = row_search_on_row_ref(&(node->pcur), BTR_MODIFY_LEAF,
182  node->table, node->ref, &mtr);
183 
184  rec = btr_pcur_get_rec(&(node->pcur));
185 
186  offsets = rec_get_offsets(rec, clust_index, offsets,
187  ULINT_UNDEFINED, &heap);
188 
189  if (!found || node->roll_ptr
190  != row_get_rec_roll_ptr(rec, clust_index, offsets)) {
191 
192  /* We must remove the reservation on the undo log record
193  BEFORE releasing the latch on the clustered index page: this
194  is to make sure that some thread will eventually undo the
195  modification corresponding to node->roll_ptr. */
196 
197  /* fputs("--------------------undoing a previous version\n",
198  stderr); */
199 
200  ret = FALSE;
201  } else {
202  row_ext_t** ext;
203 
204  if (dict_table_get_format(node->table) >= UNIV_FORMAT_B) {
205  /* In DYNAMIC or COMPRESSED format, there is
206  no prefix of externally stored columns in the
207  clustered index record. Build a cache of
208  column prefixes. */
209  ext = &node->ext;
210  } else {
211  /* REDUNDANT and COMPACT formats store a local
212  768-byte prefix of each externally stored
213  column. No cache is needed. */
214  ext = NULL;
215  node->ext = NULL;
216  }
217 
218  node->row = row_build(ROW_COPY_DATA, clust_index, rec,
219  offsets, NULL,
220  NULL, NULL, ext, node->heap);
221  if (node->rec_type == TRX_UNDO_UPD_EXIST_REC) {
222  node->undo_row = dtuple_copy(node->row, node->heap);
223  row_upd_replace(node->undo_row, &node->undo_ext,
224  clust_index, node->update, node->heap);
225  } else {
226  node->undo_row = NULL;
227  node->undo_ext = NULL;
228  }
229 
230  btr_pcur_store_position(&(node->pcur), &mtr);
231 
232  ret = TRUE;
233  }
234 
235  btr_pcur_commit_specify_mtr(&(node->pcur), &mtr);
236 
237  if (UNIV_LIKELY_NULL(heap)) {
238  mem_heap_free(heap);
239  }
240  return(ret);
241 }
242 
243 /***********************************************************/
248 static __attribute__((nonnull, warn_unused_result))
249 dberr_t
250 row_undo(
251 /*=====*/
252  undo_node_t* node,
253  que_thr_t* thr)
254 {
255  dberr_t err;
256  trx_t* trx;
257  roll_ptr_t roll_ptr;
258  ibool locked_data_dict;
259 
260  ut_ad(node && thr);
261 
262  trx = node->trx;
263 
264  if (node->state == UNDO_NODE_FETCH_NEXT) {
265 
266  node->undo_rec = trx_roll_pop_top_rec_of_trx(trx,
267  trx->roll_limit,
268  &roll_ptr,
269  node->heap);
270  if (!node->undo_rec) {
271  /* Rollback completed for this query thread */
272 
273  thr->run_node = que_node_get_parent(node);
274 
275  return(DB_SUCCESS);
276  }
277 
278  node->roll_ptr = roll_ptr;
279  node->undo_no = trx_undo_rec_get_undo_no(node->undo_rec);
280 
281  if (trx_undo_roll_ptr_is_insert(roll_ptr)) {
282 
283  node->state = UNDO_NODE_INSERT;
284  } else {
285  node->state = UNDO_NODE_MODIFY;
286  }
287  }
288 
289  /* Prevent DROP TABLE etc. while we are rolling back this row.
290  If we are doing a TABLE CREATE or some other dictionary operation,
291  then we already have dict_operation_lock locked in x-mode. Do not
292  try to lock again, because that would cause a hang. */
293 
294  locked_data_dict = (trx->dict_operation_lock_mode == 0);
295 
296  if (locked_data_dict) {
297 
298  row_mysql_freeze_data_dictionary(trx);
299  }
300 
301  if (node->state == UNDO_NODE_INSERT) {
302 
303  err = row_undo_ins(node);
304 
305  node->state = UNDO_NODE_FETCH_NEXT;
306  } else {
307  ut_ad(node->state == UNDO_NODE_MODIFY);
308  err = row_undo_mod(node, thr);
309  }
310 
311  if (locked_data_dict) {
312 
314  }
315 
316  /* Do some cleanup */
317  btr_pcur_close(&(node->pcur));
318 
319  mem_heap_empty(node->heap);
320 
321  thr->run_node = node;
322 
323  return(err);
324 }
325 
326 /***********************************************************/
330 UNIV_INTERN
331 que_thr_t*
333 /*==========*/
334  que_thr_t* thr)
335 {
336  dberr_t err;
337  undo_node_t* node;
338  trx_t* trx;
339 
340  ut_ad(thr);
341 
343 
344  trx = thr_get_trx(thr);
345 
346  node = static_cast<undo_node_t*>(thr->run_node);
347 
348  ut_ad(que_node_get_type(node) == QUE_NODE_UNDO);
349 
350  err = row_undo(node, thr);
351 
352  trx->error_state = err;
353 
354  if (err != DB_SUCCESS) {
355  /* SQL error detected */
356 
357  fprintf(stderr, "InnoDB: Fatal error (%s) in rollback.\n",
358  ut_strerr(err));
359 
360  if (err == DB_OUT_OF_FILE_SPACE) {
361  fprintf(stderr,
362  "InnoDB: Out of tablespace.\n"
363  "InnoDB: Consider increasing"
364  " your tablespace.\n");
365 
366  exit(1);
367  }
368 
369  ut_error;
370 
371  return(NULL);
372  }
373 
374  return(thr);
375 }