row0upd.c 50.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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/******************************************************
Update of a row

(c) 1996 Innobase Oy

Created 12/27/1996 Heikki Tuuri
*******************************************************/

#include "row0upd.h"

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

#include "dict0dict.h"
#include "dict0boot.h"
#include "dict0crea.h"
#include "mach0data.h"
#include "trx0undo.h"
#include "btr0btr.h"
#include "btr0cur.h"
#include "que0que.h"
#include "row0ins.h"
#include "row0sel.h"
#include "row0row.h"
#include "rem0cmp.h"
#include "lock0lock.h"
#include "log0log.h"
#include "pars0sym.h"
#include "eval0eval.h"


/* What kind of latch and lock can we assume when the control comes to
   -------------------------------------------------------------------
an update node?
--------------
Efficiency of massive updates would require keeping an x-latch on a
clustered index page through many updates, and not setting an explicit
x-lock on clustered index records, as they anyway will get an implicit
x-lock when they are updated. A problem is that the read nodes in the
graph should know that they must keep the latch when passing the control
up to the update node, and not set any record lock on the record which
will be updated. Another problem occurs if the execution is stopped,
as the kernel switches to another query thread, or the transaction must
wait for a lock. Then we should be able to release the latch and, maybe,
acquire an explicit x-lock on the record.
	Because this seems too complicated, we conclude that the less
efficient solution of releasing all the latches when the control is
transferred to another node, and acquiring explicit x-locks, is better. */

/* How is a delete performed? If there is a delete without an
explicit cursor, i.e., a searched delete, there are at least
two different situations:
the implicit select cursor may run on (1) the clustered index or
on (2) a secondary index. The delete is performed by setting
the delete bit in the record and substituting the id of the
deleting transaction for the original trx id, and substituting a
new roll ptr for previous roll ptr. The old trx id and roll ptr
are saved in the undo log record. Thus, no physical changes occur
in the index tree structure at the time of the delete. Only
when the undo log is purged, the index records will be physically
deleted from the index trees.

The query graph executing a searched delete would consist of
a delete node which has as a subtree a select subgraph.
The select subgraph should return a (persistent) cursor
in the clustered index, placed on page which is x-latched.
The delete node should look for all secondary index records for
this clustered index entry and mark them as deleted. When is
the x-latch freed? The most efficient way for performing a
searched delete is obviously to keep the x-latch for several
steps of query graph execution. */

unknown's avatar
unknown committed
74
/***************************************************************
unknown's avatar
unknown committed
75 76 77
Checks if an update vector changes some of the first ordering fields of an
index record. This is only used in foreign key checks and we can assume
that index does not contain column prefixes. */
unknown's avatar
unknown committed
78 79
static
ibool
unknown's avatar
unknown committed
80 81
row_upd_changes_first_fields_binary(
/*================================*/
unknown's avatar
unknown committed
82 83 84 85 86 87 88
				/* out: TRUE if changes */
	dtuple_t*	entry,	/* in: old value of index entry */
	dict_index_t*	index,	/* in: index of entry */
	upd_t*		update,	/* in: update vector for the row */
	ulint		n);	/* in: how many first fields to check */


89 90
/*************************************************************************
Checks if index currently is mentioned as a referenced index in a foreign
unknown's avatar
unknown committed
91
key constraint. */
92 93 94 95 96
static
ibool
row_upd_index_is_referenced(
/*========================*/
				/* out: TRUE if referenced; NOTE that since
unknown's avatar
unknown committed
97
				we do not hold dict_operation_lock
98 99 100 101
				when leaving the function, it may be that
				the referencing table has been dropped when
				we leave this function: this function is only
				for heuristic use! */
unknown's avatar
unknown committed
102 103
	dict_index_t*	index,	/* in: index */
	trx_t*		trx)	/* in: transaction */
104
{
unknown's avatar
unknown committed
105
	dict_table_t*	table		= index->table;
106
	dict_foreign_t*	foreign;
unknown's avatar
unknown committed
107
	ibool		froze_data_dict	= FALSE;
108 109 110 111 112 113

	if (!UT_LIST_GET_FIRST(table->referenced_list)) {

		return(FALSE);
	}

unknown's avatar
unknown committed
114 115 116
	if (trx->dict_operation_lock_mode == 0) {
		row_mysql_freeze_data_dictionary(trx);
		froze_data_dict = TRUE;
117 118 119 120 121 122 123
	}

	foreign = UT_LIST_GET_FIRST(table->referenced_list);

	while (foreign) {
		if (foreign->referenced_index == index) {

unknown's avatar
unknown committed
124 125
			if (froze_data_dict) {
				row_mysql_unfreeze_data_dictionary(trx);
126 127 128 129 130 131 132 133
			}

			return(TRUE);
		}

		foreign = UT_LIST_GET_NEXT(referenced_list, foreign);
	}
	
unknown's avatar
unknown committed
134 135
	if (froze_data_dict) {
		row_mysql_unfreeze_data_dictionary(trx);
136 137 138 139 140 141 142
	}

	return(FALSE);
}

/*************************************************************************
Checks if possible foreign key constraints hold after a delete of the record
unknown's avatar
unknown committed
143
under pcur. NOTE that this function will temporarily commit mtr and lose the
144 145 146 147 148
pcur position! */
static
ulint
row_upd_check_references_constraints(
/*=================================*/
unknown's avatar
unknown committed
149
				/* out: DB_SUCCESS or an error code */
unknown's avatar
unknown committed
150
	upd_node_t*	node,	/* in: row update node */
151 152 153 154 155 156 157 158 159 160
	btr_pcur_t*	pcur,	/* in: cursor positioned on a record; NOTE: the
				cursor position is lost in this function! */
	dict_table_t*	table,	/* in: table in question */
	dict_index_t*	index,	/* in: index of the cursor */
	que_thr_t*	thr,	/* in: query thread */
	mtr_t*		mtr)	/* in: mtr */
{
	dict_foreign_t*	foreign;
	mem_heap_t*	heap;
	dtuple_t*	entry;
unknown's avatar
unknown committed
161
	trx_t*		trx;
162 163
	rec_t*		rec;
	ulint		err;
unknown's avatar
unknown committed
164 165 166 167 168 169 170 171
	ibool		got_s_lock	= FALSE;

	if (UT_LIST_GET_FIRST(table->referenced_list) == NULL) {

		return(DB_SUCCESS);
	}

	trx = thr_get_trx(thr);
172 173 174 175 176 177 178 179 180 181 182

	rec = btr_pcur_get_rec(pcur);

	heap = mem_heap_create(500);

	entry = row_rec_to_index_entry(ROW_COPY_DATA, index, rec, heap);

	mtr_commit(mtr);	

	mtr_start(mtr);	
	
unknown's avatar
unknown committed
183
	if (trx->dict_operation_lock_mode == 0) {
unknown's avatar
unknown committed
184
		got_s_lock = TRUE;
185

unknown's avatar
unknown committed
186
		row_mysql_freeze_data_dictionary(trx);
unknown's avatar
unknown committed
187 188
	}
		
189 190 191
	foreign = UT_LIST_GET_FIRST(table->referenced_list);

	while (foreign) {
unknown's avatar
unknown committed
192 193 194 195 196 197 198
		/* Note that we may have an update which updates the index
		record, but does NOT update the first fields which are
		referenced in a foreign key constraint. Then the update does
		NOT break the constraint. */

		if (foreign->referenced_index == index
		    && (node->is_delete
unknown's avatar
unknown committed
199
		       || row_upd_changes_first_fields_binary(entry, index,
unknown's avatar
unknown committed
200 201
			    		node->update, foreign->n_fields))) {
			    				
unknown's avatar
unknown committed
202 203 204 205
			if (foreign->foreign_table == NULL) {
				dict_table_get(foreign->foreign_table_name,
									trx);
			}
206

unknown's avatar
unknown committed
207 208 209 210 211 212 213 214 215 216
			if (foreign->foreign_table) {
				mutex_enter(&(dict_sys->mutex));

				(foreign->foreign_table
				->n_foreign_key_checks_running)++;

				mutex_exit(&(dict_sys->mutex));
			}

			/* NOTE that if the thread ends up waiting for a lock
unknown's avatar
unknown committed
217 218 219
			we will release dict_operation_lock temporarily!
			But the counter on the table protects 'foreign' from
			being dropped while the check is running. */
unknown's avatar
unknown committed
220
			
221
			err = row_ins_check_foreign_constraint(FALSE, foreign,
unknown's avatar
unknown committed
222
							table, entry, thr);
unknown's avatar
unknown committed
223 224 225 226 227 228 229 230 231 232 233 234 235

			if (foreign->foreign_table) {
				mutex_enter(&(dict_sys->mutex));

				ut_a(foreign->foreign_table
				->n_foreign_key_checks_running > 0);

				(foreign->foreign_table
				->n_foreign_key_checks_running)--;

				mutex_exit(&(dict_sys->mutex));
			}

236
			if (err != DB_SUCCESS) {
unknown's avatar
unknown committed
237
				if (got_s_lock) {
unknown's avatar
unknown committed
238 239
					row_mysql_unfreeze_data_dictionary(
									trx);
unknown's avatar
unknown committed
240 241
				}

242 243 244 245 246 247 248 249 250
				mem_heap_free(heap);

				return(err);
			}
		}

		foreign = UT_LIST_GET_NEXT(referenced_list, foreign);
	}

unknown's avatar
unknown committed
251
	if (got_s_lock) {
unknown's avatar
unknown committed
252
		row_mysql_unfreeze_data_dictionary(trx);
unknown's avatar
unknown committed
253 254
	}

255 256 257 258 259
	mem_heap_free(heap);
	
	return(DB_SUCCESS);
}

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/*************************************************************************
Creates an update node for a query graph. */

