📄 row0ins.c
字号:
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) { dict_table_get(foreign->referenced_table_name, trx); } 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 ->n_foreign_key_checks_running)++; 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. */ err = row_ins_check_foreign_constraint(TRUE, foreign, table, entry, thr); if (foreign->referenced_table) { mutex_enter(&(dict_sys->mutex)); ut_a(foreign->referenced_table ->n_foreign_key_checks_running > 0); (foreign->referenced_table ->n_foreign_key_checks_running)--; mutex_exit(&(dict_sys->mutex)); } if (got_s_lock) { row_mysql_unfreeze_data_dictionary(trx); } if (err != DB_SUCCESS) { return(err); } } foreign = UT_LIST_GET_NEXT(foreign_list, foreign); } return(DB_SUCCESS);}/*******************************************************************Checks if a unique key violation to rec would occur at the index entryinsert. */staticiboolrow_ins_dupl_error_with_rec(/*========================*/ /* out: TRUE if error */ rec_t* rec, /* in: user record; NOTE that we assume that the caller already has a record lock on the record! */ dtuple_t* entry, /* in: entry to insert */ dict_index_t* index, /* in: index */ const ulint* offsets)/* in: rec_get_offsets(rec, index) */{ ulint matched_fields; ulint matched_bytes; ulint n_unique; ulint i; 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, &matched_fields, &matched_bytes); if (matched_fields < n_unique) { return(FALSE); } /* In a unique secondary index we allow equal key values if they contain SQL NULLs */ if (!(index->type & DICT_CLUSTERED)) { for (i = 0; i < n_unique; i++) { if (UNIV_SQL_NULL == dfield_get_len( dtuple_get_nth_field(entry, i))) { return(FALSE); } } } return(!rec_get_deleted_flag(rec, rec_offs_comp(offsets)));} /*******************************************************************Scans a unique non-clustered index at a given index entry to determinewhether a uniqueness violation has occurred for the key value of the entry.Set shared locks on possible duplicate records. */staticulintrow_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; rec_t* rec; btr_pcur_t pcur; ulint err = DB_SUCCESS; ibool moved; mtr_t mtr; mem_heap_t* heap = NULL; ulint offsets_[REC_OFFS_NORMAL_SIZE]; ulint* offsets = offsets_; *offsets_ = (sizeof offsets_) / sizeof *offsets_; 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++) { if (UNIV_SQL_NULL == dfield_get_len( dtuple_get_nth_field(entry, i))) { 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)); btr_pcur_open(index, entry, PAGE_CUR_GE, BTR_SEARCH_LEAF, &pcur, &mtr); /* Scan index records and check if there is a duplicate */ for (;;) { rec = btr_pcur_get_rec(&pcur); if (rec == page_get_infimum_rec(buf_frame_align(rec))) { goto next_rec; } offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, &heap); if (innobase_query_is_update()) { /* If the SQL-query will update or replace duplicate key we will take X-lock for duplicates ( REPLACE, LOAD DATAFILE REPLACE, INSERT ON DUPLICATE KEY UPDATE). */ err = row_ins_set_exclusive_rec_lock(LOCK_ORDINARY, rec, index, offsets, thr); } else { err = row_ins_set_shared_rec_lock(LOCK_ORDINARY, rec, index, offsets, thr); } if (err != DB_SUCCESS) { break; } if (page_rec_is_supremum(rec)) { goto next_rec; } 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);next_rec: moved = btr_pcur_move_to_next(&pcur, &mtr); if (!moved) { break; } } 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;#endif /* UNIV_HOTBACKUP */}/*******************************************************************Checks if a unique key violation error would occur at an index entryinsert. Sets shared locks on possible duplicate records. Works onlyfor a clustered index! */staticulintrow_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_; *offsets_ = (sizeof offsets_) / sizeof *offsets_; UT_NOT_USED(mtr); ut_a(cursor->index->type & DICT_CLUSTERED); ut_ad(cursor->index->type & DICT_UNIQUE); /* 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! */ /* 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); if (cursor->low_match >= n_unique) { rec = btr_cur_get_rec(cursor); if (!page_rec_is_infimum(rec)) { offsets = rec_get_offsets(rec, cursor->index, offsets, ULINT_UNDEFINED, &heap); /* 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 */ if (innobase_query_is_update()) { /* If the SQL-query will update or replace duplicate key we will take X-lock for duplicates ( REPLACE, LOAD DATAFILE REPLACE, INSERT ON DUPLICATE KEY UPDATE). */ err = row_ins_set_exclusive_rec_lock( LOCK_REC_NOT_GAP,rec,cursor->index, offsets, thr); } else { err = row_ins_set_shared_rec_lock( LOCK_REC_NOT_GAP,rec, cursor->index, offsets, thr); } if (err != DB_SUCCESS) { goto func_exit; } if (row_ins_dupl_error_with_rec(rec, entry, cursor->index, offsets)) { 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, ULINT_UNDEFINED, &heap); if (innobase_query_is_update()) { /* If the SQL-query will update or replace duplicate key we will take X-lock for duplicates ( REPLACE, LOAD DATAFILE REPLACE, INSERT ON DUPLICATE KEY UPDATE). */ err = row_ins_set_exclusive_rec_lock( LOCK_REC_NOT_GAP, rec, cursor->index, offsets, thr); } else { err = row_ins_set_shared_rec_lock( LOCK_REC_NOT_GAP, rec, cursor->index, offsets, thr); } if (err != DB_SUCCESS) { goto func_exit; } if (row_ins_dupl_error_with_rec(rec, entry, cursor->index, offsets)) { trx->error_info = cursor->index; err = DB_DUPLICATE_KEY; goto func_exit; } } ut_a(!(cursor->index->type & DICT_CLUSTERED)); /* This should never happen */ } err = DB_SUCCESS;func_exit: if (UNIV_LIKELY_NULL(heap)) { mem_heap_free(heap); } 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;#endif /* UNIV_HOTBACKUP */}/*******************************************************************Checks if an index entry has long enough common prefix with an existingrecord so that the intended insert of the entry must be changed to a modify ofthe existing record. In the case of a clustered index, the prefix must ben_unique fields long, and in the case of a secondary index, all fields must beequal. */UNIV_INLINEulintrow_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; /* 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); 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 clusteredand a record with the same unique key is found, the other record isnecessarily marked deleted by a committed transaction, or a unique keyviolation error occurs. The delete marked record is then updated to anexisting record, and we must write an undo log record on the deletemarked record. If the index is secondary, and a record with exactly thesame fields is found, the other record is necessarily marked deleted.It is then unmarked. Otherwise, the entry is just inserted to the index. */ulintrow_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 */ 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 */ que_thr_t* thr) /* in: query thread */{ btr_cur_t cursor; ulint ignore_sec_unique = 0; 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; ulint offsets_[REC_OFFS_NORMAL_SIZE]; ulint* offsets = offsets_; *offsets_ = (sizeof offsets_) / sizeof *offsets_; 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 */ if (!(thr_get_trx(thr)->check_unique_secondary)) { ignore_sec_unique = BTR_IGNORE_SEC_UNIQUE; } btr_cur_search_to_nth_level(index, 0, entry, PAGE_CUR_LE, mode | BTR_INSERT | ignore_sec_unique, &cursor, 0, &mtr); if (cursor.flag == BTR_CUR_INSERT_TO_IBUF) { /* The insertion was made to the insert buffer already during the search: we are done */ err = DB_SUCCESS; goto function_exit; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -