row0ins.c 65.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*****************************************************************************

Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

*****************************************************************************/

osku's avatar
osku committed
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
/******************************************************
Insert into a table

Created 4/20/1996 Heikki Tuuri
*******************************************************/

#include "row0ins.h"

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

#include "dict0dict.h"
#include "dict0boot.h"
#include "trx0undo.h"
#include "btr0btr.h"
#include "btr0cur.h"
#include "mach0data.h"
#include "que0que.h"
#include "row0upd.h"
#include "row0sel.h"
#include "row0row.h"
#include "rem0cmp.h"
#include "lock0lock.h"
#include "log0log.h"
#include "eval0eval.h"
#include "data0data.h"
#include "usr0sess.h"
47
#include "buf0lru.h"
osku's avatar
osku committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

#define	ROW_INS_PREV	1
#define	ROW_INS_NEXT	2


/*********************************************************************
This prototype is copied from /mysql/sql/ha_innodb.cc.
Invalidates the MySQL query cache for the table.
NOTE that the exact prototype of this function has to be in
/innobase/row/row0ins.c! */
extern
void
innobase_invalidate_query_cache(
/*============================*/
	trx_t*	trx,		/* in: transaction which modifies the table */
	char*	full_name,	/* in: concatenation of database name, null
				char '\0', table name, null char'\0';
				NOTE that in Windows this is always
				in LOWER CASE! */
	ulint	full_name_len);	/* in: full name length where also the null
				chars count */

/*************************************************************************
Creates an insert node struct. */
72
UNIV_INTERN
osku's avatar
osku committed
73 74 75 76 77
ins_node_t*
ins_node_create(
/*============*/
					/* out, own: insert node struct */
	ulint		ins_type,	/* in: INS_VALUES, ... */
78
	dict_table_t*	table,		/* in: table where to insert */
osku's avatar
osku committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	mem_heap_t*	heap)		/* in: mem heap where created */
{
	ins_node_t*	node;

	node = mem_heap_alloc(heap, sizeof(ins_node_t));

	node->common.type = QUE_NODE_INSERT;

	node->ins_type = ins_type;

	node->state = INS_NODE_SET_IX_LOCK;
	node->table = table;
	node->index = NULL;
	node->entry = NULL;

	node->select = NULL;
95

osku's avatar
osku committed
96
	node->trx_id = ut_dulint_zero;
97

osku's avatar
osku committed
98 99
	node->entry_sys_heap = mem_heap_create(128);

100 101
	node->magic_n = INS_NODE_MAGIC_N;

osku's avatar
osku committed
102 103 104 105 106
	return(node);
}

/***************************************************************
Creates an entry template for each index of a table. */
107
UNIV_INTERN
osku's avatar
osku committed
108 109 110 111 112 113 114 115 116 117 118 119 120
void
ins_node_create_entry_list(
/*=======================*/
	ins_node_t*	node)	/* in: row insert node */
{
	dict_index_t*	index;
	dtuple_t*	entry;

	ut_ad(node->entry_sys_heap);

	UT_LIST_INIT(node->entry_list);

	index = dict_table_get_first_index(node->table);
121

osku's avatar
osku committed
122
	while (index != NULL) {
123
		entry = row_build_index_entry(node->row, NULL, index,
124
					      node->entry_sys_heap);
osku's avatar
osku committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138
		UT_LIST_ADD_LAST(tuple_list, node->entry_list, entry);

		index = dict_table_get_next_index(index);
	}
}

/*********************************************************************
Adds system field buffers to a row. */
static
void
row_ins_alloc_sys_fields(
/*=====================*/
	ins_node_t*	node)	/* in: insert node */
{
139 140 141 142 143 144
	dtuple_t*		row;
	dict_table_t*		table;
	mem_heap_t*		heap;
	const dict_col_t*	col;
	dfield_t*		dfield;
	byte*			ptr;
osku's avatar
osku committed
145 146 147 148 149 150 151 152 153 154 155

	row = node->row;
	table = node->table;
	heap = node->entry_sys_heap;

	ut_ad(row && table && heap);
	ut_ad(dtuple_get_n_fields(row) == dict_table_get_n_cols(table));

	/* 1. Allocate buffer for row id */

	col = dict_table_get_sys_col(table, DATA_ROW_ID);
156

157
	dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
osku's avatar
osku committed
158 159

	ptr = mem_heap_alloc(heap, DATA_ROW_ID_LEN);
160

osku's avatar
osku committed
161 162 163 164 165 166 167
	dfield_set_data(dfield, ptr, DATA_ROW_ID_LEN);

	node->row_id_buf = ptr;

	/* 3. Allocate buffer for trx id */

	col = dict_table_get_sys_col(table, DATA_TRX_ID);
168

169
	dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
osku's avatar
osku committed
170
	ptr = mem_heap_alloc(heap, DATA_TRX_ID_LEN);
171

osku's avatar
osku committed
172 173 174 175 176 177 178
	dfield_set_data(dfield, ptr, DATA_TRX_ID_LEN);

	node->trx_id_buf = ptr;

	/* 4. Allocate buffer for roll ptr */

	col = dict_table_get_sys_col(table, DATA_ROLL_PTR);
179

180
	dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
osku's avatar
osku committed
181
	ptr = mem_heap_alloc(heap, DATA_ROLL_PTR_LEN);
182

osku's avatar
osku committed
183 184 185 186 187 188 189
	dfield_set_data(dfield, ptr, DATA_ROLL_PTR_LEN);
}

/*************************************************************************
Sets a new row to insert for an INS_DIRECT node. This function is only used
if we have constructed the row separately, which is a rare case; this
function is quite slow. */
190
UNIV_INTERN
osku's avatar
osku committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
void
ins_node_set_new_row(
/*=================*/
	ins_node_t*	node,	/* in: insert node */
	dtuple_t*	row)	/* in: new row (or first row) for the node */
{
	node->state = INS_NODE_SET_IX_LOCK;
	node->index = NULL;
	node->entry = NULL;

	node->row = row;

	mem_heap_empty(node->entry_sys_heap);

	/* Create templates for index entries */
206

osku's avatar
osku committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	ins_node_create_entry_list(node);

	/* Allocate from entry_sys_heap buffers for sys fields */

	row_ins_alloc_sys_fields(node);

	/* As we allocated a new trx id buf, the trx id should be written
	there again: */

	node->trx_id = ut_dulint_zero;
}

/***********************************************************************
Does an insert operation by updating a delete-marked existing record
in the index. This situation can occur if the delete-marked record is
kept in the index for consistent reads. */
static
ulint
row_ins_sec_index_entry_by_modify(
/*==============================*/
				/* out: DB_SUCCESS or error code */
	ulint		mode,	/* in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
				depending on whether mtr holds just a leaf
				latch or also a tree latch */
	btr_cur_t*	cursor,	/* in: B-tree cursor */
232
	const dtuple_t*	entry,	/* in: index entry to insert */
osku's avatar
osku committed
233
	que_thr_t*	thr,	/* in: query thread */
234 235
	mtr_t*		mtr)	/* in: mtr; must be committed before
				latching any further pages */
osku's avatar
osku committed
236 237 238 239 240 241
{
	big_rec_t*	dummy_big_rec;
	mem_heap_t*	heap;
	upd_t*		update;
	rec_t*		rec;
	ulint		err;
242

osku's avatar
osku committed
243
	rec = btr_cur_get_rec(cursor);
244

245
	ut_ad(!dict_index_is_clust(cursor->index));
246
	ut_ad(rec_get_deleted_flag(rec,
247
				   dict_table_is_comp(cursor->index->table)));
248

osku's avatar
osku committed
249 250 251 252
	/* We know that in the alphabetical ordering, entry and rec are
	identified. But in their binary form there may be differences if
	there are char fields in them. Therefore we have to calculate the
	difference. */
253

osku's avatar
osku committed
254
	heap = mem_heap_create(1024);
255

256 257
	update = row_upd_build_sec_rec_difference_binary(
		cursor->index, entry, rec, thr_get_trx(thr), heap);
osku's avatar
osku committed
258 259 260 261 262 263
	if (mode == BTR_MODIFY_LEAF) {
		/* Try an optimistic updating of the record, keeping changes
		within the page */

		err = btr_cur_optimistic_update(BTR_KEEP_SYS_FLAG, cursor,
						update, 0, thr, mtr);
264 265 266 267
		switch (err) {
		case DB_OVERFLOW:
		case DB_UNDERFLOW:
		case DB_ZIP_OVERFLOW:
osku's avatar
osku committed
268 269
			err = DB_FAIL;
		}
270
	} else {
osku's avatar
osku committed
271
		ut_a(mode == BTR_MODIFY_TREE);
272 273 274 275 276 277 278
		if (buf_LRU_buf_pool_running_out()) {

			err = DB_LOCK_TABLE_FULL;

			goto func_exit;
		}

osku's avatar
osku committed
279
		err = btr_cur_pessimistic_update(BTR_KEEP_SYS_FLAG, cursor,
280
						 &heap, &dummy_big_rec, update,
281
						 0, thr, mtr);
282
		ut_ad(!dummy_big_rec);
osku's avatar
osku committed
283
	}
284
func_exit:
osku's avatar
osku committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	mem_heap_free(heap);

	return(err);
}

/***********************************************************************
Does an insert operation by delete unmarking and updating a delete marked
existing record in the index. This situation can occur if the delete marked
record is kept in the index for consistent reads. */
static
ulint
row_ins_clust_index_entry_by_modify(
/*================================*/
				/* out: DB_SUCCESS, DB_FAIL, or error code */
	ulint		mode,	/* in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
				depending on whether mtr holds just a leaf
				latch or also a tree latch */
	btr_cur_t*	cursor,	/* in: B-tree cursor */
303
	mem_heap_t**	heap,	/* in/out: pointer to memory heap, or NULL */
osku's avatar
osku committed
304 305 306
	big_rec_t**	big_rec,/* out: possible big rec vector of fields
				which have to be stored externally by the
				caller */
307
	const dtuple_t*	entry,	/* in: index entry to insert */
osku's avatar
osku committed
308
	que_thr_t*	thr,	/* in: query thread */
309 310
	mtr_t*		mtr)	/* in: mtr; must be committed before
				latching any further pages */
osku's avatar
osku committed
311 312 313 314
{
	rec_t*		rec;
	upd_t*		update;
	ulint		err;
315

316
	ut_ad(dict_index_is_clust(cursor->index));
317

osku's avatar
osku committed
318 319 320 321
	*big_rec = NULL;

	rec = btr_cur_get_rec(cursor);

322
	ut_ad(rec_get_deleted_flag(rec,
323
				   dict_table_is_comp(cursor->index->table)));
osku's avatar
osku committed
324

325 326 327
	if (!*heap) {
		*heap = mem_heap_create(1024);
	}
328

osku's avatar
osku committed
329 330 331
	/* Build an update vector containing all the fields to be modified;
	NOTE that this vector may NOT contain system columns trx_id or
	roll_ptr */
332

333
	update = row_upd_build_difference_binary(cursor->index, entry, rec,
334
						 thr_get_trx(thr), *heap);
osku's avatar
osku committed
335 336 337 338 339
	if (mode == BTR_MODIFY_LEAF) {
		/* Try optimistic updating of the record, keeping changes
		within the page */

		err = btr_cur_optimistic_update(0, cursor, update, 0, thr,
340
						mtr);
341 342 343 344
		switch (err) {
		case DB_OVERFLOW:
		case DB_UNDERFLOW:
		case DB_ZIP_OVERFLOW:
osku's avatar
osku committed
345 346
			err = DB_FAIL;
		}
347
	} else {
osku's avatar
osku committed
348
		ut_a(mode == BTR_MODIFY_TREE);
349 350
		if (buf_LRU_buf_pool_running_out()) {

351
			return(DB_LOCK_TABLE_FULL);
352 353

		}
354 355
		err = btr_cur_pessimistic_update(0, cursor,
						 heap, big_rec, update,
356
						 0, thr, mtr);
osku's avatar
osku committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
	}

	return(err);
}

