trx0rec.c 36.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/******************************************************
Transaction undo log record

(c) 1996 Innobase Oy

Created 3/26/1996 Heikki Tuuri
*******************************************************/

#include "trx0rec.h"

#ifdef UNIV_NONINL
#include "trx0rec.ic"
#endif

#include "fsp0fsp.h"
#include "mach0data.h"
#include "trx0rseg.h"
#include "trx0trx.h"
#include "trx0undo.h"
#include "dict0dict.h"
#include "ut0mem.h"
#include "row0upd.h"
#include "que0que.h"
#include "trx0purge.h"
#include "row0row.h"
26
#include "mtr0log.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

/*=========== UNDO LOG RECORD CREATION AND DECODING ====================*/

/**************************************************************************
Writes the mtr log entry of the inserted undo log record on the undo log
page. */
UNIV_INLINE
void
trx_undof_page_add_undo_rec_log(
/*============================*/
	page_t* undo_page,	/* in: undo log page */
	ulint	old_free,	/* in: start offset of the inserted entry */
	ulint	new_free,	/* in: end offset of the entry */
	mtr_t*	mtr)		/* in: mtr */
{
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
42 43 44
	byte*		log_ptr;
	const byte*	log_end;
	ulint		len;
45

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
46
	log_ptr = mlog_open(mtr, 11 + 13 + MLOG_BUF_MARGIN);
47 48 49 50 51 52

	if (log_ptr == NULL) {

		return;
	}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
53
	log_end = &log_ptr[11 + 13 + MLOG_BUF_MARGIN];
54 55
	log_ptr = mlog_write_initial_log_record_fast(
		undo_page, MLOG_UNDO_INSERT, log_ptr, mtr);
56 57 58 59 60
	len = new_free - old_free - 4;

	mach_write_to_2(log_ptr, len);
	log_ptr += 2;

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
61 62 63 64 65
	if (log_ptr + len <= log_end) {
		memcpy(log_ptr, undo_page + old_free + 2, len);
		mlog_close(mtr, log_ptr + len);
	} else {
		mlog_close(mtr, log_ptr);
66 67
		mlog_catenate_string(mtr, undo_page + old_free + 2, len);
	}
68
}
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

/***************************************************************
Parses a redo log record of adding an undo log record. */

byte*
trx_undo_parse_add_undo_rec(
/*========================*/
			/* out: end of log record or NULL */
	byte*	ptr,	/* in: buffer */
	byte*	end_ptr,/* in: buffer end */
	page_t*	page)	/* in: page or NULL */
{
	ulint	len;
	byte*	rec;
	ulint	first_free;

	if (end_ptr < ptr + 2) {

		return(NULL);
	}

	len = mach_read_from_2(ptr);
	ptr += 2;

	if (end_ptr < ptr + len) {

		return(NULL);
	}
97

98 99 100 101
	if (page == NULL) {

		return(ptr + len);
	}
102

103
	first_free = mach_read_from_2(page + TRX_UNDO_PAGE_HDR
104
				      + TRX_UNDO_PAGE_FREE);
105
	rec = page + first_free;
106

107 108 109 110
	mach_write_to_2(rec, first_free + 4 + len);
	mach_write_to_2(rec + 2 + len, first_free);

	mach_write_to_2(page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE,
111
			first_free + 4 + len);
112 113 114 115
	ut_memcpy(rec + 2, ptr, len);

	return(ptr + len);
}
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/**************************************************************************
Calculates the free space left for extending an undo log record. */
UNIV_INLINE
ulint
trx_undo_left(
/*==========*/
			/* out: bytes left */
	page_t* page,	/* in: undo log page */
	byte*	ptr)	/* in: pointer to page */
{
	/* The '- 10' is a safety margin, in case we have some small
	calculation error below */

	return(UNIV_PAGE_SIZE - (ptr - page) - 10 - FIL_PAGE_DATA_END);
}

/**************************************************************************
Reports in the undo log of an insert of a clustered index record. */
static
ulint
trx_undo_page_report_insert(
/*========================*/
					/* out: offset of the inserted entry
					on the page if succeed, 0 if fail */
141
	page_t*		undo_page,	/* in: undo log page */
142 143 144 145 146 147 148 149 150 151 152 153
	trx_t*		trx,		/* in: transaction */
	dict_index_t*	index,		/* in: clustered index */
	dtuple_t*	clust_entry,	/* in: index entry which will be
					inserted to the clustered index */
	mtr_t*		mtr)		/* in: mtr */
{
	ulint		first_free;
	byte*		ptr;
	ulint		len;
	dfield_t*	field;
	ulint		flen;
	ulint		i;
154

155
	ut_ad(mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
156
			       + TRX_UNDO_PAGE_TYPE) == TRX_UNDO_INSERT);
157 158

	first_free = mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
159
				      + TRX_UNDO_PAGE_FREE);
160
	ptr = undo_page + first_free;
161

162 163 164 165 166 167 168 169 170 171 172 173
	ut_ad(first_free <= UNIV_PAGE_SIZE);

	if (trx_undo_left(undo_page, ptr) < 30) {

		/* NOTE: the value 30 must be big enough such that the general
		fields written below fit on the undo log page */

		return(0);
	}

	/* Reserve 2 bytes for the pointer to the next undo log record */
	ptr += 2;
174 175

	/* Store first some general parameters to the undo log */
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	mach_write_to_1(ptr, TRX_UNDO_INSERT_REC);
	ptr++;

	len = mach_dulint_write_much_compressed(ptr, trx->undo_no);
	ptr += len;

	len = mach_dulint_write_much_compressed(ptr, (index->table)->id);
	ptr += len;
	/*----------------------------------------*/
	/* Store then the fields required to uniquely determine the record
	to be inserted in the clustered index */

	for (i = 0; i < dict_index_get_n_unique(index); i++) {

		field = dtuple_get_nth_field(clust_entry, i);

		flen = dfield_get_len(field);

		if (trx_undo_left(undo_page, ptr) < 5) {

			return(0);
		}

199
		len = mach_write_compressed(ptr, flen);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		ptr += len;

		if (flen != UNIV_SQL_NULL) {
			if (trx_undo_left(undo_page, ptr) < flen) {

				return(0);
			}

			ut_memcpy(ptr, dfield_get_data(field), flen);
			ptr += flen;
		}
	}

	if (trx_undo_left(undo_page, ptr) < 2) {

		return(0);
	}

	/*----------------------------------------*/
	/* Write pointers to the previous and the next undo log records */

	if (trx_undo_left(undo_page, ptr) < 2) {

		return(0);
	}

	mach_write_to_2(ptr, first_free);
	ptr += 2;

	mach_write_to_2(undo_page + first_free, ptr - undo_page);

	mach_write_to_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE,
