trx0rec.c 37.9 KB
Newer Older
osku's avatar
osku committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/******************************************************
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"
22
#include "row0ext.h"
osku's avatar
osku committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "row0upd.h"
#include "que0que.h"
#include "trx0purge.h"
#include "row0row.h"

/*=========== 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 */
{
	byte*		log_ptr;
	const byte*	log_end;
	ulint		len;

	log_ptr = mlog_open(mtr, 11 + 13 + MLOG_BUF_MARGIN);

	if (log_ptr == NULL) {

		return;
	}

	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);
osku's avatar
osku committed
56 57 58 59 60 61 62 63 64 65 66 67
	len = new_free - old_free - 4;

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

	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);
		mlog_catenate_string(mtr, undo_page + old_free + 2, len);
	}
68
}
osku's avatar
osku committed
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

osku's avatar
osku committed
98 99 100 101
	if (page == NULL) {

		return(ptr + len);
	}
102

osku's avatar
osku committed
103
	first_free = mach_read_from_2(page + TRX_UNDO_PAGE_HDR
104
				      + TRX_UNDO_PAGE_FREE);
osku's avatar
osku committed
105
	rec = page + first_free;
106

osku's avatar
osku committed
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);
osku's avatar
osku committed
112 113 114 115
	ut_memcpy(rec + 2, ptr, len);

	return(ptr + len);
}
116

osku's avatar
osku committed
117 118 119 120 121 122
/**************************************************************************
Calculates the free space left for extending an undo log record. */
UNIV_INLINE
ulint
trx_undo_left(
/*==========*/
123 124 125
				/* out: bytes left */
	const page_t*	page,	/* in: undo log page */
	const byte*	ptr)	/* in: pointer to page */
osku's avatar
osku committed
126 127 128 129 130 131 132
{
	/* 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);
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/**************************************************************************
Set the next and previous pointers in the undo page for the undo record
that was written to ptr. Update the first free value by the number of bytes
written for this undo record.*/
static
ulint
trx_undo_page_set_next_prev_and_add(
/*================================*/
					/* out: offset of the inserted entry
					on the page if succeeded, 0 if fail */
	page_t*		undo_page,	/* in/out: undo log page */
	byte*		ptr,		/* in: ptr up to where data has been
					written on this undo page. */
	mtr_t*		mtr)		/* in: mtr */
{
	ulint		first_free;	/* offset within undo_page */
	ulint		end_of_rec;	/* offset within undo_page */
	byte*		ptr_to_first_free;
					/* pointer within undo_page
					that points to the next free
					offset value within undo_page.*/

	ut_ad(ptr > undo_page);
	ut_ad(ptr < undo_page + UNIV_PAGE_SIZE);

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

		return(0);
	}

	ptr_to_first_free = undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE;

	first_free = mach_read_from_2(ptr_to_first_free);

	/* Write offset of the previous undo log record */
	mach_write_to_2(ptr, first_free);
	ptr += 2;

	end_of_rec = ptr - undo_page;

	/* Write offset of the next undo log record */
	mach_write_to_2(undo_page + first_free, end_of_rec);

	/* Update the offset to first free undo record */
	mach_write_to_2(ptr_to_first_free, end_of_rec);

	/* Write this log entry to the UNDO log */
	trx_undof_page_add_undo_rec_log(undo_page, first_free,
					end_of_rec, mtr);

	return(first_free);
}

osku's avatar
osku committed
186 187 188 189 190 191 192 193
/**************************************************************************
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 */
194
	page_t*		undo_page,	/* in: undo log page */
osku's avatar
osku committed
195 196
	trx_t*		trx,		/* in: transaction */
	dict_index_t*	index,		/* in: clustered index */
197
	const dtuple_t*	clust_entry,	/* in: index entry which will be
osku's avatar
osku committed
198 199 200 201 202 203
					inserted to the clustered index */
	mtr_t*		mtr)		/* in: mtr */
{
	ulint		first_free;
	byte*		ptr;
	ulint		i;
204

osku's avatar
osku committed
205
	ut_ad(mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
206
			       + TRX_UNDO_PAGE_TYPE) == TRX_UNDO_INSERT);
osku's avatar
osku committed
207 208

	first_free = mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
209
				      + TRX_UNDO_PAGE_FREE);
osku's avatar
osku committed
210
	ptr = undo_page + first_free;
211

osku's avatar
osku committed
212 213
	ut_ad(first_free <= UNIV_PAGE_SIZE);

214
	if (trx_undo_left(undo_page, ptr) < 2 + 1 + 11 + 11) {
osku's avatar
osku committed
215

216
		/* Not enough space for writing the general parameters */
osku's avatar
osku committed
217 218 219 220 221 222

		return(0);
	}

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

	/* Store first some general parameters to the undo log */