/*************************************************************************
Returns TRUE if in a cascaded update/delete an ancestor node of node
updates (not DELETE, but UPDATE) table. */
static
ibool
row_ins_cascade_ancestor_updates_table(
/*===================================*/
				/* out: TRUE if an ancestor updates table */
	que_node_t*	node,	/* in: node in a query graph */
	dict_table_t*	table)	/* in: table */
{
	que_node_t*	parent;
	upd_node_t*	upd_node;

	parent = que_node_get_parent(node);
377

osku's avatar
osku committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	while (que_node_get_type(parent) == QUE_NODE_UPDATE) {

		upd_node = parent;

		if (upd_node->table == table && upd_node->is_delete == FALSE) {

			return(TRUE);
		}

		parent = que_node_get_parent(parent);

		ut_a(parent);
	}

	return(FALSE);
}
394

osku's avatar
osku committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408
/*************************************************************************
Returns the number of ancestor UPDATE or DELETE nodes of a
cascaded update/delete node. */
static
ulint
row_ins_cascade_n_ancestors(
/*========================*/
				/* out: number of ancestors */
	que_node_t*	node)	/* in: node in a query graph */
{
	que_node_t*	parent;
	ulint		n_ancestors = 0;

	parent = que_node_get_parent(node);
409

osku's avatar
osku committed
410 411 412 413 414 415 416 417 418 419
	while (que_node_get_type(parent) == QUE_NODE_UPDATE) {
		n_ancestors++;

		parent = que_node_get_parent(parent);

		ut_a(parent);
	}

	return(n_ancestors);
}
420

osku's avatar
osku committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
/**********************************************************************
Calculates the update vector node->cascade->update for a child table in
a cascaded update. */
static
ulint
row_ins_cascade_calc_update_vec(
/*============================*/
					/* out: number of fields in the
					calculated update vector; the value
					can also be 0 if no foreign key
					fields changed; the returned value
					is ULINT_UNDEFINED if the column
					type in the child table is too short
					to fit the new value in the parent
					table: that means the update fails */
	upd_node_t*	node,		/* in: update node of the parent
					table */
	dict_foreign_t*	foreign,	/* in: foreign key constraint whose
					type is != 0 */
	mem_heap_t*	heap)		/* in: memory heap to use as
					temporary storage */
{
	upd_node_t*	cascade		= node->cascade_node;
	dict_table_t*	table		= foreign->foreign_table;
	dict_index_t*	index		= foreign->foreign_index;
	upd_t*		update;
	upd_field_t*	ufield;
	dict_table_t*	parent_table;
	dict_index_t*	parent_index;
	upd_t*		parent_update;
	upd_field_t*	parent_ufield;
	ulint		n_fields_updated;
453
	ulint		parent_field_no;
osku's avatar
osku committed
454 455
	ulint		i;
	ulint		j;
456

457 458 459 460 461
	ut_a(node);
	ut_a(foreign);
	ut_a(cascade);
	ut_a(table);
	ut_a(index);
osku's avatar
osku committed
462 463

	/* Calculate the appropriate update vector which will set the fields
464
	in the child index record to the same value (possibly padded with
osku's avatar
osku committed
465 466 467 468 469 470 471
	spaces if the column is a fixed length CHAR or FIXBINARY column) as
	the referenced index record will get in the update. */

	parent_table = node->table;
	ut_a(parent_table == foreign->referenced_table);
	parent_index = foreign->referenced_index;
	parent_update = node->update;
472

osku's avatar
osku committed
473 474 475 476
	update = cascade->update;

	update->info_bits = 0;
	update->n_fields = foreign->n_fields;
477

osku's avatar
osku committed
478 479 480 481
	n_fields_updated = 0;

	for (i = 0; i < foreign->n_fields; i++) {

482 483 484
		parent_field_no = dict_table_get_nth_col_pos(
			parent_table,
			dict_index_get_nth_col_no(parent_index, i));
osku's avatar
osku committed
485 486 487

		for (j = 0; j < parent_update->n_fields; j++) {
			parent_ufield = parent_update->fields + j;
488

osku's avatar
osku committed
489 490
			if (parent_ufield->field_no == parent_field_no) {

491 492
				ulint			min_size;
				const dict_col_t*	col;
493
				ulint			ufield_len;
494 495

				col = dict_index_get_nth_col(index, i);
osku's avatar
osku committed
496 497 498 499 500

				/* A field in the parent index record is
				updated. Let us make the update vector
				field for the child table. */

501
				ufield = update->fields + n_fields_updated;
osku's avatar
osku committed
502

503
				ufield->field_no
504 505
					= dict_table_get_nth_col_pos(
					table, dict_col_get_no(col));
osku's avatar
osku committed
506 507 508
				ufield->exp = NULL;

				ufield->new_val = parent_ufield->new_val;
509 510 511 512
				ufield_len = dfield_get_len(&ufield->new_val);

				/* Clear the "external storage" flag */
				dfield_set_len(&ufield->new_val, ufield_len);
osku's avatar
osku committed
513 514 515 516

				/* Do not allow a NOT NULL column to be
				updated as NULL */

517
				if (dfield_is_null(&ufield->new_val)
518
				    && (col->prtype & DATA_NOT_NULL)) {
osku's avatar
osku committed
519

520
					return(ULINT_UNDEFINED);
osku's avatar
osku committed
521 522 523 524 525
				}

				/* If the new value would not fit in the
				column, do not allow the update */

526
				if (!dfield_is_null(&ufield->new_val)
527 528 529 530
				    && dtype_get_at_most_n_mbchars(
					col->prtype,
					col->mbminlen, col->mbmaxlen,
					col->len,
531 532 533
					ufield_len,
					dfield_get_data(&ufield->new_val))
				    < ufield_len) {
osku's avatar
osku committed
534 535 536 537 538 539 540 541 542

					return(ULINT_UNDEFINED);
				}

				/* If the parent column type has a different
				length than the child column type, we may
				need to pad with spaces the new value of the
				child column */

543
				min_size = dict_col_get_min_size(col);
osku's avatar
osku committed
544

545 546 547 548 549 550
				/* Because UNIV_SQL_NULL (the marker
				of SQL NULL values) exceeds all possible
				values of min_size, the test below will
				not hold for SQL NULL columns. */

				if (min_size > ufield_len) {
osku's avatar
osku committed
551 552 553

					char*		pad_start;
					const char*	pad_end;
554 555 556 557 558 559 560 561 562 563 564
					char*		padded_data
						= mem_heap_alloc(
							heap, min_size);
					pad_start = padded_data + ufield_len;
					pad_end = padded_data + min_size;

					memcpy(padded_data,
					       dfield_get_data(&ufield
							       ->new_val),
					       dfield_get_len(&ufield
							      ->new_val));
osku's avatar
osku committed
565

566
					switch (UNIV_EXPECT(col->mbminlen,1)) {
osku's avatar
osku committed
567 568
					default:
						ut_error;
569
						return(ULINT_UNDEFINED);
osku's avatar
osku committed
570
					case 1:
571
						if (UNIV_UNLIKELY
572 573
						    (dtype_get_charset_coll(
							    col->prtype)
574
						     == DATA_MYSQL_BINARY_CHARSET_COLL)) {
575 576 577
							/* Do not pad BINARY
							columns. */
							return(ULINT_UNDEFINED);
578 579
						}

osku's avatar
osku committed
580 581
						/* space=0x20 */
						memset(pad_start, 0x20,
582
						       pad_end - pad_start);
osku's avatar
osku committed
583 584 585
						break;
					case 2:
						/* space=0x0020 */
586
						ut_a(!(ufield_len % 2));
osku's avatar
osku committed
587 588 589 590 591 592 593
						ut_a(!(min_size % 2));
						do {
							*pad_start++ = 0x00;
							*pad_start++ = 0x20;
						} while (pad_start < pad_end);
						break;
					}
594 595 596

					dfield_set_data(&ufield->new_val,
							padded_data, min_size);
osku's avatar
osku committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
				}

				n_fields_updated++;
			}
		}
	}

	update->n_fields = n_fields_updated;

	return(n_fields_updated);
}

/*************************************************************************
Set detailed error message associated with foreign key errors for
the given transaction. */
static
void
row_ins_set_detailed(
/*=================*/
	trx_t*		trx,		/* in: transaction */
	dict_foreign_t*	foreign)	/* in: foreign key constraint */
{
619 620 621 622
	mutex_enter(&srv_misc_tmpfile_mutex);
	rewind(srv_misc_tmpfile);

	if (os_file_set_eof(srv_misc_tmpfile)) {
623
		ut_print_name(srv_misc_tmpfile, trx, TRUE,
624
			      foreign->foreign_table_name);
625 626
		dict_print_info_on_foreign_key_in_create_format(
			srv_misc_tmpfile, trx, foreign, FALSE);
627
		trx_set_detailed_error_from_file(trx, srv_misc_tmpfile);
osku's avatar
osku committed
628
	} else {
629
		trx_set_detailed_error(trx, "temp file operation failed");
osku's avatar
osku committed
630
	}
631 632

	mutex_exit(&srv_misc_tmpfile_mutex);
osku's avatar
osku committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646
}

/*************************************************************************
Reports a foreign key error associated with an update or a delete of a
parent table index entry. */
static
void
row_ins_foreign_report_err(
/*=======================*/
	const char*	errstr,		/* in: error string from the viewpoint
					of the parent table */
	que_thr_t*	thr,		/* in: query thread whose run_node
					is an update node */
	dict_foreign_t*	foreign,	/* in: foreign key constraint */
647
	const rec_t*	rec,		/* in: a matching index record in the
osku's avatar
osku committed
648
					child table */
649
	const dtuple_t*	entry)		/* in: index entry in the parent
osku's avatar
osku committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663
					table */
{
	FILE*	ef	= dict_foreign_err_file;
	trx_t*	trx	= thr_get_trx(thr);

	row_ins_set_detailed(trx, foreign);

	mutex_enter(&dict_foreign_err_mutex);
	rewind(ef);
	ut_print_timestamp(ef);
	fputs(" Transaction:\n", ef);
	trx_print(ef, trx, 600);

	fputs("Foreign key constraint fails for table ", ef);
664
	ut_print_name(ef, trx, TRUE, foreign->foreign_table_name);
osku's avatar
osku committed
665 666
	fputs(":\n", ef);
	dict_print_info_on_foreign_key_in_create_format(ef, trx, foreign,
667
							TRUE);
osku's avatar
osku committed
668 669 670
	putc('\n', ef);
	fputs(errstr, ef);
	fputs(" in parent table, in index ", ef);
671
	ut_print_name(ef, trx, FALSE, foreign->referenced_index->name);
osku's avatar
osku committed
672 673 674 675 676
	if (entry) {
		fputs(" tuple:\n", ef);
		dtuple_print(ef, entry);
	}
	fputs("\nBut in child table ", ef);
677
	ut_print_name(ef, trx, TRUE, foreign->foreign_table_name);
osku's avatar
osku committed
678
	fputs(", in index ", ef);
679
	ut_print_name(ef, trx, FALSE, foreign->foreign_index->name);
osku's avatar
osku committed
680 681 682 683 684 685 686 687 688 689 690 691
	if (rec) {
		fputs(", there is a record:\n", ef);
		rec_print(ef, rec, foreign->foreign_index);
	} else {
		fputs(", the record is not available\n", ef);
	}
	putc('\n', ef);

	mutex_exit(&dict_foreign_err_mutex);
}