232
			ptr - undo_page);
233

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
234
	/* Write the log entry to the REDO log of this change in the UNDO
235
	log */
236
	trx_undof_page_add_undo_rec_log(undo_page, first_free,
237
					ptr - undo_page, mtr);
238
	return(first_free);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
}

/**************************************************************************
Reads from an undo log record the general parameters. */

byte*
trx_undo_rec_get_pars(
/*==================*/
					/* out: remaining part of undo log
					record after reading these values */
	trx_undo_rec_t*	undo_rec,	/* in: undo log record */
	ulint*		type,		/* out: undo record type:
					TRX_UNDO_INSERT_REC, ... */
	ulint*		cmpl_info,	/* out: compiler info, relevant only
					for update type records */
254 255
	ibool*		updated_extern,	/* out: TRUE if we updated an
					externally stored fild */
256 257 258 259 260 261 262 263 264 265 266
	dulint*		undo_no,	/* out: undo log record number */
	dulint*		table_id)	/* out: table id */
{
	byte*		ptr;
	ulint		len;
	ulint		type_cmpl;

	ptr = undo_rec + 2;

	type_cmpl = mach_read_from_1(ptr);
	ptr++;
267 268 269 270 271 272 273 274

	if (type_cmpl & TRX_UNDO_UPD_EXTERN) {
		*updated_extern = TRUE;
		type_cmpl -= TRX_UNDO_UPD_EXTERN;
	} else {
		*updated_extern = FALSE;
	}

275 276 277
	*type = type_cmpl & (TRX_UNDO_CMPL_INFO_MULT - 1);
	*cmpl_info = type_cmpl / TRX_UNDO_CMPL_INFO_MULT;

278
	*undo_no = mach_dulint_read_much_compressed(ptr);
279 280 281
	len = mach_dulint_get_much_compressed_size(*undo_no);
	ptr += len;

282
	*table_id = mach_dulint_read_much_compressed(ptr);
283 284 285 286 287 288 289 290
	len = mach_dulint_get_much_compressed_size(*table_id);
	ptr += len;

	return(ptr);
}

/**************************************************************************
Reads from an undo log record a stored column value. */
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
291
static
292 293 294 295 296 297 298 299 300
byte*
trx_undo_rec_get_col_val(
/*=====================*/
			/* out: remaining part of undo log record after
			reading these values */
	byte*	ptr,	/* in: pointer to remaining part of undo log record */
	byte**	field,	/* out: pointer to stored field */
	ulint*	len)	/* out: length of the field, or UNIV_SQL_NULL */
{
301
	*len = mach_read_compressed(ptr);
302 303 304
	ptr += mach_get_compressed_size(*len);

	*field = ptr;
305

306
	if (*len != UNIV_SQL_NULL) {
307 308 309 310 311
		if (*len >= UNIV_EXTERN_STORAGE_FIELD) {
			ptr += (*len - UNIV_EXTERN_STORAGE_FIELD);
		} else {
			ptr += *len;
		}
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	}

	return(ptr);
}

/***********************************************************************
Builds a row reference from an undo log record. */

byte*
trx_undo_rec_get_row_ref(
/*=====================*/
				/* out: pointer to remaining part of undo
				record */
	byte*		ptr,	/* in: remaining part of a copy of an undo log
				record, at the start of the row reference;
				NOTE that this copy of the undo log record must
				be preserved as long as the row reference is
				used, as we do NOT copy the data in the
				record! */
	dict_index_t*	index,	/* in: clustered index */
	dtuple_t**	ref,	/* out, own: row reference */
	mem_heap_t*	heap)	/* in: memory heap from which the memory
				needed is allocated */
{
	dfield_t*	dfield;
	byte*		field;
	ulint		len;
	ulint		ref_len;
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
340
	ulint		i;
341

342
	ut_ad(index && ptr && ref && heap);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
343
	ut_a(index->type & DICT_CLUSTERED);
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	ref_len = dict_index_get_n_unique(index);

	*ref = dtuple_create(heap, ref_len);

	dict_index_copy_types(*ref, index, ref_len);

	for (i = 0; i < ref_len; i++) {
		dfield = dtuple_get_nth_field(*ref, i);

		ptr = trx_undo_rec_get_col_val(ptr, &field, &len);

		dfield_set_data(dfield, field, len);
	}

	return(ptr);
360
}
361 362 363 364 365 366 367 368 369 370 371 372 373

/***********************************************************************
Skips a row reference from an undo log record. */

byte*
trx_undo_rec_skip_row_ref(
/*======================*/
				/* out: pointer to remaining part of undo
				record */
	byte*		ptr,	/* in: remaining part in update undo log
				record, at the start of the row reference */
	dict_index_t*	index)	/* in: clustered index */
{
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
374 375 376 377
	byte*	field;
	ulint	len;
	ulint	ref_len;
	ulint	i;
378

379
	ut_ad(index && ptr);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
380
	ut_a(index->type & DICT_CLUSTERED);
381

382 383 384 385 386 387 388
	ref_len = dict_index_get_n_unique(index);

	for (i = 0; i < ref_len; i++) {
		ptr = trx_undo_rec_get_col_val(ptr, &field, &len);
	}

	return(ptr);
389
}
390 391 392 393 394 395 396 397 398 399 400

/**************************************************************************
Reports in the undo log of an update or delete marking of a clustered index
record. */
static
ulint
trx_undo_page_report_modify(
/*========================*/
					/* out: byte offset of the inserted
					undo log entry on the page if succeed,
					0 if fail */
401
	page_t*		undo_page,	/* in: undo log page */
402 403 404 405 406
	trx_t*		trx,		/* in: transaction */
	dict_index_t*	index,		/* in: clustered index where update or
					delete marking is done */
	rec_t*		rec,		/* in: clustered index record which
					has NOT yet been modified */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
407
	const ulint*	offsets,	/* in: rec_get_offsets(rec, index) */
408 409 410 411 412 413 414 415 416 417 418 419
	upd_t*		update,		/* in: update vector which tells the
					columns to be updated; in the case of
					a delete, this should be set to NULL */
	ulint		cmpl_info,	/* in: compiler info on secondary
					index updates */
	mtr_t*		mtr)		/* in: mtr */
{
	dict_table_t*	table;
	upd_field_t*	upd_field;
	ulint		first_free;
	byte*		ptr;
	ulint		len;
420
	byte*		field;
421 422 423 424 425 426 427 428
	ulint		flen;
	ulint		pos;
	dulint		roll_ptr;
	dulint		trx_id;
	ulint		bits;
	ulint		col_no;
	byte*		old_ptr;
	ulint		type_cmpl;
429
	byte*		type_cmpl_ptr;
430
	ulint		i;
431

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
432
	ut_a(index->type & DICT_CLUSTERED);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
433
	ut_ad(rec_offs_validate(rec, index, offsets));
434
	ut_ad(mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
435
			       + TRX_UNDO_PAGE_TYPE) == TRX_UNDO_UPDATE);
436
	table = index->table;
437

438
	first_free = mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
439
				      + TRX_UNDO_PAGE_FREE);
440
	ptr = undo_page + first_free;
441

442 443 444 445 446 447 448 449 450 451 452 453 454
	ut_ad(first_free <= UNIV_PAGE_SIZE);

	if (trx_undo_left(undo_page, ptr) < 50) {

		/* NOTE: the value 50 must be big enough so that the general
		fields written below fit on the undo log page */

		return(0);
	}

	/* Reserve 2 bytes for the pointer to the next undo log record */
	ptr += 2;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
455 456
	/* Store first some general parameters to the undo log */

457
	if (update) {
458
		if (rec_get_deleted_flag(rec, dict_table_is_comp(table))) {
459 460 461 462 463 464 465 466 467 468 469
			type_cmpl = TRX_UNDO_UPD_DEL_REC;
		} else {
			type_cmpl = TRX_UNDO_UPD_EXIST_REC;
		}
	} else {
		type_cmpl = TRX_UNDO_DEL_MARK_REC;
	}

	type_cmpl = type_cmpl | (cmpl_info * TRX_UNDO_CMPL_INFO_MULT);

	mach_write_to_1(ptr, type_cmpl);
470

471 472
	type_cmpl_ptr = ptr;

473 474 475 476 477 478 479 480 481 482
	ptr++;
	len = mach_dulint_write_much_compressed(ptr, trx->undo_no);
	ptr += len;

	len = mach_dulint_write_much_compressed(ptr, table->id);
	ptr += len;

	/*----------------------------------------*/
	/* Store the state of the info bits */

483
	bits = rec_get_info_bits(rec, dict_table_is_comp(table));
484 485 486 487
	mach_write_to_1(ptr, bits);
	ptr += 1;

	/* Store the values of the system columns */
488
	field = rec_get_nth_field(rec, offsets,
489 490
				  dict_index_get_sys_col_pos(
					  index, DATA_TRX_ID), &len);
491 492 493
	ut_ad(len == DATA_TRX_ID_LEN);
	trx_id = trx_read_trx_id(field);
	field = rec_get_nth_field(rec, offsets,
494 495
				  dict_index_get_sys_col_pos(
					  index, DATA_ROLL_PTR), &len);
496 497 498
	ut_ad(len == DATA_ROLL_PTR_LEN);
	roll_ptr = trx_read_roll_ptr(field);

499 500 501 502 503 504 505 506 507 508 509 510
	len = mach_dulint_write_compressed(ptr, trx_id);
	ptr += len;

	len = mach_dulint_write_compressed(ptr, roll_ptr);
	ptr += len;

	/*----------------------------------------*/
	/* Store then the fields required to uniquely determine the
	record which will be modified in the clustered index */

	for (i = 0; i < dict_index_get_n_unique(index); i++) {

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
511
		field = rec_get_nth_field(rec, offsets, i, &flen);
512 513 514 515 516 517

		if (trx_undo_left(undo_page, ptr) < 4) {

			return(0);
		}

518
		len = mach_write_compressed(ptr, flen);
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
		ptr += len;

		if (flen != UNIV_SQL_NULL) {
			if (trx_undo_left(undo_page, ptr) < flen) {

				return(0);
			}

			ut_memcpy(ptr, field, flen);
			ptr += flen;
		}
	}

	/*----------------------------------------*/
	/* Save to the undo log the old values of the columns to be updated. */

	if (update) {
536
		if (trx_undo_left(undo_page, ptr) < 5) {
537

538 539
			return(0);
		}
540

541 542
		len = mach_write_compressed(ptr, upd_get_n_fields(update));
		ptr += len;
543

544
		for (i = 0; i < upd_get_n_fields(update); i++) {
545

546 547
			upd_field = upd_get_nth_field(update, i);
			pos = upd_field->field_no;
548

549 550
			/* Write field number to undo log */
			if (trx_undo_left(undo_page, ptr) < 5) {
551

552 553
				return(0);
			}
554

555 556
			len = mach_write_compressed(ptr, pos);
			ptr += len;
557

558 559
			/* Save the old value of field */
			field = rec_get_nth_field(rec, offsets, pos, &flen);
560

561
			if (trx_undo_left(undo_page, ptr) < 5) {
562

563 564
				return(0);
			}
565

566 567 568
			if (rec_offs_nth_extern(offsets, pos)) {
				/* If a field has external storage, we add
				to flen the flag */
569

570 571 572
				len = mach_write_compressed(
					ptr,
					UNIV_EXTERN_STORAGE_FIELD + flen);
573

574 575
				/* Notify purge that it eventually has to
				free the old externally stored field */
576

577
				trx->update_undo->del_marks = TRUE;
578

579 580 581 582 583
				*type_cmpl_ptr = *type_cmpl_ptr
					| TRX_UNDO_UPD_EXTERN;
			} else {
				len = mach_write_compressed(ptr, flen);
			}
584

585
			ptr += len;
586

587 588
			if (flen != UNIV_SQL_NULL) {
				if (trx_undo_left(undo_page, ptr) < flen) {
589

590 591 592 593 594 595
					return(0);
				}

				ut_memcpy(ptr, field, flen);
				ptr += flen;
			}
596
		}
597
	}
