trx0undo.h
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:18k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /******************************************************
  2. Transaction undo log
  3. (c) 1996 Innobase Oy
  4. Created 3/26/1996 Heikki Tuuri
  5. *******************************************************/
  6. #ifndef trx0undo_h
  7. #define trx0undo_h
  8. #include "univ.i"
  9. #include "trx0types.h"
  10. #include "mtr0mtr.h"
  11. #include "trx0sys.h"
  12. #include "page0types.h"
  13. /***************************************************************************
  14. Builds a roll pointer dulint. */
  15. UNIV_INLINE
  16. dulint
  17. trx_undo_build_roll_ptr(
  18. /*====================*/
  19. /* out: roll pointer */
  20. ibool is_insert, /* in: TRUE if insert undo log */
  21. ulint rseg_id, /* in: rollback segment id */
  22. ulint page_no, /* in: page number */
  23. ulint offset); /* in: offset of the undo entry within page */
  24. /***************************************************************************
  25. Decodes a roll pointer dulint. */
  26. UNIV_INLINE
  27. void
  28. trx_undo_decode_roll_ptr(
  29. /*=====================*/
  30. dulint roll_ptr, /* in: roll pointer */
  31. ibool* is_insert, /* out: TRUE if insert undo log */
  32. ulint* rseg_id, /* out: rollback segment id */
  33. ulint* page_no, /* out: page number */
  34. ulint* offset); /* out: offset of the undo entry within page */
  35. /***************************************************************************
  36. Returns TRUE if the roll pointer is of the insert type. */
  37. UNIV_INLINE
  38. ibool
  39. trx_undo_roll_ptr_is_insert(
  40. /*========================*/
  41. /* out: TRUE if insert undo log */
  42. dulint roll_ptr); /* in: roll pointer */
  43. /*********************************************************************
  44. Writes a roll ptr to an index page. In case that the size changes in
  45. some future version, this function should be used instead of
  46. mach_write_... */
  47. UNIV_INLINE
  48. void
  49. trx_write_roll_ptr(
  50. /*===============*/
  51. byte* ptr, /* in: pointer to memory where written */
  52. dulint roll_ptr); /* in: roll ptr */
  53. /*********************************************************************
  54. Reads a roll ptr from an index page. In case that the roll ptr size
  55. changes in some future version, this function should be used instead of
  56. mach_read_... */
  57. UNIV_INLINE
  58. dulint
  59. trx_read_roll_ptr(
  60. /*==============*/
  61. /* out: roll ptr */
  62. byte* ptr); /* in: pointer to memory from where to read */
  63. /**********************************************************************
  64. Gets an undo log page and x-latches it. */
  65. UNIV_INLINE
  66. page_t*
  67. trx_undo_page_get(
  68. /*===============*/
  69. /* out: pointer to page x-latched */
  70. ulint space, /* in: space where placed */
  71. ulint page_no, /* in: page number */
  72. mtr_t* mtr); /* in: mtr */
  73. /**********************************************************************
  74. Gets an undo log page and s-latches it. */
  75. UNIV_INLINE
  76. page_t*
  77. trx_undo_page_get_s_latched(
  78. /*=========================*/
  79. /* out: pointer to page s-latched */
  80. ulint space, /* in: space where placed */
  81. ulint page_no, /* in: page number */
  82. mtr_t* mtr); /* in: mtr */
  83. /**********************************************************************
  84. Returns the previous undo record on the page in the specified log, or
  85. NULL if none exists. */
  86. UNIV_INLINE
  87. trx_undo_rec_t*
  88. trx_undo_page_get_prev_rec(
  89. /*=======================*/
  90. /* out: pointer to record, NULL if none */
  91. trx_undo_rec_t* rec, /* in: undo log record */
  92. ulint page_no,/* in: undo log header page number */
  93. ulint offset); /* in: undo log header offset on page */
  94. /**********************************************************************
  95. Returns the next undo log record on the page in the specified log, or
  96. NULL if none exists. */
  97. UNIV_INLINE
  98. trx_undo_rec_t*
  99. trx_undo_page_get_next_rec(
  100. /*=======================*/
  101. /* out: pointer to record, NULL if none */
  102. trx_undo_rec_t* rec, /* in: undo log record */
  103. ulint page_no,/* in: undo log header page number */
  104. ulint offset); /* in: undo log header offset on page */
  105. /**********************************************************************
  106. Returns the last undo record on the page in the specified undo log, or
  107. NULL if none exists. */
  108. UNIV_INLINE
  109. trx_undo_rec_t*
  110. trx_undo_page_get_last_rec(
  111. /*=======================*/
  112. /* out: pointer to record, NULL if none */
  113. page_t* undo_page,/* in: undo log page */
  114. ulint page_no,/* in: undo log header page number */
  115. ulint offset); /* in: undo log header offset on page */
  116. /**********************************************************************
  117. Returns the first undo record on the page in the specified undo log, or
  118. NULL if none exists. */
  119. UNIV_INLINE
  120. trx_undo_rec_t*
  121. trx_undo_page_get_first_rec(
  122. /*========================*/
  123. /* out: pointer to record, NULL if none */
  124. page_t* undo_page,/* in: undo log page */
  125. ulint page_no,/* in: undo log header page number */
  126. ulint offset); /* in: undo log header offset on page */
  127. /***************************************************************************
  128. Gets the previous record in an undo log. */
  129. trx_undo_rec_t*
  130. trx_undo_get_prev_rec(
  131. /*==================*/
  132. /* out: undo log record, the page s-latched,
  133. NULL if none */
  134. trx_undo_rec_t* rec, /* in: undo record */
  135. ulint page_no,/* in: undo log header page number */
  136. ulint offset, /* in: undo log header offset on page */
  137. mtr_t* mtr); /* in: mtr */
  138. /***************************************************************************
  139. Gets the next record in an undo log. */
  140. trx_undo_rec_t*
  141. trx_undo_get_next_rec(
  142. /*==================*/
  143. /* out: undo log record, the page s-latched,
  144. NULL if none */
  145. trx_undo_rec_t* rec, /* in: undo record */
  146. ulint page_no,/* in: undo log header page number */
  147. ulint offset, /* in: undo log header offset on page */
  148. mtr_t* mtr); /* in: mtr */
  149. /***************************************************************************
  150. Gets the first record in an undo log. */
  151. trx_undo_rec_t*
  152. trx_undo_get_first_rec(
  153. /*===================*/
  154. /* out: undo log record, the page latched, NULL if
  155. none */
  156. ulint space, /* in: undo log header space */
  157. ulint page_no,/* in: undo log header page number */
  158. ulint offset, /* in: undo log header offset on page */
  159. ulint mode, /* in: latching mode: RW_S_LATCH or RW_X_LATCH */
  160. mtr_t* mtr); /* in: mtr */
  161. /************************************************************************
  162. Tries to add a page to the undo log segment where the undo log is placed. */
  163. ulint
  164. trx_undo_add_page(
  165. /*==============*/
  166. /* out: page number if success, else
  167. FIL_NULL */
  168. trx_t* trx, /* in: transaction */
  169. trx_undo_t* undo, /* in: undo log memory object */
  170. mtr_t* mtr); /* in: mtr which does not have a latch to any
  171. undo log page; the caller must have reserved
  172. the rollback segment mutex */
  173. /***************************************************************************
  174. Truncates an undo log from the end. This function is used during a rollback
  175. to free space from an undo log. */
  176. void
  177. trx_undo_truncate_end(
  178. /*==================*/
  179. trx_t* trx, /* in: transaction whose undo log it is */
  180. trx_undo_t* undo, /* in: undo log */
  181. dulint limit); /* in: all undo records with undo number
  182. >= this value should be truncated */
  183. /***************************************************************************
  184. Truncates an undo log from the start. This function is used during a purge
  185. operation. */
  186. void
  187. trx_undo_truncate_start(
  188. /*====================*/
  189. trx_rseg_t* rseg, /* in: rollback segment */
  190. ulint space, /* in: space id of the log */
  191. ulint hdr_page_no, /* in: header page number */
  192. ulint hdr_offset, /* in: header offset on the page */
  193. dulint limit); /* in: all undo pages with undo numbers <
  194. this value should be truncated; NOTE that
  195. the function only frees whole pages; the
  196. header page is not freed, but emptied, if
  197. all the records there are < limit */
  198. /************************************************************************
  199. Initializes the undo log lists for a rollback segment memory copy.
  200. This function is only called when the database is started or a new
  201. rollback segment created. */
  202. ulint
  203. trx_undo_lists_init(
  204. /*================*/
  205. /* out: the combined size of undo log segments
  206. in pages */
  207. trx_rseg_t* rseg); /* in: rollback segment memory object */
  208. /**************************************************************************
  209. Assigns an undo log for a transaction. A new undo log is created or a cached
  210. undo log reused. */
  211. trx_undo_t*
  212. trx_undo_assign_undo(
  213. /*=================*/
  214. /* out: the undo log, NULL if did not succeed: out of
  215. space */
  216. trx_t* trx, /* in: transaction */
  217. ulint type); /* in: TRX_UNDO_INSERT or TRX_UNDO_UPDATE */
  218. /**********************************************************************
  219. Sets the state of the undo log segment at a transaction finish. */
  220. page_t*
  221. trx_undo_set_state_at_finish(
  222. /*=========================*/
  223. /* out: undo log segment header page,
  224. x-latched */
  225. trx_t* trx, /* in: transaction */
  226. trx_undo_t* undo, /* in: undo log memory copy */
  227. mtr_t* mtr); /* in: mtr */
  228. /**************************************************************************
  229. Adds the update undo log header as the first in the history list, and
  230. frees the memory object, or puts it to the list of cached update undo log
  231. segments. */
  232. void
  233. trx_undo_update_cleanup(
  234. /*====================*/
  235. trx_t* trx, /* in: trx owning the update undo log */
  236. page_t* undo_page, /* in: update undo log header page,
  237. x-latched */
  238. mtr_t* mtr); /* in: mtr */
  239. /**************************************************************************
  240. Discards an undo log and puts the segment to the list of cached update undo
  241. log segments. This optimized function is called if there is no need to
  242. keep the update undo log because there exist no read views and the transaction
  243. made no delete markings, which would make purge necessary. We restrict this
  244. to undo logs of size 1 to make things simpler. */
  245. dulint
  246. trx_undo_update_cleanup_by_discard(
  247. /*===============================*/
  248. /* out: log sequence number at which mtr is
  249. committed */
  250. trx_t* trx, /* in: trx owning the update undo log */
  251. mtr_t* mtr); /* in: mtr */
  252. /**********************************************************************
  253. Frees or caches an insert undo log after a transaction commit or rollback.
  254. Knowledge of inserts is not needed after a commit or rollback, therefore
  255. the data can be discarded. */
  256. void
  257. trx_undo_insert_cleanup(
  258. /*====================*/
  259. trx_t* trx); /* in: transaction handle */
  260. /***************************************************************
  261. Parses the redo log entry of an undo log page initialization. */
  262. byte*
  263. trx_undo_parse_page_init(
  264. /*======================*/
  265. /* out: end of log record or NULL */
  266. byte* ptr, /* in: buffer */
  267. byte* end_ptr,/* in: buffer end */
  268. page_t* page, /* in: page or NULL */
  269. mtr_t* mtr); /* in: mtr or NULL */
  270. /***************************************************************
  271. Parses the redo log entry of an undo log page header create or reuse. */
  272. byte*
  273. trx_undo_parse_page_header(
  274. /*=======================*/
  275. /* out: end of log record or NULL */
  276. ulint type, /* in: MLOG_UNDO_HDR_CREATE or MLOG_UNDO_HDR_REUSE */
  277. byte* ptr, /* in: buffer */
  278. byte* end_ptr,/* in: buffer end */
  279. page_t* page, /* in: page or NULL */
  280. mtr_t* mtr); /* in: mtr or NULL */
  281. /***************************************************************
  282. Parses the redo log entry of an undo log page header discard. */
  283. byte*
  284. trx_undo_parse_discard_latest(
  285. /*==========================*/
  286. /* out: end of log record or NULL */
  287. byte* ptr, /* in: buffer */
  288. byte* end_ptr,/* in: buffer end */
  289. page_t* page, /* in: page or NULL */
  290. mtr_t* mtr); /* in: mtr or NULL */
  291. /* Types of an undo log segment */
  292. #define TRX_UNDO_INSERT 1 /* contains undo entries for inserts */
  293. #define TRX_UNDO_UPDATE 2 /* contains undo entries for updates
  294. and delete markings: in short,
  295. modifys (the name 'UPDATE' is a
  296. historical relic) */
  297. /* States of an undo log segment */
  298. #define TRX_UNDO_ACTIVE 1 /* contains an undo log of an active
  299. transaction */
  300. #define TRX_UNDO_CACHED 2 /* cached for quick reuse */
  301. #define TRX_UNDO_TO_FREE 3 /* insert undo segment can be freed */
  302. #define TRX_UNDO_TO_PURGE 4 /* update undo segment will not be
  303. reused: it can be freed in purge when
  304. all undo data in it is removed */
  305. /* Transaction undo log memory object; this is protected by the undo_mutex
  306. in the corresponding transaction object */
  307. struct trx_undo_struct{
  308. /*-----------------------------*/
  309. ulint id; /* undo log slot number within the
  310. rollback segment */
  311. ulint type; /* TRX_UNDO_INSERT or
  312. TRX_UNDO_UPDATE */
  313. ulint state; /* state of the corresponding undo log
  314. segment */
  315. ibool del_marks; /* relevant only in an update undo log:
  316. this is TRUE if the transaction may
  317. have delete marked records, because of
  318. a delete of a row or an update of an
  319. indexed field; purge is then
  320. necessary. */
  321. dulint trx_id; /* id of the trx assigned to the undo
  322. log */
  323. ibool dict_operation; /* TRUE if a dict operation trx */
  324. dulint table_id; /* if a dict operation, then the table
  325. id */
  326. trx_rseg_t* rseg; /* rseg where the undo log belongs */
  327. /*-----------------------------*/
  328. ulint space; /* space id where the undo log
  329. placed */
  330. ulint hdr_page_no; /* page number of the header page in
  331. the undo log */
  332. ulint hdr_offset; /* header offset of the undo log on the
  333. page */
  334. ulint last_page_no; /* page number of the last page in the
  335. undo log; this may differ from
  336. top_page_no during a rollback */
  337. ulint size; /* current size in pages */
  338. /*-----------------------------*/
  339. ulint empty; /* TRUE if the stack of undo log
  340. records is currently empty */
  341. ulint top_page_no; /* page number where the latest undo
  342. log record was catenated; during
  343. rollback the page from which the latest
  344. undo record was chosen */
  345. ulint top_offset; /* offset of the latest undo record,
  346. i.e., the topmost element in the undo
  347. log if we think of it as a stack */
  348. dulint top_undo_no; /* undo number of the latest record */
  349. page_t* guess_page; /* guess for the buffer frame where
  350. the top page might reside */
  351. /*-----------------------------*/
  352. UT_LIST_NODE_T(trx_undo_t) undo_list;
  353. /* undo log objects in the rollback
  354. segment are chained into lists */
  355. };
  356. /* The offset of the undo log page header on pages of the undo log */
  357. #define TRX_UNDO_PAGE_HDR FSEG_PAGE_DATA
  358. /*-------------------------------------------------------------*/
  359. /* Transaction undo log page header offsets */
  360. #define TRX_UNDO_PAGE_TYPE 0 /* TRX_UNDO_INSERT or
  361. TRX_UNDO_UPDATE */
  362. #define TRX_UNDO_PAGE_START 2 /* Byte offset where the undo log
  363. records for the LATEST transaction
  364. start on this page (remember that
  365. in an update undo log, the first page
  366. can contain several undo logs) */
  367. #define TRX_UNDO_PAGE_FREE 4 /* On each page of the undo log this
  368. field contains the byte offset of the
  369. first free byte on the page */
  370. #define TRX_UNDO_PAGE_NODE 6 /* The file list node in the chain
  371. of undo log pages */
  372. /*-------------------------------------------------------------*/
  373. #define TRX_UNDO_PAGE_HDR_SIZE (6 + FLST_NODE_SIZE)
  374. /* An update undo segment with just one page can be reused if it has
  375. < this number bytes used */
  376. #define TRX_UNDO_PAGE_REUSE_LIMIT (3 * UNIV_PAGE_SIZE / 4)
  377. /* An update undo log segment may contain several undo logs on its first page
  378. if the undo logs took so little space that the segment could be cached and
  379. reused. All the undo log headers are then on the first page, and the last one
  380. owns the undo log records on subsequent pages if the segment is bigger than
  381. one page. If an undo log is stored in a segment, then on the first page it is
  382. allowed to have zero undo records, but if the segment extends to several
  383. pages, then all the rest of the pages must contain at least one undo log
  384. record. */
  385. /* The offset of the undo log segment header on the first page of the undo
  386. log segment */
  387. #define TRX_UNDO_SEG_HDR (TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE)
  388. /*-------------------------------------------------------------*/
  389. #define TRX_UNDO_STATE 0 /* TRX_UNDO_ACTIVE, ... */
  390. #define TRX_UNDO_LAST_LOG 2 /* Offset of the last undo log header
  391. on the segment header page, 0 if
  392. none */
  393. #define TRX_UNDO_FSEG_HEADER 4 /* Header for the file segment which
  394. the undo log segment occupies */
  395. #define TRX_UNDO_PAGE_LIST (4 + FSEG_HEADER_SIZE)
  396. /* Base node for the list of pages in
  397. the undo log segment; defined only on
  398. the undo log segment's first page */
  399. /*-------------------------------------------------------------*/
  400. /* Size of the undo log segment header */
  401. #define TRX_UNDO_SEG_HDR_SIZE (4 + FSEG_HEADER_SIZE + FLST_BASE_NODE_SIZE)
  402. /* The undo log header. There can be several undo log headers on the first
  403. page of an update undo log segment. */
  404. /*-------------------------------------------------------------*/
  405. #define TRX_UNDO_TRX_ID 0 /* Transaction id */
  406. #define TRX_UNDO_TRX_NO 8 /* Transaction number of the
  407. transaction; defined only if the log
  408. is in a history list */
  409. #define TRX_UNDO_DEL_MARKS 16 /* Defined only in an update undo
  410. log: TRUE if the transaction may have
  411. done delete markings of records, and
  412. thus purge is necessary */
  413. #define TRX_UNDO_LOG_START 18 /* Offset of the first undo log record
  414. of this log on the header page; purge
  415. may remove undo log record from the
  416. log start, and therefore this is not
  417. necessarily the same as this log
  418. header end offset */
  419. #define TRX_UNDO_DICT_OPERATION 20 /* TRUE if the transaction is a table
  420. create, index create, or drop
  421. transaction: in recovery
  422. the transaction cannot be rolled back
  423. in the usual way: a 'rollback' rather
  424. means dropping the created or dropped
  425. table, if it still exists */
  426. #define TRX_UNDO_TABLE_ID 22 /* Id of the table if the preceding
  427. field is TRUE */
  428. #define TRX_UNDO_NEXT_LOG 30 /* Offset of the next undo log header
  429. on this page, 0 if none */
  430. #define TRX_UNDO_PREV_LOG 32 /* Offset of the previous undo log
  431. header on this page, 0 if none */
  432. #define TRX_UNDO_HISTORY_NODE 34 /* If the log is put to the history
  433. list, the file list node is here */
  434. /*-------------------------------------------------------------*/
  435. #define TRX_UNDO_LOG_HDR_SIZE (34 + FLST_NODE_SIZE)
  436. #ifndef UNIV_NONINL
  437. #include "trx0undo.ic"
  438. #endif
  439. #endif