225 226 227
	*ptr++ = TRX_UNDO_INSERT_REC;
	ptr += mach_dulint_write_much_compressed(ptr, trx->undo_no);
	ptr += mach_dulint_write_much_compressed(ptr, index->table->id);
osku's avatar
osku committed
228 229 230 231 232 233
	/*----------------------------------------*/
	/* 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++) {

234 235
		const dfield_t*	field	= dtuple_get_nth_field(clust_entry, i);
		ulint		flen	= dfield_get_len(field);
osku's avatar
osku committed
236 237 238 239 240 241

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

			return(0);
		}

242
		ptr += mach_write_compressed(ptr, flen);
osku's avatar
osku committed
243 244 245 246 247 248 249

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

				return(0);
			}

250
			ut_memcpy(ptr, dfield_get_data(field), flen);
osku's avatar
osku committed
251 252 253 254
			ptr += flen;
		}
	}

255 256
	return(trx_undo_page_set_next_prev_and_add(undo_page, ptr, mtr));
}
osku's avatar
osku committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

/**************************************************************************
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 */
	ibool*		updated_extern,	/* out: TRUE if we updated an
					externally stored fild */
	dulint*		undo_no,	/* out: undo log record number */
	dulint*		table_id)	/* out: table id */
{
	byte*		ptr;
	ulint		type_cmpl;

	ptr = undo_rec + 2;

	type_cmpl = mach_read_from_1(ptr);
	ptr++;

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

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

294
	*undo_no = mach_dulint_read_much_compressed(ptr);
295
	ptr += mach_dulint_get_much_compressed_size(*undo_no);
osku's avatar
osku committed
296

297
	*table_id = mach_dulint_read_much_compressed(ptr);
298
	ptr += mach_dulint_get_much_compressed_size(*table_id);
osku's avatar
osku committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

	return(ptr);
}

/**************************************************************************
Reads from an undo log record a stored column value. */
static
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 */
{
315
	*len = mach_read_compressed(ptr);
osku's avatar
osku committed
316 317 318
	ptr += mach_get_compressed_size(*len);

	*field = ptr;
319

osku's avatar
osku committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	if (*len != UNIV_SQL_NULL) {
		if (*len >= UNIV_EXTERN_STORAGE_FIELD) {
			ptr += (*len - UNIV_EXTERN_STORAGE_FIELD);
		} else {
			ptr += *len;
		}
	}

	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;
	ulint		i;
355

osku's avatar
osku committed
356
	ut_ad(index && ptr && ref && heap);
357
	ut_a(dict_index_is_clust(index));
358

osku's avatar
osku committed
359 360 361 362 363 364 365
	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++) {
366
		dfield = dtuple_get_nth_field(*ref, i);
osku's avatar
osku committed
367 368 369 370 371 372 373

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

		dfield_set_data(dfield, field, len);
	}

	return(ptr);
374
}
osku's avatar
osku committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

/***********************************************************************
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 */
{
	byte*	field;
	ulint	len;
	ulint	ref_len;
	ulint	i;
392

osku's avatar
osku committed
393
	ut_ad(index && ptr);
394
	ut_a(dict_index_is_clust(index));
395

osku's avatar
osku committed
396 397 398 399 400 401 402
	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);
403
}
osku's avatar
osku committed
404 405 406 407 408 409 410 411 412 413 414

/**************************************************************************
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 */
415
	page_t*		undo_page,	/* in: undo log page */
osku's avatar
osku committed
416 417 418
	trx_t*		trx,		/* in: transaction */
	dict_index_t*	index,		/* in: clustered index where update or
					delete marking is done */
marko's avatar
marko committed
419
	const rec_t*	rec,		/* in: clustered index record which
osku's avatar
osku committed
420 421
					has NOT yet been modified */
	const ulint*	offsets,	/* in: rec_get_offsets(rec, index) */
422
	const upd_t*	update,		/* in: update vector which tells the
osku's avatar
osku committed
423 424 425 426 427 428 429 430 431
					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;
	ulint		first_free;
	byte*		ptr;
marko's avatar
marko committed
432
	const byte*	field;
osku's avatar
osku committed
433 434 435 436 437
	ulint		flen;
	ulint		col_no;
	ulint		type_cmpl;
	byte*		type_cmpl_ptr;
	ulint		i;
438

439
	ut_a(dict_index_is_clust(index));
osku's avatar
osku committed
440 441
	ut_ad(rec_offs_validate(rec, index, offsets));
	ut_ad(mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
442
			       + TRX_UNDO_PAGE_TYPE) == TRX_UNDO_UPDATE);
osku's avatar
osku committed
443
	table = index->table;
444

osku's avatar
osku committed
445
	first_free = mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
446
				      + TRX_UNDO_PAGE_FREE);
osku's avatar
osku committed
447
	ptr = undo_page + first_free;
448

osku's avatar
osku committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	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;

	/* Store first some general parameters to the undo log */