upd_node_t*
upd_node_create(
/*============*/
				/* out, own: update node */
	mem_heap_t*	heap)	/* in: mem heap where created */
{
	upd_node_t*	node;

	node = mem_heap_alloc(heap, sizeof(upd_node_t));
	node->common.type = QUE_NODE_UPDATE;

	node->state = UPD_NODE_UPDATE_CLUSTERED;
	node->select_will_do_update = FALSE;
	node->in_mysql_interface = FALSE;

	node->row = NULL;
279
	node->ext_vec = NULL;
280
	node->index = NULL;
281 282
	node->update = NULL;
	
unknown's avatar
unknown committed
283
	node->foreign = NULL;
unknown's avatar
unknown committed
284 285 286
	node->cascade_heap = NULL;
	node->cascade_node = NULL;
	
287 288 289
	node->select = NULL;
	
	node->heap = mem_heap_create(128);
290
	node->magic_n = UPD_NODE_MAGIC_N;	
291 292 293 294 295 296 297 298 299 300 301 302 303

	node->cmpl_info = 0;
	
	return(node);
}

/*************************************************************************
Updates the trx id and roll ptr field in a clustered index record in database
recovery. */

void
row_upd_rec_sys_fields_in_recovery(
/*===============================*/
unknown's avatar
unknown committed
304 305 306 307 308
	rec_t*		rec,	/* in: record */
	const ulint*	offsets,/* in: array returned by rec_get_offsets() */
	ulint		pos,	/* in: TRX_ID position in rec */
	dulint		trx_id,	/* in: transaction id */
	dulint		roll_ptr)/* in: roll ptr of the undo log record */
309 310 311 312
{
	byte*	field;
	ulint	len;

unknown's avatar
unknown committed
313
	field = rec_get_nth_field(rec, offsets, pos, &len);
314 315 316
	ut_ad(len == DATA_TRX_ID_LEN);
	trx_write_trx_id(field, trx_id);

unknown's avatar
unknown committed
317
	field = rec_get_nth_field(rec, offsets, pos + 1, &len);
318 319 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 355
	ut_ad(len == DATA_ROLL_PTR_LEN);
	trx_write_roll_ptr(field, roll_ptr);
}

/*************************************************************************
Sets the trx id or roll ptr field of a clustered index entry. */

void
row_upd_index_entry_sys_field(
/*==========================*/
	dtuple_t*	entry,	/* in: index entry, where the memory buffers
				for sys fields are already allocated:
				the function just copies the new values to
				them */
	dict_index_t*	index,	/* in: clustered index */
	ulint		type,	/* in: DATA_TRX_ID or DATA_ROLL_PTR */
	dulint		val)	/* in: value to write */
{
	dfield_t*	dfield;
	byte*		field;
	ulint		pos;

	ut_ad(index->type & DICT_CLUSTERED);

	pos = dict_index_get_sys_col_pos(index, type);

	dfield = dtuple_get_nth_field(entry, pos);
	field = dfield_get_data(dfield);

	if (type == DATA_TRX_ID) {
		trx_write_trx_id(field, val);
	} else {
		ut_ad(type == DATA_ROLL_PTR);
		trx_write_roll_ptr(field, val);
	}
}

/***************************************************************
unknown's avatar
unknown committed
356 357
Returns TRUE if row update changes size of some field in index or if some
field to be updated is stored externally in rec or update. */
358 359

ibool
unknown's avatar
unknown committed
360 361
row_upd_changes_field_size_or_external(
/*===================================*/
362
				/* out: TRUE if the update changes the size of
unknown's avatar
unknown committed
363 364
				some field in index or the field is external
				in rec or update */
unknown's avatar
unknown committed
365
	dict_index_t*	index,	/* in: index */
unknown's avatar
unknown committed
366
	const ulint*	offsets,/* in: rec_get_offsets(rec, index) */
367 368 369 370 371 372 373 374 375
	upd_t*		update)	/* in: update vector */
{
	upd_field_t*	upd_field;
	dfield_t*	new_val;
	ulint		old_len;
	ulint		new_len;
	ulint		n_fields;
	ulint		i;

unknown's avatar
unknown committed
376
	ut_ad(rec_offs_validate(NULL, index, offsets));
377 378 379 380 381 382 383 384
	n_fields = upd_get_n_fields(update);

	for (i = 0; i < n_fields; i++) {
		upd_field = upd_get_nth_field(update, i);

		new_val = &(upd_field->new_val);
		new_len = new_val->len;

unknown's avatar
unknown committed
385
		if (new_len == UNIV_SQL_NULL && !rec_offs_comp(offsets)) {
unknown's avatar
unknown committed
386 387 388 389 390
			/* A bug fixed on Dec 31st, 2004: we looked at the
			SQL NULL size from the wrong field! We may backport
			this fix also to 4.0. The merge to 5.0 will be made
			manually immediately after we commit this to 4.1. */

391
			new_len = dtype_get_sql_null_size(
unknown's avatar
unknown committed
392 393
					dict_index_get_nth_type(index,
						upd_field->field_no));
394 395
		}

unknown's avatar
unknown committed
396 397
		old_len = rec_offs_nth_size(offsets, upd_field->field_no);

398 399 400 401
		if (old_len != new_len) {

			return(TRUE);
		}
402
		
unknown's avatar
unknown committed
403
		if (rec_offs_nth_extern(offsets, upd_field->field_no)) {
404 405 406 407 408 409 410 411

			return(TRUE);
		}

		if (upd_field->extern_storage) {

			return(TRUE);
		}
412 413 414 415 416 417 418 419 420 421 422 423 424
	}

	return(FALSE);
}

/***************************************************************
Replaces the new column values stored in the update vector to the record
given. No field size changes are allowed. This function is used only for
a clustered index */

void
row_upd_rec_in_place(
/*=================*/
unknown's avatar
unknown committed
425 426 427
	rec_t*		rec,	/* in/out: record where replaced */
	const ulint*	offsets,/* in: array returned by rec_get_offsets() */
	upd_t*		update)	/* in: update vector */
428 429 430 431 432 433
{
	upd_field_t*	upd_field;
	dfield_t*	new_val;
	ulint		n_fields;
	ulint		i;

unknown's avatar
unknown committed
434 435 436
	ut_ad(rec_offs_validate(rec, NULL, offsets));

	rec_set_info_bits(rec, rec_offs_comp(offsets), update->info_bits);
437 438 439 440 441 442 443

	n_fields = upd_get_n_fields(update);

	for (i = 0; i < n_fields; i++) {
		upd_field = upd_get_nth_field(update, i);
		new_val = &(upd_field->new_val);
		
unknown's avatar
unknown committed
444
		rec_set_nth_field(rec, offsets, upd_field->field_no,
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
						dfield_get_data(new_val),
						dfield_get_len(new_val));
	}
}

/*************************************************************************
Writes into the redo log the values of trx id and roll ptr and enough info
to determine their positions within a clustered index record. */

byte*
row_upd_write_sys_vals_to_log(
/*==========================*/
				/* out: new pointer to mlog */
	dict_index_t*	index,	/* in: clustered index */
	trx_t*		trx,	/* in: transaction */
	dulint		roll_ptr,/* in: roll ptr of the undo log record */
	byte*		log_ptr,/* pointer to a buffer of size > 20 opened
				in mlog */
unknown's avatar
unknown committed
463
	mtr_t*		mtr __attribute__((unused))) /* in: mtr */
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 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 593 594 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
{
	ut_ad(index->type & DICT_CLUSTERED);
	ut_ad(mtr);

	log_ptr += mach_write_compressed(log_ptr,
			dict_index_get_sys_col_pos(index, DATA_TRX_ID));

	trx_write_roll_ptr(log_ptr, roll_ptr);
	log_ptr += DATA_ROLL_PTR_LEN;	

	log_ptr += mach_dulint_write_compressed(log_ptr, trx->id);

	return(log_ptr);
}

