📄 row0upd.c
字号:
/******************************************************Update of a row(c) 1996 Innobase OyCreated 12/27/1996 Heikki Tuuri*******************************************************/#include "row0upd.h"#ifdef UNIV_NONINL#include "row0upd.ic"#endif#include "dict0dict.h"#include "dict0boot.h"#include "dict0crea.h"#include "mach0data.h"#include "trx0undo.h"#include "btr0btr.h"#include "btr0cur.h"#include "que0que.h"#include "row0ins.h"#include "row0sel.h"#include "row0row.h"#include "rem0cmp.h"#include "lock0lock.h"#include "log0log.h"#include "pars0sym.h"#include "eval0eval.h"#include "buf0lru.h"/* What kind of latch and lock can we assume when the control comes to -------------------------------------------------------------------an update node?--------------Efficiency of massive updates would require keeping an x-latch on aclustered index page through many updates, and not setting an explicitx-lock on clustered index records, as they anyway will get an implicitx-lock when they are updated. A problem is that the read nodes in thegraph should know that they must keep the latch when passing the controlup to the update node, and not set any record lock on the record whichwill be updated. Another problem occurs if the execution is stopped,as the kernel switches to another query thread, or the transaction mustwait for a lock. Then we should be able to release the latch and, maybe,acquire an explicit x-lock on the record. Because this seems too complicated, we conclude that the lessefficient solution of releasing all the latches when the control istransferred to another node, and acquiring explicit x-locks, is better. *//* How is a delete performed? If there is a delete without anexplicit cursor, i.e., a searched delete, there are at leasttwo different situations:the implicit select cursor may run on (1) the clustered index oron (2) a secondary index. The delete is performed by settingthe delete bit in the record and substituting the id of thedeleting transaction for the original trx id, and substituting anew roll ptr for previous roll ptr. The old trx id and roll ptrare saved in the undo log record. Thus, no physical changes occurin the index tree structure at the time of the delete. Onlywhen the undo log is purged, the index records will be physicallydeleted from the index trees.The query graph executing a searched delete would consist ofa delete node which has as a subtree a select subgraph.The select subgraph should return a (persistent) cursorin the clustered index, placed on page which is x-latched.The delete node should look for all secondary index records forthis clustered index entry and mark them as deleted. When isthe x-latch freed? The most efficient way for performing asearched delete is obviously to keep the x-latch for severalsteps of query graph execution. *//***************************************************************Checks if an update vector changes some of the first ordering fields of anindex record. This is only used in foreign key checks and we can assumethat index does not contain column prefixes. */staticiboolrow_upd_changes_first_fields_binary(/*================================*/ /* out: TRUE if changes */ dtuple_t* entry, /* in: old value of index entry */ dict_index_t* index, /* in: index of entry */ upd_t* update, /* in: update vector for the row */ ulint n); /* in: how many first fields to check *//*************************************************************************Checks if index currently is mentioned as a referenced index in a foreignkey constraint. */staticiboolrow_upd_index_is_referenced(/*========================*/ /* out: TRUE if referenced; NOTE that since we do not hold dict_operation_lock when leaving the function, it may be that the referencing table has been dropped when we leave this function: this function is only for heuristic use! */ dict_index_t* index, /* in: index */ trx_t* trx) /* in: transaction */{ dict_table_t* table = index->table; dict_foreign_t* foreign; ibool froze_data_dict = FALSE; if (!UT_LIST_GET_FIRST(table->referenced_list)) { return(FALSE); } if (trx->dict_operation_lock_mode == 0) { row_mysql_freeze_data_dictionary(trx); froze_data_dict = TRUE; } foreign = UT_LIST_GET_FIRST(table->referenced_list); while (foreign) { if (foreign->referenced_index == index) { if (froze_data_dict) { row_mysql_unfreeze_data_dictionary(trx); } return(TRUE); } foreign = UT_LIST_GET_NEXT(referenced_list, foreign); } if (froze_data_dict) { row_mysql_unfreeze_data_dictionary(trx); } return(FALSE);}/*************************************************************************Checks if possible foreign key constraints hold after a delete of the recordunder pcur. NOTE that this function will temporarily commit mtr and lose thepcur position! */staticulintrow_upd_check_references_constraints(/*=================================*/ /* out: DB_SUCCESS or an error code */ upd_node_t* node, /* in: row update node */ btr_pcur_t* pcur, /* in: cursor positioned on a record; NOTE: the cursor position is lost in this function! */ dict_table_t* table, /* in: table in question */ dict_index_t* index, /* in: index of the cursor */ que_thr_t* thr, /* in: query thread */ mtr_t* mtr) /* in: mtr */{ dict_foreign_t* foreign; mem_heap_t* heap; dtuple_t* entry; trx_t* trx; rec_t* rec; ulint err; ibool got_s_lock = FALSE; if (UT_LIST_GET_FIRST(table->referenced_list) == NULL) { return(DB_SUCCESS); } trx = thr_get_trx(thr); rec = btr_pcur_get_rec(pcur); heap = mem_heap_create(500); entry = row_rec_to_index_entry(ROW_COPY_DATA, index, rec, heap); mtr_commit(mtr); mtr_start(mtr); if (trx->dict_operation_lock_mode == 0) { got_s_lock = TRUE; row_mysql_freeze_data_dictionary(trx); } foreign = UT_LIST_GET_FIRST(table->referenced_list); while (foreign) { /* Note that we may have an update which updates the index record, but does NOT update the first fields which are referenced in a foreign key constraint. Then the update does NOT break the constraint. */ if (foreign->referenced_index == index && (node->is_delete || row_upd_changes_first_fields_binary(entry, index, node->update, foreign->n_fields))) { if (foreign->foreign_table == NULL) { dict_table_get(foreign->foreign_table_name, trx); } if (foreign->foreign_table) { mutex_enter(&(dict_sys->mutex)); (foreign->foreign_table ->n_foreign_key_checks_running)++; mutex_exit(&(dict_sys->mutex)); } /* NOTE that if the thread ends up waiting for a lock we will release dict_operation_lock temporarily! But the counter on the table protects 'foreign' from being dropped while the check is running. */ err = row_ins_check_foreign_constraint(FALSE, foreign, table, entry, thr); if (foreign->foreign_table) { mutex_enter(&(dict_sys->mutex)); ut_a(foreign->foreign_table ->n_foreign_key_checks_running > 0); (foreign->foreign_table ->n_foreign_key_checks_running)--; mutex_exit(&(dict_sys->mutex)); } if (err != DB_SUCCESS) { if (got_s_lock) { row_mysql_unfreeze_data_dictionary( trx); } mem_heap_free(heap); return(err); } } foreign = UT_LIST_GET_NEXT(referenced_list, foreign); } if (got_s_lock) { row_mysql_unfreeze_data_dictionary(trx); } mem_heap_free(heap); return(DB_SUCCESS);}/*************************************************************************Creates an update node for a query graph. */upd_node_t*upd_node_create(/*============*/ /* out, own: update node */ mem_heap_t* heap) /* in: mem heap where created */{ upd_node_t* node; node = mem_heap_alloc(heap, sizeof(upd_node_t)); node->common.type = QUE_NODE_UPDATE; node->state = UPD_NODE_UPDATE_CLUSTERED; node->select_will_do_update = FALSE; node->in_mysql_interface = FALSE; node->row = NULL; node->ext_vec = NULL; node->index = NULL; node->update = NULL; node->foreign = NULL; node->cascade_heap = NULL; node->cascade_node = NULL; node->select = NULL; node->heap = mem_heap_create(128); node->magic_n = UPD_NODE_MAGIC_N; node->cmpl_info = 0; return(node);}/*************************************************************************Updates the trx id and roll ptr field in a clustered index record in databaserecovery. */voidrow_upd_rec_sys_fields_in_recovery(/*===============================*/ rec_t* rec, /* in: record */ const ulint* offsets,/* in: array returned by rec_get_offsets() */ ulint pos, /* in: TRX_ID position in rec */ dulint trx_id, /* in: transaction id */ dulint roll_ptr)/* in: roll ptr of the undo log record */{ byte* field; ulint len; field = rec_get_nth_field(rec, offsets, pos, &len); ut_ad(len == DATA_TRX_ID_LEN); trx_write_trx_id(field, trx_id); field = rec_get_nth_field(rec, offsets, pos + 1, &len); ut_ad(len == DATA_ROLL_PTR_LEN); trx_write_roll_ptr(field, roll_ptr);}/*************************************************************************Sets the trx id or roll ptr field of a clustered index entry. */voidrow_upd_index_entry_sys_field(/*==========================*/ dtuple_t* entry, /* in: index entry, where the memory buffers for sys fields are already allocated: the function just copies the new values to them */ dict_index_t* index, /* in: clustered index */ ulint type, /* in: DATA_TRX_ID or DATA_ROLL_PTR */ dulint val) /* in: value to write */{ dfield_t* dfield; byte* field; ulint pos; ut_ad(index->type & DICT_CLUSTERED); pos = dict_index_get_sys_col_pos(index, type); dfield = dtuple_get_nth_field(entry, pos); field = dfield_get_data(dfield); if (type == DATA_TRX_ID) { trx_write_trx_id(field, val); } else { ut_ad(type == DATA_ROLL_PTR); trx_write_roll_ptr(field, val); }}/***************************************************************Returns TRUE if row update changes size of some field in index or if somefield to be updated is stored externally in rec or update. */iboolrow_upd_changes_field_size_or_external(/*===================================*/ /* out: TRUE if the update changes the size of some field in index or the field is external in rec or update */ dict_index_t* index, /* in: index */ const ulint* offsets,/* in: rec_get_offsets(rec, index) */ upd_t* update) /* in: update vector */{ upd_field_t* upd_field; dfield_t* new_val; ulint old_len; ulint new_len; ulint n_fields; ulint i; ut_ad(rec_offs_validate(NULL, index, offsets)); n_fields = upd_get_n_fields(update); for (i = 0; i < n_fields; i++) { upd_field = upd_get_nth_field(update, i); new_val = &(upd_field->new_val); new_len = new_val->len; if (new_len == UNIV_SQL_NULL && !rec_offs_comp(offsets)) { /* A bug fixed on Dec 31st, 2004: we looked at the SQL NULL size from the wrong field! We may backport this fix also to 4.0. The merge to 5.0 will be made manually immediately after we commit this to 4.1. */ new_len = dtype_get_sql_null_size( dict_index_get_nth_type(index, upd_field->field_no)); } old_len = rec_offs_nth_size(offsets, upd_field->field_no); if (rec_offs_comp(offsets) && rec_offs_nth_sql_null(offsets, upd_field->field_no)) { /* Note that in the compact table format, for a variable length field, an SQL NULL will use zero bytes in the offset array at the start of the physical record, but a zero-length value (empty string) will use one byte! Thus, we cannot use update-in-place if we update an SQL NULL varchar to an empty string! */ old_len = UNIV_SQL_NULL; } if (old_len != new_len) { return(TRUE); } if (rec_offs_nth_extern(offsets, upd_field->field_no)) { return(TRUE); } if (upd_field->extern_storage) { return(TRUE); } } return(FALSE);}/***************************************************************Replaces the new column values stored in the update vector to the recordgiven. No field size changes are allowed. This function is used only fora clustered index */voidrow_upd_rec_in_place(/*=================*/ rec_t* rec, /* in/out: record where replaced */ const ulint* offsets,/* in: array returned by rec_get_offsets() */ upd_t* update) /* in: update vector */{ upd_field_t* upd_field; dfield_t* new_val; ulint n_fields; ulint i; ut_ad(rec_offs_validate(rec, NULL, offsets)); rec_set_info_bits(rec, rec_offs_comp(offsets), update->info_bits); n_fields = upd_get_n_fields(update); for (i = 0; i < n_fields; i++) { upd_field = upd_get_nth_field(update, i); new_val = &(upd_field->new_val); rec_set_nth_field(rec, offsets, upd_field->field_no, dfield_get_data(new_val), dfield_get_len(new_val)); }}/*************************************************************************Writes into the redo log the values of trx id and roll ptr and enough infoto determine their positions within a clustered index record. */byte*row_upd_write_sys_vals_to_log(/*==========================*/ /* out: new pointer to mlog */ dict_index_t* index, /* in: clustered index */ trx_t* trx, /* in: transaction */ dulint roll_ptr,/* in: roll ptr of the undo log record */ byte* log_ptr,/* pointer to a buffer of size > 20 opened in mlog */ mtr_t* mtr __attribute__((unused))) /* in: mtr */{ ut_ad(index->type & DICT_CLUSTERED); ut_ad(mtr); log_ptr += mach_write_compressed(log_ptr, dict_index_get_sys_col_pos(index, DATA_TRX_ID)); trx_write_roll_ptr(log_ptr, roll_ptr); log_ptr += DATA_ROLL_PTR_LEN; log_ptr += mach_dulint_write_compressed(log_ptr, trx->id); return(log_ptr);}/*************************************************************************Parses the log data of system field values. */byte*row_upd_parse_sys_vals(/*===================*/ /* out: log data end or NULL */ byte* ptr, /* in: buffer */ byte* end_ptr,/* in: buffer end */ ulint* pos, /* out: TRX_ID position in record */ dulint* trx_id, /* out: trx id */ dulint* roll_ptr)/* out: roll ptr */{ ptr = mach_parse_compressed(ptr, end_ptr, pos); if (ptr == NULL) { return(NULL); } if (end_ptr < ptr + DATA_ROLL_PTR_LEN) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -