row0vers.c 18.7 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 26 27 28 29
/******************************************************
Row versions

(c) 1997 Innobase Oy

Created 2/6/1997 Heikki Tuuri
*******************************************************/

#include "row0vers.h"

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

#include "dict0dict.h"
#include "dict0boot.h"
#include "btr0btr.h"
#include "mach0data.h"
#include "trx0rseg.h"
#include "trx0trx.h"
#include "trx0roll.h"
#include "trx0undo.h"
#include "trx0purge.h"
#include "trx0rec.h"
#include "que0que.h"
#include "row0row.h"
#include "row0upd.h"
#include "rem0cmp.h"
#include "read0read.h"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
30
#include "lock0lock.h"
31 32 33 34 35 36 37 38 39 40 41 42 43

/*********************************************************************
Finds out if an active transaction has inserted or modified a secondary
index record. NOTE: the kernel mutex is temporarily released in this
function! */

trx_t*
row_vers_impl_x_locked_off_kernel(
/*==============================*/
				/* out: NULL if committed, else the active
				transaction; NOTE that the kernel mutex is
				temporarily released! */
	rec_t*		rec,	/* in: record in a secondary index */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
44 45
	dict_index_t*	index,	/* in: the secondary index */
	const ulint*	offsets)/* in: rec_get_offsets(rec, index) */