/*************************************************************************
692
Reports a foreign key error to dict_foreign_err_file when we are trying
osku's avatar
osku committed
693 694 695 696 697 698 699 700
to add an index entry to a child table. Note that the adding may be the result
of an update, too. */
static
void
row_ins_foreign_report_add_err(
/*===========================*/
	trx_t*		trx,		/* in: transaction */
	dict_foreign_t*	foreign,	/* in: foreign key constraint */
701
	const rec_t*	rec,		/* in: a record in the parent table:
osku's avatar
osku committed
702 703
					it does not match entry because we
					have an error! */
704
	const dtuple_t*	entry)		/* in: index entry to insert in the
osku's avatar
osku committed
705 706 707
					child table */
{
	FILE*	ef	= dict_foreign_err_file;
708

osku's avatar
osku committed
709
	row_ins_set_detailed(trx, foreign);
710

osku's avatar
osku committed
711 712 713 714 715 716
	mutex_enter(&dict_foreign_err_mutex);
	rewind(ef);
	ut_print_timestamp(ef);
	fputs(" Transaction:\n", ef);
	trx_print(ef, trx, 600);
	fputs("Foreign key constraint fails for table ", ef);
717
	ut_print_name(ef, trx, TRUE, foreign->foreign_table_name);
osku's avatar
osku committed
718 719
	fputs(":\n", ef);
	dict_print_info_on_foreign_key_in_create_format(ef, trx, foreign,
720
							TRUE);
osku's avatar
osku committed
721
	fputs("\nTrying to add in child table, in index ", ef);
722
	ut_print_name(ef, trx, FALSE, foreign->foreign_index->name);
osku's avatar
osku committed
723 724
	if (entry) {
		fputs(" tuple:\n", ef);
725 726
		/* TODO: DB_TRX_ID and DB_ROLL_PTR may be uninitialized.
		It would be better to only display the user columns. */
osku's avatar
osku committed
727 728 729
		dtuple_print(ef, entry);
	}
	fputs("\nBut in parent table ", ef);
730
	ut_print_name(ef, trx, TRUE, foreign->referenced_table_name);
osku's avatar
osku committed
731
	fputs(", in index ", ef);
732
	ut_print_name(ef, trx, FALSE, foreign->referenced_index->name);
osku's avatar
osku committed
733 734 735 736 737
	fputs(",\nthe closest match we can find is record:\n", ef);
	if (rec && page_rec_is_supremum(rec)) {
		/* If the cursor ended on a supremum record, it is better
		to report the previous record in the error message, so that
		the user gets a more descriptive error message. */
738
		rec = page_rec_get_prev_const(rec);
osku's avatar
osku committed
739 740 741
	}

	if (rec) {
742
		rec_print(ef, rec, foreign->referenced_index);
osku's avatar
osku committed
743 744 745 746 747 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 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
	}
	putc('\n', ef);

	mutex_exit(&dict_foreign_err_mutex);
}

/*************************************************************************
Invalidate the query cache for the given table. */
static
void
row_ins_invalidate_query_cache(
/*===========================*/
	que_thr_t*	thr,		/* in: query thread whose run_node
					is an update node */
	const char*	name)		/* in: table name prefixed with
					database name and a '/' character */
{
	char*	buf;
	char*	ptr;
	ulint	len = strlen(name) + 1;

	buf = mem_strdupl(name, len);

	ptr = strchr(buf, '/');
	ut_a(ptr);
	*ptr = '\0';

	/* We call a function in ha_innodb.cc */
#ifndef UNIV_HOTBACKUP
	innobase_invalidate_query_cache(thr_get_trx(thr), buf, len);
#endif
	mem_free(buf);
}

/*************************************************************************
Perform referential actions or checks when a parent row is deleted or updated
and the constraint had an ON DELETE or ON UPDATE condition which was not
RESTRICT. */
static
ulint
row_ins_foreign_check_on_constraint(
/*================================*/
					/* out: DB_SUCCESS, DB_LOCK_WAIT,
					or error code */
	que_thr_t*	thr,		/* in: query thread whose run_node
					is an update node */
	dict_foreign_t*	foreign,	/* in: foreign key constraint whose
					type is != 0 */
	btr_pcur_t*	pcur,		/* in: cursor placed on a matching
					index record in the child table */
	dtuple_t*	entry,		/* in: index entry in the parent
					table */
	mtr_t*		mtr)		/* in: mtr holding the latch of pcur
					page */
{
	upd_node_t*	node;
	upd_node_t*	cascade;
	dict_table_t*	table		= foreign->foreign_table;
	dict_index_t*	index;
	dict_index_t*	clust_index;
	dtuple_t*	ref;
	mem_heap_t*	upd_vec_heap	= NULL;
805 806 807
	const rec_t*	rec;
	const rec_t*	clust_rec;
	const buf_block_t* clust_block;
osku's avatar
osku committed
808 809 810 811 812 813 814
	upd_t*		update;
	ulint		n_to_update;
	ulint		err;
	ulint		i;
	trx_t*		trx;
	mem_heap_t*	tmp_heap	= NULL;

815 816 817 818
	ut_a(thr);
	ut_a(foreign);
	ut_a(pcur);
	ut_a(mtr);
osku's avatar
osku committed
819 820 821 822 823 824 825 826 827 828 829 830 831

	trx = thr_get_trx(thr);

	/* Since we are going to delete or update a row, we have to invalidate
	the MySQL query cache for table. A deadlock of threads is not possible
	here because the caller of this function does not hold any latches with
	the sync0sync.h rank above the kernel mutex. The query cache mutex has
	a rank just above the kernel mutex. */

	row_ins_invalidate_query_cache(thr, table->name);

	node = thr->run_node;

832 833 834
	if (node->is_delete && 0 == (foreign->type
				     & (DICT_FOREIGN_ON_DELETE_CASCADE
					| DICT_FOREIGN_ON_DELETE_SET_NULL))) {
osku's avatar
osku committed
835 836

		row_ins_foreign_report_err("Trying to delete",
837 838
					   thr, foreign,
					   btr_pcur_get_rec(pcur), entry);
osku's avatar
osku committed
839

840
		return(DB_ROW_IS_REFERENCED);
osku's avatar
osku committed
841 842
	}

843 844 845
	if (!node->is_delete && 0 == (foreign->type
				      & (DICT_FOREIGN_ON_UPDATE_CASCADE
					 | DICT_FOREIGN_ON_UPDATE_SET_NULL))) {
osku's avatar
osku committed
846 847

		/* This is an UPDATE */
848

osku's avatar
osku committed
849
		row_ins_foreign_report_err("Trying to update",
850 851
					   thr, foreign,
					   btr_pcur_get_rec(pcur), entry);
osku's avatar
osku committed
852

853
		return(DB_ROW_IS_REFERENCED);
osku's avatar
osku committed
854 855 856 857 858 859 860 861
	}

	if (node->cascade_node == NULL) {
		/* Extend our query graph by creating a child to current
		update node. The child is used in the cascade or set null
		operation. */

		node->cascade_heap = mem_heap_create(128);
862 863
		node->cascade_node = row_create_update_node_for_mysql(
			table, node->cascade_heap);
osku's avatar
osku committed
864 865 866 867 868 869 870 871 872
		que_node_set_parent(node->cascade_node, node);
	}

	/* Initialize cascade_node to do the operation we want. Note that we
	use the SAME cascade node to do all foreign key operations of the
	SQL DELETE: the table of the cascade node may change if there are
	several child tables to the table where the delete is done! */

	cascade = node->cascade_node;
873

osku's avatar
osku committed
874 875 876
	cascade->table = table;

	cascade->foreign = foreign;
877

osku's avatar
osku committed
878
	if (node->is_delete
879
	    && (foreign->type & DICT_FOREIGN_ON_DELETE_CASCADE)) {
osku's avatar
osku committed
880 881 882 883 884 885 886 887
		cascade->is_delete = TRUE;
	} else {
		cascade->is_delete = FALSE;

		if (foreign->n_fields > cascade->update_n_fields) {
			/* We have to make the update vector longer */

			cascade->update = upd_create(foreign->n_fields,
888
						     node->cascade_heap);
osku's avatar
osku committed
889 890 891 892 893 894 895 896 897 898 899 900 901
			cascade->update_n_fields = foreign->n_fields;
		}
	}

	/* We do not allow cyclic cascaded updating (DELETE is allowed,
	but not UPDATE) of the same table, as this can lead to an infinite
	cycle. Check that we are not updating the same table which is
	already being modified in this cascade chain. We have to check
	this also because the modification of the indexes of a 'parent'
	table may still be incomplete, and we must avoid seeing the indexes
	of the parent table in an inconsistent state! */

	if (!cascade->is_delete
902
	    && row_ins_cascade_ancestor_updates_table(cascade, table)) {
osku's avatar
osku committed
903

904 905
		/* We do not know if this would break foreign key
		constraints, but play safe and return an error */
osku's avatar
osku committed
906

907
		err = DB_ROW_IS_REFERENCED;
osku's avatar
osku committed
908

909 910 911 912 913
		row_ins_foreign_report_err(
			"Trying an update, possibly causing a cyclic"
			" cascaded update\n"
			"in the child table,", thr, foreign,
			btr_pcur_get_rec(pcur), entry);
osku's avatar
osku committed
914 915 916 917 918 919 920

		goto nonstandard_exit_func;
	}

	if (row_ins_cascade_n_ancestors(cascade) >= 15) {
		err = DB_ROW_IS_REFERENCED;

921 922 923
		row_ins_foreign_report_err(
			"Trying a too deep cascaded delete or update\n",
			thr, foreign, btr_pcur_get_rec(pcur), entry);
osku's avatar
osku committed
924 925 926 927 928 929 930

		goto nonstandard_exit_func;
	}

	index = btr_pcur_get_btr_cur(pcur)->index;

	ut_a(index == foreign->foreign_index);
931

osku's avatar
osku committed
932 933
	rec = btr_pcur_get_rec(pcur);

934
	if (dict_index_is_clust(index)) {
osku's avatar
osku committed
935 936
		/* pcur is already positioned in the clustered index of
		the child table */
937

osku's avatar
osku committed
938 939
		clust_index = index;
		clust_rec = rec;
940
		clust_block = btr_pcur_get_block(pcur);
osku's avatar
osku committed
941 942 943 944 945 946 947
	} else {
		/* We have to look for the record in the clustered index
		in the child table */

		clust_index = dict_table_get_first_index(table);

		tmp_heap = mem_heap_create(256);
948

osku's avatar
osku committed
949
		ref = row_build_row_ref(ROW_COPY_POINTERS, index, rec,
950
					tmp_heap);
osku's avatar
osku committed
951
		btr_pcur_open_with_no_init(clust_index, ref,
952 953
					   PAGE_CUR_LE, BTR_SEARCH_LEAF,
					   cascade->pcur, 0, mtr);
osku's avatar
osku committed
954 955

		clust_rec = btr_pcur_get_rec(cascade->pcur);
956
		clust_block = btr_pcur_get_block(cascade->pcur);
osku's avatar
osku committed
957 958

		if (!page_rec_is_user_rec(clust_rec)
959 960
		    || btr_pcur_get_low_match(cascade->pcur)
		    < dict_index_get_n_unique(clust_index)) {
osku's avatar
osku committed
961

962 963
			fputs("InnoDB: error in cascade of a foreign key op\n"
			      "InnoDB: ", stderr);
osku's avatar
osku committed
964 965 966
			dict_index_name_print(stderr, trx, index);

			fputs("\n"
967
			      "InnoDB: record ", stderr);
osku's avatar
osku committed
968 969
			rec_print(stderr, rec, index);
			fputs("\n"
970
			      "InnoDB: clustered record ", stderr);
osku's avatar
osku committed
971 972
			rec_print(stderr, clust_rec, clust_index);
			fputs("\n"
973 974
			      "InnoDB: Submit a detailed bug report to"
			      " http://bugs.mysql.com\n", stderr);
osku's avatar
osku committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990

			err = DB_SUCCESS;

			goto nonstandard_exit_func;
		}
	}

	/* Set an X-lock on the row to delete or update in the child table */

	err = lock_table(0, table, LOCK_IX, thr);

	if (err == DB_SUCCESS) {
		/* Here it suffices to use a LOCK_REC_NOT_GAP type lock;
		we already have a normal shared lock on the appropriate
		gap if the search criterion was not unique */

991
		err = lock_clust_rec_read_check_and_lock_alt(
992 993
			0, clust_block, clust_rec, clust_index,
			LOCK_X, LOCK_REC_NOT_GAP, thr);
osku's avatar
osku committed
994
	}
995

osku's avatar
osku committed
996 997 998 999 1000
	if (err != DB_SUCCESS) {

		goto nonstandard_exit_func;
	}

1001
	if (rec_get_deleted_flag(clust_rec, dict_table_is_comp(table))) {
osku's avatar
osku committed
1002 1003 1004
		/* This can happen if there is a circular reference of
		rows such that cascading delete comes to delete a row
		already in the process of being delete marked */
1005
		err = DB_SUCCESS;
osku's avatar
osku committed
1006 1007 1008 1009 1010

		goto nonstandard_exit_func;
	}

	if ((node->is_delete
1011 1012 1013
	     && (foreign->type & DICT_FOREIGN_ON_DELETE_SET_NULL))
	    || (!node->is_delete
		&& (foreign->type & DICT_FOREIGN_ON_UPDATE_SET_NULL))) {
1014

osku's avatar
osku committed
1015 1016 1017 1018 1019 1020 1021
		/* Build the appropriate update vector which sets
		foreign->n_fields first fields in rec to SQL NULL */

		update = cascade->update;

		update->info_bits = 0;
		update->n_fields = foreign->n_fields;
1022

osku's avatar
osku committed
1023
		for (i = 0; i < foreign->n_fields; i++) {
1024 1025 1026 1027 1028 1029 1030 1031
			upd_field_t*	ufield = &update->fields[i];

			ufield->field_no = dict_table_get_nth_col_pos(
				table,
				dict_index_get_nth_col_no(index, i));
			ufield->orig_len = 0;
			ufield->exp = NULL;
			dfield_set_null(&ufield->new_val);
osku's avatar
osku committed
1032 1033 1034 1035
		}
	}

	if (!node->is_delete
1036
	    && (foreign->type & DICT_FOREIGN_ON_UPDATE_CASCADE)) {
osku's avatar
osku committed
1037 1038 1039 1040 1041 1042 1043

		/* Build the appropriate update vector which sets changing
		foreign->n_fields first fields in rec to new values */

		upd_vec_heap = mem_heap_create(256);

		n_to_update = row_ins_cascade_calc_update_vec(node, foreign,
1044
							      upd_vec_heap);
osku's avatar
osku committed
1045
		if (n_to_update == ULINT_UNDEFINED) {
1046
			err = DB_ROW_IS_REFERENCED;
osku's avatar
osku committed
1047

1048 1049 1050 1051 1052 1053 1054 1055
			row_ins_foreign_report_err(
				"Trying a cascaded update where the"
				" updated value in the child\n"
				"table would not fit in the length"
				" of the column, or the value would\n"
				"be NULL and the column is"
				" declared as not NULL in the child table,",
				thr, foreign, btr_pcur_get_rec(pcur), entry);
osku's avatar
osku committed
1056

1057
			goto nonstandard_exit_func;
osku's avatar
osku committed
1058 1059 1060 1061 1062 1063 1064 1065
		}

		if (cascade->update->n_fields == 0) {

			/* The update does not change any columns referred
			to in this foreign key constraint: no need to do
			anything */

1066
			err = DB_SUCCESS;
osku's avatar
osku committed
1067

1068
			goto nonstandard_exit_func;
osku's avatar
osku committed
1069 1070
		}
	}
1071

osku's avatar
osku committed
1072 1073
	/* Store pcur position and initialize or store the cascade node
	pcur stored position */
1074

osku's avatar
osku committed
1075
	btr_pcur_store_position(pcur, mtr);
1076

osku's avatar
osku committed
1077 1078 1079 1080 1081
	if (index == clust_index) {
		btr_pcur_copy_stored_position(cascade->pcur, pcur);
	} else {
		btr_pcur_store_position(cascade->pcur, mtr);
	}
1082

osku's avatar
osku committed
1083 1084 1085 1086 1087
	mtr_commit(mtr);

	ut_a(cascade->pcur->rel_pos == BTR_PCUR_ON);

	cascade->state = UPD_NODE_UPDATE_CLUSTERED;
1088

osku's avatar
osku committed
1089
	err = row_update_cascade_for_mysql(thr, cascade,
1090
					   foreign->foreign_table);
osku's avatar
osku committed
1091 1092 1093

	if (foreign->foreign_table->n_foreign_key_checks_running == 0) {
		fprintf(stderr,
1094 1095 1096
			"InnoDB: error: table %s has the counter 0"
			" though there is\n"
			"InnoDB: a FOREIGN KEY check running on it.\n",
osku's avatar
osku committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
			foreign->foreign_table->name);
	}

	/* Release the data dictionary latch for a while, so that we do not
	starve other threads from doing CREATE TABLE etc. if we have a huge
	cascaded operation running. The counter n_foreign_key_checks_running
	will prevent other users from dropping or ALTERing the table when we
	release the latch. */

	row_mysql_unfreeze_data_dictionary(thr_get_trx(thr));
	row_mysql_freeze_data_dictionary(thr_get_trx(thr));

	mtr_start(mtr);

	/* Restore pcur position */
1112

osku's avatar
osku committed
1113 1114 1115 1116 1117 1118 1119
	btr_pcur_restore_position(BTR_SEARCH_LEAF, pcur, mtr);

	if (tmp_heap) {
		mem_heap_free(tmp_heap);
	}

	if (upd_vec_heap) {
1120
		mem_heap_free(upd_vec_heap);
osku's avatar
osku committed
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	}

	return(err);

nonstandard_exit_func:
	if (tmp_heap) {
		mem_heap_free(tmp_heap);
	}

	if (upd_vec_heap) {
1131
		mem_heap_free(upd_vec_heap);
osku's avatar
osku committed
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
	}

	btr_pcur_store_position(pcur, mtr);

	mtr_commit(mtr);
	mtr_start(mtr);

	btr_pcur_restore_position(BTR_SEARCH_LEAF, pcur, mtr);

	return(err);
}

