drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
row0undo.cc
1/*****************************************************************************
2
3Copyright (C) 1997, 2009, Innobase Oy. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free Software
7Foundation; version 2 of the License.
8
9This program is distributed in the hope that it will be useful, but WITHOUT
10ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License along with
14this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15St, Fifth Floor, Boston, MA 02110-1301 USA
16
17*****************************************************************************/
18
19/**************************************************/
25
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
50in the undo log. Using it, we look for the clustered record, and using
51that we look for the records in the secondary indexes. The insert operation
52may have been left incomplete, if the database crashed, for example.
53We may have look at the trx id and roll ptr to make sure the record in the
54clustered index is really the one for which the undo log record was
55written. 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
57delete 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
59we get from the original update op.
60
61What if the same trx repeatedly deletes and inserts an identical row.
62Then the row id changes and also roll ptr. What if the row id was not
63part of the ordering fields in the clustered index? Maybe we have to write
64it to undo log. Well, maybe not, because if we order the row id and trx id
65in descending order, then the only undeleted copy is the first in the
66index. Our searches in row operations always position the cursor before
67the first record in the result set. But, if there is no key defined for
68a table, then it would be desirable that row id is in ascending order.
69So, lets store row id in descending order only if it is not an ordering
70field in the clustered index.
71
72NOTE: Deletes and inserts may lead to situation where there are identical
73records in a secondary index. Is that a problem in the B-tree? Yes.
74Also updates can lead to this, unless trx id and roll ptr are included in
75ord fields.
76(1) Fix in clustered indexes: include row id, trx id, and roll ptr
77in node pointers of B-tree.
78(2) Fix in secondary indexes: include all fields in node pointers, and
79if an entry is inserted, check if it is equal to the right neighbor,
80in which case update the right neighbor: the neighbor must be delete
81marked, set it unmarked and write the trx id of the current transaction.
82
83What if the same trx repeatedly updates the same row, updating a secondary
84index 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
87ord field. Then the secondary index record stays unchanged, but the
88trx id in the secondary index record may be smaller than in the clustered
89index record. This is no problem?
90(2) If it updates secondary index ord field but not clustered: then in
91secondary index there are delete marked records, which differ in an
92ord field. No problem.
93(3) Updates clustered ord field but not secondary, and secondary index
94is unique. Then the record in secondary index is just updated at the
95clustered ord field.
96(4)
97
98Problem with duplicate records:
99Fix 1: Add a trx op no field to all indexes. A problem: if a trx with a
100bigger trx id has inserted and delete marked a similar row, our trx inserts
101again a similar row, and a trx with an even bigger id delete marks it. Then
102the position of the row should change in the index if the trx id affects
103the alphabetical ordering.
104
105Fix 2: If an insert encounters a similar row marked deleted, we turn the
106insert into an 'update' of the row marked deleted. Then we must write undo
107info on the update. A problem: what if a purge operation tries to remove
108the delete marked row?
109
110We can think of the database row versions as a linked list which starts
111from the record in the clustered index, and is linked by roll ptrs
112through undo logs. The secondary index records are references which tell
113what kinds of records can be found in this linked list for a record
114in the clustered index.
115
116How to do the purge? A record can be removed from the clustered index
117if its linked list becomes empty, i.e., the row has been marked deleted
118and its roll ptr points to the record in the undo log we are going through,
119doing the purge. Similarly, during a rollback, a record can be removed
120if the stored roll ptr in the undo log points to a trx already (being) purged,
121or if the roll ptr is NULL, i.e., it was a fresh insert. */
122
123/********************************************************************/
126UNIV_INTERN
127undo_node_t*
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 *>(mem_heap_alloc(heap, sizeof(undo_node_t)));
139
140 undo->common.type = QUE_NODE_UNDO;
141 undo->common.parent = parent;
142
144 undo->trx = trx;
145
146 btr_pcur_init(&(undo->pcur));
147
148 undo->heap = mem_heap_create(256);
149
150 return(undo);
151}
152
153/***********************************************************/
160UNIV_INTERN
161ibool
163/*==========================*/
164 undo_node_t* node)
165{
166 dict_index_t* clust_index;
167 ibool found;
168 mtr_t mtr;
169 ibool ret;
170 rec_t* rec;
171 mem_heap_t* heap = NULL;
172 ulint offsets_[REC_OFFS_NORMAL_SIZE];
173 ulint* offsets = offsets_;
174 rec_offs_init(offsets_);
175
176 mtr_start(&mtr);
177
178 clust_index = dict_table_get_first_index(node->table);
179
181 node->table, node->ref, &mtr);
182
183 rec = btr_pcur_get_rec(&(node->pcur));
184
185 offsets = rec_get_offsets(rec, clust_index, offsets,
186 ULINT_UNDEFINED, &heap);
187
188 if (!found || node->roll_ptr
189 != row_get_rec_roll_ptr(rec, clust_index, offsets)) {
190
191 /* We must remove the reservation on the undo log record
192 BEFORE releasing the latch on the clustered index page: this
193 is to make sure that some thread will eventually undo the
194 modification corresponding to node->roll_ptr. */
195
196 /* fputs("--------------------undoing a previous version\n",
197 stderr); */
198
199 ret = FALSE;
200 } else {
201 row_ext_t** ext;
202
204 /* In DYNAMIC or COMPRESSED format, there is
205 no prefix of externally stored columns in the
206 clustered index record. Build a cache of
207 column prefixes. */
208 ext = &node->ext;
209 } else {
210 /* REDUNDANT and COMPACT formats store a local
211 768-byte prefix of each externally stored
212 column. No cache is needed. */
213 ext = NULL;
214 node->ext = NULL;
215 }
216
217 node->row = row_build(ROW_COPY_DATA, clust_index, rec,
218 offsets, NULL, ext, node->heap);
219 if (node->update) {
220 node->undo_row = dtuple_copy(node->row, node->heap);
221 row_upd_replace(node->undo_row, &node->undo_ext,
222 clust_index, node->update, node->heap);
223 } else {
224 node->undo_row = NULL;
225 node->undo_ext = NULL;
226 }
227
228 btr_pcur_store_position(&(node->pcur), &mtr);
229
230 ret = TRUE;
231 }
232
233 btr_pcur_commit_specify_mtr(&(node->pcur), &mtr);
234
235 if (UNIV_LIKELY_NULL(heap)) {
236 mem_heap_free(heap);
237 }
238 return(ret);
239}
240
241/***********************************************************/
246static
247ulint
248row_undo(
249/*=====*/
250 undo_node_t* node,
251 que_thr_t* thr)
252{
253 ulint err;
254 trx_t* trx;
255 roll_ptr_t roll_ptr;
256 ibool locked_data_dict;
257
258 ut_ad(node && thr);
259
260 trx = node->trx;
261
262 if (node->state == UNDO_NODE_FETCH_NEXT) {
263
265 trx->roll_limit,
266 &roll_ptr,
267 node->heap);
268 if (!node->undo_rec) {
269 /* Rollback completed for this query thread */
270
271 thr->run_node = que_node_get_parent(node);
272
273 return(DB_SUCCESS);
274 }
275
276 node->roll_ptr = roll_ptr;
278
279 if (trx_undo_roll_ptr_is_insert(roll_ptr)) {
280
281 node->state = UNDO_NODE_INSERT;
282 } else {
283 node->state = UNDO_NODE_MODIFY;
284 }
285
286 } else if (node->state == UNDO_NODE_PREV_VERS) {
287
288 /* Undo should be done to the same clustered index record
289 again in this same rollback, restoring the previous version */
290
291 roll_ptr = node->new_roll_ptr;
292
293 node->undo_rec = trx_undo_get_undo_rec_low(roll_ptr,
294 node->heap);
295 node->roll_ptr = roll_ptr;
297
298 if (trx_undo_roll_ptr_is_insert(roll_ptr)) {
299
300 node->state = UNDO_NODE_INSERT;
301 } else {
302 node->state = UNDO_NODE_MODIFY;
303 }
304 }
305
306 /* Prevent DROP TABLE etc. while we are rolling back this row.
307 If we are doing a TABLE CREATE or some other dictionary operation,
308 then we already have dict_operation_lock locked in x-mode. Do not
309 try to lock again, because that would cause a hang. */
310
311 locked_data_dict = (trx->dict_operation_lock_mode == 0);
312
313 if (locked_data_dict) {
314
315 row_mysql_freeze_data_dictionary(trx);
316 }
317
318 if (node->state == UNDO_NODE_INSERT) {
319
320 err = row_undo_ins(node);
321
323 } else {
324 ut_ad(node->state == UNDO_NODE_MODIFY);
325 err = row_undo_mod(node, thr);
326 }
327
328 if (locked_data_dict) {
329
331 }
332
333 /* Do some cleanup */
334 btr_pcur_close(&(node->pcur));
335
336 mem_heap_empty(node->heap);
337
338 thr->run_node = node;
339
340 return(err);
341}
342
343/***********************************************************/
347UNIV_INTERN
348que_thr_t*
350/*==========*/
351 que_thr_t* thr)
352{
353 ulint err;
354 undo_node_t* node;
355 trx_t* trx;
356
357 ut_ad(thr);
358
359 srv_activity_count++;
360
361 trx = thr_get_trx(thr);
362
363 node = static_cast<undo_node_t *>(thr->run_node);
364
365 ut_ad(que_node_get_type(node) == QUE_NODE_UNDO);
366
367 err = row_undo(node, thr);
368
369 trx->error_state = err;
370
371 if (err != DB_SUCCESS) {
372 /* SQL error detected */
373
374 fprintf(stderr, "InnoDB: Fatal error %lu in rollback.\n",
375 (ulong) err);
376
377 if (err == DB_OUT_OF_FILE_SPACE) {
378 fprintf(stderr,
379 "InnoDB: Error 13 means out of tablespace.\n"
380 "InnoDB: Consider increasing"
381 " your tablespace.\n");
382
383 exit(1);
384 }
385
386 ut_error;
387
388 return(NULL);
389 }
390
391 return(thr);
392}
@ BTR_MODIFY_LEAF
Definition btr0btr.h:59
UNIV_INLINE void btr_pcur_commit_specify_mtr(btr_pcur_t *pcur, mtr_t *mtr)
UNIV_INLINE void btr_pcur_init(btr_pcur_t *pcur)
UNIV_INLINE void btr_pcur_close(btr_pcur_t *cursor)
UNIV_INLINE dtuple_t * dtuple_copy(const dtuple_t *tuple, mem_heap_t *heap)
UNIV_INLINE ulint dict_table_get_format(const dict_table_t *table)
#define DICT_TF_FORMAT_ZIP
Definition dict0mem.h:83
#define mem_heap_create(N)
Definition mem0mem.h:91
UNIV_INLINE void mem_heap_empty(mem_heap_t *heap)
UNIV_INLINE void * mem_heap_alloc(mem_heap_t *heap, ulint n)
#define mem_heap_free(heap)
Definition mem0mem.h:111
UNIV_INLINE void mtr_start(mtr_t *mtr) __attribute__((nonnull))
UNIV_INLINE ulint que_node_get_type(que_node_t *node)
UNIV_INLINE que_node_t * que_node_get_parent(que_node_t *node)
UNIV_INLINE trx_t * thr_get_trx(que_thr_t *thr)
UNIV_INTERN void row_mysql_unfreeze_data_dictionary(trx_t *trx)
UNIV_INTERN ibool row_search_on_row_ref(btr_pcur_t *pcur, ulint mode, const dict_table_t *table, const dtuple_t *ref, mtr_t *mtr)
Definition row0row.cc:655
UNIV_INLINE roll_ptr_t row_get_rec_roll_ptr(const rec_t *rec, dict_index_t *index, const ulint *offsets)
UNIV_INTERN dtuple_t * row_build(ulint type, const dict_index_t *index, const rec_t *rec, const ulint *offsets, const dict_table_t *col_table, row_ext_t **ext, mem_heap_t *heap)
Definition row0row.cc:175
UNIV_INTERN ulint row_undo_ins(undo_node_t *node)
Definition row0uins.cc:320
UNIV_INTERN ulint row_undo_mod(undo_node_t *node, que_thr_t *thr)
Definition row0umod.cc:837
@ UNDO_NODE_FETCH_NEXT
Definition row0undo.h:89
@ UNDO_NODE_INSERT
Definition row0undo.h:95
@ UNDO_NODE_MODIFY
Definition row0undo.h:97
@ UNDO_NODE_PREV_VERS
Definition row0undo.h:91
UNIV_INTERN ibool row_undo_search_clust_to_pcur(undo_node_t *node)
Definition row0undo.cc:162
UNIV_INTERN undo_node_t * row_undo_node_create(trx_t *trx, que_thr_t *parent, mem_heap_t *heap)
Definition row0undo.cc:128
UNIV_INTERN que_thr_t * row_undo_step(que_thr_t *thr)
Definition row0undo.cc:349
UNIV_INTERN void row_upd_replace(dtuple_t *row, row_ext_t **ext, const dict_index_t *index, const upd_t *update, mem_heap_t *heap)
Definition row0upd.cc:1152
que_node_t * parent
Definition que0types.h:49
ulint error_state
Definition trx0trx.h:601
undo_no_t roll_limit
Definition trx0trx.h:700
ulint dict_operation_lock_mode
Definition trx0trx.h:530
enum undo_exec state
Definition row0undo.h:105
mem_heap_t * heap
Definition row0undo.h:133
row_ext_t * ext
Definition row0undo.h:126
dtuple_t * row
Definition row0undo.h:124
row_ext_t * undo_ext
Definition row0undo.h:129
undo_no_t undo_no
Definition row0undo.h:109
que_common_t common
Definition row0undo.h:104
dtuple_t * undo_row
Definition row0undo.h:128
trx_undo_rec_t * undo_rec
Definition row0undo.h:108
roll_ptr_t roll_ptr
Definition row0undo.h:107
dict_table_t * table
Definition row0undo.h:119
btr_pcur_t pcur
Definition row0undo.h:117
dtuple_t * ref
Definition row0undo.h:123
roll_ptr_t new_roll_ptr
Definition row0undo.h:112
UNIV_INLINE undo_no_t trx_undo_rec_get_undo_no(const trx_undo_rec_t *undo_rec)
UNIV_INTERN trx_undo_rec_t * trx_undo_get_undo_rec_low(roll_ptr_t roll_ptr, mem_heap_t *heap)
Definition trx0rec.cc:1337
UNIV_INTERN trx_undo_rec_t * trx_roll_pop_top_rec_of_trx(trx_t *trx, undo_no_t limit, roll_ptr_t *roll_ptr, mem_heap_t *heap)
Definition trx0roll.cc:899
ib_id_t roll_ptr_t
Definition trx0types.h:87
UNIV_INLINE ibool trx_undo_roll_ptr_is_insert(roll_ptr_t roll_ptr)
#define ut_error
Definition ut0dbg.h:115
#define ut_ad(EXPR)
Definition ut0dbg.h:127