/*************************************************************************
Parses the log data of system field values. */

byte*
row_upd_parse_sys_vals(
/*===================*/
			/* out: log data end or NULL */
	byte*	ptr,	/* in: buffer */
	byte*	end_ptr,/* in: buffer end */
	ulint*	pos,	/* out: TRX_ID position in record */
	dulint*	trx_id,	/* out: trx id */
	dulint*	roll_ptr)/* out: roll ptr */
{
	ptr = mach_parse_compressed(ptr, end_ptr, pos);

	if (ptr == NULL) {

		return(NULL);
	}

	if (end_ptr < ptr + DATA_ROLL_PTR_LEN) {

		return(NULL);
	}
	
	*roll_ptr = trx_read_roll_ptr(ptr);
	ptr += DATA_ROLL_PTR_LEN;	

	ptr = mach_dulint_parse_compressed(ptr, end_ptr, trx_id);

	return(ptr);
}

/***************************************************************
Writes to the redo log the new values of the fields occurring in the index. */

void
row_upd_index_write_log(
/*====================*/
	upd_t*	update,	/* in: update vector */
	byte*	log_ptr,/* in: pointer to mlog buffer: must contain at least
			MLOG_BUF_MARGIN bytes of free space; the buffer is
			closed within this function */
	mtr_t*	mtr)	/* in: mtr into whose log to write */
{
	upd_field_t*	upd_field;
	dfield_t*	new_val;
	ulint		len;
	ulint		n_fields;
	byte*		buf_end;
	ulint		i;

	n_fields = upd_get_n_fields(update);

	buf_end = log_ptr + MLOG_BUF_MARGIN;
	
	mach_write_to_1(log_ptr, update->info_bits);
	log_ptr++;
	log_ptr += mach_write_compressed(log_ptr, n_fields);
	
	for (i = 0; i < n_fields; i++) {

		ut_ad(MLOG_BUF_MARGIN > 30);

		if (log_ptr + 30 > buf_end) {
			mlog_close(mtr, log_ptr);
			
			log_ptr = mlog_open(mtr, MLOG_BUF_MARGIN);
			buf_end = log_ptr + MLOG_BUF_MARGIN;
		}

		upd_field = upd_get_nth_field(update, i);

		new_val = &(upd_field->new_val);

		len = new_val->len;

		log_ptr += mach_write_compressed(log_ptr, upd_field->field_no);
		log_ptr += mach_write_compressed(log_ptr, len);

		if (len != UNIV_SQL_NULL) {
			if (log_ptr + len < buf_end) {
				ut_memcpy(log_ptr, new_val->data, len);

				log_ptr += len;
			} else {
				mlog_close(mtr, log_ptr);
			
				mlog_catenate_string(mtr, new_val->data, len);

				log_ptr = mlog_open(mtr, MLOG_BUF_MARGIN);
				buf_end = log_ptr + MLOG_BUF_MARGIN;
			}
		}
	}

	mlog_close(mtr, log_ptr);
}

/*************************************************************************
Parses the log data written by row_upd_index_write_log. */

byte*
row_upd_index_parse(
/*================*/
				/* out: log data end or NULL */
	byte*		ptr,	/* in: buffer */
	byte*		end_ptr,/* in: buffer end */
	mem_heap_t*	heap,	/* in: memory heap where update vector is
				built */
	upd_t**		update_out)/* out: update vector */
{
	upd_t*		update;
	upd_field_t*	upd_field;
	dfield_t*	new_val;
	ulint		len;
	ulint		n_fields;
	byte*		buf;
	ulint		info_bits;
	ulint		i;

	if (end_ptr < ptr + 1) {

		return(NULL);
	}

	info_bits = mach_read_from_1(ptr);
	ptr++;
	ptr = mach_parse_compressed(ptr, end_ptr, &n_fields);

	if (ptr == NULL) {

		return(NULL);
	}

	update = upd_create(n_fields, heap);
	update->info_bits = info_bits;

	for (i = 0; i < n_fields; i++) {
		upd_field = upd_get_nth_field(update, i);
		new_val = &(upd_field->new_val);

		ptr = mach_parse_compressed(ptr, end_ptr,
						&(upd_field->field_no));
		if (ptr == NULL) {

			return(NULL);
		}

		ptr = mach_parse_compressed(ptr, end_ptr, &len);

		if (ptr == NULL) {

			return(NULL);
		}

		new_val->len = len;

		if (len != UNIV_SQL_NULL) {

			if (end_ptr < ptr + len) {

				return(NULL);
			} else {
				buf = mem_heap_alloc(heap, len);
				ut_memcpy(buf, ptr, len);

				ptr += len;

				new_val->data = buf;
			}
		}
	}

	*update_out = update;

	return(ptr);
}
657 658 659

/*******************************************************************
Returns TRUE if ext_vec contains i. */
unknown's avatar
unknown committed
660
static
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
ibool
upd_ext_vec_contains(
/*=================*/
				/* out: TRUE if i is in ext_vec */
	ulint*	ext_vec,	/* in: array of indexes or NULL */
	ulint	n_ext_vec,	/* in: number of numbers in ext_vec */
	ulint	i)		/* in: a number */
{
	ulint	j;

	if (ext_vec == NULL) {

		return(FALSE);
	}

	for (j = 0; j < n_ext_vec; j++) {
		if (ext_vec[j] == i) {

			return(TRUE);
		}
	}

	return(FALSE);
}
685
	
686 687 688 689 690 691 692 693 694 695 696 697 698
/*******************************************************************
Builds an update vector from those fields which in a secondary index entry
differ from a record that has the equal ordering fields. NOTE: we compare
the fields as binary strings! */

upd_t*
row_upd_build_sec_rec_difference_binary(
/*====================================*/
				/* out, own: update vector of differing
				fields */
	dict_index_t*	index,	/* in: index */
	dtuple_t*	entry,	/* in: entry to insert */
	rec_t*		rec,	/* in: secondary index record */
699
	trx_t*		trx,	/* in: transaction */
700 701 702 703 704 705 706 707 708
	mem_heap_t*	heap)	/* in: memory heap from which allocated */
{
	upd_field_t*	upd_field;
	dfield_t*	dfield;
	byte*		data;
	ulint		len;
	upd_t*		update;
	ulint		n_diff;
	ulint		i;
709
	ulint		offsets_[REC_OFFS_SMALL_SIZE];
unknown's avatar
unknown committed
710
	const ulint*	offsets;
711
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;
712 713

	/* This function is used only for a secondary index */
unknown's avatar
unknown committed
714
	ut_a(0 == (index->type & DICT_CLUSTERED));
715 716 717 718

	update = upd_create(dtuple_get_n_fields(entry), heap);

	n_diff = 0;
719 720
	offsets = rec_get_offsets(rec, index, offsets_,
						ULINT_UNDEFINED, &heap);
721 722 723

	for (i = 0; i < dtuple_get_n_fields(entry); i++) {

unknown's avatar
unknown committed
724
		data = rec_get_nth_field(rec, offsets, i, &len);
725 726 727

		dfield = dtuple_get_nth_field(entry, i);

unknown's avatar
unknown committed
728 729 730 731 732 733 734
		/* NOTE that it may be that len != dfield_get_len(dfield) if we
		are updating in a character set and collation where strings of
		different length can be equal in an alphabetical comparison,
		and also in the case where we have a column prefix index
		and the last characters in the index field are spaces; the
		latter case probably caused the assertion failures reported at
		row0upd.c line 713 in versions 4.0.14 - 4.0.16. */
735 736 737 738 739 740 741 742 743 744

		/* NOTE: we compare the fields as binary strings!
		(No collation) */

		if (!dfield_data_is_binary_equal(dfield, len, data)) {

			upd_field = upd_get_nth_field(update, n_diff);

			dfield_copy(&(upd_field->new_val), dfield);

745
			upd_field_set_field_no(upd_field, i, index, trx);
746 747 748 749 750 751 752 753 754 755 756 757

			upd_field->extern_storage = FALSE;

			n_diff++;
		}
	}

	update->n_fields = n_diff;

	return(update);
}

758 759 760
/*******************************************************************
Builds an update vector from those fields, excluding the roll ptr and
trx id fields, which in an index entry differ from a record that has
761
the equal ordering fields. NOTE: we compare the fields as binary strings! */
762 763