598 599 600 601 602 603 604

	/*----------------------------------------*/
	/* In the case of a delete marking, and also in the case of an update
	where any ordering field of any index changes, store the values of all
	columns which occur as ordering fields in any index. This info is used
	in the purge of old versions where we use it to build and search the
	delete marked index records, to look if we can remove them from the
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
605 606 607 608 609
	index tree. Note that starting from 4.0.14 also externally stored
	fields can be ordering in some index. But we always store at least
	384 first bytes locally to the clustered index record, which means
	we can construct the column prefix fields in the index from the
	stored data. */
610

611
	if (!update || !(cmpl_info & UPD_NODE_NO_ORD_CHANGE)) {
612

613
		trx->update_undo->del_marks = TRUE;
614

615
		if (trx_undo_left(undo_page, ptr) < 5) {
616

617 618
			return(0);
		}
619

620
		old_ptr = ptr;
621

622 623
		/* Reserve 2 bytes to write the number of bytes the stored
		fields take in this undo record */
624

625
		ptr += 2;
626

627 628
		for (col_no = 0; col_no < dict_table_get_n_cols(table);
		     col_no++) {
629

630 631
			const dict_col_t*	col
				= dict_table_get_nth_col(table, col_no);
632

633
			if (col->ord_part > 0) {
634

635
				pos = dict_index_get_nth_col_pos(index,
636
								 col_no);
637

638 639
				/* Write field number to undo log */
				if (trx_undo_left(undo_page, ptr) < 5) {
640 641 642 643

					return(0);
				}

644 645 646 647 648
				len = mach_write_compressed(ptr, pos);
				ptr += len;

				/* Save the old value of field */
				field = rec_get_nth_field(rec, offsets, pos,
649
							  &flen);
650 651 652 653 654 655 656 657 658 659 660

				if (trx_undo_left(undo_page, ptr) < 5) {

					return(0);
				}

				len = mach_write_compressed(ptr, flen);
				ptr += len;

				if (flen != UNIV_SQL_NULL) {
					if (trx_undo_left(undo_page, ptr)
661
					    < flen) {
662 663 664 665 666 667 668

						return(0);
					}

					ut_memcpy(ptr, field, flen);
					ptr += flen;
				}
669 670 671
			}
		}

672 673
		mach_write_to_2(old_ptr, ptr - old_ptr);
	}
674 675 676 677 678 679 680

	/*----------------------------------------*/
	/* Write pointers to the previous and the next undo log records */
	if (trx_undo_left(undo_page, ptr) < 2) {

		return(0);
	}
681

682 683 684 685 686
	mach_write_to_2(ptr, first_free);
	ptr += 2;
	mach_write_to_2(undo_page + first_free, ptr - undo_page);

	mach_write_to_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE,
687
			ptr - undo_page);
688 689 690 691

	/* Write to the REDO log about this change in the UNDO log */

	trx_undof_page_add_undo_rec_log(undo_page, first_free,
692
					ptr - undo_page, mtr);
693
	return(first_free);
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
}

/**************************************************************************
Reads from an undo log update record the system field values of the old
version. */

byte*
trx_undo_update_rec_get_sys_cols(
/*=============================*/
				/* out: remaining part of undo log
				record after reading these values */
	byte*	ptr,		/* in: remaining part of undo log
				record after reading general
				parameters */
	dulint*	trx_id,		/* out: trx id */
	dulint*	roll_ptr,	/* out: roll ptr */
	ulint*	info_bits)	/* out: info bits state */
{
	ulint	len;

	/* Read the state of the info bits */
	*info_bits = mach_read_from_1(ptr);
	ptr += 1;

	/* Read the values of the system columns */

720
	*trx_id = mach_dulint_read_compressed(ptr);
721 722 723
	len = mach_dulint_get_compressed_size(*trx_id);
	ptr += len;

724
	*roll_ptr = mach_dulint_read_compressed(ptr);
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
	len = mach_dulint_get_compressed_size(*roll_ptr);
	ptr += len;

	return(ptr);
}

/**************************************************************************
Reads from an update undo log record the number of updated fields. */
UNIV_INLINE
byte*
trx_undo_update_rec_get_n_upd_fields(
/*=================================*/
			/* out: remaining part of undo log record after
			reading this value */
	byte*	ptr,	/* in: pointer to remaining part of undo log record */
	ulint*	n)	/* out: number of fields */
{
742
	*n = mach_read_compressed(ptr);
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	ptr += mach_get_compressed_size(*n);

	return(ptr);
}

/**************************************************************************
Reads from an update undo log record a stored field number. */
UNIV_INLINE
byte*
trx_undo_update_rec_get_field_no(
/*=============================*/
			/* out: remaining part of undo log record after
			reading this value */
	byte*	ptr,	/* in: pointer to remaining part of undo log record */
	ulint*	field_no)/* out: field number */
{
759
	*field_no = mach_read_compressed(ptr);
760 761 762 763 764 765 766 767 768 769 770
	ptr += mach_get_compressed_size(*field_no);

	return(ptr);
}

/***********************************************************************
Builds an update vector based on a remaining part of an undo log record. */