marko's avatar
marko committed
464
	if (!update) {
osku's avatar
osku committed
465
		type_cmpl = TRX_UNDO_DEL_MARK_REC;
marko's avatar
marko committed
466 467 468 469
	} else if (rec_get_deleted_flag(rec, dict_table_is_comp(table))) {
		type_cmpl = TRX_UNDO_UPD_DEL_REC;
	} else {
		type_cmpl = TRX_UNDO_UPD_EXIST_REC;
osku's avatar
osku committed
470 471
	}

marko's avatar
marko committed
472
	type_cmpl |= cmpl_info * TRX_UNDO_CMPL_INFO_MULT;
osku's avatar
osku committed
473 474
	type_cmpl_ptr = ptr;

475 476
	*ptr++ = type_cmpl;
	ptr += mach_dulint_write_much_compressed(ptr, trx->undo_no);
osku's avatar
osku committed
477

478
	ptr += mach_dulint_write_much_compressed(ptr, table->id);
osku's avatar
osku committed
479 480 481 482

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

marko's avatar
marko committed
483
	*ptr++ = rec_get_info_bits(rec, dict_table_is_comp(table));
osku's avatar
osku committed
484 485 486

	/* Store the values of the system columns */
	field = rec_get_nth_field(rec, offsets,
487
				  dict_index_get_sys_col_pos(
488 489
					  index, DATA_TRX_ID), &flen);
	ut_ad(flen == DATA_TRX_ID_LEN);
marko's avatar
marko committed
490

491
	ptr += mach_dulint_write_compressed(ptr, trx_read_trx_id(field));
marko's avatar
marko committed
492

osku's avatar
osku committed
493
	field = rec_get_nth_field(rec, offsets,
494
				  dict_index_get_sys_col_pos(
495 496
					  index, DATA_ROLL_PTR), &flen);
	ut_ad(flen == DATA_ROLL_PTR_LEN);
osku's avatar
osku committed
497

498
	ptr += mach_dulint_write_compressed(ptr, trx_read_roll_ptr(field));
osku's avatar
osku committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512

	/*----------------------------------------*/
	/* 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++) {

		field = rec_get_nth_field(rec, offsets, i, &flen);

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

			return(0);
		}

513
		ptr += mach_write_compressed(ptr, flen);
osku's avatar
osku committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

		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) {
530
		if (trx_undo_left(undo_page, ptr) < 5) {
osku's avatar
osku committed
531

532 533
			return(0);
		}
osku's avatar
osku committed
534

535
		ptr += mach_write_compressed(ptr, upd_get_n_fields(update));
osku's avatar
osku committed
536

537
		for (i = 0; i < upd_get_n_fields(update); i++) {
osku's avatar
osku committed
538

marko's avatar
marko committed
539
			ulint	pos = upd_get_nth_field(update, i)->field_no;
osku's avatar
osku committed
540

541 542
			/* Write field number to undo log */
			if (trx_undo_left(undo_page, ptr) < 5) {
osku's avatar
osku committed
543

544 545
				return(0);
			}
osku's avatar
osku committed
546

547
			ptr += mach_write_compressed(ptr, pos);
osku's avatar
osku committed
548

549 550
			/* Save the old value of field */
			field = rec_get_nth_field(rec, offsets, pos, &flen);
osku's avatar
osku committed
551

552
			if (trx_undo_left(undo_page, ptr) < 5) {
osku's avatar
osku committed
553

554 555
				return(0);
			}
osku's avatar
osku committed
556

557 558 559
			if (rec_offs_nth_extern(offsets, pos)) {
				/* If a field has external storage, we add
				to flen the flag */
osku's avatar
osku committed
560

561
				ptr += mach_write_compressed(
562 563
					ptr,
					UNIV_EXTERN_STORAGE_FIELD + flen);
osku's avatar
osku committed
564

565 566
				/* Notify purge that it eventually has to
				free the old externally stored field */
osku's avatar
osku committed
567

568
				trx->update_undo->del_marks = TRUE;
osku's avatar
osku committed
569

marko's avatar
marko committed
570
				*type_cmpl_ptr |= TRX_UNDO_UPD_EXTERN;
571
			} else {
572
				ptr += mach_write_compressed(ptr, flen);
573
			}
osku's avatar
osku committed
574

575 576
			if (flen != UNIV_SQL_NULL) {
				if (trx_undo_left(undo_page, ptr) < flen) {
osku's avatar
osku committed
577

578 579 580 581 582 583
					return(0);
				}

				ut_memcpy(ptr, field, flen);
				ptr += flen;
			}
osku's avatar
osku committed
584
		}
585
	}
osku's avatar
osku committed
586 587 588 589 590 591 592 593

	/*----------------------------------------*/
	/* 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
	index tree. Note that starting from 4.0.14 also externally stored
594 595 596 597 598 599
	fields can be ordering in some index. Starting from 5.2, we no longer
	store REC_MAX_INDEX_COL_LEN first bytes to the undo log record,
	but we can construct the column prefix fields in the index by
	fetching the first page of the BLOB that is pointed to by the
	clustered index. This works also in crash recovery, because all pages
	(including BLOBs) are recovered before anything is rolled back. */
osku's avatar
osku committed
600

601
	if (!update || !(cmpl_info & UPD_NODE_NO_ORD_CHANGE)) {
marko's avatar
marko committed
602
		byte*	old_ptr = ptr;
osku's avatar
osku committed
603

604
		trx->update_undo->del_marks = TRUE;
osku's avatar
osku committed
605

606
		if (trx_undo_left(undo_page, ptr) < 5) {
osku's avatar
osku committed
607

608 609
			return(0);
		}
osku's avatar
osku committed
610

611 612
		/* Reserve 2 bytes to write the number of bytes the stored
		fields take in this undo record */
osku's avatar
osku committed
613

614
		ptr += 2;
osku's avatar
osku committed
615

616 617
		for (col_no = 0; col_no < dict_table_get_n_cols(table);
		     col_no++) {
osku's avatar
osku committed
618

619 620
			const dict_col_t*	col
				= dict_table_get_nth_col(table, col_no);
osku's avatar
osku committed
621

622 623
			if (col->ord_part) {
				ulint	pos;
osku's avatar
osku committed
624

625
				/* Write field number to undo log */
626
				if (trx_undo_left(undo_page, ptr) < 5 + 5) {
osku's avatar
osku committed
627 628 629 630

					return(0);
				}

631 632 633
				pos = dict_index_get_nth_col_pos(index,
								 col_no);
				ptr += mach_write_compressed(ptr, pos);
634 635 636

				/* Save the old value of field */
				field = rec_get_nth_field(rec, offsets, pos,
637
							  &flen);
638

639 640 641 642 643 644 645 646 647 648 649
				if (rec_offs_nth_extern(offsets, pos)) {
					/* If a field has external
					storage, we add to flen the flag */

					ptr += mach_write_compressed(
						ptr, flen
						+ UNIV_EXTERN_STORAGE_FIELD);
				} else {
					ptr += mach_write_compressed(
						ptr, flen);
				}
650 651 652

				if (flen != UNIV_SQL_NULL) {
					if (trx_undo_left(undo_page, ptr)
653
					    < flen) {
654 655 656 657 658 659 660

						return(0);
					}

					ut_memcpy(ptr, field, flen);
					ptr += flen;
				}
osku's avatar
osku committed
661 662 663
			}
		}

664 665
		mach_write_to_2(old_ptr, ptr - old_ptr);
	}
osku's avatar
osku committed
666 667 668 669 670 671 672

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

		return(0);
	}
673

osku's avatar
osku committed
674 675 676 677 678
	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,
679
			ptr - undo_page);
osku's avatar
osku committed
680 681 682 683

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

	trx_undof_page_add_undo_rec_log(undo_page, first_free,
684
					ptr - undo_page, mtr);
685
	return(first_free);
osku's avatar
osku committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
}

/**************************************************************************
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 */
{
	/* Read the state of the info bits */
	*info_bits = mach_read_from_1(ptr);
	ptr += 1;

	/* Read the values of the system columns */

710
	*trx_id = mach_dulint_read_compressed(ptr);
711
	ptr += mach_dulint_get_compressed_size(*trx_id);
osku's avatar
osku committed
712

713
	*roll_ptr = mach_dulint_read_compressed(ptr);
714
	ptr += mach_dulint_get_compressed_size(*roll_ptr);
osku's avatar
osku committed
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729

	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 */
{
730
	*n = mach_read_compressed(ptr);
osku's avatar
osku committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
	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 */
{
747
	*field_no = mach_read_compressed(ptr);
osku's avatar
osku committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
	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(
/*===========================*/
				/* out: remaining part of the record,
				NULL if an error detected, which means that
				the record is corrupted */
	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 */
	dulint		trx_id,	/* in: transaction id from this undo record */
	dulint		roll_ptr,/* in: roll pointer from this undo record */
	ulint		info_bits,/* in: info bits from this undo record */
	trx_t*		trx,	/* in: transaction */
	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;
790

791
	ut_a(dict_index_is_clust(index));
osku's avatar
osku committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

	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);
807
	trx_write_trx_id(buf, trx_id);
osku's avatar
osku committed
808 809

	upd_field_set_field_no(upd_field,
810 811
			       dict_index_get_sys_col_pos(index, DATA_TRX_ID),
			       index, trx);
osku's avatar
osku committed
812 813 814 815
	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);
816
	trx_write_roll_ptr(buf, roll_ptr);
osku's avatar
osku committed
817

818 819 820
	upd_field_set_field_no(
		upd_field, dict_index_get_sys_col_pos(index, DATA_ROLL_PTR),
		index, trx);
osku's avatar
osku committed
821
	dfield_set_data(&(upd_field->new_val), buf, DATA_ROLL_PTR_LEN);
822

osku's avatar
osku committed
823 824 825 826 827 828 829 830
	/* Store then the updated ordinary columns to the update vector */

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

		ptr = trx_undo_update_rec_get_field_no(ptr, &field_no);

		if (field_no >= dict_index_get_n_fields(index)) {
			fprintf(stderr,
831 832 833
				"InnoDB: Error: trying to access"
				" update undo rec field %lu in ",
				(ulong) field_no);
osku's avatar
osku committed
834 835
			dict_index_name_print(stderr, trx, index);
			fprintf(stderr, "\n"
836 837 838 839
				"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 ",
osku's avatar
osku committed
840
				(ulong) dict_index_get_n_fields(index));
841
			ut_print_name(stderr, trx, TRUE, index->table_name);
osku's avatar
osku committed
842
			fprintf(stderr, "\n"
843
				"InnoDB: n_fields = %lu, i = %lu, ptr %p\n",
osku's avatar
osku committed
844 845 846 847 848 849 850 851 852 853 854 855 856
				(ulong) n_fields, (ulong) i, ptr);
			return(NULL);
		}

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

		upd_field = upd_get_nth_field(update, i);

		upd_field_set_field_no(upd_field, field_no, index, trx);

		if (len != UNIV_SQL_NULL && len >= UNIV_EXTERN_STORAGE_FIELD) {

			len -= UNIV_EXTERN_STORAGE_FIELD;
857 858 859 860
			dfield_set_data(&upd_field->new_val, field, len);
			dfield_set_ext(&upd_field->new_val);
		} else {
			dfield_set_data(&upd_field->new_val, field, len);
osku's avatar
osku committed
861 862 863 864 865 866 867
		}
	}

	*upd = update;

	return(ptr);
}
868

osku's avatar
osku committed
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
/***********************************************************************
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 */
887 888
	row_ext_t**	ext,	/* out, own: prefix cache for
				externally stored columns */
osku's avatar
osku committed
889 890 891
	mem_heap_t*	heap)	/* in: memory heap from which the memory
				needed is allocated */
{
892
	const byte*	end_ptr;
osku's avatar
osku committed
893
	ulint		row_len;
894 895
	ulint		n_ext_cols;
	ulint*		ext_cols;
896

897
	ut_ad(index && ptr && row && ext && heap);
898

osku's avatar
osku committed
899
	row_len = dict_table_get_n_cols(index->table);
900 901
	n_ext_cols = 0;
	ext_cols = mem_heap_alloc(heap, row_len * sizeof *ext_cols);
osku's avatar
osku committed
902 903 904 905 906

	*row = dtuple_create(heap, row_len);

	dict_table_copy_types(*row, index->table);

907
	end_ptr = ptr + mach_read_from_2(ptr);
osku's avatar
osku committed
908
	ptr += 2;
909

910 911 912 913 914 915
	while (ptr != end_ptr) {
		dfield_t*	dfield;
		byte*		field;
		ulint		field_no;
		ulint		col_no;
		ulint		len;
916

osku's avatar
osku committed
917 918 919
		ptr = trx_undo_update_rec_get_field_no(ptr, &field_no);

		col_no = dict_index_get_nth_col_no(index, field_no);
920

osku's avatar
osku committed
921 922
		ptr = trx_undo_rec_get_col_val(ptr, &field, &len);

923
		dfield = dtuple_get_nth_field(*row, col_no);
osku's avatar
osku committed
924 925

		dfield_set_data(dfield, field, len);
926

927 928
		if (len != UNIV_SQL_NULL
		    && len >= UNIV_EXTERN_STORAGE_FIELD) {
929 930
			dfield_set_len(dfield,
				       len - UNIV_EXTERN_STORAGE_FIELD);
931 932 933
			dfield_set_ext(dfield);
			ext_cols[n_ext_cols++] = col_no;
		}
osku's avatar
osku committed
934 935
	}

936 937 938 939 940 941 942 943
	if (n_ext_cols) {
		*ext = row_ext_create(n_ext_cols, ext_cols,
				      dict_table_zip_size(index->table),
				      heap);
	} else {
		*ext = NULL;
	}

osku's avatar
osku committed
944
	return(ptr);
945
}
osku's avatar
osku committed
946 947 948 949 950 951 952 953 954 955 956 957 958

