00001 /****************************************************** 00002 Transaction undo log 00003 00004 (c) 1996 Innobase Oy 00005 00006 Created 3/26/1996 Heikki Tuuri 00007 *******************************************************/ 00008 00009 #ifndef trx0undo_h 00010 #define trx0undo_h 00011 00012 #include "univ.i" 00013 #include "trx0types.h" 00014 #include "mtr0mtr.h" 00015 #include "trx0sys.h" 00016 #include "page0types.h" 00017 #include "trx0xa.h" 00018 00019 /*************************************************************************** 00020 Builds a roll pointer dulint. */ 00021 UNIV_INLINE 00022 dulint 00023 trx_undo_build_roll_ptr( 00024 /*====================*/ 00025 /* out: roll pointer */ 00026 ibool is_insert, /* in: TRUE if insert undo log */ 00027 ulint rseg_id, /* in: rollback segment id */ 00028 ulint page_no, /* in: page number */ 00029 ulint offset); /* in: offset of the undo entry within page */ 00030 /*************************************************************************** 00031 Decodes a roll pointer dulint. */ 00032 UNIV_INLINE 00033 void 00034 trx_undo_decode_roll_ptr( 00035 /*=====================*/ 00036 dulint roll_ptr, /* in: roll pointer */ 00037 ibool* is_insert, /* out: TRUE if insert undo log */ 00038 ulint* rseg_id, /* out: rollback segment id */ 00039 ulint* page_no, /* out: page number */ 00040 ulint* offset); /* out: offset of the undo entry within page */ 00041 /*************************************************************************** 00042 Returns TRUE if the roll pointer is of the insert type. */ 00043 UNIV_INLINE 00044 ibool 00045 trx_undo_roll_ptr_is_insert( 00046 /*========================*/ 00047 /* out: TRUE if insert undo log */ 00048 dulint roll_ptr); /* in: roll pointer */ 00049 /********************************************************************* 00050 Writes a roll ptr to an index page. In case that the size changes in 00051 some future version, this function should be used instead of 00052 mach_write_... */ 00053 UNIV_INLINE 00054 void 00055 trx_write_roll_ptr( 00056 /*===============*/ 00057 byte* ptr, /* in: pointer to memory where written */ 00058 dulint roll_ptr); /* in: roll ptr */ 00059 /********************************************************************* 00060 Reads a roll ptr from an index page. In case that the roll ptr size 00061 changes in some future version, this function should be used instead of 00062 mach_read_... */ 00063 UNIV_INLINE 00064 dulint 00065 trx_read_roll_ptr( 00066 /*==============*/ 00067 /* out: roll ptr */ 00068 byte* ptr); /* in: pointer to memory from where to read */ 00069 /********************************************************************** 00070 Gets an undo log page and x-latches it. */ 00071 UNIV_INLINE 00072 page_t* 00073 trx_undo_page_get( 00074 /*==============*/ 00075 /* out: pointer to page x-latched */ 00076 ulint space, /* in: space where placed */ 00077 ulint page_no, /* in: page number */ 00078 mtr_t* mtr); /* in: mtr */ 00079 /********************************************************************** 00080 Gets an undo log page and s-latches it. */ 00081 UNIV_INLINE 00082 page_t* 00083 trx_undo_page_get_s_latched( 00084 /*========================*/ 00085 /* out: pointer to page s-latched */ 00086 ulint space, /* in: space where placed */ 00087 ulint page_no, /* in: page number */ 00088 mtr_t* mtr); /* in: mtr */ 00089 /********************************************************************** 00090 Returns the previous undo record on the page in the specified log, or 00091 NULL if none exists. */ 00092 UNIV_INLINE 00093 trx_undo_rec_t* 00094 trx_undo_page_get_prev_rec( 00095 /*=======================*/ 00096 /* out: pointer to record, NULL if none */ 00097 trx_undo_rec_t* rec, /* in: undo log record */ 00098 ulint page_no,/* in: undo log header page number */ 00099 ulint offset); /* in: undo log header offset on page */ 00100 /********************************************************************** 00101 Returns the next undo log record on the page in the specified log, or 00102 NULL if none exists. */ 00103 UNIV_INLINE 00104 trx_undo_rec_t* 00105 trx_undo_page_get_next_rec( 00106 /*=======================*/ 00107 /* out: pointer to record, NULL if none */ 00108 trx_undo_rec_t* rec, /* in: undo log record */ 00109 ulint page_no,/* in: undo log header page number */ 00110 ulint offset); /* in: undo log header offset on page */ 00111 /********************************************************************** 00112 Returns the last undo record on the page in the specified undo log, or 00113 NULL if none exists. */ 00114 UNIV_INLINE 00115 trx_undo_rec_t* 00116 trx_undo_page_get_last_rec( 00117 /*=======================*/ 00118 /* out: pointer to record, NULL if none */ 00119 page_t* undo_page,/* in: undo log page */ 00120 ulint page_no,/* in: undo log header page number */ 00121 ulint offset); /* in: undo log header offset on page */ 00122 /********************************************************************** 00123 Returns the first undo record on the page in the specified undo log, or 00124 NULL if none exists. */ 00125 UNIV_INLINE 00126 trx_undo_rec_t* 00127 trx_undo_page_get_first_rec( 00128 /*========================*/ 00129 /* out: pointer to record, NULL if none */ 00130 page_t* undo_page,/* in: undo log page */ 00131 ulint page_no,/* in: undo log header page number */ 00132 ulint offset); /* in: undo log header offset on page */ 00133 /*************************************************************************** 00134 Gets the previous record in an undo log. */ 00135 00136 trx_undo_rec_t* 00137 trx_undo_get_prev_rec( 00138 /*==================*/ 00139 /* out: undo log record, the page s-latched, 00140 NULL if none */ 00141 trx_undo_rec_t* rec, /* in: undo record */ 00142 ulint page_no,/* in: undo log header page number */ 00143 ulint offset, /* in: undo log header offset on page */ 00144 mtr_t* mtr); /* in: mtr */ 00145 /*************************************************************************** 00146 Gets the next record in an undo log. */ 00147 00148 trx_undo_rec_t* 00149 trx_undo_get_next_rec( 00150 /*==================*/ 00151 /* out: undo log record, the page s-latched, 00152 NULL if none */ 00153 trx_undo_rec_t* rec, /* in: undo record */ 00154 ulint page_no,/* in: undo log header page number */ 00155 ulint offset, /* in: undo log header offset on page */ 00156 mtr_t* mtr); /* in: mtr */ 00157 /*************************************************************************** 00158 Gets the first record in an undo log. */ 00159 00160 trx_undo_rec_t* 00161 trx_undo_get_first_rec( 00162 /*===================*/ 00163 /* out: undo log record, the page latched, NULL if 00164 none */ 00165 ulint space, /* in: undo log header space */ 00166 ulint page_no,/* in: undo log header page number */ 00167 ulint offset, /* in: undo log header offset on page */ 00168 ulint mode, /* in: latching mode: RW_S_LATCH or RW_X_LATCH */ 00169 mtr_t* mtr); /* in: mtr */ 00170 /************************************************************************ 00171 Tries to add a page to the undo log segment where the undo log is placed. */ 00172 00173 ulint 00174 trx_undo_add_page( 00175 /*==============*/ 00176 /* out: page number if success, else 00177 FIL_NULL */ 00178 trx_t* trx, /* in: transaction */ 00179 trx_undo_t* undo, /* in: undo log memory object */ 00180 mtr_t* mtr); /* in: mtr which does not have a latch to any 00181 undo log page; the caller must have reserved 00182 the rollback segment mutex */ 00183 /*************************************************************************** 00184 Truncates an undo log from the end. This function is used during a rollback 00185 to free space from an undo log. */ 00186 00187 void 00188 trx_undo_truncate_end( 00189 /*==================*/ 00190 trx_t* trx, /* in: transaction whose undo log it is */ 00191 trx_undo_t* undo, /* in: undo log */ 00192 dulint limit); /* in: all undo records with undo number 00193 >= this value should be truncated */ 00194 /*************************************************************************** 00195 Truncates an undo log from the start. This function is used during a purge 00196 operation. */ 00197 00198 void 00199 trx_undo_truncate_start( 00200 /*====================*/ 00201 trx_rseg_t* rseg, /* in: rollback segment */ 00202 ulint space, /* in: space id of the log */ 00203 ulint hdr_page_no, /* in: header page number */ 00204 ulint hdr_offset, /* in: header offset on the page */ 00205 dulint limit); /* in: all undo pages with undo numbers < 00206 this value should be truncated; NOTE that 00207 the function only frees whole pages; the 00208 header page is not freed, but emptied, if 00209 all the records there are < limit */ 00210 /************************************************************************ 00211 Initializes the undo log lists for a rollback segment memory copy. 00212 This function is only called when the database is started or a new 00213 rollback segment created. */ 00214 00215 ulint 00216 trx_undo_lists_init( 00217 /*================*/ 00218 /* out: the combined size of undo log segments 00219 in pages */ 00220 trx_rseg_t* rseg); /* in: rollback segment memory object */ 00221 /************************************************************************** 00222 Assigns an undo log for a transaction. A new undo log is created or a cached 00223 undo log reused. */ 00224 00225 trx_undo_t* 00226 trx_undo_assign_undo( 00227 /*=================*/ 00228 /* out: the undo log, NULL if did not succeed: out of 00229 space */ 00230 trx_t* trx, /* in: transaction */ 00231 ulint type); /* in: TRX_UNDO_INSERT or TRX_UNDO_UPDATE */ 00232 /********************************************************************** 00233 Sets the state of the undo log segment at a transaction finish. */ 00234 00235 page_t* 00236 trx_undo_set_state_at_finish( 00237 /*=========================*/ 00238 /* out: undo log segment header page, 00239 x-latched */ 00240 trx_t* trx, /* in: transaction */ 00241 trx_undo_t* undo, /* in: undo log memory copy */ 00242 mtr_t* mtr); /* in: mtr */ 00243 /********************************************************************** 00244 Sets the state of the undo log segment at a transaction prepare. */ 00245 00246 page_t* 00247 trx_undo_set_state_at_prepare( 00248 /*==========================*/ 00249 /* out: undo log segment header page, 00250 x-latched */ 00251 trx_t* trx, /* in: transaction */ 00252 trx_undo_t* undo, /* in: undo log memory copy */ 00253 mtr_t* mtr); /* in: mtr */ 00254 00255 /************************************************************************** 00256 Adds the update undo log header as the first in the history list, and 00257 frees the memory object, or puts it to the list of cached update undo log 00258 segments. */ 00259 00260 void 00261 trx_undo_update_cleanup( 00262 /*====================*/ 00263 trx_t* trx, /* in: trx owning the update undo log */ 00264 page_t* undo_page, /* in: update undo log header page, 00265 x-latched */ 00266 mtr_t* mtr); /* in: mtr */ 00267 /********************************************************************** 00268 Frees or caches an insert undo log after a transaction commit or rollback. 00269 Knowledge of inserts is not needed after a commit or rollback, therefore 00270 the data can be discarded. */ 00271 00272 void 00273 trx_undo_insert_cleanup( 00274 /*====================*/ 00275 trx_t* trx); /* in: transaction handle */ 00276 /*************************************************************** 00277 Parses the redo log entry of an undo log page initialization. */ 00278 00279 byte* 00280 trx_undo_parse_page_init( 00281 /*=====================*/ 00282 /* out: end of log record or NULL */ 00283 byte* ptr, /* in: buffer */ 00284 byte* end_ptr,/* in: buffer end */ 00285 page_t* page, /* in: page or NULL */ 00286 mtr_t* mtr); /* in: mtr or NULL */ 00287 /*************************************************************** 00288 Parses the redo log entry of an undo log page header create or reuse. */ 00289 00290 byte* 00291 trx_undo_parse_page_header( 00292 /*=======================*/ 00293 /* out: end of log record or NULL */ 00294 ulint type, /* in: MLOG_UNDO_HDR_CREATE or MLOG_UNDO_HDR_REUSE */ 00295 byte* ptr, /* in: buffer */ 00296 byte* end_ptr,/* in: buffer end */ 00297 page_t* page, /* in: page or NULL */ 00298 mtr_t* mtr); /* in: mtr or NULL */ 00299 /*************************************************************** 00300 Parses the redo log entry of an undo log page header discard. */ 00301 00302 byte* 00303 trx_undo_parse_discard_latest( 00304 /*==========================*/ 00305 /* out: end of log record or NULL */ 00306 byte* ptr, /* in: buffer */ 00307 byte* end_ptr,/* in: buffer end */ 00308 page_t* page, /* in: page or NULL */ 00309 mtr_t* mtr); /* in: mtr or NULL */ 00310 00311 /* Types of an undo log segment */ 00312 #define TRX_UNDO_INSERT 1 /* contains undo entries for inserts */ 00313 #define TRX_UNDO_UPDATE 2 /* contains undo entries for updates 00314 and delete markings: in short, 00315 modifys (the name 'UPDATE' is a 00316 historical relic) */ 00317 /* States of an undo log segment */ 00318 #define TRX_UNDO_ACTIVE 1 /* contains an undo log of an active 00319 transaction */ 00320 #define TRX_UNDO_CACHED 2 /* cached for quick reuse */ 00321 #define TRX_UNDO_TO_FREE 3 /* insert undo segment can be freed */ 00322 #define TRX_UNDO_TO_PURGE 4 /* update undo segment will not be 00323 reused: it can be freed in purge when 00324 all undo data in it is removed */ 00325 #define TRX_UNDO_PREPARED 5 /* contains an undo log of an 00326 prepared transaction */ 00327 00328 /* Transaction undo log memory object; this is protected by the undo_mutex 00329 in the corresponding transaction object */ 00330 00331 struct trx_undo_struct{ 00332 /*-----------------------------*/ 00333 ulint id; /* undo log slot number within the 00334 rollback segment */ 00335 ulint type; /* TRX_UNDO_INSERT or 00336 TRX_UNDO_UPDATE */ 00337 ulint state; /* state of the corresponding undo log 00338 segment */ 00339 ibool del_marks; /* relevant only in an update undo log: 00340 this is TRUE if the transaction may 00341 have delete marked records, because of 00342 a delete of a row or an update of an 00343 indexed field; purge is then 00344 necessary; also TRUE if the transaction 00345 has updated an externally stored 00346 field */ 00347 dulint trx_id; /* id of the trx assigned to the undo 00348 log */ 00349 XID xid; /* X/Open XA transaction 00350 identification */ 00351 ibool dict_operation; /* TRUE if a dict operation trx */ 00352 dulint table_id; /* if a dict operation, then the table 00353 id */ 00354 trx_rseg_t* rseg; /* rseg where the undo log belongs */ 00355 /*-----------------------------*/ 00356 ulint space; /* space id where the undo log 00357 placed */ 00358 ulint hdr_page_no; /* page number of the header page in 00359 the undo log */ 00360 ulint hdr_offset; /* header offset of the undo log on the 00361 page */ 00362 ulint last_page_no; /* page number of the last page in the 00363 undo log; this may differ from 00364 top_page_no during a rollback */ 00365 ulint size; /* current size in pages */ 00366 /*-----------------------------*/ 00367 ulint empty; /* TRUE if the stack of undo log 00368 records is currently empty */ 00369 ulint top_page_no; /* page number where the latest undo 00370 log record was catenated; during 00371 rollback the page from which the latest 00372 undo record was chosen */ 00373 ulint top_offset; /* offset of the latest undo record, 00374 i.e., the topmost element in the undo 00375 log if we think of it as a stack */ 00376 dulint top_undo_no; /* undo number of the latest record */ 00377 page_t* guess_page; /* guess for the buffer frame where 00378 the top page might reside */ 00379 /*-----------------------------*/ 00380 UT_LIST_NODE_T(trx_undo_t) undo_list; 00381 /* undo log objects in the rollback 00382 segment are chained into lists */ 00383 }; 00384 00385 /* The offset of the undo log page header on pages of the undo log */ 00386 #define TRX_UNDO_PAGE_HDR FSEG_PAGE_DATA 00387 /*-------------------------------------------------------------*/ 00388 /* Transaction undo log page header offsets */ 00389 #define TRX_UNDO_PAGE_TYPE 0 /* TRX_UNDO_INSERT or 00390 TRX_UNDO_UPDATE */ 00391 #define TRX_UNDO_PAGE_START 2 /* Byte offset where the undo log 00392 records for the LATEST transaction 00393 start on this page (remember that 00394 in an update undo log, the first page 00395 can contain several undo logs) */ 00396 #define TRX_UNDO_PAGE_FREE 4 /* On each page of the undo log this 00397 field contains the byte offset of the 00398 first free byte on the page */ 00399 #define TRX_UNDO_PAGE_NODE 6 /* The file list node in the chain 00400 of undo log pages */ 00401 /*-------------------------------------------------------------*/ 00402 #define TRX_UNDO_PAGE_HDR_SIZE (6 + FLST_NODE_SIZE) 00403 00404 /* An update undo segment with just one page can be reused if it has 00405 < this number bytes used; we must leave space at least for one new undo 00406 log header on the page */ 00407 00408 #define TRX_UNDO_PAGE_REUSE_LIMIT (3 * UNIV_PAGE_SIZE / 4) 00409 00410 /* An update undo log segment may contain several undo logs on its first page 00411 if the undo logs took so little space that the segment could be cached and 00412 reused. All the undo log headers are then on the first page, and the last one 00413 owns the undo log records on subsequent pages if the segment is bigger than 00414 one page. If an undo log is stored in a segment, then on the first page it is 00415 allowed to have zero undo records, but if the segment extends to several 00416 pages, then all the rest of the pages must contain at least one undo log 00417 record. */ 00418 00419 /* The offset of the undo log segment header on the first page of the undo 00420 log segment */ 00421 00422 #define TRX_UNDO_SEG_HDR (TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE) 00423 /*-------------------------------------------------------------*/ 00424 #define TRX_UNDO_STATE 0 /* TRX_UNDO_ACTIVE, ... */ 00425 #define TRX_UNDO_LAST_LOG 2 /* Offset of the last undo log header 00426 on the segment header page, 0 if 00427 none */ 00428 #define TRX_UNDO_FSEG_HEADER 4 /* Header for the file segment which 00429 the undo log segment occupies */ 00430 #define TRX_UNDO_PAGE_LIST (4 + FSEG_HEADER_SIZE) 00431 /* Base node for the list of pages in 00432 the undo log segment; defined only on 00433 the undo log segment's first page */ 00434 /*-------------------------------------------------------------*/ 00435 /* Size of the undo log segment header */ 00436 #define TRX_UNDO_SEG_HDR_SIZE (4 + FSEG_HEADER_SIZE + FLST_BASE_NODE_SIZE) 00437 00438 00439 /* The undo log header. There can be several undo log headers on the first 00440 page of an update undo log segment. */ 00441 /*-------------------------------------------------------------*/ 00442 #define TRX_UNDO_TRX_ID 0 /* Transaction id */ 00443 #define TRX_UNDO_TRX_NO 8 /* Transaction number of the 00444 transaction; defined only if the log 00445 is in a history list */ 00446 #define TRX_UNDO_DEL_MARKS 16 /* Defined only in an update undo 00447 log: TRUE if the transaction may have 00448 done delete markings of records, and 00449 thus purge is necessary */ 00450 #define TRX_UNDO_LOG_START 18 /* Offset of the first undo log record 00451 of this log on the header page; purge 00452 may remove undo log record from the 00453 log start, and therefore this is not 00454 necessarily the same as this log 00455 header end offset */ 00456 #define TRX_UNDO_XID_EXISTS 20 /* TRUE if undo log header includes 00457 X/Open XA transaction identification 00458 XID */ 00459 #define TRX_UNDO_DICT_TRANS 21 /* TRUE if the transaction is a table 00460 create, index create, or drop 00461 transaction: in recovery 00462 the transaction cannot be rolled back 00463 in the usual way: a 'rollback' rather 00464 means dropping the created or dropped 00465 table, if it still exists */ 00466 #define TRX_UNDO_TABLE_ID 22 /* Id of the table if the preceding 00467 field is TRUE */ 00468 #define TRX_UNDO_NEXT_LOG 30 /* Offset of the next undo log header 00469 on this page, 0 if none */ 00470 #define TRX_UNDO_PREV_LOG 32 /* Offset of the previous undo log 00471 header on this page, 0 if none */ 00472 #define TRX_UNDO_HISTORY_NODE 34 /* If the log is put to the history 00473 list, the file list node is here */ 00474 /*-------------------------------------------------------------*/ 00475 #define TRX_UNDO_LOG_OLD_HDR_SIZE (34 + FLST_NODE_SIZE) 00476 00477 /* Note: the writing of the undo log old header is coded by a log record 00478 MLOG_UNDO_HDR_CREATE or MLOG_UNDO_HDR_REUSE. The appending of an XID to the 00479 header is logged separately. In this sense, the XID is not really a member 00480 of the undo log header. TODO: do not append the XID to the log header if XA 00481 is not needed by the user. The XID wastes about 150 bytes of space in every 00482 undo log. In the history list we may have millions of undo logs, which means 00483 quite a large overhead. */ 00484 00485 /* X/Open XA Transaction Identification (XID) */ 00486 00487 #define TRX_UNDO_XA_FORMAT (TRX_UNDO_LOG_OLD_HDR_SIZE) 00488 #define TRX_UNDO_XA_TRID_LEN (TRX_UNDO_XA_FORMAT + 4) 00489 #define TRX_UNDO_XA_BQUAL_LEN (TRX_UNDO_XA_TRID_LEN + 4) 00490 #define TRX_UNDO_XA_XID (TRX_UNDO_XA_BQUAL_LEN + 4) 00491 /*--------------------------------------------------------------*/ 00492 #define TRX_UNDO_LOG_XA_HDR_SIZE (TRX_UNDO_XA_XID + XIDDATASIZE) 00493 /* Total size of the header with the XA XID */ 00494 00495 #ifndef UNIV_NONINL 00496 #include "trx0undo.ic" 00497 #endif 00498 00499 #endif
1.4.7