byte*
trx_undo_update_rec_get_update(
/*===========================*/
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
771 772 773
				/* out: remaining part of the record,
				NULL if an error detected, which means that
				the record is corrupted */
774 775 776 777 778 779 780 781 782 783 784 785
	byte*		ptr,	/* in: remaining part in update undo log
				record, after reading the row reference
				NOTE that this copy of the undo log record must
				be preserved as long as the update vector is
				used, as we do NOT copy the data in the
				record! */
	dict_index_t*	index,	/* in: clustered index */
	ulint		type,	/* in: TRX_UNDO_UPD_EXIST_REC,
				TRX_UNDO_UPD_DEL_REC, or
				TRX_UNDO_DEL_MARK_REC; in the last case,
				only trx id and roll ptr fields are added to
				the update vector */
786
	dulint		trx_id,	/* in: transaction id from this undo record */
787 788
	dulint		roll_ptr,/* in: roll pointer from this undo record */
	ulint		info_bits,/* in: info bits from this undo record */
789
	trx_t*		trx,	/* in: transaction */
790 791 792 793 794 795 796 797 798 799 800 801
	mem_heap_t*	heap,	/* in: memory heap from which the memory
				needed is allocated */
	upd_t**		upd)	/* out, own: update vector */
{
	upd_field_t*	upd_field;
	upd_t*		update;
	ulint		n_fields;
	byte*		buf;
	byte*		field;
	ulint		len;
	ulint		field_no;
	ulint		i;
802

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
803 804
	ut_a(index->type & DICT_CLUSTERED);

805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
	if (type != TRX_UNDO_DEL_MARK_REC) {
		ptr = trx_undo_update_rec_get_n_upd_fields(ptr, &n_fields);
	} else {
		n_fields = 0;
	}

	update = upd_create(n_fields + 2, heap);

	update->info_bits = info_bits;

	/* Store first trx id and roll ptr to update vector */

	upd_field = upd_get_nth_field(update, n_fields);
	buf = mem_heap_alloc(heap, DATA_TRX_ID_LEN);
	trx_write_trx_id(buf, trx_id);

	upd_field_set_field_no(upd_field,
822 823
			       dict_index_get_sys_col_pos(index, DATA_TRX_ID),
			       index, trx);
824 825 826 827 828 829
	dfield_set_data(&(upd_field->new_val), buf, DATA_TRX_ID_LEN);

	upd_field = upd_get_nth_field(update, n_fields + 1);
	buf = mem_heap_alloc(heap, DATA_ROLL_PTR_LEN);
	trx_write_roll_ptr(buf, roll_ptr);

830 831 832
	upd_field_set_field_no(
		upd_field, dict_index_get_sys_col_pos(index, DATA_ROLL_PTR),
		index, trx);
833
	dfield_set_data(&(upd_field->new_val), buf, DATA_ROLL_PTR_LEN);
834

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
835
	/* Store then the updated ordinary columns to the update vector */
836 837 838 839

	for (i = 0; i < n_fields; i++) {

		ptr = trx_undo_update_rec_get_field_no(ptr, &field_no);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
840 841 842

		if (field_no >= dict_index_get_n_fields(index)) {
			fprintf(stderr,
843 844 845
				"InnoDB: Error: trying to access"
				" update undo rec field %lu in ",
				(ulong) field_no);
846
			dict_index_name_print(stderr, trx, index);
847
			fprintf(stderr, "\n"
848 849 850 851
				"InnoDB: but index has only %lu fields\n"
				"InnoDB: Submit a detailed bug report"
				" to http://bugs.mysql.com\n"
				"InnoDB: Run also CHECK TABLE ",
monty@mishka.local's avatar
monty@mishka.local committed
852
				(ulong) dict_index_get_n_fields(index));
853
			ut_print_name(stderr, trx, TRUE, index->table_name);
854
			fprintf(stderr, "\n"
855
				"InnoDB: n_fields = %lu, i = %lu, ptr %p\n",
monty@mishka.local's avatar
monty@mishka.local committed
856
				(ulong) n_fields, (ulong) i, ptr);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
857 858 859
			return(NULL);
		}

860 861 862 863
		ptr = trx_undo_rec_get_col_val(ptr, &field, &len);

		upd_field = upd_get_nth_field(update, i);

864
		upd_field_set_field_no(upd_field, field_no, index, trx);
865

866 867 868
		if (len != UNIV_SQL_NULL && len >= UNIV_EXTERN_STORAGE_FIELD) {

			upd_field->extern_storage = TRUE;
869

870 871
			len -= UNIV_EXTERN_STORAGE_FIELD;
		}
872

873 874 875 876 877 878 879
		dfield_set_data(&(upd_field->new_val), field, len);
	}

	*upd = update;

	return(ptr);
}
880

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
/***********************************************************************
Builds a partial row from an update undo log record. It contains the
columns which occur as ordering in any index of the table. */

byte*
trx_undo_rec_get_partial_row(
/*=========================*/
				/* out: pointer to remaining part of undo
				record */
	byte*		ptr,	/* in: remaining part in update undo log
				record of a suitable type, at the start of
				the stored index columns;
				NOTE that this copy of the undo log record must
				be preserved as long as the partial row is
				used, as we do NOT copy the data in the
				record! */
	dict_index_t*	index,	/* in: clustered index */
	dtuple_t**	row,	/* out, own: partial row */
	mem_heap_t*	heap)	/* in: memory heap from which the memory
				needed is allocated */
{
	dfield_t*	dfield;
	byte*		field;
	ulint		len;
	ulint		field_no;
	ulint		col_no;
	ulint		row_len;
	ulint		total_len;
	byte*		start_ptr;
	ulint		i;
911

912
	ut_ad(index && ptr && row && heap);
913

914 915 916 917 918 919 920 921 922 923
	row_len = dict_table_get_n_cols(index->table);

	*row = dtuple_create(heap, row_len);

	dict_table_copy_types(*row, index->table);

	start_ptr = ptr;

	total_len = mach_read_from_2(ptr);
	ptr += 2;
924

925 926 927 928 929 930
	for (i = 0;; i++) {

		if (ptr == start_ptr + total_len) {

			break;
		}
931

932 933 934
		ptr = trx_undo_update_rec_get_field_no(ptr, &field_no);

		col_no = dict_index_get_nth_col_no(index, field_no);
935

936 937 938 939 940 941 942 943
		ptr = trx_undo_rec_get_col_val(ptr, &field, &len);

		dfield = dtuple_get_nth_field(*row, col_no);

		dfield_set_data(dfield, field, len);
	}

	return(ptr);
944
}
945 946 947 948 949 950 951 952 953 954 955

/***************************************************************************
Erases the unused undo log page end. */
static
void
trx_undo_erase_page_end(
/*====================*/
	page_t*	undo_page,	/* in: undo page whose end to erase */
	mtr_t*	mtr)		/* in: mtr */
{
	ulint	first_free;
956

957
	first_free = mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
958
				      + TRX_UNDO_PAGE_FREE);
959
	memset(undo_page + first_free, 0xff,
960
	       (UNIV_PAGE_SIZE - FIL_PAGE_DATA_END) - first_free);
961 962 963

	mlog_write_initial_log_record(undo_page, MLOG_UNDO_ERASE_END, mtr);
}
964

965 966 967 968 969 970 971 972
/***************************************************************
Parses a redo log record of erasing of an undo page end. */

byte*
trx_undo_parse_erase_page_end(
/*==========================*/
			/* out: end of log record or NULL */
	byte*	ptr,	/* in: buffer */
973
	byte*	end_ptr __attribute__((unused)), /* in: buffer end */
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	page_t*	page,	/* in: page or NULL */
	mtr_t*	mtr)	/* in: mtr or NULL */
{
	ut_ad(ptr && end_ptr);

	if (page == NULL) {

		return(ptr);
	}

	trx_undo_erase_page_end(page, mtr);

	return(ptr);
}

/***************************************************************************
Writes information to an undo log about an insert, update, or a delete marking
of a clustered index record. This information is used in a rollback of the
transaction and in consistent reads that must look to the history of this
transaction. */

ulint
trx_undo_report_row_operation(
/*==========================*/
					/* out: DB_SUCCESS or error code */
	ulint		flags,		/* in: if BTR_NO_UNDO_LOG_FLAG bit is
					set, does nothing */
	ulint		op_type,	/* in: TRX_UNDO_INSERT_OP or
					TRX_UNDO_MODIFY_OP */
	que_thr_t*	thr,		/* in: query thread */
	dict_index_t*	index,		/* in: clustered index */
	dtuple_t*	clust_entry,	/* in: in the case of an insert,
					index entry to insert into the
					clustered index, otherwise NULL */
	upd_t*		update,		/* in: in the case of an update,
					the update vector, otherwise NULL */
	ulint		cmpl_info,	/* in: compiler info on secondary
					index updates */
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1012
	rec_t*		rec,		/* in: in case of an update or delete
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
					marking, the record in the clustered
					index, otherwise NULL */
	dulint*		roll_ptr)	/* out: rollback pointer to the
					inserted undo log record,
					ut_dulint_zero if BTR_NO_UNDO_LOG
					flag was specified */
{
	trx_t*		trx;
	trx_undo_t*	undo;
	page_t*		undo_page;
	ulint		offset;
	ulint		page_no;
	ibool		is_insert;
	trx_rseg_t*	rseg;
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1027
	mtr_t		mtr;
1028
	ulint		err		= DB_SUCCESS;
1029
	mem_heap_t*	heap		= NULL;
1030
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
1031
	ulint*		offsets		= offsets_;
1032
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1033

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1034 1035
	ut_a(index->type & DICT_CLUSTERED);

1036 1037 1038 1039
	if (flags & BTR_NO_UNDO_LOG_FLAG) {

		*roll_ptr = ut_dulint_zero;

1040
		return(err);
1041
	}
1042

1043 1044
	ut_ad(thr);
	ut_ad((op_type != TRX_UNDO_INSERT_OP)
1045
	      || (clust_entry && !update && !rec));
1046

1047 1048
	trx = thr_get_trx(thr);
	rseg = trx->rseg;
1049

1050 1051 1052 1053 1054 1055 1056 1057
	mutex_enter(&(trx->undo_mutex));

	/* If the undo log is not assigned yet, assign one */

	if (op_type == TRX_UNDO_INSERT_OP) {

		if (trx->insert_undo == NULL) {

1058
			err = trx_undo_assign_undo(trx, TRX_UNDO_INSERT);
1059 1060 1061 1062 1063 1064 1065 1066 1067
		}

		undo = trx->insert_undo;
		is_insert = TRUE;
	} else {
		ut_ad(op_type == TRX_UNDO_MODIFY_OP);

		if (trx->update_undo == NULL) {

1068
			err = trx_undo_assign_undo(trx, TRX_UNDO_UPDATE);
1069 1070 1071 1072 1073 1074 1075

		}

		undo = trx->update_undo;
		is_insert = FALSE;
	}

1076 1077
	if (err != DB_SUCCESS) {
		/* Did not succeed: return the error encountered */
1078 1079
		mutex_exit(&(trx->undo_mutex));

1080
		return(err);
1081 1082 1083
	}

	page_no = undo->last_page_no;
1084

1085 1086 1087 1088
	mtr_start(&mtr);

	for (;;) {
		undo_page = buf_page_get_gen(undo->space, page_no,
1089 1090 1091 1092
					     RW_X_LATCH, undo->guess_page,
					     BUF_GET,
					     __FILE__, __LINE__,
					     &mtr);
1093

1094
#ifdef UNIV_SYNC_DEBUG
1095
		buf_page_dbg_add_level(undo_page, SYNC_TRX_UNDO_PAGE);
1096
#endif /* UNIV_SYNC_DEBUG */
1097 1098

		if (op_type == TRX_UNDO_INSERT_OP) {
1099 1100
			offset = trx_undo_page_report_insert(
				undo_page, trx, index, clust_entry, &mtr);
1101
		} else {
1102
			offsets = rec_get_offsets(rec, index, offsets,
1103
						  ULINT_UNDEFINED, &heap);
1104 1105 1106
			offset = trx_undo_page_report_modify(
				undo_page, trx, index, rec, offsets, update,
				cmpl_info, &mtr);
1107 1108 1109 1110 1111
		}

		if (offset == 0) {
			/* The record did not fit on the page. We erase the
			end segment of the undo log page and write a log
1112 1113 1114
			record of it: this is to ensure that in the debug
			version the replicate page constructed using the log
			records stays identical to the original page */
1115 1116 1117

			trx_undo_erase_page_end(undo_page, &mtr);
		}
1118

1119 1120 1121 1122 1123 1124 1125 1126 1127
		mtr_commit(&mtr);

		if (offset != 0) {
			/* Success */

			break;
		}

		ut_ad(page_no == undo->last_page_no);
1128

1129 1130 1131 1132 1133 1134 1135 1136 1137
		/* We have to extend the undo log by one page */

		mtr_start(&mtr);

		/* When we add a page to an undo log, this is analogous to
		a pessimistic insert in a B-tree, and we must reserve the
		counterpart of the tree latch, which is the rseg mutex. */

		mutex_enter(&(rseg->mutex));
1138

1139 1140 1141
		page_no = trx_undo_add_page(trx, undo, &mtr);

		mutex_exit(&(rseg->mutex));
1142

1143 1144 1145 1146 1147
		if (page_no == FIL_NULL) {
			/* Did not succeed: out of space */

			mutex_exit(&(trx->undo_mutex));
			mtr_commit(&mtr);
1148
			if (UNIV_LIKELY_NULL(heap)) {
1149 1150
				mem_heap_free(heap);
			}
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
			return(DB_OUT_OF_FILE_SPACE);
		}
	}

	undo->empty = FALSE;
	undo->top_page_no = page_no;
	undo->top_offset  = offset;
	undo->top_undo_no = trx->undo_no;
	undo->guess_page = undo_page;

	UT_DULINT_INC(trx->undo_no);
1162

1163 1164 1165
	mutex_exit(&(trx->undo_mutex));

	*roll_ptr = trx_undo_build_roll_ptr(is_insert, rseg->id, page_no,
1166
					    offset);
1167
	if (UNIV_LIKELY_NULL(heap)) {
1168 1169
		mem_heap_free(heap);
	}
1170
	return(err);
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
}