/*************************************************************************
Sets a shared lock on a record. Used in locking possible duplicate key
records and also in checking foreign key constraints. */
static
ulint
row_ins_set_shared_rec_lock(
/*========================*/
1151 1152 1153 1154 1155 1156 1157 1158
					/* out: DB_SUCCESS or error code */
	ulint			type,	/* in: LOCK_ORDINARY, LOCK_GAP, or
					LOCK_REC_NOT_GAP type lock */
	const buf_block_t*	block,	/* in: buffer block of rec */
	const rec_t*		rec,	/* in: record */
	dict_index_t*		index,	/* in: index */
	const ulint*		offsets,/* in: rec_get_offsets(rec, index) */
	que_thr_t*		thr)	/* in: query thread */
osku's avatar
osku committed
1159 1160 1161 1162 1163
{
	ulint	err;

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

1164
	if (dict_index_is_clust(index)) {
1165
		err = lock_clust_rec_read_check_and_lock(
1166
			0, block, rec, index, offsets, LOCK_S, type, thr);
osku's avatar
osku committed
1167
	} else {
1168
		err = lock_sec_rec_read_check_and_lock(
1169
			0, block, rec, index, offsets, LOCK_S, type, thr);
osku's avatar
osku committed
1170 1171 1172 1173 1174
	}

	return(err);
}

1175
#ifndef UNIV_HOTBACKUP
osku's avatar
osku committed
1176 1177 1178 1179 1180 1181
/*************************************************************************
Sets a exclusive lock on a record. Used in locking possible duplicate key
records */
static
ulint
row_ins_set_exclusive_rec_lock(
1182
/*===========================*/
1183 1184 1185 1186 1187 1188 1189 1190
					/* out: DB_SUCCESS or error code */
	ulint			type,	/* in: LOCK_ORDINARY, LOCK_GAP, or
					LOCK_REC_NOT_GAP type lock */
	const buf_block_t*	block,	/* in: buffer block of rec */
	const rec_t*		rec,	/* in: record */
	dict_index_t*		index,	/* in: index */
	const ulint*		offsets,/* in: rec_get_offsets(rec, index) */
	que_thr_t*		thr)	/* in: query thread */
osku's avatar
osku committed
1191 1192 1193 1194 1195
{
	ulint	err;

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

1196
	if (dict_index_is_clust(index)) {
1197
		err = lock_clust_rec_read_check_and_lock(
1198
			0, block, rec, index, offsets, LOCK_X, type, thr);
osku's avatar
osku committed
1199
	} else {
1200
		err = lock_sec_rec_read_check_and_lock(
1201
			0, block, rec, index, offsets, LOCK_X, type, thr);
osku's avatar
osku committed
1202 1203 1204 1205
	}

	return(err);
}
1206 1207
#endif /* !UNIV_HOTBACKUP */

osku's avatar
osku committed
1208 1209 1210 1211
/*******************************************************************
Checks if foreign key constraint fails for an index entry. Sets shared locks
which lock either the success or the failure of the constraint. NOTE that
the caller must have a shared latch on dict_operation_lock. */
1212
UNIV_INTERN
osku's avatar
osku committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
ulint
row_ins_check_foreign_constraint(
/*=============================*/
				/* out: DB_SUCCESS,
				DB_NO_REFERENCED_ROW,
				or DB_ROW_IS_REFERENCED */
	ibool		check_ref,/* in: TRUE if we want to check that
				the referenced table is ok, FALSE if we
				want to to check the foreign key table */
	dict_foreign_t*	foreign,/* in: foreign constraint; NOTE that the
				tables mentioned in it must be in the
				dictionary cache if they exist at all */
	dict_table_t*	table,	/* in: if check_ref is TRUE, then the foreign
				table, else the referenced table */
	dtuple_t*	entry,	/* in: index entry for index */
	que_thr_t*	thr)	/* in: query thread */
{
1230
	upd_node_t*	upd_node;
osku's avatar
osku committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	dict_table_t*	check_table;
	dict_index_t*	check_index;
	ulint		n_fields_cmp;
	btr_pcur_t	pcur;
	ibool		moved;
	int		cmp;
	ulint		err;
	ulint		i;
	mtr_t		mtr;
	trx_t*		trx		= thr_get_trx(thr);
	mem_heap_t*	heap		= NULL;
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
	ulint*		offsets		= offsets_;
1244
	rec_offs_init(offsets_);
osku's avatar
osku committed
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263

run_again:
#ifdef UNIV_SYNC_DEBUG
	ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_SHARED));