46 47 48
{
	dict_index_t*	clust_index;
	rec_t*		clust_rec;
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
49
	ulint*		clust_offsets;
50 51 52 53 54 55 56
	rec_t*		version;
	rec_t*		prev_version;
	dulint		trx_id;
	dulint		prev_trx_id;
	mem_heap_t*	heap;
	mem_heap_t*	heap2;
	dtuple_t*	row;
57 58
	dtuple_t*	entry	= NULL; /* assignment to eliminate compiler
					warning */
59
	trx_t*		trx;
60 61
	ulint		vers_del;
	ulint		rec_del;
62 63
	ulint		err;
	mtr_t		mtr;
64
	ulint		comp;
65

66
#ifdef UNIV_SYNC_DEBUG
67 68
	ut_ad(mutex_own(&kernel_mutex));
	ut_ad(!rw_lock_own(&(purge_sys->latch), RW_LOCK_SHARED));
69
#endif /* UNIV_SYNC_DEBUG */
70 71 72 73

	mutex_exit(&kernel_mutex);

	mtr_start(&mtr);
74

75 76 77 78
	/* Search for the clustered index record: this is a time-consuming
	operation: therefore we release the kernel mutex; also, the release
	is required by the latching order convention. The latch on the
	clustered index locks the top of the stack of versions. We also
79
	reserve purge_latch to lock the bottom of the version stack. */
80 81 82

	clust_rec = row_get_clust_rec(BTR_SEARCH_LEAF, rec, index,
							&clust_index, &mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
83
	if (!clust_rec) {
84 85 86 87 88 89 90 91 92 93 94
		/* In a rare case it is possible that no clust rec is found
		for a secondary index record: if in row0umod.c
		row_undo_mod_remove_clust_low() we have already removed the
		clust rec, while purge is still cleaning and removing
		secondary index records associated with earlier versions of
		the clustered index record. In that case there cannot be
		any implicit lock on the secondary index record, because
		an active transaction which has modified the secondary index
		record has also modified the clustered index record. And in
		a rollback we always undo the modifications to secondary index
		records before the clustered index record. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
95

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
96
		mutex_enter(&kernel_mutex);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
97 98
		mtr_commit(&mtr);

99
		return(NULL);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
100
	}
101

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
102
	heap = mem_heap_create(1024);
103 104
	clust_offsets = rec_get_offsets(clust_rec, clust_index, NULL,
					ULINT_UNDEFINED, &heap);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
105
	trx_id = row_get_rec_trx_id(clust_rec, clust_index, clust_offsets);
106 107 108 109

	mtr_s_lock(&(purge_sys->latch), &mtr);

	mutex_enter(&kernel_mutex);
110

111
	trx = NULL;
112 113 114
	if (!trx_is_active(trx_id)) {
		/* The transaction that modified or inserted clust_rec is no
		longer active: no implicit lock on rec */
115
		goto exit_func;
116 117
	}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
118 119
	if (!lock_check_trx_id_sanity(trx_id, clust_rec, clust_index,
					clust_offsets, TRUE)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
120
		/* Corruption noticed: try to avoid a crash by returning */
121
		goto exit_func;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
122 123
	}

124
	comp = page_rec_is_comp(rec);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
125
	ut_ad(index->table == clust_index->table);
126
	ut_ad(!!comp == dict_table_is_comp(index->table));
127
	ut_ad(!comp == !page_rec_is_comp(clust_rec));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
128

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
129 130 131 132
	/* We look up if some earlier version, which was modified by the trx_id
	transaction, of the clustered index record would require rec to be in
	a different state (delete marked or unmarked, or have different field
	values, or not existing). If there is such a version, then rec was
133 134 135 136 137
	modified by the trx_id transaction, and it has an implicit x-lock on
	rec. Note that if clust_rec itself would require rec to be in a
	different state, then the trx_id transaction has not yet had time to
	modify rec, and does not necessarily have an implicit x-lock on rec. */

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
138
	rec_del = rec_get_deleted_flag(rec, comp);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	trx = NULL;

	version = clust_rec;

	for (;;) {
		mutex_exit(&kernel_mutex);

		/* While we retrieve an earlier version of clust_rec, we
		release the kernel mutex, because it may take time to access
		the disk. After the release, we have to check if the trx_id
		transaction is still active. We keep the semaphore in mtr on
		the clust_rec page, so that no other transaction can update
		it and get an implicit x-lock on rec. */

		heap2 = heap;
		heap = mem_heap_create(1024);
		err = trx_undo_prev_version_build(clust_rec, &mtr, version,
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
156 157 158
					clust_index, clust_offsets, heap,
					&prev_version);
		mem_heap_free(heap2); /* free version and clust_offsets */
159 160

		if (prev_version) {
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
161
			clust_offsets = rec_get_offsets(prev_version,
162 163
					clust_index, NULL,
					ULINT_UNDEFINED, &heap);
164
			row = row_build(ROW_COPY_POINTERS, clust_index,
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
165
					prev_version, clust_offsets, heap);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
			entry = row_build_index_entry(row, index, heap);
		}

		mutex_enter(&kernel_mutex);

		if (!trx_is_active(trx_id)) {
			/* Transaction no longer active: no implicit x-lock */

			break;
		}

		/* If the transaction is still active, the previous version
		of clust_rec must be accessible if not a fresh insert; we
		may assert the following: */

		ut_ad(err == DB_SUCCESS);
182

183 184 185 186 187 188 189 190 191 192 193
		if (prev_version == NULL) {
			/* It was a freshly inserted version: there is an
			implicit x-lock on rec */

			trx = trx_get_on_id(trx_id);

			break;
		}

		/* If we get here, we know that the trx_id transaction is
		still active and it has modified prev_version. Let us check
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
194 195
		if prev_version would require rec to be in a different
		state. */
196

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
197
		vers_del = rec_get_deleted_flag(prev_version, comp);
198

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
199 200
		/* We check if entry and rec are identified in the alphabetical
		ordering */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
201
		if (0 == cmp_dtuple_rec(entry, rec, offsets)) {
202 203 204 205 206 207 208 209 210
			/* The delete marks of rec and prev_version should be
			equal for rec to be in the state required by
			prev_version */

			if (rec_del != vers_del) {
				trx = trx_get_on_id(trx_id);

				break;
			}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
211 212 213 214 215 216 217 218

			/* It is possible that the row was updated so that the
			secondary index record remained the same in
			alphabetical ordering, but the field values changed
			still. For example, 'abc' -> 'ABC'. Check also that. */

			dtuple_set_types_binary(entry,
						dtuple_get_n_fields(entry));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
219
			if (0 != cmp_dtuple_rec(entry, rec, offsets)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
220 221 222 223 224

				trx = trx_get_on_id(trx_id);

				break;
			}
225 226 227 228 229 230 231 232 233
		} else if (!rec_del) {
			/* The delete mark should be set in rec for it to be
			in the state required by prev_version */

			trx = trx_get_on_id(trx_id);

			break;
		}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
234 235
		prev_trx_id = row_get_rec_trx_id(prev_version, clust_index,
								clust_offsets);
236 237 238 239 240 241 242 243 244 245 246

		if (0 != ut_dulint_cmp(trx_id, prev_trx_id)) {
			/* The versions modified by the trx_id transaction end
			to prev_version: no implicit x-lock */

			break;
		}

		version = prev_version;
	}/* for (;;) */

247
exit_func:
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	mtr_commit(&mtr);
	mem_heap_free(heap);

	return(trx);
}

/*********************************************************************
Finds out if we must preserve a delete marked earlier version of a clustered
index record, because it is >= the purge view. */

ibool
row_vers_must_preserve_del_marked(
/*==============================*/
			/* out: TRUE if earlier version should be preserved */
	dulint	trx_id,	/* in: transaction id in the version */
	mtr_t*	mtr)	/* in: mtr holding the latch on the clustered index
			record; it will also hold the latch on purge_view */
{
266
#ifdef UNIV_SYNC_DEBUG
267
	ut_ad(!rw_lock_own(&(purge_sys->latch), RW_LOCK_SHARED));
268
#endif /* UNIV_SYNC_DEBUG */
269 270 271 272 273 274 275

	mtr_s_lock(&(purge_sys->latch), mtr);

	if (trx_purge_update_undo_must_exist(trx_id)) {

		/* A purge operation is not yet allowed to remove this
		delete marked record */
276

277 278 279 280 281 282 283 284 285 286
		return(TRUE);
	}

	return(FALSE);
}

/*********************************************************************
Finds out if a version of the record, where the version >= the current
purge view, should have ientry as its secondary index entry. We check
if there is any not delete marked version of the record where the trx
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
287 288
id >= purge view, and the secondary index entry and ientry are identified in
the alphabetical ordering; exactly in this case we return TRUE. */
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

ibool
row_vers_old_has_index_entry(
/*=========================*/
				/* out: TRUE if earlier version should have */
	ibool		also_curr,/* in: TRUE if also rec is included in the
				versions to search; otherwise only versions
				prior to it are searched */
	rec_t*		rec,	/* in: record in the clustered index; the
				caller must have a latch on the page */
	mtr_t*		mtr,	/* in: mtr holding the latch on rec; it will
				also hold the latch on purge_view */
	dict_index_t*	index,	/* in: the secondary index */
	dtuple_t*	ientry)	/* in: the secondary index entry */
{
	rec_t*		version;
	rec_t*		prev_version;
	dict_index_t*	clust_index;
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
307
	ulint*		clust_offsets;
308 309 310 311 312
	mem_heap_t*	heap;
	mem_heap_t*	heap2;
	dtuple_t*	row;
	dtuple_t*	entry;
	ulint		err;
313
	ulint		comp;
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
314

315
	ut_ad(mtr_memo_contains(mtr, buf_block_align(rec), MTR_MEMO_PAGE_X_FIX)
316
		|| mtr_memo_contains(mtr, buf_block_align(rec),
317
						MTR_MEMO_PAGE_S_FIX));
318
#ifdef UNIV_SYNC_DEBUG
319
	ut_ad(!rw_lock_own(&(purge_sys->latch), RW_LOCK_SHARED));
320
#endif /* UNIV_SYNC_DEBUG */
321 322 323 324
	mtr_s_lock(&(purge_sys->latch), mtr);

	clust_index = dict_table_get_first_index(index->table);

325
	comp = page_rec_is_comp(rec);
326
	ut_ad(!dict_table_is_comp(index->table) == !comp);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
327
	heap = mem_heap_create(1024);
328 329
	clust_offsets = rec_get_offsets(rec, clust_index, NULL,
					ULINT_UNDEFINED, &heap);
330

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
331 332 333
	if (also_curr && !rec_get_deleted_flag(rec, comp)) {
		row = row_build(ROW_COPY_POINTERS, clust_index,
						rec, clust_offsets, heap);
334 335
		entry = row_build_index_entry(row, index, heap);

336
		/* NOTE that we cannot do the comparison as binary
337 338 339 340 341 342
		fields because the row is maybe being modified so that
		the clustered index record has already been updated
		to a different binary value in a char field, but the
		collation identifies the old and new value anyway! */

		if (dtuple_datas_are_ordering_equal(ientry, entry)) {
343 344 345 346 347 348 349 350 351 352 353 354 355

			mem_heap_free(heap);

			return(TRUE);
		}
	}

	version = rec;

	for (;;) {
		heap2 = heap;
		heap = mem_heap_create(1024);
		err = trx_undo_prev_version_build(rec, mtr, version,
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
356 357 358
					clust_index, clust_offsets, heap,
					&prev_version);
		mem_heap_free(heap2); /* free version and clust_offsets */
359

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
360
		if (err != DB_SUCCESS || !prev_version) {
361 362 363 364 365 366 367
			/* Versions end here */

			mem_heap_free(heap);

			return(FALSE);
		}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
368
		clust_offsets = rec_get_offsets(prev_version, clust_index,
369
						NULL, ULINT_UNDEFINED, &heap);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
370 371

		if (!rec_get_deleted_flag(prev_version, comp)) {
372
			row = row_build(ROW_COPY_POINTERS, clust_index,
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
373
					prev_version, clust_offsets, heap);
374 375
			entry = row_build_index_entry(row, index, heap);

376
			/* NOTE that we cannot do the comparison as binary
377 378 379 380 381 382
			fields because maybe the secondary index record has
			already been updated to a different binary value in
			a char field, but the collation identifies the old
			and new value anyway! */

			if (dtuple_datas_are_ordering_equal(ientry, entry)) {
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

				mem_heap_free(heap);

				return(TRUE);
			}
		}

		version = prev_version;
	}
}

/*********************************************************************
Constructs the version of a clustered index record which a consistent
read should see. We assume that the trx id stored in rec is such that
the consistent read should not see rec in its present version. */

ulint
row_vers_build_for_consistent_read(
/*===============================*/
				/* out: DB_SUCCESS or DB_MISSING_HISTORY */
	rec_t*		rec,	/* in: record in a clustered index; the
				caller must have a latch on the page; this
				latch locks the top of the stack of versions
				of this records */
	mtr_t*		mtr,	/* in: mtr holding the latch on rec */
	dict_index_t*	index,	/* in: the clustered index */
409 410
	ulint**		offsets,/* in/out: offsets returned by
				rec_get_offsets(rec, index) */
411
	read_view_t*	view,	/* in: the consistent read view */
412 413
	mem_heap_t**	offset_heap,/* in/out: memory heap from which
				the offsets are allocated */
414 415 416 417 418 419 420 421 422 423
	mem_heap_t*	in_heap,/* in: memory heap from which the memory for
				old_vers is allocated; memory for possible
				intermediate versions is allocated and freed
				locally within the function */
	rec_t**		old_vers)/* out, own: old version, or NULL if the
				record does not exist in the view, that is,
				it was freshly inserted afterwards */
{
	rec_t*		version;
	rec_t*		prev_version;
424
	dulint		trx_id;
425
	mem_heap_t*	heap		= NULL;
426 427 428 429 430
	byte*		buf;
	ulint		err;

	ut_ad(index->type & DICT_CLUSTERED);
	ut_ad(mtr_memo_contains(mtr, buf_block_align(rec), MTR_MEMO_PAGE_X_FIX)
431
		|| mtr_memo_contains(mtr, buf_block_align(rec),
432
						MTR_MEMO_PAGE_S_FIX));
433
#ifdef UNIV_SYNC_DEBUG
434
	ut_ad(!rw_lock_own(&(purge_sys->latch), RW_LOCK_SHARED));
435
#endif /* UNIV_SYNC_DEBUG */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
436

437
	ut_ad(rec_offs_validate(rec, index, *offsets));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
438

439 440 441
	trx_id = row_get_rec_trx_id(rec, index, *offsets);

	ut_ad(!read_view_sees_trx_id(view, trx_id));
442 443 444 445 446

	rw_lock_s_lock(&(purge_sys->latch));
	version = rec;

	for (;;) {
447
		mem_heap_t*	heap2	= heap;
448 449 450
		trx_undo_rec_t* undo_rec;
		dulint		roll_ptr;
		dulint		undo_no;
451 452
		heap = mem_heap_create(1024);

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
		/* If we have high-granularity consistent read view and
		creating transaction of the view is the same as trx_id in
		the record we see this record only in the case when
		undo_no of the record is < undo_no in the view. */

		if (view->type == VIEW_HIGH_GRANULARITY 
		&& ut_dulint_cmp(view->creator_trx_id, trx_id) == 0) {

			roll_ptr = row_get_rec_roll_ptr(version, index, 
								*offsets);
			undo_rec = trx_undo_get_undo_rec_low(roll_ptr, heap);
			undo_no = trx_undo_rec_get_undo_no(undo_rec);
			mem_heap_empty(heap);

			if (ut_dulint_cmp(view->undo_no, undo_no) > 0) {
				/* The view already sees this version: we can 
				copy it to in_heap and return */

				buf = mem_heap_alloc(in_heap, 
						rec_offs_size(*offsets));
				*old_vers = rec_copy(buf, version, *offsets);
				rec_offs_make_valid(*old_vers, index, *offsets);
				err = DB_SUCCESS;

				break;
			}
		}

481
		err = trx_undo_prev_version_build(rec, mtr, version, index,
482 483 484 485
						*offsets, heap, &prev_version);
		if (heap2) {
			mem_heap_free(heap2); /* free version */
		}
486 487 488 489 490 491 492 493 494 495 496 497 498

		if (err != DB_SUCCESS) {
			break;
		}

		if (prev_version == NULL) {
			/* It was a freshly inserted version */
			*old_vers = NULL;
			err = DB_SUCCESS;

			break;
		}

499 500
		*offsets = rec_get_offsets(prev_version, index, *offsets,
					ULINT_UNDEFINED, offset_heap);
501

502 503 504
		trx_id = row_get_rec_trx_id(prev_version, index, *offsets);

		if (read_view_sees_trx_id(view, trx_id)) {
505 506 507 508

			/* The view already sees this version: we can copy
			it to in_heap and return */

509 510 511
			buf = mem_heap_alloc(in_heap, rec_offs_size(*offsets));
			*old_vers = rec_copy(buf, prev_version, *offsets);
			rec_offs_make_valid(*old_vers, index, *offsets);
512 513 514 515 516 517 518 519 520 521 522 523 524
			err = DB_SUCCESS;

			break;
		}

		version = prev_version;
	}/* for (;;) */

	mem_heap_free(heap);
	rw_lock_s_unlock(&(purge_sys->latch));

	return(err);
}
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

/*********************************************************************
Constructs the last committed version of a clustered index record,
which should be seen by a semi-consistent read. */

ulint
row_vers_build_for_semi_consistent_read(
/*====================================*/
				/* out: DB_SUCCESS or DB_MISSING_HISTORY */
	rec_t*		rec,	/* in: record in a clustered index; the
				caller must have a latch on the page; this
				latch locks the top of the stack of versions
				of this records */
	mtr_t*		mtr,	/* in: mtr holding the latch on rec */
	dict_index_t*	index,	/* in: the clustered index */
	ulint**		offsets,/* in/out: offsets returned by
				rec_get_offsets(rec, index) */
	mem_heap_t**	offset_heap,/* in/out: memory heap from which
				the offsets are allocated */
	mem_heap_t*	in_heap,/* in: memory heap from which the memory for
				old_vers is allocated; memory for possible
				intermediate versions is allocated and freed
				locally within the function */
	rec_t**		old_vers)/* out, own: rec, old version, or NULL if the
				record does not exist in the view, that is,
				it was freshly inserted afterwards */
{
	rec_t*		version;
	mem_heap_t*	heap		= NULL;
	byte*		buf;
	ulint		err;
	dulint		rec_trx_id;

	ut_ad(index->type & DICT_CLUSTERED);
	ut_ad(mtr_memo_contains(mtr, buf_block_align(rec), MTR_MEMO_PAGE_X_FIX)
		|| mtr_memo_contains(mtr, buf_block_align(rec),
						MTR_MEMO_PAGE_S_FIX));
#ifdef UNIV_SYNC_DEBUG
	ut_ad(!rw_lock_own(&(purge_sys->latch), RW_LOCK_SHARED));
#endif /* UNIV_SYNC_DEBUG */

	ut_ad(rec_offs_validate(rec, index, *offsets));

	rw_lock_s_lock(&(purge_sys->latch));
	/* The S-latch on purge_sys prevents the purge view from
	changing.  Thus, if we have an uncommitted transaction at
	this point, then purge cannot remove its undo log even if
	the transaction could commit now. */

	version = rec;

	for (;;) {
		trx_t*		version_trx;
		mem_heap_t*	heap2;
		rec_t*		prev_version;
		dulint		version_trx_id;

		version_trx_id = row_get_rec_trx_id(
					version, index, *offsets);
		if (rec == version) {
			rec_trx_id = version_trx_id;
		}

		mutex_enter(&kernel_mutex);
		version_trx = trx_get_on_id(version_trx_id);
		mutex_exit(&kernel_mutex);

		if (!version_trx
593 594
			|| version_trx->conc_state == TRX_NOT_STARTED
			|| version_trx->conc_state == TRX_COMMITTED_IN_MEMORY) {
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

			/* We found a version that belongs to a
			committed transaction: return it. */

			if (rec == version) {
				*old_vers = rec;
				err = DB_SUCCESS;
				break;
			}

			/* We assume that a rolled-back transaction stays in
			TRX_ACTIVE state until all the changes have been
			rolled back and the transaction is removed from
			the global list of transactions. */

			if (!ut_dulint_cmp(rec_trx_id, version_trx_id)) {
				/* The transaction was committed while
				we searched for earlier versions.
				Return the current version as a
				semi-consistent read. */

				version = rec;
				*offsets = rec_get_offsets(version,
					index, *offsets,
					ULINT_UNDEFINED, offset_heap);
			}

			buf = mem_heap_alloc(in_heap, rec_offs_size(*offsets));
			*old_vers = rec_copy(buf, version, *offsets);
			rec_offs_make_valid(*old_vers, index, *offsets);
			err = DB_SUCCESS;

			break;
		}

		heap2 = heap;
		heap = mem_heap_create(1024);

		err = trx_undo_prev_version_build(rec, mtr, version, index,
						*offsets, heap, &prev_version);
		if (heap2) {
			mem_heap_free(heap2); /* free version */
		}

		if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
			break;
		}

		if (prev_version == NULL) {
			/* It was a freshly inserted version */
			*old_vers = NULL;
			err = DB_SUCCESS;

			break;
		}

		version = prev_version;
		*offsets = rec_get_offsets(version, index, *offsets,
					ULINT_UNDEFINED, offset_heap);
	}/* for (;;) */

	if (heap) {
		mem_heap_free(heap);
	}
	rw_lock_s_unlock(&(purge_sys->latch));

	return(err);
}