/*============== BUILDING PREVIOUS VERSION OF A RECORD ===============*/

/**********************************************************************
Copies an undo record to heap. This function can be called if we know that
the undo log record exists. */

trx_undo_rec_t*
trx_undo_get_undo_rec_low(
/*======================*/
					/* out, own: copy of the record */
	dulint		roll_ptr,	/* in: roll pointer to record */
	mem_heap_t*	heap)		/* in: memory heap where copied */
{
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1186
	trx_undo_rec_t*	undo_rec;
1187 1188 1189 1190 1191 1192 1193
	ulint		rseg_id;
	ulint		page_no;
	ulint		offset;
	page_t*		undo_page;
	trx_rseg_t*	rseg;
	ibool		is_insert;
	mtr_t		mtr;
1194

1195
	trx_undo_decode_roll_ptr(roll_ptr, &is_insert, &rseg_id, &page_no,
1196
				 &offset);
1197 1198 1199
	rseg = trx_rseg_get_on_id(rseg_id);

	mtr_start(&mtr);
1200

1201
	undo_page = trx_undo_page_get_s_latched(rseg->space, page_no, &mtr);
1202

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
	undo_rec = trx_undo_rec_copy(undo_page + offset, heap);

	mtr_commit(&mtr);

	return(undo_rec);
}

/**********************************************************************
Copies an undo record to heap. */

ulint
trx_undo_get_undo_rec(
/*==================*/
					/* out: DB_SUCCESS, or
					DB_MISSING_HISTORY if the undo log
					has been truncated and we cannot
					fetch the old version; NOTE: the
					caller must have latches on the
					clustered index page and purge_view */
	dulint		roll_ptr,	/* in: roll pointer to record */
	dulint		trx_id,		/* in: id of the trx that generated
					the roll pointer: it points to an
					undo log of this transaction */
	trx_undo_rec_t** undo_rec,	/* out, own: copy of the record */
	mem_heap_t*	heap)		/* in: memory heap where copied */
{
1229
#ifdef UNIV_SYNC_DEBUG
1230
	ut_ad(rw_lock_own(&(purge_sys->latch), RW_LOCK_SHARED));
1231
#endif /* UNIV_SYNC_DEBUG */
1232 1233 1234

	if (!trx_purge_update_undo_must_exist(trx_id)) {

1235
		/* It may be that the necessary undo log has already been
1236 1237 1238 1239 1240 1241
		deleted */

		return(DB_MISSING_HISTORY);
	}

	*undo_rec = trx_undo_get_undo_rec_low(roll_ptr, heap);
1242

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
	return(DB_SUCCESS);
}

/***********************************************************************
Build a previous version of a clustered index record. This function checks
that the caller has a latch on the index page of the clustered index record
and an s-latch on the purge_view. This guarantees that the stack of versions
is locked. */