/***************************************************************************
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;

	first_free = mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
959
				      + TRX_UNDO_PAGE_FREE);
osku's avatar
osku committed
960
	memset(undo_page + first_free, 0xff,
961
	       (UNIV_PAGE_SIZE - FIL_PAGE_DATA_END) - first_free);
osku's avatar
osku committed
962 963 964

	mlog_write_initial_log_record(undo_page, MLOG_UNDO_ERASE_END, mtr);
}
965

osku's avatar
osku committed
966 967 968 969 970 971 972 973 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
/***************************************************************
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 */
	byte*	end_ptr __attribute__((unused)), /* in: buffer end */
	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 */
1006
	const dtuple_t*	clust_entry,	/* in: in the case of an insert,
osku's avatar
osku committed
1007 1008
					index entry to insert into the
					clustered index, otherwise NULL */
1009
	const upd_t*	update,		/* in: in the case of an update,
osku's avatar
osku committed
1010 1011 1012
					the update vector, otherwise NULL */
	ulint		cmpl_info,	/* in: compiler info on secondary
					index updates */
marko's avatar
marko committed
1013
	const rec_t*	rec,		/* in: in case of an update or delete
osku's avatar
osku committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
					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;
	ulint		page_no;
	trx_rseg_t*	rseg;
	mtr_t		mtr;
1026
	ulint		err		= DB_SUCCESS;
osku's avatar
osku committed
1027 1028 1029
	mem_heap_t*	heap		= NULL;
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
	ulint*		offsets		= offsets_;
1030
	rec_offs_init(offsets_);
osku's avatar
osku committed
1031

1032
	ut_a(dict_index_is_clust(index));
osku's avatar
osku committed
1033 1034 1035 1036 1037 1038 1039

	if (flags & BTR_NO_UNDO_LOG_FLAG) {

		*roll_ptr = ut_dulint_zero;

		return(DB_SUCCESS);
	}
1040

osku's avatar
osku committed
1041 1042
	ut_ad(thr);
	ut_ad((op_type != TRX_UNDO_INSERT_OP)
1043
	      || (clust_entry && !update && !rec));
1044

osku's avatar
osku committed
1045 1046
	trx = thr_get_trx(thr);
	rseg = trx->rseg;
1047

osku's avatar
osku committed
1048 1049 1050 1051 1052 1053 1054 1055
	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) {

1056
			err = trx_undo_assign_undo(trx, TRX_UNDO_INSERT);
osku's avatar
osku committed
1057 1058 1059
		}

		undo = trx->insert_undo;
marko's avatar
marko committed
1060 1061

		if (UNIV_UNLIKELY(!undo)) {
1062
			/* Did not succeed */
marko's avatar
marko committed
1063 1064
			mutex_exit(&(trx->undo_mutex));

1065
			return(err);
marko's avatar
marko committed
1066
		}
osku's avatar
osku committed
1067 1068 1069 1070 1071
	} else {
		ut_ad(op_type == TRX_UNDO_MODIFY_OP);

		if (trx->update_undo == NULL) {

1072
			err = trx_undo_assign_undo(trx, TRX_UNDO_UPDATE);
osku's avatar
osku committed
1073 1074 1075 1076 1077

		}

		undo = trx->update_undo;

marko's avatar
marko committed
1078
		if (UNIV_UNLIKELY(!undo)) {
1079
			/* Did not succeed */
marko's avatar
marko committed
1080
			mutex_exit(&(trx->undo_mutex));
1081
			return(err);
marko's avatar
marko committed
1082
		}
osku's avatar
osku committed
1083

marko's avatar
marko committed
1084 1085
		offsets = rec_get_offsets(rec, index, offsets,
					  ULINT_UNDEFINED, &heap);
osku's avatar
osku committed
1086 1087 1088
	}

	page_no = undo->last_page_no;
1089