upd_t*
764 765
row_upd_build_difference_binary(
/*============================*/
766 767 768 769
				/* out, own: update vector of differing
				fields, excluding roll ptr and trx id */
	dict_index_t*	index,	/* in: clustered index */
	dtuple_t*	entry,	/* in: entry to insert */
770 771 772
	ulint*		ext_vec,/* in: array containing field numbers of
				externally stored fields in entry, or NULL */
	ulint		n_ext_vec,/* in: number of fields in ext_vec */
773
	rec_t*		rec,	/* in: clustered index record */
774
	trx_t*		trx,	/* in: transaction */
775 776 777 778 779 780 781 782 783 784
	mem_heap_t*	heap)	/* in: memory heap from which allocated */
{
	upd_field_t*	upd_field;
	dfield_t*	dfield;
	byte*		data;
	ulint		len;
	upd_t*		update;
	ulint		n_diff;
	ulint		roll_ptr_pos;
	ulint		trx_id_pos;
unknown's avatar
unknown committed
785
	ibool		extern_bit;
786
	ulint		i;
787
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
unknown's avatar
unknown committed
788
	const ulint*	offsets;
789
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;
790 791

	/* This function is used only for a clustered index */
unknown's avatar
Merge  
unknown committed
792
	ut_a(index->type & DICT_CLUSTERED);
793 794 795 796 797 798 799 800

	update = upd_create(dtuple_get_n_fields(entry), heap);

	n_diff = 0;

	roll_ptr_pos = dict_index_get_sys_col_pos(index, DATA_ROLL_PTR);
	trx_id_pos = dict_index_get_sys_col_pos(index, DATA_TRX_ID);

801 802
	offsets = rec_get_offsets(rec, index, offsets_,
				ULINT_UNDEFINED, &heap);
unknown's avatar
unknown committed
803

804 805
	for (i = 0; i < dtuple_get_n_fields(entry); i++) {

unknown's avatar
unknown committed
806
		data = rec_get_nth_field(rec, offsets, i, &len);
807

808 809
		dfield = dtuple_get_nth_field(entry, i);

810 811 812
		/* NOTE: we compare the fields as binary strings!
		(No collation) */

unknown's avatar
Merge  
unknown committed
813 814 815 816
		if (i == trx_id_pos || i == roll_ptr_pos) {

			goto skip_compare;
		}
unknown's avatar
unknown committed
817

unknown's avatar
unknown committed
818
		extern_bit = rec_offs_nth_extern(offsets, i);
unknown's avatar
Merge  
unknown committed
819
		
unknown's avatar
unknown committed
820
		if (extern_bit != upd_ext_vec_contains(ext_vec, n_ext_vec, i)
unknown's avatar
Merge  
unknown committed
821
		    || !dfield_data_is_binary_equal(dfield, len, data)) {
822 823 824 825 826

			upd_field = upd_get_nth_field(update, n_diff);

			dfield_copy(&(upd_field->new_val), dfield);

827
			upd_field_set_field_no(upd_field, i, index, trx);
828 829 830 831 832 833

			if (upd_ext_vec_contains(ext_vec, n_ext_vec, i)) {
				upd_field->extern_storage = TRUE;
			} else {
				upd_field->extern_storage = FALSE;
			}
834 835 836
				
			n_diff++;
		}
unknown's avatar
Merge  
unknown committed
837 838
skip_compare:
		;
839 840 841 842 843 844 845 846 847 848 849
	}

	update->n_fields = n_diff;

	return(update);
}

/***************************************************************
Replaces the new column values stored in the update vector to the index entry
given. */

unknown's avatar
unknown committed
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
void
row_upd_index_replace_new_col_vals_index_pos(
/*=========================================*/
	dtuple_t*	entry,	/* in/out: index entry where replaced */
	dict_index_t*	index,	/* in: index; NOTE that this may also be a
				non-clustered index */
	upd_t*		update,	/* in: an update vector built for the index so
				that the field number in an upd_field is the
				index position */
	mem_heap_t*	heap)	/* in: memory heap to which we allocate and
				copy the new values, set this as NULL if you
				do not want allocation */
{
	dict_field_t*	field;
	upd_field_t*	upd_field;
	dfield_t*	dfield;
	dfield_t*	new_val;
	ulint		j;
	ulint		i;
869
	dtype_t*	cur_type;
unknown's avatar
unknown committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898

	ut_ad(index);

	dtuple_set_info_bits(entry, update->info_bits);

	for (j = 0; j < dict_index_get_n_fields(index); j++) {

	        field = dict_index_get_nth_field(index, j);

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

		        upd_field = upd_get_nth_field(update, i);

			if (upd_field->field_no == j) {

			        dfield = dtuple_get_nth_field(entry, j);

				new_val = &(upd_field->new_val);

				dfield_set_data(dfield, new_val->data,
								new_val->len);
				if (heap && new_val->len != UNIV_SQL_NULL) {
				        dfield->data = mem_heap_alloc(heap,
								new_val->len);
					ut_memcpy(dfield->data, new_val->data,
								new_val->len);
				}

				if (field->prefix_len > 0
899
			            && new_val->len != UNIV_SQL_NULL) {
unknown's avatar
unknown committed
900

unknown's avatar
unknown committed
901 902 903 904 905 906 907 908 909
				  	cur_type = dict_col_get_type(
						dict_field_get_col(field));

				  	dfield->len = 
				    		dtype_get_at_most_n_mbchars(
				      			cur_type,
							field->prefix_len,
							new_val->len,
							new_val->data);
unknown's avatar
unknown committed
910 911 912 913 914 915 916 917 918 919
				}
			}
		}
	}
}

/***************************************************************
Replaces the new column values stored in the update vector to the index entry
given. */

920 921 922 923
void
row_upd_index_replace_new_col_vals(
/*===============================*/
	dtuple_t*	entry,	/* in/out: index entry where replaced */
unknown's avatar
unknown committed
924
	dict_index_t*	index,	/* in: index; NOTE that this may also be a
925
				non-clustered index */
unknown's avatar
unknown committed
926 927 928
	upd_t*		update,	/* in: an update vector built for the
				CLUSTERED index so that the field number in
				an upd_field is the clustered index position */
unknown's avatar
unknown committed
929 930 931
	mem_heap_t*	heap)	/* in: memory heap to which we allocate and
				copy the new values, set this as NULL if you
				do not want allocation */
932
{
unknown's avatar
unknown committed
933
	dict_field_t*	field;
934 935 936
	upd_field_t*	upd_field;
	dfield_t*	dfield;
	dfield_t*	new_val;
unknown's avatar
unknown committed
937
	ulint		j;
938
	ulint		i;
939
	dtype_t*	cur_type;
940 941 942 943 944

	ut_ad(index);

	dtuple_set_info_bits(entry, update->info_bits);

unknown's avatar
unknown committed
945
	for (j = 0; j < dict_index_get_n_fields(index); j++) {
946

unknown's avatar
unknown committed
947
	        field = dict_index_get_nth_field(index, j);
948

unknown's avatar
unknown committed
949
		for (i = 0; i < upd_get_n_fields(update); i++) {
950

unknown's avatar
unknown committed
951
		        upd_field = upd_get_nth_field(update, i);
952

unknown's avatar
unknown committed
953
			if (upd_field->field_no == field->col->clust_pos) {
954

unknown's avatar
unknown committed
955
			        dfield = dtuple_get_nth_field(entry, j);
956

unknown's avatar
unknown committed
957
				new_val = &(upd_field->new_val);
958

unknown's avatar
unknown committed
959 960 961 962 963 964 965 966
				dfield_set_data(dfield, new_val->data,
								new_val->len);
				if (heap && new_val->len != UNIV_SQL_NULL) {
				        dfield->data = mem_heap_alloc(heap,
								new_val->len);
					ut_memcpy(dfield->data, new_val->data,
								new_val->len);
				}
967

unknown's avatar
unknown committed
968
				if (field->prefix_len > 0
969 970
			            && new_val->len != UNIV_SQL_NULL) {

unknown's avatar
unknown committed
971 972
					cur_type = dict_col_get_type(
						dict_field_get_col(field));
973

unknown's avatar
unknown committed
974 975 976 977 978 979
				  	dfield->len =
				    		dtype_get_at_most_n_mbchars(
				      			cur_type,
							field->prefix_len,
							new_val->len,
							new_val->data);
unknown's avatar
unknown committed
980 981 982
				}
			}
		}
983 984 985 986 987 988
	}
}

/***************************************************************
Checks if an update vector changes an ordering field of an index record.
This function is fast if the update vector is short or the number of ordering
989 990
fields in the index is small. Otherwise, this can be quadratic.
NOTE: we compare the fields as binary strings! */
991 992