#endif /* UNIV_SYNC_DEBUG */

	err = DB_SUCCESS;

	if (trx->check_foreigns == FALSE) {
		/* The user has suppressed foreign key checks currently for
		this session */
		goto exit_func;
	}

	/* If any of the foreign key fields in entry is SQL NULL, we
	suppress the foreign key check: this is compatible with Oracle,
	for example */

	for (i = 0; i < foreign->n_fields; i++) {
1264 1265
		if (UNIV_SQL_NULL == dfield_get_len(
			    dtuple_get_nth_field(entry, i))) {
osku's avatar
osku committed
1266 1267 1268 1269 1270 1271

			goto exit_func;
		}
	}

	if (que_node_get_type(thr->run_node) == QUE_NODE_UPDATE) {
1272
		upd_node = thr->run_node;
osku's avatar
osku committed
1273

1274 1275
		if (!(upd_node->is_delete) && upd_node->foreign == foreign) {
			/* If a cascaded update is done as defined by a
osku's avatar
osku committed
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
			foreign key constraint, do not check that
			constraint for the child row. In ON UPDATE CASCADE
			the update of the parent row is only half done when
			we come here: if we would check the constraint here
			for the child row it would fail.

			A QUESTION remains: if in the child table there are
			several constraints which refer to the same parent
			table, we should merge all updates to the child as
			one update? And the updates can be contradictory!
			Currently we just perform the update associated
			with each foreign key constraint, one after
			another, and the user has problems predicting in
			which order they are performed. */

			goto exit_func;
		}
	}

	if (check_ref) {
		check_table = foreign->referenced_table;
		check_index = foreign->referenced_index;
	} else {
		check_table = foreign->foreign_table;
		check_index = foreign->foreign_index;
	}

	if (check_table == NULL || check_table->ibd_file_missing) {
		if (check_ref) {
			FILE*	ef = dict_foreign_err_file;

			row_ins_set_detailed(trx, foreign);
1308

osku's avatar
osku committed
1309 1310 1311 1312 1313 1314
			mutex_enter(&dict_foreign_err_mutex);
			rewind(ef);
			ut_print_timestamp(ef);
			fputs(" Transaction:\n", ef);
			trx_print(ef, trx, 600);
			fputs("Foreign key constraint fails for table ", ef);
1315
			ut_print_name(ef, trx, TRUE,
1316
				      foreign->foreign_table_name);
osku's avatar
osku committed
1317
			fputs(":\n", ef);
1318 1319
			dict_print_info_on_foreign_key_in_create_format(
				ef, trx, foreign, TRUE);
osku's avatar
osku committed
1320
			fputs("\nTrying to add to index ", ef);
1321
			ut_print_name(ef, trx, FALSE,
1322
				      foreign->foreign_index->name);
osku's avatar
osku committed
1323 1324 1325
			fputs(" tuple:\n", ef);
			dtuple_print(ef, entry);
			fputs("\nBut the parent table ", ef);
1326
			ut_print_name(ef, trx, TRUE,
1327 1328 1329
				      foreign->referenced_table_name);
			fputs("\nor its .ibd file does"
			      " not currently exist!\n", ef);
osku's avatar
osku committed
1330 1331 1332 1333 1334 1335 1336 1337
			mutex_exit(&dict_foreign_err_mutex);

			err = DB_NO_REFERENCED_ROW;
		}

		goto exit_func;
	}

1338 1339
	ut_a(check_table);
	ut_a(check_index);
osku's avatar
osku committed
1340 1341 1342 1343

	if (check_table != table) {
		/* We already have a LOCK_IX on table, but not necessarily
		on check_table */
1344

osku's avatar
osku committed
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
		err = lock_table(0, check_table, LOCK_IS, thr);

		if (err != DB_SUCCESS) {

			goto do_possible_lock_wait;
		}
	}

	mtr_start(&mtr);

	/* Store old value on n_fields_cmp */

	n_fields_cmp = dtuple_get_n_fields_cmp(entry);

	dtuple_set_n_fields_cmp(entry, foreign->n_fields);

	btr_pcur_open(check_index, entry, PAGE_CUR_GE,
1362
		      BTR_SEARCH_LEAF, &pcur, &mtr);
osku's avatar
osku committed
1363 1364 1365 1366

	/* Scan index records and check if there is a matching record */

	for (;;) {
1367 1368
		const rec_t*		rec = btr_pcur_get_rec(&pcur);
		const buf_block_t*	block = btr_pcur_get_block(&pcur);
osku's avatar
osku committed
1369

1370
		if (page_rec_is_infimum(rec)) {
osku's avatar
osku committed
1371 1372 1373

			goto next_rec;
		}
1374

osku's avatar
osku committed
1375
		offsets = rec_get_offsets(rec, check_index,
1376
					  offsets, ULINT_UNDEFINED, &heap);
osku's avatar
osku committed
1377

1378
		if (page_rec_is_supremum(rec)) {
osku's avatar
osku committed
1379

1380 1381 1382
			err = row_ins_set_shared_rec_lock(LOCK_ORDINARY, block,
							  rec, check_index,
							  offsets, thr);
osku's avatar
osku committed
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
			if (err != DB_SUCCESS) {

				break;
			}

			goto next_rec;
		}

		cmp = cmp_dtuple_rec(entry, rec, offsets);

		if (cmp == 0) {
			if (rec_get_deleted_flag(rec,
1395
						 rec_offs_comp(offsets))) {
1396
				err = row_ins_set_shared_rec_lock(
1397 1398
					LOCK_ORDINARY, block,
					rec, check_index, offsets, thr);
osku's avatar
osku committed
1399 1400 1401 1402 1403 1404 1405 1406
				if (err != DB_SUCCESS) {

					break;
				}
			} else {
				/* Found a matching record. Lock only
				a record because we can allow inserts
				into gaps */
1407

1408
				err = row_ins_set_shared_rec_lock(
1409 1410
					LOCK_REC_NOT_GAP, block,
					rec, check_index, offsets, thr);
osku's avatar
osku committed
1411 1412 1413 1414 1415 1416

				if (err != DB_SUCCESS) {

					break;
				}

1417
				if (check_ref) {
osku's avatar
osku committed
1418 1419 1420 1421 1422 1423 1424 1425
					err = DB_SUCCESS;

					break;
				} else if (foreign->type != 0) {
					/* There is an ON UPDATE or ON DELETE
					condition: check them in a separate
					function */

1426 1427 1428
					err = row_ins_foreign_check_on_constraint(
						thr, foreign, &pcur, entry,
						&mtr);
osku's avatar
osku committed
1429
					if (err != DB_SUCCESS) {
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
						/* Since reporting a plain
						"duplicate key" error
						message to the user in
						cases where a long CASCADE
						operation would lead to a
						duplicate key in some
						other table is very
						confusing, map duplicate
						key errors resulting from
						FK constraints to a
						separate error code. */
1441

1442
						if (err == DB_DUPLICATE_KEY) {
1443
							err = DB_FOREIGN_DUPLICATE_KEY;
1444
						}
osku's avatar
osku committed
1445 1446 1447

						break;
					}
1448 1449 1450 1451 1452

					/* row_ins_foreign_check_on_constraint
					may have repositioned pcur on a
					different block */
					block = btr_pcur_get_block(&pcur);
osku's avatar
osku committed
1453
				} else {
1454 1455 1456
					row_ins_foreign_report_err(
						"Trying to delete or update",
						thr, foreign, rec, entry);
osku's avatar
osku committed
1457 1458 1459 1460 1461 1462 1463 1464

					err = DB_ROW_IS_REFERENCED;
					break;
				}
			}
		}

		if (cmp < 0) {
1465
			err = row_ins_set_shared_rec_lock(
1466 1467
				LOCK_GAP, block,
				rec, check_index, offsets, thr);
osku's avatar
osku committed
1468 1469 1470 1471 1472
			if (err != DB_SUCCESS) {

				break;
			}

1473
			if (check_ref) {
osku's avatar
osku committed
1474
				err = DB_NO_REFERENCED_ROW;
1475 1476
				row_ins_foreign_report_add_err(
					trx, foreign, rec, entry);
osku's avatar
osku committed
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
			} else {
				err = DB_SUCCESS;
			}

			break;
		}

		ut_a(cmp == 0);
next_rec:
		moved = btr_pcur_move_to_next(&pcur, &mtr);

		if (!moved) {
1489
			if (check_ref) {
osku's avatar
osku committed
1490
				rec = btr_pcur_get_rec(&pcur);
1491 1492
				row_ins_foreign_report_add_err(
					trx, foreign, rec, entry);
osku's avatar
osku committed
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
				err = DB_NO_REFERENCED_ROW;
			} else {
				err = DB_SUCCESS;
			}

			break;
		}
	}

	btr_pcur_close(&pcur);

	mtr_commit(&mtr);

	/* Restore old value */
	dtuple_set_n_fields_cmp(entry, n_fields_cmp);

do_possible_lock_wait:
	if (err == DB_LOCK_WAIT) {
		trx->error_state = err;

		que_thr_stop_for_mysql(thr);

		srv_suspend_mysql_thread(thr);
1516

osku's avatar
osku committed
1517 1518
		if (trx->error_state == DB_SUCCESS) {

1519
			goto run_again;
osku's avatar
osku committed
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
		}

		err = trx->error_state;
	}

exit_func:
	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}
	return(err);
}

/*******************************************************************
Checks if foreign key constraints fail for an index entry. If index
is not mentioned in any constraint, this function does nothing,
Otherwise does searches to the indexes of referenced tables and
sets shared locks which lock either the success or the failure of
a constraint. */
static
ulint
row_ins_check_foreign_constraints(
/*==============================*/
				/* out: DB_SUCCESS or error code */
	dict_table_t*	table,	/* in: table */
	dict_index_t*	index,	/* in: index */
	dtuple_t*	entry,	/* in: index entry for index */
	que_thr_t*	thr)	/* in: query thread */
{
	dict_foreign_t*	foreign;
	ulint		err;
	trx_t*		trx;
	ibool		got_s_lock	= FALSE;

	trx = thr_get_trx(thr);

	foreign = UT_LIST_GET_FIRST(table->foreign_list);

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

			if (foreign->referenced_table == NULL) {
1561 1562
				dict_table_get(foreign->referenced_table_name,
					       FALSE);
osku's avatar
osku committed
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
			}

			if (0 == trx->dict_operation_lock_mode) {
				got_s_lock = TRUE;

				row_mysql_freeze_data_dictionary(trx);
			}

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

				(foreign->referenced_table
1575
				 ->n_foreign_key_checks_running)++;
osku's avatar
osku committed
1576 1577 1578 1579 1580 1581 1582 1583 1584

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

			/* NOTE that if the thread ends up waiting for a lock
			we will release dict_operation_lock temporarily!
			But the counter on the table protects the referenced
			table from being dropped while the check is running. */

1585 1586
			err = row_ins_check_foreign_constraint(
				TRUE, foreign, table, entry, thr);
osku's avatar
osku committed
1587 1588 1589 1590 1591

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

				ut_a(foreign->referenced_table
1592
				     ->n_foreign_key_checks_running > 0);
osku's avatar
osku committed
1593
				(foreign->referenced_table
1594
				 ->n_foreign_key_checks_running)--;
osku's avatar
osku committed
1595 1596 1597 1598 1599 1600 1601

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

			if (got_s_lock) {
				row_mysql_unfreeze_data_dictionary(trx);
			}
1602

osku's avatar
osku committed
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
			if (err != DB_SUCCESS) {
				return(err);
			}
		}

		foreign = UT_LIST_GET_NEXT(foreign_list, foreign);
	}

	return(DB_SUCCESS);
}

1614
#ifndef UNIV_HOTBACKUP
osku's avatar
osku committed
1615 1616 1617 1618 1619 1620 1621 1622
/*******************************************************************
Checks if a unique key violation to rec would occur at the index entry
insert. */
static
ibool
row_ins_dupl_error_with_rec(
/*========================*/
				/* out: TRUE if error */
1623
	const rec_t*	rec,	/* in: user record; NOTE that we assume
osku's avatar
osku committed
1624 1625
				that the caller already has a record lock on
				the record! */
1626
	const dtuple_t*	entry,	/* in: entry to insert */
osku's avatar
osku committed
1627 1628 1629 1630 1631 1632
	dict_index_t*	index,	/* in: index */
	const ulint*	offsets)/* in: rec_get_offsets(rec, index) */
{
	ulint	matched_fields;
	ulint	matched_bytes;
	ulint	n_unique;
1633
	ulint	i;
osku's avatar
osku committed
1634 1635 1636 1637 1638 1639 1640 1641 1642

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

	n_unique = dict_index_get_n_unique(index);

	matched_fields = 0;
	matched_bytes = 0;

	cmp_dtuple_rec_with_match(entry, rec, offsets,
1643
				  &matched_fields, &matched_bytes);
osku's avatar
osku committed
1644 1645 1646

	if (matched_fields < n_unique) {

1647
		return(FALSE);
osku's avatar
osku committed
1648 1649 1650 1651 1652
	}

	/* In a unique secondary index we allow equal key values if they
	contain SQL NULLs */

1653
	if (!dict_index_is_clust(index)) {
osku's avatar
osku committed
1654

1655
		for (i = 0; i < n_unique; i++) {
1656 1657
			if (UNIV_SQL_NULL == dfield_get_len(
				    dtuple_get_nth_field(entry, i))) {
osku's avatar
osku committed
1658

1659 1660 1661
				return(FALSE);
			}
		}
osku's avatar
osku committed
1662 1663 1664
	}

	return(!rec_get_deleted_flag(rec, rec_offs_comp(offsets)));
1665 1666
}
#endif /* !UNIV_HOTBACKUP */
osku's avatar
osku committed
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688

/*******************************************************************
Scans a unique non-clustered index at a given index entry to determine
whether a uniqueness violation has occurred for the key value of the entry.
Set shared locks on possible duplicate records. */
static
ulint
row_ins_scan_sec_index_for_duplicate(
/*=================================*/
				/* out: DB_SUCCESS, DB_DUPLICATE_KEY, or
				DB_LOCK_WAIT */
	dict_index_t*	index,	/* in: non-clustered unique index */
	dtuple_t*	entry,	/* in: index entry */
	que_thr_t*	thr)	/* in: query thread */
{
#ifndef UNIV_HOTBACKUP
	ulint		n_unique;
	ulint		i;
	int		cmp;
	ulint		n_fields_cmp;
	btr_pcur_t	pcur;
	ulint		err		= DB_SUCCESS;
1689
	unsigned	allow_duplicates;
osku's avatar
osku committed
1690 1691 1692 1693
	mtr_t		mtr;
	mem_heap_t*	heap		= NULL;
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
	ulint*		offsets		= offsets_;
1694
	rec_offs_init(offsets_);
osku's avatar
osku committed
1695 1696 1697 1698 1699 1700 1701 1702

	n_unique = dict_index_get_n_unique(index);

	/* If the secondary index is unique, but one of the fields in the
	n_unique first fields is NULL, a unique key violation cannot occur,
	since we define NULL != NULL in this case */

	for (i = 0; i < n_unique; i++) {
1703 1704
		if (UNIV_SQL_NULL == dfield_get_len(
			    dtuple_get_nth_field(entry, i))) {
osku's avatar
osku committed
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716

			return(DB_SUCCESS);
		}
	}

	mtr_start(&mtr);

	/* Store old value on n_fields_cmp */

	n_fields_cmp = dtuple_get_n_fields_cmp(entry);

	dtuple_set_n_fields_cmp(entry, dict_index_get_n_unique(index));
1717

osku's avatar
osku committed
1718 1719
	btr_pcur_open(index, entry, PAGE_CUR_GE, BTR_SEARCH_LEAF, &pcur, &mtr);

1720 1721
	allow_duplicates = thr_get_trx(thr)->duplicates & TRX_DUP_IGNORE;

osku's avatar
osku committed
1722 1723
	/* Scan index records and check if there is a duplicate */

1724 1725 1726
	do {
		const rec_t*		rec	= btr_pcur_get_rec(&pcur);
		const buf_block_t*	block	= btr_pcur_get_block(&pcur);
osku's avatar
osku committed
1727

1728
		if (page_rec_is_infimum(rec)) {
osku's avatar
osku committed
1729

1730
			continue;
osku's avatar
osku committed
1731
		}
1732

osku's avatar
osku committed
1733
		offsets = rec_get_offsets(rec, index, offsets,
1734
					  ULINT_UNDEFINED, &heap);
osku's avatar
osku committed
1735

1736
		if (allow_duplicates) {
osku's avatar
osku committed
1737 1738

			/* If the SQL-query will update or replace
1739 1740
			duplicate key we will take X-lock for
			duplicates ( REPLACE, LOAD DATAFILE REPLACE,
osku's avatar
osku committed
1741
			INSERT ON DUPLICATE KEY UPDATE). */
1742

1743
			err = row_ins_set_exclusive_rec_lock(
1744 1745
				LOCK_ORDINARY, block,
				rec, index, offsets, thr);
osku's avatar
osku committed
1746 1747
		} else {

1748
			err = row_ins_set_shared_rec_lock(
1749 1750
				LOCK_ORDINARY, block,
				rec, index, offsets, thr);
osku's avatar
osku committed
1751 1752 1753 1754 1755 1756 1757 1758
		}

		if (err != DB_SUCCESS) {

			break;
		}

		if (page_rec_is_supremum(rec)) {
1759

1760
			continue;
osku's avatar
osku committed
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
		}

		cmp = cmp_dtuple_rec(entry, rec, offsets);

		if (cmp == 0) {
			if (row_ins_dupl_error_with_rec(rec, entry,
							index, offsets)) {
				err = DB_DUPLICATE_KEY;

				thr_get_trx(thr)->error_info = index;

				break;
			}
		}

		if (cmp < 0) {
			break;
		}

		ut_a(cmp == 0);
1781
	} while (btr_pcur_move_to_next(&pcur, &mtr));
osku's avatar
osku committed
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}
	mtr_commit(&mtr);

	/* Restore old value */
	dtuple_set_n_fields_cmp(entry, n_fields_cmp);

	return(err);
#else /* UNIV_HOTBACKUP */
	/* This function depends on MySQL code that is not included in
	InnoDB Hot Backup builds.  Besides, this function should never
	be called in InnoDB Hot Backup. */
	ut_error;
1797
	return(DB_FAIL);
osku's avatar
osku committed
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
#endif /* UNIV_HOTBACKUP */
}

/*******************************************************************
Checks if a unique key violation error would occur at an index entry
insert. Sets shared locks on possible duplicate records. Works only
for a clustered index! */
static
ulint
row_ins_duplicate_error_in_clust(
/*=============================*/
				/* out: DB_SUCCESS if no error,
				DB_DUPLICATE_KEY if error, DB_LOCK_WAIT if we
				have to wait for a lock on a possible
				duplicate record */
	btr_cur_t*	cursor,	/* in: B-tree cursor */
	dtuple_t*	entry,	/* in: entry to insert */
	que_thr_t*	thr,	/* in: query thread */
	mtr_t*		mtr)	/* in: mtr */
{
#ifndef UNIV_HOTBACKUP
	ulint	err;
	rec_t*	rec;
	ulint	n_unique;
	trx_t*	trx		= thr_get_trx(thr);
	mem_heap_t*heap		= NULL;
	ulint	offsets_[REC_OFFS_NORMAL_SIZE];
	ulint*	offsets		= offsets_;
1826
	rec_offs_init(offsets_);
osku's avatar
osku committed
1827 1828

	UT_NOT_USED(mtr);
1829

1830
	ut_a(dict_index_is_clust(cursor->index));
1831
	ut_ad(dict_index_is_unique(cursor->index));
osku's avatar
osku committed
1832 1833 1834 1835 1836 1837 1838 1839

	/* NOTE: For unique non-clustered indexes there may be any number
	of delete marked records with the same value for the non-clustered
	index key (remember multiversioning), and which differ only in
	the row refererence part of the index record, containing the
	clustered index key fields. For such a secondary index record,
	to avoid race condition, we must FIRST do the insertion and after
	that check that the uniqueness condition is not breached! */
1840

osku's avatar
osku committed
1841 1842 1843 1844 1845 1846
	/* NOTE: A problem is that in the B-tree node pointers on an
	upper level may match more to the entry than the actual existing
	user records on the leaf level. So, even if low_match would suggest
	that a duplicate key violation may occur, this may not be the case. */

	n_unique = dict_index_get_n_unique(cursor->index);
1847

osku's avatar
osku committed
1848
	if (cursor->low_match >= n_unique) {
1849

osku's avatar
osku committed
1850 1851 1852 1853
		rec = btr_cur_get_rec(cursor);

		if (!page_rec_is_infimum(rec)) {
			offsets = rec_get_offsets(rec, cursor->index, offsets,
1854
						  ULINT_UNDEFINED, &heap);
osku's avatar
osku committed
1855 1856 1857 1858 1859 1860

			/* We set a lock on the possible duplicate: this
			is needed in logical logging of MySQL to make
			sure that in roll-forward we get the same duplicate
			errors as in original execution */

1861
			if (trx->duplicates & TRX_DUP_IGNORE) {
osku's avatar
osku committed
1862 1863

				/* If the SQL-query will update or replace
1864 1865
				duplicate key we will take X-lock for
				duplicates ( REPLACE, LOAD DATAFILE REPLACE,
osku's avatar
osku committed
1866
				INSERT ON DUPLICATE KEY UPDATE). */
1867

1868
				err = row_ins_set_exclusive_rec_lock(
1869 1870 1871
					LOCK_REC_NOT_GAP,
					btr_cur_get_block(cursor),
					rec, cursor->index, offsets, thr);
osku's avatar
osku committed
1872
			} else {
1873

1874
				err = row_ins_set_shared_rec_lock(
1875 1876
					LOCK_REC_NOT_GAP,
					btr_cur_get_block(cursor), rec,
1877
					cursor->index, offsets, thr);
1878
			}
osku's avatar
osku committed
1879 1880 1881 1882 1883

			if (err != DB_SUCCESS) {
				goto func_exit;
			}

1884 1885
			if (row_ins_dupl_error_with_rec(
				    rec, entry, cursor->index, offsets)) {
osku's avatar
osku committed
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
				trx->error_info = cursor->index;
				err = DB_DUPLICATE_KEY;
				goto func_exit;
			}
		}
	}

	if (cursor->up_match >= n_unique) {

		rec = page_rec_get_next(btr_cur_get_rec(cursor));

		if (!page_rec_is_supremum(rec)) {
			offsets = rec_get_offsets(rec, cursor->index, offsets,
1899
						  ULINT_UNDEFINED, &heap);
osku's avatar
osku committed
1900

1901
			if (trx->duplicates & TRX_DUP_IGNORE) {
osku's avatar
osku committed
1902 1903

				/* If the SQL-query will update or replace
1904 1905
				duplicate key we will take X-lock for
				duplicates ( REPLACE, LOAD DATAFILE REPLACE,
osku's avatar
osku committed
1906 1907
				INSERT ON DUPLICATE KEY UPDATE). */

1908
				err = row_ins_set_exclusive_rec_lock(
1909 1910 1911
					LOCK_REC_NOT_GAP,
					btr_cur_get_block(cursor),
					rec, cursor->index, offsets, thr);
osku's avatar
osku committed
1912 1913
			} else {

1914
				err = row_ins_set_shared_rec_lock(
1915 1916 1917
					LOCK_REC_NOT_GAP,
					btr_cur_get_block(cursor),
					rec, cursor->index, offsets, thr);
osku's avatar
osku committed
1918 1919 1920 1921 1922 1923
			}

			if (err != DB_SUCCESS) {
				goto func_exit;
			}

1924 1925
			if (row_ins_dupl_error_with_rec(
				    rec, entry, cursor->index, offsets)) {
osku's avatar
osku committed
1926 1927 1928 1929 1930 1931
				trx->error_info = cursor->index;
				err = DB_DUPLICATE_KEY;
				goto func_exit;
			}
		}

1932
		ut_a(!dict_index_is_clust(cursor->index));
1933
		/* This should never happen */
osku's avatar
osku committed
1934 1935 1936 1937
	}

	err = DB_SUCCESS;
func_exit:
1938 1939 1940
	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}