osku's avatar
osku committed
1090 1091 1092
	mtr_start(&mtr);

	for (;;) {
marko's avatar
marko committed
1093 1094
		buf_block_t*	undo_block;
		page_t*		undo_page;
marko's avatar
marko committed
1095
		ulint		offset;
marko's avatar
marko committed
1096

1097 1098
		undo_block = buf_page_get_gen(undo->space, undo->zip_size,
					      page_no, RW_X_LATCH,
1099 1100
					      undo->guess_block, BUF_GET,
					      __FILE__, __LINE__, &mtr);
osku's avatar
osku committed
1101
#ifdef UNIV_SYNC_DEBUG
1102
		buf_block_dbg_add_level(undo_block, SYNC_TRX_UNDO_PAGE);
osku's avatar
osku committed
1103
#endif /* UNIV_SYNC_DEBUG */
1104
		undo_page = buf_block_get_frame(undo_block);
osku's avatar
osku committed
1105 1106

		if (op_type == TRX_UNDO_INSERT_OP) {
1107 1108
			offset = trx_undo_page_report_insert(
				undo_page, trx, index, clust_entry, &mtr);
osku's avatar
osku committed
1109
		} else {
1110 1111 1112
			offset = trx_undo_page_report_modify(
				undo_page, trx, index, rec, offsets, update,
				cmpl_info, &mtr);
osku's avatar
osku committed
1113 1114
		}

marko's avatar
marko committed
1115
		if (UNIV_UNLIKELY(offset == 0)) {
osku's avatar
osku committed
1116 1117
			/* The record did not fit on the page. We erase the
			end segment of the undo log page and write a log
1118 1119 1120
			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 */
osku's avatar
osku committed
1121 1122

			trx_undo_erase_page_end(undo_page, &mtr);
marko's avatar
marko committed
1123 1124 1125
			mtr_commit(&mtr);
		} else {
			/* Success */
1126

marko's avatar
marko committed
1127
			mtr_commit(&mtr);
osku's avatar
osku committed
1128

marko's avatar
marko committed
1129 1130 1131 1132 1133
			undo->empty = FALSE;
			undo->top_page_no = page_no;
			undo->top_offset  = offset;
			undo->top_undo_no = trx->undo_no;
			undo->guess_block = undo_block;
osku's avatar
osku committed
1134

marko's avatar
marko committed
1135 1136 1137
			UT_DULINT_INC(trx->undo_no);

			mutex_exit(&trx->undo_mutex);
marko's avatar
marko committed
1138 1139 1140 1141 1142 1143 1144 1145

			*roll_ptr = trx_undo_build_roll_ptr(
				op_type == TRX_UNDO_INSERT_OP,
				rseg->id, page_no, offset);
			if (UNIV_LIKELY_NULL(heap)) {
				mem_heap_free(heap);
			}
			return(DB_SUCCESS);
osku's avatar
osku committed
1146 1147 1148
		}

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

osku's avatar
osku committed
1150 1151 1152 1153 1154 1155 1156 1157 1158
		/* 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));
1159

osku's avatar
osku committed
1160 1161 1162
		page_no = trx_undo_add_page(trx, undo, &mtr);

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

marko's avatar
marko committed
1164
		if (UNIV_UNLIKELY(page_no == FIL_NULL)) {
osku's avatar
osku committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
			/* Did not succeed: out of space */

			mutex_exit(&(trx->undo_mutex));
			mtr_commit(&mtr);
			if (UNIV_LIKELY_NULL(heap)) {
				mem_heap_free(heap);
			}
			return(DB_OUT_OF_FILE_SPACE);
		}
	}
}