ulint
trx_undo_prev_version_build(
/*========================*/
				/* out: DB_SUCCESS, or DB_MISSING_HISTORY if
				the previous version is not >= purge_view,
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1257 1258
				which means that it may have been removed,
				DB_ERROR if corrupted record */
1259 1260
	rec_t*		index_rec,/* in: clustered index record in the
				index tree */
1261
	mtr_t*		index_mtr __attribute__((unused)),
1262
				/* in: mtr which contains the latch to
1263 1264 1265
				index_rec page and purge_view */
	rec_t*		rec,	/* in: version of a clustered index record */
	dict_index_t*	index,	/* in: clustered index */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1266
	ulint*		offsets,/* in: rec_get_offsets(rec, index) */
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	mem_heap_t*	heap,	/* in: memory heap from which the memory
				needed is allocated */
	rec_t**		old_vers)/* out, own: previous version, or NULL if
				rec is the first inserted version, or if
				history data has been deleted */
{
	trx_undo_rec_t*	undo_rec;
	dtuple_t*	entry;
	dulint		rec_trx_id;
	ulint		type;
	dulint		undo_no;
	dulint		table_id;
	dulint		trx_id;
	dulint		roll_ptr;
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1281
	dulint		old_roll_ptr;
1282 1283 1284 1285
	upd_t*		update;
	byte*		ptr;
	ulint		info_bits;
	ulint		cmpl_info;
1286
	ibool		dummy_extern;
1287 1288
	byte*		buf;
	ulint		err;
1289
#ifdef UNIV_SYNC_DEBUG
1290
	ut_ad(rw_lock_own(&(purge_sys->latch), RW_LOCK_SHARED));
1291
#endif /* UNIV_SYNC_DEBUG */
1292
	ut_ad(mtr_memo_contains(index_mtr, buf_block_align(index_rec),
1293 1294 1295
				MTR_MEMO_PAGE_S_FIX)
	      || mtr_memo_contains(index_mtr, buf_block_align(index_rec),
				   MTR_MEMO_PAGE_X_FIX));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1296 1297
	ut_ad(rec_offs_validate(rec, index, offsets));

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1298
	if (!(index->type & DICT_CLUSTERED)) {
1299 1300 1301 1302 1303
		fprintf(stderr, "InnoDB: Error: trying to access"
			" update undo rec for non-clustered index %s\n"
			"InnoDB: Submit a detailed bug report to"
			" http://bugs.mysql.com\n"
			"InnoDB: index record ", index->name);
1304
		rec_print(stderr, index_rec, index);
1305
		fputs("\n"
1306
		      "InnoDB: record version ", stderr);
1307
		rec_print_new(stderr, rec, offsets);
1308
		putc('\n', stderr);
1309 1310
		return(DB_ERROR);
	}
1311

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1312
	roll_ptr = row_get_rec_roll_ptr(rec, index, offsets);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1313
	old_roll_ptr = roll_ptr;
1314

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1315
	*old_vers = NULL;
1316 1317 1318 1319 1320 1321 1322 1323

	if (trx_undo_roll_ptr_is_insert(roll_ptr)) {

		/* The record rec is the first inserted version */

		return(DB_SUCCESS);
	}

1324 1325
	rec_trx_id = row_get_rec_trx_id(rec, index, offsets);

1326 1327 1328 1329 1330 1331 1332
	err = trx_undo_get_undo_rec(roll_ptr, rec_trx_id, &undo_rec, heap);

	if (err != DB_SUCCESS) {

		return(err);
	}

1333
	ptr = trx_undo_rec_get_pars(undo_rec, &type, &cmpl_info,
1334
				    &dummy_extern, &undo_no, &table_id);
1335

1336
	ptr = trx_undo_update_rec_get_sys_cols(ptr, &trx_id, &roll_ptr,
1337
					       &info_bits);
1338 1339
	ptr = trx_undo_rec_skip_row_ref(ptr, index);

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1340
	ptr = trx_undo_update_rec_get_update(ptr, index, type, trx_id,
1341 1342
					     roll_ptr, info_bits,
					     NULL, heap, &update);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1343 1344 1345 1346

	if (ut_dulint_cmp(table_id, index->table->id) != 0) {
		ptr = NULL;

1347
		fprintf(stderr,
1348 1349 1350 1351 1352 1353 1354
			"InnoDB: Error: trying to access update undo rec"
			" for table %s\n"
			"InnoDB: but the table id in the"
			" undo record is wrong\n"
			"InnoDB: Submit a detailed bug report"
			" to http://bugs.mysql.com\n"
			"InnoDB: Run also CHECK TABLE %s\n",
1355
			index->table_name, index->table_name);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1356 1357 1358 1359 1360 1361
	}

	if (ptr == NULL) {
		/* The record was corrupted, return an error; these printfs
		should catch an elusive bug in row_vers_old_has_index_entry */

1362
		fprintf(stderr,
1363 1364 1365 1366 1367
			"InnoDB: table %s, index %s, n_uniq %lu\n"
			"InnoDB: undo rec address %p, type %lu cmpl_info %lu\n"
			"InnoDB: undo rec table id %lu %lu,"
			" index table id %lu %lu\n"
			"InnoDB: dump of 150 bytes in undo rec: ",
1368
			index->table_name, index->name,
monty@mishka.local's avatar
monty@mishka.local committed
1369 1370
			(ulong) dict_index_get_n_unique(index),
			undo_rec, (ulong) type, (ulong) cmpl_info,
1371 1372 1373 1374
			(ulong) ut_dulint_get_high(table_id),
			(ulong) ut_dulint_get_low(table_id),
			(ulong) ut_dulint_get_high(index->table->id),
			(ulong) ut_dulint_get_low(index->table->id));
1375 1376
		ut_print_buf(stderr, undo_rec, 150);
		fputs("\n"
1377
		      "InnoDB: index record ", stderr);
1378
		rec_print(stderr, index_rec, index);
1379
		fputs("\n"
1380
		      "InnoDB: record version ", stderr);
1381
		rec_print_new(stderr, rec, offsets);
1382
		fprintf(stderr, "\n"
1383 1384 1385 1386
			"InnoDB: Record trx id %lu %lu, update rec"
			" trx id %lu %lu\n"
			"InnoDB: Roll ptr in rec %lu %lu, in update rec"
			" %lu %lu\n",
1387 1388 1389 1390 1391 1392 1393 1394 1395
			(ulong) ut_dulint_get_high(rec_trx_id),
			(ulong) ut_dulint_get_low(rec_trx_id),
			(ulong) ut_dulint_get_high(trx_id),
			(ulong) ut_dulint_get_low(trx_id),
			(ulong) ut_dulint_get_high(old_roll_ptr),
			(ulong) ut_dulint_get_low(old_roll_ptr),
			(ulong) ut_dulint_get_high(roll_ptr),
			(ulong) ut_dulint_get_low(roll_ptr));

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1396 1397 1398
		trx_purge_sys_print();
		return(DB_ERROR);
	}
1399

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1400
	if (row_upd_changes_field_size_or_external(index, offsets, update)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1401 1402 1403 1404 1405 1406 1407 1408 1409
		ulint*	ext_vect;
		ulint	n_ext_vect;

		/* We have to set the appropriate extern storage bits in the
		old version of the record: the extern bits in rec for those
		fields that update does NOT update, as well as the the bits for
		those fields that update updates to become externally stored
		fields. Store the info to ext_vect: */

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1410
		ext_vect = mem_alloc(sizeof(ulint)
1411
				     * rec_offs_n_fields(offsets));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1412
		n_ext_vect = btr_push_update_extern_fields(ext_vect, offsets,
1413
							   update);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1414
		entry = row_rec_to_index_entry(ROW_COPY_DATA, index, rec,
1415
					       heap);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1416
		row_upd_index_replace_new_col_vals(entry, index, update, heap);
1417

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1418
		buf = mem_heap_alloc(heap,
1419
				     rec_get_converted_size(index, entry));
1420

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1421
		*old_vers = rec_convert_dtuple_to_rec(buf, index, entry);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1422 1423

		/* Now set the extern bits in the old version of the record */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1424
		rec_set_field_extern_bits(*old_vers, index,
1425
					  ext_vect, n_ext_vect, NULL);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1426
		mem_free(ext_vect);
1427
	} else {
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1428 1429 1430 1431
		buf = mem_heap_alloc(heap, rec_offs_size(offsets));
		*old_vers = rec_copy(buf, rec, offsets);
		rec_offs_make_valid(*old_vers, index, offsets);
		row_upd_rec_in_place(*old_vers, offsets, update);
1432 1433 1434 1435
	}

	return(DB_SUCCESS);
}