osku's avatar
osku committed
1941 1942 1943 1944 1945 1946
	return(err);
#else /* UNIV_HOTBACKUP */
	/* This function depends on MySQL code that is not included in
	InnoDB Hot Backup builds.  Besides, this function should never
	be called in InnoDB Hot Backup. */
	ut_error;
1947
	return(DB_FAIL);
osku's avatar
osku committed
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
#endif /* UNIV_HOTBACKUP */
}

/*******************************************************************
Checks if an index entry has long enough common prefix with an existing
record so that the intended insert of the entry must be changed to a modify of
the existing record. In the case of a clustered index, the prefix must be
n_unique fields long, and in the case of a secondary index, all fields must be
equal. */
UNIV_INLINE
ulint
row_ins_must_modify(
/*================*/
				/* out: 0 if no update, ROW_INS_PREV if
				previous should be updated; currently we
				do the search so that only the low_match
				record can match enough to the search tuple,
				not the next record */
	btr_cur_t*	cursor)	/* in: B-tree cursor */
{
	ulint	enough_match;
	rec_t*	rec;
1970

osku's avatar
osku committed
1971 1972 1973 1974 1975 1976 1977 1978
	/* NOTE: (compare to the note in row_ins_duplicate_error) Because node
	pointers on upper levels of the B-tree may match more to entry than
	to actual user records on the leaf level, we have to check if the
	candidate record is actually a user record. In a clustered index
	node pointers contain index->n_unique first fields, and in the case
	of a secondary index, all fields of the index. */

	enough_match = dict_index_get_n_unique_in_tree(cursor->index);
1979

osku's avatar
osku committed
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
	if (cursor->low_match >= enough_match) {

		rec = btr_cur_get_rec(cursor);

		if (!page_rec_is_infimum(rec)) {

			return(ROW_INS_PREV);
		}
	}

	return(0);
}

/*******************************************************************
Tries to insert an index entry to an index. If the index is clustered
and a record with the same unique key is found, the other record is
necessarily marked deleted by a committed transaction, or a unique key
violation error occurs. The delete marked record is then updated to an
existing record, and we must write an undo log record on the delete
marked record. If the index is secondary, and a record with exactly the
same fields is found, the other record is necessarily marked deleted.
It is then unmarked. Otherwise, the entry is just inserted to the index. */
2002
static
osku's avatar
osku committed
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
ulint
row_ins_index_entry_low(
/*====================*/
				/* out: DB_SUCCESS, DB_LOCK_WAIT, DB_FAIL
				if pessimistic retry needed, or error code */
	ulint		mode,	/* in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
				depending on whether we wish optimistic or
				pessimistic descent down the index tree */
	dict_index_t*	index,	/* in: index */
	dtuple_t*	entry,	/* in: index entry to insert */
2013
	ulint		n_ext,	/* in: number of externally stored columns */
osku's avatar
osku committed
2014 2015 2016
	que_thr_t*	thr)	/* in: query thread */
{
	btr_cur_t	cursor;
2017
	ulint		search_mode;
osku's avatar
osku committed
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
	ulint		modify = 0; /* remove warning */
	rec_t*		insert_rec;
	rec_t*		rec;
	ulint		err;
	ulint		n_unique;
	big_rec_t*	big_rec			= NULL;
	mtr_t		mtr;
	mem_heap_t*	heap			= NULL;

	log_free_check();

	mtr_start(&mtr);

	cursor.thr = thr;

	/* Note that we use PAGE_CUR_LE as the search mode, because then
	the function will return in both low_match and up_match of the
	cursor sensible values */
2036

2037 2038 2039 2040 2041 2042
	if (dict_index_is_clust(index)) {
		search_mode = mode;
	} else if (!(thr_get_trx(thr)->check_unique_secondary)) {
		search_mode = mode | BTR_INSERT | BTR_IGNORE_SEC_UNIQUE;
	} else {
		search_mode = mode | BTR_INSERT;
osku's avatar
osku committed
2043 2044 2045
	}

	btr_cur_search_to_nth_level(index, 0, entry, PAGE_CUR_LE,
2046
				    search_mode, &cursor, 0, &mtr);
osku's avatar
osku committed
2047 2048 2049 2050 2051

	if (cursor.flag == BTR_CUR_INSERT_TO_IBUF) {
		/* The insertion was made to the insert buffer already during
		the search: we are done */

2052
		ut_ad(search_mode & BTR_INSERT);
osku's avatar
osku committed
2053 2054 2055 2056 2057 2058 2059 2060
		err = DB_SUCCESS;

		goto function_exit;
	}

#ifdef UNIV_DEBUG
	{
		page_t*	page = btr_cur_get_page(&cursor);
2061 2062
		rec_t*	first_rec = page_rec_get_next(
			page_get_infimum_rec(page));
osku's avatar
osku committed
2063

marko's avatar
marko committed
2064
		ut_ad(page_rec_is_supremum(first_rec)
2065 2066
		      || rec_get_n_fields(first_rec, index)
		      == dtuple_get_n_fields(entry));
osku's avatar
osku committed
2067 2068 2069 2070 2071
	}
#endif

	n_unique = dict_index_get_n_unique(index);

2072 2073
	if (dict_index_is_unique(index) && (cursor.up_match >= n_unique
					    || cursor.low_match >= n_unique)) {
osku's avatar
osku committed
2074

2075
		if (dict_index_is_clust(index)) {
osku's avatar
osku committed
2076 2077 2078
			/* Note that the following may return also
			DB_LOCK_WAIT */

2079 2080
			err = row_ins_duplicate_error_in_clust(
				&cursor, entry, thr, &mtr);
osku's avatar
osku committed
2081 2082 2083 2084 2085 2086
			if (err != DB_SUCCESS) {

				goto function_exit;
			}
		} else {
			mtr_commit(&mtr);
2087 2088
			err = row_ins_scan_sec_index_for_duplicate(
				index, entry, thr);
osku's avatar
osku committed
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
			mtr_start(&mtr);

			if (err != DB_SUCCESS) {

				goto function_exit;
			}

			/* We did not find a duplicate and we have now
			locked with s-locks the necessary records to
			prevent any insertion of a duplicate by another
			transaction. Let us now reposition the cursor and
			continue the insertion. */
2101

osku's avatar
osku committed
2102
			btr_cur_search_to_nth_level(index, 0, entry,
2103 2104 2105
						    PAGE_CUR_LE,
						    mode | BTR_INSERT,
						    &cursor, 0, &mtr);
2106
		}
osku's avatar
osku committed
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
	}

	modify = row_ins_must_modify(&cursor);

	if (modify != 0) {
		/* There is already an index entry with a long enough common
		prefix, we must convert the insert into a modify of an
		existing record */

		if (modify == ROW_INS_NEXT) {
			rec = page_rec_get_next(btr_cur_get_rec(&cursor));

2119 2120
			btr_cur_position(index, rec,
					 btr_cur_get_block(&cursor),&cursor);
osku's avatar
osku committed
2121 2122
		}

2123
		if (dict_index_is_clust(index)) {
2124
			err = row_ins_clust_index_entry_by_modify(
2125
				mode, &cursor, &heap, &big_rec, entry,
2126
				thr, &mtr);
osku's avatar
osku committed
2127
		} else {
2128
			ut_ad(!n_ext);
2129 2130
			err = row_ins_sec_index_entry_by_modify(
				mode, &cursor, entry, thr, &mtr);
osku's avatar
osku committed
2131 2132 2133
		}
	} else {
		if (mode == BTR_MODIFY_LEAF) {
2134 2135
			err = btr_cur_optimistic_insert(
				0, &cursor, entry, &insert_rec, &big_rec,
2136
				n_ext, thr, &mtr);
osku's avatar
osku committed
2137 2138
		} else {
			ut_a(mode == BTR_MODIFY_TREE);
2139 2140 2141 2142 2143 2144
			if (buf_LRU_buf_pool_running_out()) {

				err = DB_LOCK_TABLE_FULL;

				goto function_exit;
			}
2145 2146
			err = btr_cur_pessimistic_insert(
				0, &cursor, entry, &insert_rec, &big_rec,
2147
				n_ext, thr, &mtr);
osku's avatar
osku committed
2148 2149 2150 2151 2152 2153
		}
	}

function_exit:
	mtr_commit(&mtr);

2154
	if (UNIV_LIKELY_NULL(big_rec)) {
2155 2156
		rec_t*	rec;
		ulint*	offsets;
osku's avatar
osku committed
2157
		mtr_start(&mtr);
2158

osku's avatar
osku committed
2159
		btr_cur_search_to_nth_level(index, 0, entry, PAGE_CUR_LE,
2160
					    BTR_MODIFY_TREE, &cursor, 0, &mtr);
osku's avatar
osku committed
2161
		rec = btr_cur_get_rec(&cursor);
2162
		offsets = rec_get_offsets(rec, index, NULL,
2163
					  ULINT_UNDEFINED, &heap);
osku's avatar
osku committed
2164

2165 2166 2167
		err = btr_store_big_rec_extern_fields(
			index, btr_cur_get_block(&cursor),
			rec, offsets, big_rec, &mtr);
osku's avatar
osku committed
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188

		if (modify) {
			dtuple_big_rec_free(big_rec);
		} else {
			dtuple_convert_back_big_rec(index, entry, big_rec);
		}

		mtr_commit(&mtr);
	}

	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}
	return(err);
}

/*******************************************************************
Inserts an index entry to index. Tries first optimistic, then pessimistic
descent down the tree. If the entry matches enough to a delete marked record,
performs the insert by updating or delete unmarking the delete marked
record. */
2189
UNIV_INTERN
osku's avatar
osku committed
2190 2191 2192 2193 2194 2195 2196
ulint
row_ins_index_entry(
/*================*/
				/* out: DB_SUCCESS, DB_LOCK_WAIT,
				DB_DUPLICATE_KEY, or some other error code */
	dict_index_t*	index,	/* in: index */
	dtuple_t*	entry,	/* in: index entry to insert */
2197 2198
	ulint		n_ext,	/* in: number of externally stored columns */
	ibool		foreign,/* in: TRUE=check foreign key constraints */
osku's avatar
osku committed
2199 2200 2201 2202
	que_thr_t*	thr)	/* in: query thread */
{
	ulint	err;

2203
	if (foreign && UT_LIST_GET_FIRST(index->table->foreign_list)) {
osku's avatar
osku committed
2204
		err = row_ins_check_foreign_constraints(index->table, index,
2205
							entry, thr);
osku's avatar
osku committed
2206 2207 2208 2209 2210 2211 2212 2213 2214
		if (err != DB_SUCCESS) {

			return(err);
		}
	}

	/* Try first optimistic descent to the B-tree */

	err = row_ins_index_entry_low(BTR_MODIFY_LEAF, index, entry,
2215
				      n_ext, thr);
osku's avatar
osku committed
2216 2217 2218 2219 2220 2221 2222 2223
	if (err != DB_FAIL) {

		return(err);
	}

	/* Try then pessimistic descent to the B-tree */

	err = row_ins_index_entry_low(BTR_MODIFY_TREE, index, entry,
2224
				      n_ext, thr);
osku's avatar
osku committed
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
	return(err);
}

/***************************************************************
Sets the values of the dtuple fields in entry from the values of appropriate
columns in row. */
static
void
row_ins_index_entry_set_vals(
/*=========================*/
	dict_index_t*	index,	/* in: index */
	dtuple_t*	entry,	/* in: index entry to make */
2237
	const dtuple_t*	row)	/* in: row */
osku's avatar
osku committed
2238
{
2239 2240
	ulint	n_fields;
	ulint	i;
osku's avatar
osku committed
2241 2242 2243 2244 2245 2246

	ut_ad(entry && row);

	n_fields = dtuple_get_n_fields(entry);

	for (i = 0; i < n_fields; i++) {
2247 2248 2249 2250 2251
		dict_field_t*	ind_field;
		dfield_t*	field;
		const dfield_t*	row_field;
		ulint		len;

2252
		field = dtuple_get_nth_field(entry, i);
osku's avatar
osku committed
2253 2254
		ind_field = dict_index_get_nth_field(index, i);
		row_field = dtuple_get_nth_field(row, ind_field->col->ind);
2255
		len = dfield_get_len(row_field);
osku's avatar
osku committed
2256 2257 2258

		/* Check column prefix indexes */
		if (ind_field->prefix_len > 0
2259
		    && dfield_get_len(row_field) != UNIV_SQL_NULL) {
osku's avatar
osku committed
2260

2261 2262
			const	dict_col_t*	col
				= dict_field_get_col(ind_field);
osku's avatar
osku committed
2263

2264
			len = dtype_get_at_most_n_mbchars(
2265 2266
				col->prtype, col->mbminlen, col->mbmaxlen,
				ind_field->prefix_len,
2267 2268 2269
				len, dfield_get_data(row_field));

			ut_ad(!dfield_is_ext(row_field));
osku's avatar
osku committed
2270 2271
		}

2272
		dfield_set_data(field, dfield_get_data(row_field), len);
2273 2274 2275 2276
		if (dfield_is_ext(row_field)) {
			ut_ad(dict_index_is_clust(index));
			dfield_set_ext(field);
		}
osku's avatar
osku committed
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
	}
}

/***************************************************************
Inserts a single index entry to the table. */
static
ulint
row_ins_index_entry_step(
/*=====================*/
				/* out: DB_SUCCESS if operation successfully
				completed, else error code or DB_LOCK_WAIT */
	ins_node_t*	node,	/* in: row insert node */
	que_thr_t*	thr)	/* in: query thread */
{
	ulint	err;

	ut_ad(dtuple_check_typed(node->row));
2294

osku's avatar
osku committed
2295
	row_ins_index_entry_set_vals(node->index, node->entry, node->row);
2296

osku's avatar
osku committed
2297 2298
	ut_ad(dtuple_check_typed(node->entry));

2299
	err = row_ins_index_entry(node->index, node->entry, 0, TRUE, thr);
osku's avatar
osku committed
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312

	return(err);
}

/***************************************************************
Allocates a row id for row and inits the node->index field. */
UNIV_INLINE
void
row_ins_alloc_row_id_step(
/*======================*/
	ins_node_t*	node)	/* in: row insert node */
{
	dulint	row_id;
2313

osku's avatar
osku committed
2314
	ut_ad(node->state == INS_NODE_ALLOC_ROW_ID);
2315

2316
	if (dict_index_is_unique(dict_table_get_first_index(node->table))) {
osku's avatar
osku committed
2317 2318 2319 2320 2321

		/* No row id is stored if the clustered index is unique */

		return;
	}
2322

osku's avatar
osku committed
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
	/* Fill in row id value to row */

	row_id = dict_sys_get_new_row_id();

	dict_sys_write_row_id(node->row_id_buf, row_id);
}

/***************************************************************
Gets a row to insert from the values list. */
UNIV_INLINE
void
row_ins_get_row_from_values(
/*========================*/
	ins_node_t*	node)	/* in: row insert node */
{
	que_node_t*	list_node;
	dfield_t*	dfield;
	dtuple_t*	row;
	ulint		i;
2342

osku's avatar
osku committed
2343 2344 2345 2346
	/* The field values are copied in the buffers of the select node and
	it is safe to use them until we fetch from select again: therefore
	we can just copy the pointers */

2347
	row = node->row;
osku's avatar
osku committed
2348 2349 2350 2351 2352 2353 2354

	i = 0;
	list_node = node->values_list;

	while (list_node) {
		eval_exp(list_node);

2355
		dfield = dtuple_get_nth_field(row, i);
osku's avatar
osku committed
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
		dfield_copy_data(dfield, que_node_get_val(list_node));

		i++;
		list_node = que_node_get_next(list_node);
	}
}

/***************************************************************
Gets a row to insert from the select list. */
UNIV_INLINE
void
row_ins_get_row_from_select(
/*========================*/
	ins_node_t*	node)	/* in: row insert node */
{
	que_node_t*	list_node;
	dfield_t*	dfield;
	dtuple_t*	row;
	ulint		i;

	/* The field values are copied in the buffers of the select node and
	it is safe to use them until we fetch from select again: therefore
	we can just copy the pointers */

2380
	row = node->row;
osku's avatar
osku committed
2381 2382 2383 2384 2385

	i = 0;
	list_node = node->select->select_list;

	while (list_node) {
2386
		dfield = dtuple_get_nth_field(row, i);
osku's avatar
osku committed
2387 2388 2389 2390 2391 2392
		dfield_copy_data(dfield, que_node_get_val(list_node));

		i++;
		list_node = que_node_get_next(list_node);
	}
}
2393

osku's avatar
osku committed
2394 2395
/***************************************************************
Inserts a row to a table. */
2396
static
osku's avatar
osku committed
2397 2398 2399 2400 2401 2402 2403 2404 2405
ulint
row_ins(
/*====*/
				/* out: DB_SUCCESS if operation successfully
				completed, else error code or DB_LOCK_WAIT */
	ins_node_t*	node,	/* in: row insert node */
	que_thr_t*	thr)	/* in: query thread */
{
	ulint	err;
2406

osku's avatar
osku committed
2407 2408 2409 2410 2411
	ut_ad(node && thr);

	if (node->state == INS_NODE_ALLOC_ROW_ID) {

		row_ins_alloc_row_id_step(node);
2412

osku's avatar
osku committed
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
		node->index = dict_table_get_first_index(node->table);
		node->entry = UT_LIST_GET_FIRST(node->entry_list);

		if (node->ins_type == INS_SEARCHED) {

			row_ins_get_row_from_select(node);

		} else if (node->ins_type == INS_VALUES) {

			row_ins_get_row_from_values(node);
		}

		node->state = INS_NODE_INSERT_ENTRIES;
	}

	ut_ad(node->state == INS_NODE_INSERT_ENTRIES);

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

osku's avatar
osku committed
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
		if (err != DB_SUCCESS) {

			return(err);
		}

		node->index = dict_table_get_next_index(node->index);
		node->entry = UT_LIST_GET_NEXT(tuple_list, node->entry);
	}

	ut_ad(node->entry == NULL);
2443

osku's avatar
osku committed
2444
	node->state = INS_NODE_ALLOC_ROW_ID;
2445

osku's avatar
osku committed
2446 2447 2448 2449 2450 2451
	return(DB_SUCCESS);
}

/***************************************************************
Inserts a row to a table. This is a high-level function used in SQL execution
graphs. */
2452
UNIV_INTERN
osku's avatar
osku committed
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
que_thr_t*
row_ins_step(
/*=========*/
				/* out: query thread to run next or NULL */
	que_thr_t*	thr)	/* in: query thread */
{
	ins_node_t*	node;
	que_node_t*	parent;
	sel_node_t*	sel_node;
	trx_t*		trx;
	ulint		err;

	ut_ad(thr);
2466

osku's avatar
osku committed
2467 2468 2469
	trx = thr_get_trx(thr);

	trx_start_if_not_started(trx);
2470

osku's avatar
osku committed
2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
	node = thr->run_node;

	ut_ad(que_node_get_type(node) == QUE_NODE_INSERT);

	parent = que_node_get_parent(node);
	sel_node = node->select;

	if (thr->prev_node == parent) {
		node->state = INS_NODE_SET_IX_LOCK;
	}

	/* If this is the first time this node is executed (or when
	execution resumes after wait for the table IX lock), set an
2484 2485 2486 2487 2488 2489 2490 2491 2492
	IX lock on the table and reset the possible select node. MySQL's
	partitioned table code may also call an insert within the same
	SQL statement AFTER it has used this table handle to do a search.
	This happens, for example, when a row update moves it to another
	partition. In that case, we have already set the IX lock on the
	table during the search operation, and there is no need to set
	it again here. But we must write trx->id to node->trx_id_buf. */

	trx_write_trx_id(node->trx_id_buf, trx->id);
osku's avatar
osku committed
2493 2494 2495 2496 2497

	if (node->state == INS_NODE_SET_IX_LOCK) {

		/* It may be that the current session has not yet started
		its transaction, or it has been committed: */
2498

osku's avatar
osku committed
2499
		if (UT_DULINT_EQ(trx->id, node->trx_id)) {
2500
			/* No need to do IX-locking */
osku's avatar
osku committed
2501 2502

			goto same_trx;
2503
		}
osku's avatar
osku committed
2504 2505 2506 2507 2508 2509 2510 2511 2512

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

		if (err != DB_SUCCESS) {

			goto error_handling;
		}

		node->trx_id = trx->id;
2513
same_trx:
osku's avatar
osku committed
2514 2515 2516 2517 2518
		node->state = INS_NODE_ALLOC_ROW_ID;

		if (node->ins_type == INS_SEARCHED) {
			/* Reset the cursor */
			sel_node->state = SEL_NODE_OPEN;
2519

osku's avatar
osku committed
2520
			/* Fetch a row to insert */
2521

osku's avatar
osku committed
2522
			thr->run_node = sel_node;
2523

osku's avatar
osku committed
2524 2525 2526 2527 2528
			return(thr);
		}
	}

	if ((node->ins_type == INS_SEARCHED)
2529
	    && (sel_node->state != SEL_NODE_FETCH)) {
osku's avatar
osku committed
2530 2531 2532 2533 2534

		ut_ad(sel_node->state == SEL_NODE_NO_MORE_ROWS);

		/* No more rows to insert */
		thr->run_node = parent;
2535

osku's avatar
osku committed
2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554
		return(thr);
	}

	/* DO THE CHECKS OF THE CONSISTENCY CONSTRAINTS HERE */

	err = row_ins(node, thr);

error_handling:
	trx->error_state = err;

	if (err != DB_SUCCESS) {
		/* err == DB_LOCK_WAIT or SQL error detected */
		return(NULL);
	}

	/* DO THE TRIGGER ACTIONS HERE */

	if (node->ins_type == INS_SEARCHED) {
		/* Fetch a row to insert */
2555

osku's avatar
osku committed
2556 2557 2558 2559 2560 2561 2562
		thr->run_node = sel_node;
	} else {
		thr->run_node = que_node_get_parent(node);
	}

	return(thr);
}