/*============== 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 */
{
	trx_undo_rec_t*	undo_rec;
	ulint		rseg_id;
	ulint		page_no;
	ulint		offset;
	page_t*		undo_page;
	trx_rseg_t*	rseg;
	ibool		is_insert;
	mtr_t		mtr;
1198

osku's avatar
osku committed
1199
	trx_undo_decode_roll_ptr(roll_ptr, &is_insert, &rseg_id, &page_no,
1200
				 &offset);
osku's avatar
osku committed
1201 1202 1203
	rseg = trx_rseg_get_on_id(rseg_id);

	mtr_start(&mtr);
1204

1205 1206
	undo_page = trx_undo_page_get_s_latched(rseg->space, rseg->zip_size,
						page_no, &mtr);
1207

osku's avatar
osku committed
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
	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 */
{
#ifdef UNIV_SYNC_DEBUG
	ut_ad(rw_lock_own(&(purge_sys->latch), RW_LOCK_SHARED));
#endif /* UNIV_SYNC_DEBUG */

	if (!trx_purge_update_undo_must_exist(trx_id)) {

1240
		/* It may be that the necessary undo log has already been
osku's avatar
osku committed
1241 1242 1243 1244 1245 1246
		deleted */

		return(DB_MISSING_HISTORY);
	}

	*undo_rec = trx_undo_get_undo_rec_low(roll_ptr, heap);
1247

osku's avatar
osku committed
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	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,
				which means that it may have been removed,
				DB_ERROR if corrupted record */
	rec_t*		index_rec,/* in: clustered index record in the
				index tree */
	mtr_t*		index_mtr __attribute__((unused)),
1267
				/* in: mtr which contains the latch to
osku's avatar
osku committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
				index_rec page and purge_view */
	rec_t*		rec,	/* in: version of a clustered index record */
	dict_index_t*	index,	/* in: clustered index */
	ulint*		offsets,/* in: rec_get_offsets(rec, index) */
	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;
	dulint		old_roll_ptr;
	upd_t*		update;
	byte*		ptr;
	ulint		info_bits;
	ulint		cmpl_info;
	ibool		dummy_extern;
	byte*		buf;
	ulint		err;
#ifdef UNIV_SYNC_DEBUG
	ut_ad(rw_lock_own(&(purge_sys->latch), RW_LOCK_SHARED));
#endif /* UNIV_SYNC_DEBUG */
1297 1298 1299
	ut_ad(mtr_memo_contains_page(index_mtr, index_rec, MTR_MEMO_PAGE_S_FIX)
	      || mtr_memo_contains_page(index_mtr, index_rec,
					MTR_MEMO_PAGE_X_FIX));
osku's avatar
osku committed
1300 1301
	ut_ad(rec_offs_validate(rec, index, offsets));

1302
	if (!dict_index_is_clust(index)) {
osku's avatar
osku committed
1303 1304 1305 1306 1307 1308 1309
		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);
		rec_print(stderr, index_rec, index);
		fputs("\n"
1310
		      "InnoDB: record version ", stderr);
osku's avatar
osku committed
1311 1312
		rec_print_new(stderr, rec, offsets);
		putc('\n', stderr);
1313 1314
		return(DB_ERROR);
	}
osku's avatar
osku committed
1315 1316 1317

	roll_ptr = row_get_rec_roll_ptr(rec, index, offsets);
	old_roll_ptr = roll_ptr;
1318

osku's avatar
osku committed
1319 1320 1321 1322 1323 1324 1325 1326 1327
	*old_vers = NULL;

	if (trx_undo_roll_ptr_is_insert(roll_ptr)) {

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

		return(DB_SUCCESS);
	}

1328 1329
	rec_trx_id = row_get_rec_trx_id(rec, index, offsets);

osku's avatar
osku committed
1330 1331 1332 1333 1334 1335 1336 1337
	err = trx_undo_get_undo_rec(roll_ptr, rec_trx_id, &undo_rec, heap);

	if (err != DB_SUCCESS) {

		return(err);
	}

	ptr = trx_undo_rec_get_pars(undo_rec, &type, &cmpl_info,
1338
				    &dummy_extern, &undo_no, &table_id);
osku's avatar
osku committed
1339 1340

	ptr = trx_undo_update_rec_get_sys_cols(ptr, &trx_id, &roll_ptr,
1341
					       &info_bits);
osku's avatar
osku committed
1342 1343 1344
	ptr = trx_undo_rec_skip_row_ref(ptr, index);

	ptr = trx_undo_update_rec_get_update(ptr, index, type, trx_id,
1345 1346
					     roll_ptr, info_bits,
					     NULL, heap, &update);
osku's avatar
osku committed
1347 1348 1349 1350 1351

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

		fprintf(stderr,
1352 1353 1354 1355 1356 1357 1358
			"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",
osku's avatar
osku committed
1359 1360 1361 1362 1363 1364 1365 1366
			index->table_name, index->table_name);
	}

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

		fprintf(stderr,
1367 1368 1369 1370 1371
			"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: ",
osku's avatar
osku committed
1372 1373 1374 1375 1376 1377 1378 1379 1380
			index->table_name, index->name,
			(ulong) dict_index_get_n_unique(index),
			undo_rec, (ulong) type, (ulong) cmpl_info,
			(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));
		ut_print_buf(stderr, undo_rec, 150);
		fputs("\n"
1381
		      "InnoDB: index record ", stderr);
osku's avatar
osku committed
1382 1383
		rec_print(stderr, index_rec, index);
		fputs("\n"
1384
		      "InnoDB: record version ", stderr);
osku's avatar
osku committed
1385 1386
		rec_print_new(stderr, rec, offsets);
		fprintf(stderr, "\n"
1387 1388 1389 1390
			"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",
1391 1392 1393 1394 1395 1396 1397 1398 1399
			(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));

osku's avatar
osku committed
1400 1401 1402 1403 1404
		trx_purge_sys_print();
		return(DB_ERROR);
	}

	if (row_upd_changes_field_size_or_external(index, offsets, update)) {
1405
		ulint	n_ext;
osku's avatar
osku committed
1406 1407 1408 1409 1410

		/* 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
1411
		fields. Store the info: */
osku's avatar
osku committed
1412

1413 1414 1415
		entry = row_rec_to_index_entry(ROW_COPY_DATA, rec, index,
					       offsets, &n_ext, heap);
		n_ext += btr_push_update_extern_fields(entry, update);
osku's avatar
osku committed
1416 1417
		row_upd_index_replace_new_col_vals(entry, index, update, heap);

1418 1419
		buf = mem_heap_alloc(heap, rec_get_converted_size(index, entry,
								  n_ext));
osku's avatar
osku committed
1420

1421 1422
		*old_vers = rec_convert_dtuple_to_rec(buf, index,
						      entry, n_ext);
osku's avatar
osku committed
1423 1424 1425 1426
	} else {
		buf = mem_heap_alloc(heap, rec_offs_size(offsets));
		*old_vers = rec_copy(buf, rec, offsets);
		rec_offs_make_valid(*old_vers, index, offsets);
1427
		row_upd_rec_in_place(*old_vers, index, offsets, update, NULL);
osku's avatar
osku committed
1428 1429 1430 1431
	}

	return(DB_SUCCESS);
}