ibool
993 994
row_upd_changes_ord_field_binary(
/*=============================*/
995
				/* out: TRUE if update vector changes
996 997 998
				an ordering field in the index record;
				NOTE: the fields are compared as binary
				strings */
999 1000 1001 1002 1003
	dtuple_t*	row,	/* in: old value of row, or NULL if the
				row and the data values in update are not
				known when this function is called, e.g., at
				compile time */
	dict_index_t*	index,	/* in: index of the record */
unknown's avatar
unknown committed
1004 1005 1006
	upd_t*		update)	/* in: update vector for the row; NOTE: the
				field numbers in this MUST be clustered index
				positions! */
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
{
	upd_field_t*	upd_field;
	dict_field_t*	ind_field;
	dict_col_t*	col;
	ulint		n_unique;
	ulint		n_upd_fields;
	ulint		col_pos;
	ulint		col_no;
	ulint		i, j;
	
	ut_ad(update && index);

	n_unique = dict_index_get_n_unique(index);
	n_upd_fields = upd_get_n_fields(update);

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

		ind_field = dict_index_get_nth_field(index, i);
		col = dict_field_get_col(ind_field);
		col_pos = dict_col_get_clust_pos(col);
		col_no = dict_col_get_no(col);

		for (j = 0; j < n_upd_fields; j++) {

			upd_field = upd_get_nth_field(update, j);

unknown's avatar
unknown committed
1033 1034 1035 1036 1037
			/* Note that if the index field is a column prefix
			then it may be that row does not contain an externally
			stored part of the column value, and we cannot compare
			the datas */

1038
			if (col_pos == upd_field->field_no
unknown's avatar
unknown committed
1039 1040 1041
			    && (row == NULL
			        || ind_field->prefix_len > 0
				|| !dfield_datas_are_binary_equal(
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
					dtuple_get_nth_field(row, col_no),
						&(upd_field->new_val)))) {
				return(TRUE);
			}
		}
	}

	return(FALSE);
}

/***************************************************************
1053 1054
Checks if an update vector changes an ordering field of an index record.
NOTE: we compare the fields as binary strings! */
1055 1056

ibool
1057 1058
row_upd_changes_some_index_ord_field_binary(
/*========================================*/
1059 1060 1061 1062 1063
				/* out: TRUE if update vector may change
				an ordering field in an index record */
	dict_table_t*	table,	/* in: table */
	upd_t*		update)	/* in: update vector for the row */
{
1064
	upd_field_t*	upd_field;
1065
	dict_index_t*	index;
1066 1067
	ulint		i;
	
1068 1069
	index = dict_table_get_first_index(table);
	
1070
	for (i = 0; i < upd_get_n_fields(update); i++) {
1071

1072
		upd_field = upd_get_nth_field(update, i);
1073

1074 1075 1076
		if (dict_field_get_col(dict_index_get_nth_field(index,
						upd_field->field_no))
		    ->ord_part) {
1077

1078 1079 1080 1081
		    	return(TRUE);
		}
	}
	
1082 1083 1084
	return(FALSE);
}

unknown's avatar
unknown committed
1085
/***************************************************************
unknown's avatar
unknown committed
1086 1087 1088
Checks if an update vector changes some of the first ordering fields of an
index record. This is only used in foreign key checks and we can assume
that index does not contain column prefixes. */
unknown's avatar
unknown committed
1089 1090
static
ibool
unknown's avatar
unknown committed
1091 1092
row_upd_changes_first_fields_binary(
/*================================*/
unknown's avatar
unknown committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
				/* out: TRUE if changes */
	dtuple_t*	entry,	/* in: index entry */
	dict_index_t*	index,	/* in: index of entry */
	upd_t*		update,	/* in: update vector for the row */
	ulint		n)	/* in: how many first fields to check */
{
	upd_field_t*	upd_field;
	dict_field_t*	ind_field;
	dict_col_t*	col;
	ulint		n_upd_fields;
	ulint		col_pos;
	ulint		i, j;
	
	ut_a(update && index);
	ut_a(n <= dict_index_get_n_fields(index));
	
	n_upd_fields = upd_get_n_fields(update);

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

		ind_field = dict_index_get_nth_field(index, i);
		col = dict_field_get_col(ind_field);
		col_pos = dict_col_get_clust_pos(col);

unknown's avatar
unknown committed
1117 1118
		ut_a(ind_field->prefix_len == 0);

unknown's avatar
unknown committed
1119 1120 1121 1122 1123
		for (j = 0; j < n_upd_fields; j++) {

			upd_field = upd_get_nth_field(update, j);

			if (col_pos == upd_field->field_no
unknown's avatar
unknown committed
1124
			    && !dfield_datas_are_binary_equal(
unknown's avatar
unknown committed
1125
					     dtuple_get_nth_field(entry, i),
unknown's avatar
unknown committed
1126
					     &(upd_field->new_val))) {
unknown's avatar
unknown committed
1127 1128 1129 1130 1131 1132 1133 1134
				return(TRUE);
			}
		}
	}

	return(FALSE);
}

1135 1136 1137 1138 1139 1140 1141
/*************************************************************************
Copies the column values from a record. */
UNIV_INLINE
void
row_upd_copy_columns(
/*=================*/
	rec_t*		rec,	/* in: record in a clustered index */
unknown's avatar
unknown committed
1142
	const ulint*	offsets,/* in: array returned by rec_get_offsets() */
1143 1144 1145 1146 1147 1148 1149
	sym_node_t*	column)	/* in: first column in a column list, or
				NULL */
{
	byte*	data;
	ulint	len;

	while (column) {
unknown's avatar
unknown committed
1150
		data = rec_get_nth_field(rec, offsets,
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
					column->field_nos[SYM_CLUST_FIELD_NO],
									&len);
		eval_node_copy_and_alloc_val(column, data, len);

		column = UT_LIST_GET_NEXT(col_var_list, column);
	}
}

/*************************************************************************
Calculates the new values for fields to update. Note that row_upd_copy_columns
must have been called first. */
UNIV_INLINE
void
row_upd_eval_new_vals(
/*==================*/
	upd_t*	update)	/* in: update vector */
{
	que_node_t*	exp;
	upd_field_t*	upd_field;
	ulint		n_fields;
	ulint		i;

	n_fields = upd_get_n_fields(update);

	for (i = 0; i < n_fields; i++) {
		upd_field = upd_get_nth_field(update, i);

		exp = upd_field->exp;

		eval_exp(exp);

		dfield_copy_data(&(upd_field->new_val), que_node_get_val(exp));
	}
}

/***************************************************************
Stores to the heap the row on which the node->pcur is positioned. */
1188
static
1189 1190 1191 1192 1193 1194
void
row_upd_store_row(
/*==============*/
	upd_node_t*	node)	/* in: row update node */
{
	dict_index_t*	clust_index;
1195 1196
	upd_t*		update;
	rec_t*		rec;
1197
	mem_heap_t*	heap		= NULL;
1198
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
unknown's avatar
unknown committed
1199
	const ulint*	offsets;
1200
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;
unknown's avatar
unknown committed
1201

1202
	ut_ad(node->pcur->latch_mode != BTR_NO_LATCHES);
1203 1204 1205 1206 1207 1208 1209 1210

	if (node->row != NULL) {
		mem_heap_empty(node->heap);
		node->row = NULL;
	}
	
	clust_index = dict_table_get_first_index(node->table);

1211 1212
	rec = btr_pcur_get_rec(node->pcur);
	
1213 1214
	offsets = rec_get_offsets(rec, clust_index, offsets_,
						ULINT_UNDEFINED, &heap);
unknown's avatar
unknown committed
1215 1216
	node->row = row_build(ROW_COPY_DATA, clust_index, rec, offsets,
								node->heap);
1217
	node->ext_vec = mem_heap_alloc(node->heap, sizeof(ulint)
unknown's avatar
unknown committed
1218
					* rec_offs_n_fields(offsets));
1219 1220 1221 1222 1223 1224 1225
	if (node->is_delete) {
		update = NULL;
	} else {
		update = node->update;
	}
	
	node->n_ext_vec = btr_push_update_extern_fields(node->ext_vec,
unknown's avatar
unknown committed
1226
						offsets, update);
1227
	if (UNIV_LIKELY_NULL(heap)) {
1228 1229
		mem_heap_free(heap);
	}
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
}

/***************************************************************
Updates a secondary index entry of a row. */
static
ulint
row_upd_sec_index_entry(
/*====================*/
				/* out: DB_SUCCESS if operation successfully
				completed, else error code or DB_LOCK_WAIT */
	upd_node_t*	node,	/* in: row update node */
	que_thr_t*	thr)	/* in: query thread */
{
1243
	ibool		check_ref;
1244 1245 1246 1247 1248 1249 1250
	ibool		found;
	dict_index_t*	index;
	dtuple_t*	entry;
	btr_pcur_t	pcur;
	btr_cur_t*	btr_cur;
	mem_heap_t*	heap;
	rec_t*		rec;
1251
	ulint		err	= DB_SUCCESS;
unknown's avatar
Merge  
unknown committed
1252
	mtr_t		mtr;
1253 1254
	trx_t*		trx	= thr_get_trx(thr);

1255 1256
	index = node->index;
	
1257
	check_ref = row_upd_index_is_referenced(index, trx);
1258

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	heap = mem_heap_create(1024);

	/* Build old index entry */
	entry = row_build_index_entry(node->row, index, heap);

	log_free_check();
	mtr_start(&mtr);
	
	found = row_search_index_entry(index, entry, BTR_MODIFY_LEAF, &pcur,
									&mtr);
	btr_cur = btr_pcur_get_btr_cur(&pcur);

	rec = btr_cur_get_rec(btr_cur);

unknown's avatar
unknown committed
1273
	if (UNIV_UNLIKELY(!found)) {
1274 1275
		fputs("InnoDB: error in sec index entry update in\n"
			"InnoDB: ", stderr);
1276
		dict_index_name_print(stderr, trx, index);
1277 1278 1279 1280 1281
		fputs("\n"
			"InnoDB: tuple ", stderr);
		dtuple_print(stderr, entry);
		fputs("\n"
			"InnoDB: record ", stderr);
1282
		rec_print(stderr, rec, index);
1283 1284
		putc('\n', stderr);

1285
		trx_print(stderr, trx);
1286 1287

		fputs("\n"
1288
"InnoDB: Submit a detailed bug report to http://bugs.mysql.com\n", stderr);
1289
	} else {
1290 1291 1292 1293
 	  	/* Delete mark the old index record; it can already be
          	delete marked if we return after a lock wait in
          	row_ins_index_entry below */

unknown's avatar
unknown committed
1294
		if (!rec_get_deleted_flag(rec, index->table->comp)) {
1295 1296
			err = btr_cur_del_mark_set_sec_rec(0, btr_cur, TRUE,
								thr, &mtr);
1297
			if (err == DB_SUCCESS && check_ref) {
unknown's avatar
unknown committed
1298
			    	
1299 1300 1301
				/* NOTE that the following call loses
				the position of pcur ! */
				err = row_upd_check_references_constraints(
unknown's avatar
unknown committed
1302
							node,
1303 1304 1305 1306 1307 1308 1309 1310
							&pcur, index->table,
							index, thr, &mtr);
				if (err != DB_SUCCESS) {

					goto close_cur;
				}
			}

1311
	  	}
1312
	}
1313
close_cur:
1314 1315 1316
	btr_pcur_close(&pcur);
	mtr_commit(&mtr);

1317
	if (node->is_delete || err != DB_SUCCESS) {
1318 1319 1320 1321 1322 1323 1324

		mem_heap_free(heap);	

        	return(err);
	}

	/* Build a new index entry */
unknown's avatar
unknown committed
1325
	row_upd_index_replace_new_col_vals(entry, index, node->update, NULL);
1326 1327

	/* Insert new index entry */
1328
	err = row_ins_index_entry(index, entry, NULL, 0, thr);
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352

	mem_heap_free(heap);	

        return(err);
}

/***************************************************************
Updates secondary index record if it is changed in the row update. This
should be quite rare in database applications. */
UNIV_INLINE
ulint
row_upd_sec_step(
/*=============*/
				/* out: DB_SUCCESS if operation successfully
				completed, else error code or DB_LOCK_WAIT */
	upd_node_t*	node,	/* in: row update node */
	que_thr_t*	thr)	/* in: query thread */
{
	ulint	err;

	ut_ad((node->state == UPD_NODE_UPDATE_ALL_SEC)
				|| (node->state == UPD_NODE_UPDATE_SOME_SEC));
	ut_ad(!(node->index->type & DICT_CLUSTERED));
	
1353
	if (node->state == UPD_NODE_UPDATE_ALL_SEC
1354 1355
	    || row_upd_changes_ord_field_binary(node->row, node->index,
							   node->update)) {
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
		err = row_upd_sec_index_entry(node, thr);

		return(err);
	}

	return(DB_SUCCESS);
}

/***************************************************************
Marks the clustered index record deleted and inserts the updated version
of the record to the index. This function should be used when the ordering
fields of the clustered index record change. This should be quite rare in
database applications. */
static
ulint
row_upd_clust_rec_by_insert(
/*========================*/
				/* out: DB_SUCCESS if operation successfully
				completed, else error code or DB_LOCK_WAIT */
	upd_node_t*	node,	/* in: row update node */
	dict_index_t*	index,	/* in: clustered index of the record */
	que_thr_t*	thr,	/* in: query thread */
1378 1379
	ibool		check_ref,/* in: TRUE if index may be referenced in
				a foreign key constraint */
1380
	mtr_t*		mtr)	/* in: mtr; gets committed here */
1381
{
1382
	mem_heap_t*	heap	= NULL;
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
	btr_pcur_t*	pcur;
	btr_cur_t*	btr_cur;
	trx_t*		trx;
	dict_table_t*	table;
	dtuple_t*	entry;
	ulint		err;
	
	ut_ad(node);
	ut_ad(index->type & DICT_CLUSTERED);

	trx = thr_get_trx(thr);
	table = node->table;
	pcur = node->pcur;
	btr_cur	= btr_pcur_get_btr_cur(pcur);
	
	if (node->state != UPD_NODE_INSERT_CLUSTERED) {
1399
		ulint	offsets_[REC_OFFS_NORMAL_SIZE];
1400
		*offsets_ = (sizeof offsets_) / sizeof *offsets_;
1401 1402 1403 1404 1405 1406 1407

		err = btr_cur_del_mark_set_clust_rec(BTR_NO_LOCKING_FLAG,
						btr_cur, TRUE, thr, mtr);
		if (err != DB_SUCCESS) {
			mtr_commit(mtr);
			return(err);
		}
1408

1409 1410 1411 1412 1413 1414
		/* Mark as not-owned the externally stored fields which the new
		row inherits from the delete marked record: purge should not
		free those externally stored fields even if the delete marked
		record is removed from the index tree, or updated. */

		btr_cur_mark_extern_inherited_fields(btr_cur_get_rec(btr_cur),
unknown's avatar
unknown committed
1415
				rec_get_offsets(btr_cur_get_rec(btr_cur),
1416 1417
				dict_table_get_first_index(table), offsets_,
				ULINT_UNDEFINED, &heap), node->update, mtr);
1418 1419 1420
		if (check_ref) {
			/* NOTE that the following call loses
			the position of pcur ! */
unknown's avatar
unknown committed
1421
			err = row_upd_check_references_constraints(node,
1422 1423 1424 1425
							pcur, table,
							index, thr, mtr);
			if (err != DB_SUCCESS) {
				mtr_commit(mtr);
1426
				if (UNIV_LIKELY_NULL(heap)) {
1427 1428
					mem_heap_free(heap);
				}
1429 1430 1431 1432
				return(err);
			}
		}

1433 1434 1435 1436
	} 

	mtr_commit(mtr);

1437 1438 1439
	if (!heap) {
		heap = mem_heap_create(500);
	}
1440 1441 1442 1443
	node->state = UPD_NODE_INSERT_CLUSTERED;

	entry = row_build_index_entry(node->row, index, heap);

unknown's avatar
unknown committed
1444
	row_upd_index_replace_new_col_vals(entry, index, node->update, NULL);
1445
	
1446 1447
	row_upd_index_entry_sys_field(entry, index, DATA_TRX_ID, trx->id);
	
1448
	/* If we return from a lock wait, for example, we may have
unknown's avatar
unknown committed
1449
	extern fields marked as not-owned in entry (marked in the
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
	if-branch above). We must unmark them. */
	
	btr_cur_unmark_dtuple_extern_fields(entry, node->ext_vec,
							node->n_ext_vec);
	/* We must mark non-updated extern fields in entry as inherited,
	so that a possible rollback will not free them */
	
	btr_cur_mark_dtuple_inherited_extern(entry, node->ext_vec,
						node->n_ext_vec,
						node->update);
	
1461 1462
	err = row_ins_index_entry(index, entry, node->ext_vec,
						node->n_ext_vec, thr);
1463 1464
	mem_heap_free(heap);
	
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
	return(err);
}

/***************************************************************
Updates a clustered index record of a row when the ordering fields do
not change. */
static
ulint
row_upd_clust_rec(
/*==============*/
				/* out: DB_SUCCESS if operation successfully
				completed, else error code or DB_LOCK_WAIT */
	upd_node_t*	node,	/* in: row update node */
	dict_index_t*	index,	/* in: clustered index */
	que_thr_t*	thr,	/* in: query thread */
	mtr_t*		mtr)	/* in: mtr; gets committed here */
{
1482
	big_rec_t*	big_rec	= NULL;
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
	btr_pcur_t*	pcur;
	btr_cur_t*	btr_cur;
	ulint		err;
	
	ut_ad(node);
	ut_ad(index->type & DICT_CLUSTERED);

	pcur = node->pcur;
	btr_cur = btr_pcur_get_btr_cur(pcur);

unknown's avatar
unknown committed
1493 1494
	ut_ad(!rec_get_deleted_flag(btr_pcur_get_rec(pcur),
					index->table->comp));
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
	
	/* Try optimistic updating of the record, keeping changes within
	the page; we do not check locks because we assume the x-lock on the
	record to update */

	if (node->cmpl_info & UPD_NODE_NO_SIZE_CHANGE) {
		err = btr_cur_update_in_place(BTR_NO_LOCKING_FLAG,
						btr_cur, node->update,
						node->cmpl_info, thr, mtr);
	} else {
		err = btr_cur_optimistic_update(BTR_NO_LOCKING_FLAG,
						btr_cur, node->update,
						node->cmpl_info, thr, mtr);
	}

	mtr_commit(mtr);
	
	if (err == DB_SUCCESS) {

		return(err);
	}

	/* We may have to modify the tree structure: do a pessimistic descent
	down the index tree */

	mtr_start(mtr);
	
	/* NOTE: this transaction has an s-lock or x-lock on the record and
	therefore other transactions cannot modify the record when we have no
	latch on the page. In addition, we assume that other query threads of
	the same transaction do not modify the record in the meantime.
	Therefore we can assert that the restoration of the cursor succeeds. */

	ut_a(btr_pcur_restore_position(BTR_MODIFY_TREE, pcur, mtr));

unknown's avatar
unknown committed
1530 1531
	ut_ad(!rec_get_deleted_flag(btr_pcur_get_rec(pcur),
					index->table->comp));
1532 1533
	
	err = btr_cur_pessimistic_update(BTR_NO_LOCKING_FLAG, btr_cur,
1534 1535
					&big_rec, node->update,
					node->cmpl_info, thr, mtr);
1536 1537
	mtr_commit(mtr);

1538
	if (err == DB_SUCCESS && big_rec) {
1539
		mem_heap_t*	heap		= NULL;
1540
		ulint		offsets_[REC_OFFS_NORMAL_SIZE];
unknown's avatar
unknown committed
1541
		rec_t*		rec;
1542 1543
		*offsets_ = (sizeof offsets_) / sizeof *offsets_;

1544
		mtr_start(mtr);
unknown's avatar
unknown committed
1545

1546
		ut_a(btr_pcur_restore_position(BTR_MODIFY_TREE, pcur, mtr));
unknown's avatar
unknown committed
1547
		rec = btr_cur_get_rec(btr_cur);
unknown's avatar
unknown committed
1548
		err = btr_store_big_rec_extern_fields(index, rec,
1549 1550
			rec_get_offsets(rec, index, offsets_,
				ULINT_UNDEFINED, &heap),
unknown's avatar
unknown committed
1551
			big_rec, mtr);
1552
		if (UNIV_LIKELY_NULL(heap)) {
1553 1554
			mem_heap_free(heap);
		}
1555 1556 1557 1558 1559 1560 1561
		mtr_commit(mtr);
	}

	if (big_rec) {
		dtuple_big_rec_free(big_rec);
	}
		
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
	return(err);
}

/***************************************************************
Delete marks a clustered index record. */
static
ulint
row_upd_del_mark_clust_rec(
/*=======================*/
				/* out: DB_SUCCESS if operation successfully
unknown's avatar
unknown committed
1572
				completed, else error code */
1573 1574 1575
	upd_node_t*	node,	/* in: row update node */
	dict_index_t*	index,	/* in: clustered index */
	que_thr_t*	thr,	/* in: query thread */
1576 1577
	ibool		check_ref,/* in: TRUE if index may be referenced in
				a foreign key constraint */
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
	mtr_t*		mtr)	/* in: mtr; gets committed here */
{
	btr_pcur_t*	pcur;
	btr_cur_t*	btr_cur;
	ulint		err;
	
	ut_ad(node);
	ut_ad(index->type & DICT_CLUSTERED);
	ut_ad(node->is_delete);

	pcur = node->pcur;
	btr_cur = btr_pcur_get_btr_cur(pcur);

	/* Store row because we have to build also the secondary index
	entries */
	
	row_upd_store_row(node);

	/* Mark the clustered index record deleted; we do not have to check
	locks, because we assume that we have an x-lock on the record */

unknown's avatar
unknown committed
1599 1600
	err = btr_cur_del_mark_set_clust_rec(BTR_NO_LOCKING_FLAG,
						btr_cur, TRUE, thr, mtr);
1601
	if (err == DB_SUCCESS && check_ref) {
unknown's avatar
unknown committed
1602 1603
		/* NOTE that the following call loses the position of pcur ! */

unknown's avatar
unknown committed
1604 1605
		err = row_upd_check_references_constraints(node,
							pcur, index->table,
1606 1607 1608 1609 1610 1611 1612 1613
							index, thr, mtr);
		if (err != DB_SUCCESS) {
			mtr_commit(mtr);

			return(err);
		}
	}

1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
	mtr_commit(mtr);
	
	return(err);
}

/***************************************************************
Updates the clustered index record. */
static
ulint
row_upd_clust_step(
/*===============*/
				/* out: DB_SUCCESS if operation successfully
				completed, DB_LOCK_WAIT in case of a lock wait,
				else error code */
	upd_node_t*	node,	/* in: row update node */
	que_thr_t*	thr)	/* in: query thread */
{
	dict_index_t*	index;
	btr_pcur_t*	pcur;
	ibool		success;
1634
	ibool		check_ref;
1635 1636
	ulint		err;
	mtr_t*		mtr;
1637
	mtr_t		mtr_buf;
unknown's avatar
unknown committed
1638
	rec_t*		rec;
1639
	mem_heap_t*	heap		= NULL;
1640
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
unknown's avatar
unknown committed
1641
	const ulint*	offsets;
1642
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;
unknown's avatar
unknown committed
1643

1644 1645
	index = dict_table_get_first_index(node->table);

unknown's avatar
unknown committed
1646
	check_ref = row_upd_index_is_referenced(index, thr_get_trx(thr));
1647

1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
	pcur = node->pcur;

	/* We have to restore the cursor to its position */
	mtr = &mtr_buf;

	mtr_start(mtr);
	
	/* If the restoration does not succeed, then the same
	transaction has deleted the record on which the cursor was,
	and that is an SQL error. If the restoration succeeds, it may
	still be that the same transaction has successively deleted
	and inserted a record with the same ordering fields, but in
	that case we know that the transaction has at least an
	implicit x-lock on the record. */
	
	ut_a(pcur->rel_pos == BTR_PCUR_ON);

	success = btr_pcur_restore_position(BTR_MODIFY_LEAF, pcur, mtr);

	if (!success) {
		err = DB_RECORD_NOT_FOUND;

		mtr_commit(mtr);

		return(err);
	}

	/* If this is a row in SYS_INDEXES table of the data dictionary,
	then we have to free the file segments of the index tree associated
	with the index */

unknown's avatar
unknown committed
1679 1680
	if (node->is_delete
	    && ut_dulint_cmp(node->table->id, DICT_INDEXES_ID) == 0) {
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698

		dict_drop_index_tree(btr_pcur_get_rec(pcur), mtr);

		mtr_commit(mtr);

		mtr_start(mtr);

		success = btr_pcur_restore_position(BTR_MODIFY_LEAF, pcur,
									mtr);
		if (!success) {
			err = DB_ERROR;

			mtr_commit(mtr);

			return(err);
		}
	} 

unknown's avatar
unknown committed
1699
	rec = btr_pcur_get_rec(pcur);
1700 1701
	offsets = rec_get_offsets(rec, index, offsets_,
						ULINT_UNDEFINED, &heap);
unknown's avatar
unknown committed
1702

1703 1704
	if (!node->has_clust_rec_x_lock) {
		err = lock_clust_rec_modify_check_and_lock(0,
unknown's avatar
unknown committed
1705
						rec, index, offsets, thr);
1706 1707
		if (err != DB_SUCCESS) {
			mtr_commit(mtr);
1708
			goto exit_func;
1709 1710 1711 1712 1713 1714
		}
	}

	/* NOTE: the following function calls will also commit mtr */

	if (node->is_delete) {
1715 1716
		err = row_upd_del_mark_clust_rec(node, index, thr, check_ref,
									mtr);
1717 1718 1719 1720 1721
		if (err == DB_SUCCESS) {
			node->state = UPD_NODE_UPDATE_ALL_SEC;
			node->index = dict_table_get_next_index(index);
		}
	exit_func:
1722
		if (UNIV_LIKELY_NULL(heap)) {
1723
			mem_heap_free(heap);
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
		}
		return(err);
	}
	
	/* If the update is made for MySQL, we already have the update vector
	ready, else we have to do some evaluation: */
 
	if (!node->in_mysql_interface) {
		/* Copy the necessary columns from clust_rec and calculate the
		new values to set */
unknown's avatar
unknown committed
1734
		row_upd_copy_columns(rec, offsets,
1735 1736 1737
					UT_LIST_GET_FIRST(node->columns));
		row_upd_eval_new_vals(node->update);
	}
unknown's avatar
unknown committed
1738

1739
	if (UNIV_LIKELY_NULL(heap)) {
1740 1741 1742
		mem_heap_free(heap);
	}
		
1743 1744 1745 1746 1747 1748 1749 1750
	if (node->cmpl_info & UPD_NODE_NO_ORD_CHANGE) {

		err = row_upd_clust_rec(node, index, thr, mtr);
		return(err);
	}
	
	row_upd_store_row(node);

1751
	if (row_upd_changes_ord_field_binary(node->row, index, node->update)) {
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763

		/* Update causes an ordering field (ordering fields within
		the B-tree) of the clustered index record to change: perform
		the update by delete marking and inserting.

		TODO! What to do to the 'Halloween problem', where an update
		moves the record forward in index so that it is again
		updated when the cursor arrives there? Solution: the
		read operation must check the undo record undo number when
		choosing records to update. MySQL solves now the problem
		externally! */

1764 1765
		err = row_upd_clust_rec_by_insert(node, index, thr, check_ref,
									mtr);
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
		if (err != DB_SUCCESS) {

			return(err);
		}

		node->state = UPD_NODE_UPDATE_ALL_SEC;
	} else {
		err = row_upd_clust_rec(node, index, thr, mtr);

		if (err != DB_SUCCESS) {

			return(err);
		}

		node->state = UPD_NODE_UPDATE_SOME_SEC;
	}

	node->index = dict_table_get_next_index(index);

	return(err);
}

/***************************************************************
Updates the affected index records of a row. When the control is transferred
to this node, we assume that we have a persistent cursor which was on a
record, and the position of the cursor is stored in the cursor. */
static
ulint
row_upd(
/*====*/
				/* out: DB_SUCCESS if operation successfully
				completed, else error code or DB_LOCK_WAIT */
	upd_node_t*	node,	/* in: row update node */
	que_thr_t*	thr)	/* in: query thread */
{
1801
	ulint	err	= DB_SUCCESS;
1802 1803 1804 1805
	
	ut_ad(node && thr);

	if (node->in_mysql_interface) {
1806
	
1807 1808 1809
		/* We do not get the cmpl_info value from the MySQL
		interpreter: we must calculate it on the fly: */
		
1810
		if (node->is_delete ||
1811 1812
			row_upd_changes_some_index_ord_field_binary(
					node->table, node->update)) {
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
			node->cmpl_info = 0; 
		} else {
			node->cmpl_info = UPD_NODE_NO_ORD_CHANGE;
		}
	}

	if (node->state == UPD_NODE_UPDATE_CLUSTERED
				|| node->state == UPD_NODE_INSERT_CLUSTERED) {

		err = row_upd_clust_step(node, thr);
		
		if (err != DB_SUCCESS) {

			goto function_exit;
		}
	}

1830
	if (!node->is_delete && (node->cmpl_info & UPD_NODE_NO_ORD_CHANGE)) {
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851

		goto function_exit;
	}

	while (node->index != NULL) {
		err = row_upd_sec_step(node, thr);

		if (err != DB_SUCCESS) {

			goto function_exit;
		}

		node->index = dict_table_get_next_index(node->index);
        }

function_exit:
	if (err == DB_SUCCESS) {
		/* Do some cleanup */

		if (node->row != NULL) {
			node->row = NULL;
1852
			node->n_ext_vec = 0;
unknown's avatar
unknown committed
1853
			mem_heap_empty(node->heap);
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
		}

		node->state = UPD_NODE_UPDATE_CLUSTERED;
	}

        return(err);
}

/***************************************************************
Updates a row in a table. This is a high-level function used in SQL execution
graphs. */

que_thr_t*
row_upd_step(
/*=========*/
				/* out: query thread to run next or NULL */
	que_thr_t*	thr)	/* in: query thread */
{
	upd_node_t*	node;
	sel_node_t*	sel_node;
	que_node_t*	parent;
	ulint		err		= DB_SUCCESS;
	trx_t*		trx;

	ut_ad(thr);
	
	trx = thr_get_trx(thr);

unknown's avatar
Merge  
unknown committed
1882 1883
	trx_start_if_not_started(trx);

1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
	node = thr->run_node;
	
	sel_node = node->select;

	parent = que_node_get_parent(node);
	
	ut_ad(que_node_get_type(node) == QUE_NODE_UPDATE);
	
	if (thr->prev_node == parent) {
		node->state = UPD_NODE_SET_IX_LOCK;
	}

	if (node->state == UPD_NODE_SET_IX_LOCK) {

		if (!node->has_clust_rec_x_lock) {
			/* It may be that the current session has not yet
			started its transaction, or it has been committed: */

			err = lock_table(0, node->table, LOCK_IX, thr);

			if (err != DB_SUCCESS) {

				goto error_handling;
			}
		}
	
		node->state = UPD_NODE_UPDATE_CLUSTERED;

		if (node->searched_update) {
			/* Reset the cursor */
			sel_node->state = SEL_NODE_OPEN;
		
			/* Fetch a row to update */
		
			thr->run_node = sel_node;
	
			return(thr);
		}
	}

	/* sel_node is NULL if we are in the MySQL interface */
	
	if (sel_node && (sel_node->state != SEL_NODE_FETCH)) {

		if (!node->searched_update) {
			/* An explicit cursor should be positioned on a row
			to update */

			ut_error;
			
			err = DB_ERROR;

			goto error_handling;
		}

		ut_ad(sel_node->state == SEL_NODE_NO_MORE_ROWS);

		/* No more rows to update, or the select node performed the
		updates directly in-place */

		thr->run_node = parent;
	
		return(thr);
	}

	/* DO THE CHECKS OF THE CONSISTENCY CONSTRAINTS HERE */
	
	err = row_upd(node, thr);

error_handling:
	trx->error_state = err;

	if (err == DB_SUCCESS) {
		/* Ok: do nothing */
	} else if (err == DB_LOCK_WAIT) {

		return(NULL);
	} else {
		return(NULL);
	}

	/* DO THE TRIGGER ACTIONS HERE */

	if (node->searched_update) {
		/* Fetch next row to update */

		thr->run_node = sel_node;
	} else {
		/* It was an explicit cursor update */

		thr->run_node = parent;
	}

	node->state = UPD_NODE_UPDATE_CLUSTERED;

	return(thr);
} 

/*************************************************************************
Performs an in-place update for the current clustered index record in
select. */

void
row_upd_in_place_in_select(
/*=======================*/
	sel_node_t*	sel_node,	/* in: select node */
	que_thr_t*	thr,		/* in: query thread */
	mtr_t*		mtr)		/* in: mtr */
{
	upd_node_t*	node;
	btr_pcur_t*	pcur;
	btr_cur_t*	btr_cur;
	ulint		err;
1997
	mem_heap_t*	heap		= NULL;
1998
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
1999
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014

	ut_ad(sel_node->select_will_do_update);
	ut_ad(sel_node->latch_mode == BTR_MODIFY_LEAF);
	ut_ad(sel_node->asc);

	node = que_node_get_parent(sel_node);

	ut_ad(que_node_get_type(node) == QUE_NODE_UPDATE);

	pcur = node->pcur;
	btr_cur = btr_pcur_get_btr_cur(pcur);

	/* Copy the necessary columns from clust_rec and calculate the new
	values to set */

unknown's avatar
unknown committed
2015
	row_upd_copy_columns(btr_pcur_get_rec(pcur), rec_get_offsets(
2016 2017
			btr_pcur_get_rec(pcur), btr_cur->index, offsets_,
			ULINT_UNDEFINED, &heap),
unknown's avatar
unknown committed
2018
		UT_LIST_GET_FIRST(node->columns));
2019
	if (UNIV_LIKELY_NULL(heap)) {
2020 2021
		mem_heap_free(heap);
	}
2022 2023
	row_upd_eval_new_vals(node->update);

unknown's avatar
unknown committed
2024 2025
	ut_ad(!rec_get_deleted_flag(btr_pcur_get_rec(pcur),
					btr_cur->index->table->comp));
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
	
	ut_ad(node->cmpl_info & UPD_NODE_NO_SIZE_CHANGE);
	ut_ad(node->cmpl_info & UPD_NODE_NO_ORD_CHANGE);
	ut_ad(node->select_will_do_update);

	err = btr_cur_update_in_place(BTR_NO_LOCKING_FLAG, btr_cur,
						node->update, node->cmpl_info,
						thr, mtr);
	ut_ad(err == DB_SUCCESS);
}