1
/*****************************************************************************
3
Copyright (C) 1996, 2009, Innobase Oy. All Rights Reserved.
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.
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.
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., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
17
*****************************************************************************/
19
/**************************************************//**
20
@file include/trx0undo.h
23
Created 3/26/1996 Heikki Tuuri
24
*******************************************************/
30
#include "trx0types.h"
33
#include "page0types.h"
36
#ifndef UNIV_HOTBACKUP
37
/***********************************************************************//**
38
Builds a roll pointer.
39
@return roll pointer */
42
trx_undo_build_roll_ptr(
43
/*====================*/
44
ibool is_insert, /*!< in: TRUE if insert undo log */
45
ulint rseg_id, /*!< in: rollback segment id */
46
ulint page_no, /*!< in: page number */
47
ulint offset); /*!< in: offset of the undo entry within page */
48
/***********************************************************************//**
49
Decodes a roll pointer. */
52
trx_undo_decode_roll_ptr(
53
/*=====================*/
54
roll_ptr_t roll_ptr, /*!< in: roll pointer */
55
ibool* is_insert, /*!< out: TRUE if insert undo log */
56
ulint* rseg_id, /*!< out: rollback segment id */
57
ulint* page_no, /*!< out: page number */
58
ulint* offset); /*!< out: offset of the undo
60
/***********************************************************************//**
61
Returns TRUE if the roll pointer is of the insert type.
62
@return TRUE if insert undo log */
65
trx_undo_roll_ptr_is_insert(
66
/*========================*/
67
roll_ptr_t roll_ptr); /*!< in: roll pointer */
68
#endif /* !UNIV_HOTBACKUP */
69
/*****************************************************************//**
70
Writes a roll ptr to an index page. In case that the size changes in
71
some future version, this function should be used instead of
77
byte* ptr, /*!< in: pointer to memory where
79
roll_ptr_t roll_ptr); /*!< in: roll ptr */
80
/*****************************************************************//**
81
Reads a roll ptr from an index page. In case that the roll ptr size
82
changes in some future version, this function should be used instead of
89
const byte* ptr); /*!< in: pointer to memory from where to read */
90
#ifndef UNIV_HOTBACKUP
91
/******************************************************************//**
92
Gets an undo log page and x-latches it.
93
@return pointer to page x-latched */
98
ulint space, /*!< in: space where placed */
99
ulint zip_size, /*!< in: compressed page size in bytes
100
or 0 for uncompressed pages */
101
ulint page_no, /*!< in: page number */
102
mtr_t* mtr); /*!< in: mtr */
103
/******************************************************************//**
104
Gets an undo log page and s-latches it.
105
@return pointer to page s-latched */
108
trx_undo_page_get_s_latched(
109
/*========================*/
110
ulint space, /*!< in: space where placed */
111
ulint zip_size, /*!< in: compressed page size in bytes
112
or 0 for uncompressed pages */
113
ulint page_no, /*!< in: page number */
114
mtr_t* mtr); /*!< in: mtr */
115
/******************************************************************//**
116
Returns the previous undo record on the page in the specified log, or
118
@return pointer to record, NULL if none */
121
trx_undo_page_get_prev_rec(
122
/*=======================*/
123
trx_undo_rec_t* rec, /*!< in: undo log record */
124
ulint page_no,/*!< in: undo log header page number */
125
ulint offset);/*!< in: undo log header offset on page */
126
/******************************************************************//**
127
Returns the next undo log record on the page in the specified log, or
129
@return pointer to record, NULL if none */
132
trx_undo_page_get_next_rec(
133
/*=======================*/
134
trx_undo_rec_t* rec, /*!< in: undo log record */
135
ulint page_no,/*!< in: undo log header page number */
136
ulint offset);/*!< in: undo log header offset on page */
137
/******************************************************************//**
138
Returns the last undo record on the page in the specified undo log, or
140
@return pointer to record, NULL if none */
143
trx_undo_page_get_last_rec(
144
/*=======================*/
145
page_t* undo_page,/*!< in: undo log page */
146
ulint page_no,/*!< in: undo log header page number */
147
ulint offset); /*!< in: undo log header offset on page */
148
/******************************************************************//**
149
Returns the first undo record on the page in the specified undo log, or
151
@return pointer to record, NULL if none */
154
trx_undo_page_get_first_rec(
155
/*========================*/
156
page_t* undo_page,/*!< in: undo log page */
157
ulint page_no,/*!< in: undo log header page number */
158
ulint offset);/*!< in: undo log header offset on page */
159
/***********************************************************************//**
160
Gets the previous record in an undo log.
161
@return undo log record, the page s-latched, NULL if none */
164
trx_undo_get_prev_rec(
165
/*==================*/
166
trx_undo_rec_t* rec, /*!< in: undo record */
167
ulint page_no,/*!< in: undo log header page number */
168
ulint offset, /*!< in: undo log header offset on page */
169
mtr_t* mtr); /*!< in: mtr */
170
/***********************************************************************//**
171
Gets the next record in an undo log.
172
@return undo log record, the page s-latched, NULL if none */
175
trx_undo_get_next_rec(
176
/*==================*/
177
trx_undo_rec_t* rec, /*!< in: undo record */
178
ulint page_no,/*!< in: undo log header page number */
179
ulint offset, /*!< in: undo log header offset on page */
180
mtr_t* mtr); /*!< in: mtr */
181
/***********************************************************************//**
182
Gets the first record in an undo log.
183
@return undo log record, the page latched, NULL if none */
186
trx_undo_get_first_rec(
187
/*===================*/
188
ulint space, /*!< in: undo log header space */
189
ulint zip_size,/*!< in: compressed page size in bytes
190
or 0 for uncompressed pages */
191
ulint page_no,/*!< in: undo log header page number */
192
ulint offset, /*!< in: undo log header offset on page */
193
ulint mode, /*!< in: latching mode: RW_S_LATCH or RW_X_LATCH */
194
mtr_t* mtr); /*!< in: mtr */
195
/********************************************************************//**
196
Tries to add a page to the undo log segment where the undo log is placed.
197
@return page number if success, else FIL_NULL */
202
trx_t* trx, /*!< in: transaction */
203
trx_undo_t* undo, /*!< in: undo log memory object */
204
mtr_t* mtr); /*!< in: mtr which does not have a latch to any
205
undo log page; the caller must have reserved
206
the rollback segment mutex */
207
/***********************************************************************//**
208
Truncates an undo log from the end. This function is used during a rollback
209
to free space from an undo log. */
212
trx_undo_truncate_end(
213
/*==================*/
214
trx_t* trx, /*!< in: transaction whose undo log it is */
215
trx_undo_t* undo, /*!< in: undo log */
216
undo_no_t limit); /*!< in: all undo records with undo number
217
>= this value should be truncated */
218
/***********************************************************************//**
219
Truncates an undo log from the start. This function is used during a purge
223
trx_undo_truncate_start(
224
/*====================*/
225
trx_rseg_t* rseg, /*!< in: rollback segment */
226
ulint space, /*!< in: space id of the log */
227
ulint hdr_page_no, /*!< in: header page number */
228
ulint hdr_offset, /*!< in: header offset on the page */
229
undo_no_t limit); /*!< in: all undo pages with
230
undo numbers < this value
231
should be truncated; NOTE that
232
the function only frees whole
233
pages; the header page is not
234
freed, but emptied, if all the
235
records there are < limit */
236
/********************************************************************//**
237
Initializes the undo log lists for a rollback segment memory copy.
238
This function is only called when the database is started or a new
239
rollback segment created.
240
@return the combined size of undo log segments in pages */
245
trx_rseg_t* rseg); /*!< in: rollback segment memory object */
246
/**********************************************************************//**
247
Assigns an undo log for a transaction. A new undo log is created or a cached
249
@return DB_SUCCESS if undo log assign successful, possible error codes
250
are: DB_TOO_MANY_CONCURRENT_TRXS DB_OUT_OF_FILE_SPACE
254
trx_undo_assign_undo(
255
/*=================*/
256
trx_t* trx, /*!< in: transaction */
257
ulint type); /*!< in: TRX_UNDO_INSERT or TRX_UNDO_UPDATE */
258
/******************************************************************//**
259
Sets the state of the undo log segment at a transaction finish.
260
@return undo log segment header page, x-latched */
263
trx_undo_set_state_at_finish(
264
/*=========================*/
265
trx_undo_t* undo, /*!< in: undo log memory copy */
266
mtr_t* mtr); /*!< in: mtr */
267
/******************************************************************//**
268
Sets the state of the undo log segment at a transaction prepare.
269
@return undo log segment header page, x-latched */
272
trx_undo_set_state_at_prepare(
273
/*==========================*/
274
trx_t* trx, /*!< in: transaction */
275
trx_undo_t* undo, /*!< in: undo log memory copy */
276
mtr_t* mtr); /*!< in: mtr */
278
/**********************************************************************//**
279
Adds the update undo log header as the first in the history list, and
280
frees the memory object, or puts it to the list of cached update undo log
284
trx_undo_update_cleanup(
285
/*====================*/
286
trx_t* trx, /*!< in: trx owning the update undo log */
287
page_t* undo_page, /*!< in: update undo log header page,
289
mtr_t* mtr); /*!< in: mtr */
290
/******************************************************************//**
291
Frees or caches an insert undo log after a transaction commit or rollback.
292
Knowledge of inserts is not needed after a commit or rollback, therefore
293
the data can be discarded. */
296
trx_undo_insert_cleanup(
297
/*====================*/
298
trx_t* trx); /*!< in: transaction handle */
299
#endif /* !UNIV_HOTBACKUP */
300
/***********************************************************//**
301
Parses the redo log entry of an undo log page initialization.
302
@return end of log record or NULL */
305
trx_undo_parse_page_init(
306
/*=====================*/
307
byte* ptr, /*!< in: buffer */
308
byte* end_ptr,/*!< in: buffer end */
309
page_t* page, /*!< in: page or NULL */
310
mtr_t* mtr); /*!< in: mtr or NULL */
311
/***********************************************************//**
312
Parses the redo log entry of an undo log page header create or reuse.
313
@return end of log record or NULL */
316
trx_undo_parse_page_header(
317
/*=======================*/
318
ulint type, /*!< in: MLOG_UNDO_HDR_CREATE or MLOG_UNDO_HDR_REUSE */
319
byte* ptr, /*!< in: buffer */
320
byte* end_ptr,/*!< in: buffer end */
321
page_t* page, /*!< in: page or NULL */
322
mtr_t* mtr); /*!< in: mtr or NULL */
323
/***********************************************************//**
324
Parses the redo log entry of an undo log page header discard.
325
@return end of log record or NULL */
328
trx_undo_parse_discard_latest(
329
/*==========================*/
330
byte* ptr, /*!< in: buffer */
331
byte* end_ptr,/*!< in: buffer end */
332
page_t* page, /*!< in: page or NULL */
333
mtr_t* mtr); /*!< in: mtr or NULL */
334
/************************************************************************
335
Frees an undo log memory copy. */
340
trx_undo_t* undo); /* in: the undo object to be freed */
342
/* Types of an undo log segment */
343
#define TRX_UNDO_INSERT 1 /* contains undo entries for inserts */
344
#define TRX_UNDO_UPDATE 2 /* contains undo entries for updates
345
and delete markings: in short,
346
modifys (the name 'UPDATE' is a
348
/* States of an undo log segment */
349
#define TRX_UNDO_ACTIVE 1 /* contains an undo log of an active
351
#define TRX_UNDO_CACHED 2 /* cached for quick reuse */
352
#define TRX_UNDO_TO_FREE 3 /* insert undo segment can be freed */
353
#define TRX_UNDO_TO_PURGE 4 /* update undo segment will not be
354
reused: it can be freed in purge when
355
all undo data in it is removed */
356
#define TRX_UNDO_PREPARED 5 /* contains an undo log of an
357
prepared transaction */
359
#ifndef UNIV_HOTBACKUP
360
/** Transaction undo log memory object; this is protected by the undo_mutex
361
in the corresponding transaction object */
363
struct trx_undo_struct{
364
/*-----------------------------*/
365
ulint id; /*!< undo log slot number within the
367
ulint type; /*!< TRX_UNDO_INSERT or
369
ulint state; /*!< state of the corresponding undo log
371
ibool del_marks; /*!< relevant only in an update undo log:
372
this is TRUE if the transaction may
373
have delete marked records, because of
374
a delete of a row or an update of an
375
indexed field; purge is then
376
necessary; also TRUE if the transaction
377
has updated an externally stored
379
trx_id_t trx_id; /*!< id of the trx assigned to the undo
381
XID xid; /*!< X/Open XA transaction
383
ibool dict_operation; /*!< TRUE if a dict operation trx */
384
table_id_t table_id; /*!< if a dict operation, then the table
386
trx_rseg_t* rseg; /*!< rseg where the undo log belongs */
387
/*-----------------------------*/
388
ulint space; /*!< space id where the undo log
390
ulint zip_size; /*!< compressed page size of space
391
in bytes, or 0 for uncompressed */
392
ulint hdr_page_no; /*!< page number of the header page in
394
ulint hdr_offset; /*!< header offset of the undo log on the
396
ulint last_page_no; /*!< page number of the last page in the
397
undo log; this may differ from
398
top_page_no during a rollback */
399
ulint size; /*!< current size in pages */
400
/*-----------------------------*/
401
ulint empty; /*!< TRUE if the stack of undo log
402
records is currently empty */
403
ulint top_page_no; /*!< page number where the latest undo
404
log record was catenated; during
405
rollback the page from which the latest
406
undo record was chosen */
407
ulint top_offset; /*!< offset of the latest undo record,
408
i.e., the topmost element in the undo
409
log if we think of it as a stack */
410
undo_no_t top_undo_no; /*!< undo number of the latest record */
411
buf_block_t* guess_block; /*!< guess for the buffer block where
412
the top page might reside */
413
/*-----------------------------*/
414
UT_LIST_NODE_T(trx_undo_t) undo_list;
415
/*!< undo log objects in the rollback
416
segment are chained into lists */
418
#endif /* !UNIV_HOTBACKUP */
420
/** The offset of the undo log page header on pages of the undo log */
421
#define TRX_UNDO_PAGE_HDR FSEG_PAGE_DATA
422
/*-------------------------------------------------------------*/
423
/** Transaction undo log page header offsets */
425
#define TRX_UNDO_PAGE_TYPE 0 /*!< TRX_UNDO_INSERT or
427
#define TRX_UNDO_PAGE_START 2 /*!< Byte offset where the undo log
428
records for the LATEST transaction
429
start on this page (remember that
430
in an update undo log, the first page
431
can contain several undo logs) */
432
#define TRX_UNDO_PAGE_FREE 4 /*!< On each page of the undo log this
433
field contains the byte offset of the
434
first free byte on the page */
435
#define TRX_UNDO_PAGE_NODE 6 /*!< The file list node in the chain
437
/*-------------------------------------------------------------*/
438
#define TRX_UNDO_PAGE_HDR_SIZE (6 + FLST_NODE_SIZE)
439
/*!< Size of the transaction undo
440
log page header, in bytes */
443
/** An update undo segment with just one page can be reused if it has
444
at most this many bytes used; we must leave space at least for one new undo
445
log header on the page */
447
#define TRX_UNDO_PAGE_REUSE_LIMIT (3 * UNIV_PAGE_SIZE / 4)
449
/* An update undo log segment may contain several undo logs on its first page
450
if the undo logs took so little space that the segment could be cached and
451
reused. All the undo log headers are then on the first page, and the last one
452
owns the undo log records on subsequent pages if the segment is bigger than
453
one page. If an undo log is stored in a segment, then on the first page it is
454
allowed to have zero undo records, but if the segment extends to several
455
pages, then all the rest of the pages must contain at least one undo log
458
/** The offset of the undo log segment header on the first page of the undo
461
#define TRX_UNDO_SEG_HDR (TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE)
462
/** Undo log segment header */
464
/*-------------------------------------------------------------*/
465
#define TRX_UNDO_STATE 0 /*!< TRX_UNDO_ACTIVE, ... */
466
#define TRX_UNDO_LAST_LOG 2 /*!< Offset of the last undo log header
467
on the segment header page, 0 if
469
#define TRX_UNDO_FSEG_HEADER 4 /*!< Header for the file segment which
470
the undo log segment occupies */
471
#define TRX_UNDO_PAGE_LIST (4 + FSEG_HEADER_SIZE)
472
/*!< Base node for the list of pages in
473
the undo log segment; defined only on
474
the undo log segment's first page */
475
/*-------------------------------------------------------------*/
476
/** Size of the undo log segment header */
477
#define TRX_UNDO_SEG_HDR_SIZE (4 + FSEG_HEADER_SIZE + FLST_BASE_NODE_SIZE)
481
/** The undo log header. There can be several undo log headers on the first
482
page of an update undo log segment. */
484
/*-------------------------------------------------------------*/
485
#define TRX_UNDO_TRX_ID 0 /*!< Transaction id */
486
#define TRX_UNDO_TRX_NO 8 /*!< Transaction number of the
487
transaction; defined only if the log
488
is in a history list */
489
#define TRX_UNDO_DEL_MARKS 16 /*!< Defined only in an update undo
490
log: TRUE if the transaction may have
491
done delete markings of records, and
492
thus purge is necessary */
493
#define TRX_UNDO_LOG_START 18 /*!< Offset of the first undo log record
494
of this log on the header page; purge
495
may remove undo log record from the
496
log start, and therefore this is not
497
necessarily the same as this log
499
#define TRX_UNDO_XID_EXISTS 20 /*!< TRUE if undo log header includes
500
X/Open XA transaction identification
502
#define TRX_UNDO_DICT_TRANS 21 /*!< TRUE if the transaction is a table
503
create, index create, or drop
504
transaction: in recovery
505
the transaction cannot be rolled back
506
in the usual way: a 'rollback' rather
507
means dropping the created or dropped
508
table, if it still exists */
509
#define TRX_UNDO_TABLE_ID 22 /*!< Id of the table if the preceding
511
#define TRX_UNDO_NEXT_LOG 30 /*!< Offset of the next undo log header
512
on this page, 0 if none */
513
#define TRX_UNDO_PREV_LOG 32 /*!< Offset of the previous undo log
514
header on this page, 0 if none */
515
#define TRX_UNDO_HISTORY_NODE 34 /*!< If the log is put to the history
516
list, the file list node is here */
517
/*-------------------------------------------------------------*/
518
/** Size of the undo log header without XID information */
519
#define TRX_UNDO_LOG_OLD_HDR_SIZE (34 + FLST_NODE_SIZE)
521
/* Note: the writing of the undo log old header is coded by a log record
522
MLOG_UNDO_HDR_CREATE or MLOG_UNDO_HDR_REUSE. The appending of an XID to the
523
header is logged separately. In this sense, the XID is not really a member
524
of the undo log header. TODO: do not append the XID to the log header if XA
525
is not needed by the user. The XID wastes about 150 bytes of space in every
526
undo log. In the history list we may have millions of undo logs, which means
527
quite a large overhead. */
529
/** X/Open XA Transaction Identification (XID) */
531
/** xid_t::formatID */
532
#define TRX_UNDO_XA_FORMAT (TRX_UNDO_LOG_OLD_HDR_SIZE)
533
/** xid_t::gtrid_length */
534
#define TRX_UNDO_XA_TRID_LEN (TRX_UNDO_XA_FORMAT + 4)
535
/** xid_t::bqual_length */
536
#define TRX_UNDO_XA_BQUAL_LEN (TRX_UNDO_XA_TRID_LEN + 4)
537
/** Distributed transaction identifier data */
538
#define TRX_UNDO_XA_XID (TRX_UNDO_XA_BQUAL_LEN + 4)
539
/*--------------------------------------------------------------*/
540
#define TRX_UNDO_LOG_XA_HDR_SIZE (TRX_UNDO_XA_XID + XIDDATASIZE)
541
/*!< Total size of the undo log header
546
#include "trx0undo.ic"