/* What kind of latch and lock can we assume when the control comes to ------------------------------------------------------------------- anupdatenode? -------------- Efficiencyofmassiveupdateswouldrequirekeepinganx-latchona clusteredindexpagethroughmanyupdates,andnotsettinganexplicit x-lockonclusteredindexrecords,astheyanywaywillgetanimplicit x-lockwhentheyareupdated.Aproblemisthatthereadnodesinthe graphshouldknowthattheymustkeepthelatchwhenpassingthecontrol uptotheupdatenode,andnotsetanyrecordlockontherecordwhich willbeupdated.Anotherproblemoccursiftheexecutionisstopped, asthekernelswitchestoanotherquerythread,orthetransactionmust waitforalock.Thenweshouldbeabletoreleasethelatchand,maybe, acquireanexplicitx-lockontherecord. Becausethisseemstoocomplicated,weconcludethattheless efficientsolutionofreleasingallthelatcheswhenthecontrolis
transferred to another node, and acquiring explicit x-locks, is better. */
/* How is a delete performed? If there is a delete without an explicitcursor,i.e.,asearcheddelete,thereareatleast twodifferentsituations: theimplicitselectcursormayrunon(1)theclusteredindexor on(2)asecondaryindex.Thedeleteisperformedbysetting thedeletebitintherecordandsubstitutingtheidofthe deletingtransactionfortheoriginaltrxid,andsubstitutinga newrollptrforpreviousrollptr.Theoldtrxidandrollptr aresavedintheundologrecord.Thus,nophysicalchangesoccur intheindextreestructureatthetimeofthedelete.Only whentheundologispurged,theindexrecordswillbephysically deletedfromtheindextrees.
/************************************************************************* IMPORTANTNOTE:AnyoperationthatgeneratesredoMUSTcheckthatthere isenoughspaceintheredologbeforeforthatoperation.Thisis donebycallinglog_free_check().Thereasonforcheckingthe availabilityoftheredologspacebeforethestartoftheoperationis thatweMUSTnotholdanysynchonizationobjectswhenperformingthe check. Ifyoumakeachangeinthismodulemakesurethatnocodepathis
introduced where a call to log_free_check() is bypassed. */
/***********************************************************//**
Checks if an update vector changes some of the first ordering fields of an
index record. This is only used in foreign key checks and we can assume
that index does not contain column prefixes.
@returnTRUEif changes */ static
ibool
row_upd_changes_first_fields_binary( /*================================*/
dtuple_t* entry, /*!< in: old value of index entry */
dict_index_t* index, /*!< in: index of entry */ const 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 foreign
key constraint.
@returntrueif referenced */ static bool
row_upd_index_is_referenced( /*========================*/
dict_index_t* index, /*!< in: index */
trx_t* trx) /*!< in: transaction */
{
dict_table_t *table= index->table; /* The pointers in table->referenced_set are safe to dereference
thanks to the SQL layer having acquired MDL on all (grand)parent tables. */
dict_foreign_set::iterator end= table->referenced_set.end(); return end != std::find_if(table->referenced_set.begin(), end,
dict_foreign_with_index(index));
}
/* No MDL protects dereferencing the members of table->foreign_set. */ constbool no_lock= !trx->dict_operation_lock_mode; if (no_lock)
dict_sys.freeze(SRW_LOCK_CALL);
auto end= table->foreign_set.end(); constbool is_referenced= end !=
std::find_if(table->foreign_set.begin(), end,
[index](const dict_foreign_t* f)
{return f->foreign_index == index;}); if (no_lock)
dict_sys.unfreeze();
return is_referenced;
} #endif/* WITH_WSREP */
/*********************************************************************//**
Checks if possible foreign key constraints hold after a delete of the record
under pcur.
NOTE that this function will temporarily commit mtr and lose the
pcur position!
@return DB_SUCCESS or an error code */ static MY_ATTRIBUTE((nonnull, warn_unused_result))
dberr_t
row_upd_check_references_constraints( /*=================================*/
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 */
rec_offs* offsets,/*!< in/out: rec_get_offsets(pcur.rec, index) */
que_thr_t* thr, /*!< in: query thread */
mtr_t* mtr) /*!< in: mtr */
{
dict_foreign_t* foreign;
mem_heap_t* heap;
dtuple_t* entry; const rec_t* rec;
dberr_t err;
for (dict_foreign_set::iterator it = table->referenced_set.begin();
it != table->referenced_set.end();
++it) {
foreign = *it;
/* Note that we may have an update which updates the index record,butdoesNOTupdatethefirstfieldswhichare referencedinaforeignkeyconstraint.Thentheupdatedoes
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))) {
dict_table_t* ref_table = nullptr;
if (!foreign->foreign_table) {
ref_table = dict_table_open_on_name(
foreign->foreign_table_name_lookup, false, DICT_ERR_IGNORE_NONE);
}
for (dict_foreign_set::iterator it = table->foreign_set.begin();
it != table->foreign_set.end();
++it) {
foreign = *it; /* Note that we may have an update which updates the index record,butdoesNOTupdatethefirstfieldswhichare referencedinaforeignkeyconstraint.Thentheupdatedoes
NOT break the constraint. */
if (foreign->foreign_index == index
&& (node->is_delete
|| row_upd_changes_first_fields_binary(
entry, index, node->update,
foreign->n_fields))) {
dict_table_t *opened = nullptr;
if (!foreign->referenced_table) {
foreign->referenced_table =
dict_table_open_on_name(
foreign->referenced_table_name_lookup, false, DICT_ERR_IGNORE_NONE);
opened = foreign->referenced_table;
}
/** Determine if a FOREIGN KEY constraint needs to be processed. @param[in]nodequerynode @param[in]trxtransaction
@return whether the node cannot be ignored */
/*********************************************************************//**
Creates an update node for a query graph.
@return own: update node */
upd_node_t*
upd_node_create( /*============*/
mem_heap_t* heap) /*!< in: mem heap where created */
{
upd_node_t* node;
/***********************************************************//**
Returns TRUEif row update changes size of some field in index orif some
field to be updated is stored externally in rec or update.
@returnTRUEif the update changes the size of some field in index or
the field is external in rec or update */
ibool
row_upd_changes_field_size_or_external( /*===================================*/
dict_index_t* index, /*!< in: index */ const rec_offs* offsets,/*!< in: rec_get_offsets(rec, index) */ const upd_t* update) /*!< in: update vector */
{ const upd_field_t* upd_field; const dfield_t* new_val;
ulint old_len;
ulint new_len;
ulint n_fields;
ulint i;
if (rec_offs_nth_default(offsets, upd_field->field_no)) { /* This is an instantly added column that is
at the initial default value. */ return(TRUE);
}
if (rec_offs_comp(offsets)
&& rec_offs_nth_sql_null(offsets, upd_field->field_no)) { /* Note that in the compact table format, for a variablelengthfield,anSQLNULLwillusezero bytesintheoffsetarrayatthestartofthephysical record,butazero-lengthvalue(emptystring)will useonebyte!Thus,wecannotuseupdate-in-place
if we update an SQL NULL varchar to an empty string! */
if (old_len != new_len
|| rec_offs_nth_extern(offsets, upd_field->field_no)) {
return(TRUE);
}
}
return(FALSE);
}
/***************************************************************//**
Builds an update vector from those fields which in a secondary index entry
differ from a record that has the equal ordering fields. NOTE: we compare
the fields as binary strings!
@return own: update vector of differing fields */
upd_t*
row_upd_build_sec_rec_difference_binary( /*====================================*/ const rec_t* rec, /*!< in: secondary index record */
dict_index_t* index, /*!< in: index */ const rec_offs* offsets,/*!< in: rec_get_offsets(rec, index) */ const dtuple_t* entry, /*!< in: entry to insert */
mem_heap_t* heap) /*!< in: memory heap from which allocated */
{
upd_field_t* upd_field; const dfield_t* dfield; const byte* data;
ulint len;
upd_t* update;
ulint n_diff;
/* This function is used only for a secondary index */
ut_a(!dict_index_is_clust(index));
ut_ad(rec_offs_validate(rec, index, offsets));
ut_ad(rec_offs_n_fields(offsets) == dtuple_get_n_fields(entry));
ut_ad(!rec_offs_any_extern(offsets));
ut_ad(!rec_offs_any_default(offsets));
ut_ad(!index->table->skip_alter_undo);
for (uint16_t i = 0; i < dtuple_get_n_fields(entry); i++) {
data = rec_get_nth_field(rec, offsets, i, &len);
dfield = dtuple_get_nth_field(entry, i);
/* NOTE that it may be that len != dfield_get_len(dfield) if we areupdatinginacharactersetandcollationwherestringsof differentlengthcanbeequalinanalphabeticalcomparison, andalsointhecasewherewehaveacolumnprefixindex andthelastcharactersintheindexfieldarespaces;the lattercaseprobablycausedtheassertionfailuresreportedat
row0upd.cc line 713 in versions 4.0.14 - 4.0.16. */
/* NOTE: we compare the fields as binary strings!
(No collation) */
if (!dfield_data_is_binary_equal(dfield, len, data)) {
upd_field = upd_get_nth_field(update, n_diff);
dfield_copy(&(upd_field->new_val), dfield);
upd_field_set_field_no(upd_field, i, index);
n_diff++;
}
}
update->n_fields = n_diff;
return(update);
}
/** Builds an update vector from those fields, excluding the roll ptr and trxidfields,whichinanindexentrydifferfromarecordthathas theequalorderingfields.NOTE:wecomparethefieldsasbinarystrings! @param[in]indexclusteredindex @param[in]entryclusteredindexentrytoinsert @param[in]recclusteredindexrecord @param[in]offsetsrec_get_offsets(rec,index),orNULL @param[in]no_sysskipthesystemcolumns DB_TRX_IDandDB_ROLL_PTR @param[in]trxtransaction(fordiagnostics), orNULL @param[in]heapmemoryheapfromwhichallocated @param[in]mysql_tableNULL,ormysqltableobjectwhen userthreadinvokesdml @param[out]errorerrornumberincaseoffailure @returnown:updatevectorofdifferingfields,excludingrollptrand
trx id,if error is not equal to DB_SUCCESS, return NULL */
upd_t*
row_upd_build_difference_binary(
dict_index_t* index, const dtuple_t* entry, const rec_t* rec, const rec_offs* offsets, bool no_sys, bool ignore_warnings,
trx_t* trx,
mem_heap_t* heap,
TABLE* mysql_table,
dberr_t* error)
{
ulint len;
upd_t* update;
ulint n_diff;
rec_offs offsets_[REC_OFFS_NORMAL_SIZE]; const ulint n_v_fld = dtuple_get_n_v_fields(entry);
rec_offs_init(offsets_);
/* This function is used only for a clustered index */
ut_a(dict_index_is_clust(index));
ut_ad(!index->table->skip_alter_undo);
ut_ad(entry->n_fields <= index->n_fields);
ut_ad(entry->n_fields >= index->n_core_fields);
for (uint16_t i = 0; i < entry->n_fields; i++) { const byte* data = rec_get_nth_cfield(rec, index, offsets, i,
&len); const dfield_t* dfield = dtuple_get_nth_field(entry, i);
/* NOTE: we compare the fields as binary strings!
(No collation) */ if (no_sys && (i == index->db_trx_id()
|| i == index->db_roll_ptr())) { continue;
}
for (uint16_t i = static_cast<uint16_t>(entry->n_fields);
i < index->n_fields; i++) {
upd_field_t* uf = upd_get_nth_field(update, n_diff++); const dict_col_t* col = dict_index_get_nth_col(index, i); /* upd_create() zero-initialized uf */
uf->new_val.data = const_cast<byte*>(col->instant_value(&len));
uf->new_val.len = static_cast<unsigned>(len);
dict_col_copy_type(col, &uf->new_val.type);
upd_field_set_field_no(uf, i, index);
}
/* Check the virtual columns updates. Even if there is no non-virtual column(basecolumns)change,wewillstillneedtobuildthe indexedvirtualcolumnvaluesothatundologwouldlogthem(
for purge/mvcc purpose) */ if (n_v_fld > 0) {
row_ext_t* ext;
THD* thd;
/** Replaces the new column value stored in the update vector in thegivenindexentryfield. @param[in,out]dfielddatafieldoftheindexentry @param[in]fieldindexfield @param[in]colfield->col @param[in]ufupdatefield @param[in,out]heapmemoryheapforallocatingandcopying thenewvalue @param[in]zip_sizeROW_FORMAT=COMPRESSEDpagesize,or0
@return whether the previous version was built successfully */
MY_ATTRIBUTE((nonnull, warn_unused_result)) static bool
row_upd_index_replace_new_col_val(
dfield_t* dfield, const dict_field_t* field, const dict_col_t* col, const upd_field_t* uf,
mem_heap_t* heap,
ulint zip_size)
{
ulint len; const byte* data;
dfield_copy_data(dfield, &uf->new_val);
if (dfield_is_null(dfield)) { returntrue;
}
len = dfield_get_len(dfield);
data = static_cast<const byte*>(dfield_get_data(dfield));
if (field->prefix_len > 0) {
ibool fetch_ext = dfield_is_ext(dfield)
&& len < (ulint) field->prefix_len
+ BTR_EXTERN_FIELD_REF_SIZE;
if (fetch_ext) {
ulint l = len;
len = field->prefix_len;
data = row_upd_ext_fetch(data, l, zip_size,
&len, heap); if (UNIV_UNLIKELY(!data)) { returnfalse;
}
}
len = dtype_get_at_most_n_mbchars(col->prtype,
col->mbminlen, col->mbmaxlen,
field->prefix_len, len,
(constchar*) data);
dfield_set_data(dfield, data, len);
if (!fetch_ext) {
dfield_dup(dfield, heap);
}
returntrue;
}
switch (uf->orig_len) {
byte* buf; case BTR_EXTERN_FIELD_REF_SIZE: /* Restore the original locally stored partofthecolumn.Intheundolog, InnoDBwritesalongerprefixofexternally storedcolumns,sothatcolumnprefixes
in secondary indexes can be reconstructed. */
dfield_set_data(dfield,
data + len - BTR_EXTERN_FIELD_REF_SIZE,
BTR_EXTERN_FIELD_REF_SIZE);
dfield_set_ext(dfield); /* fall through */ case0:
dfield_dup(dfield, heap); break; default: /* Reconstruct the original locally storedpartofthecolumn.Thedata
will have to be copied. */
ut_a(uf->orig_len > BTR_EXTERN_FIELD_REF_SIZE);
buf = static_cast<byte*>(mem_heap_alloc(heap, uf->orig_len));
/***********************************************************//**
Checks if an update vector changes an ordering field of an index record.
This function is fast if the update vector is shortor the number of ordering
fields in the index is small. Otherwise, this can be quadratic.
NOTE: we compare the fields as binary strings!
@returnTRUEif update vector changes an ordering field in the index record */
ibool
row_upd_changes_ord_field_binary_func( /*==================================*/
dict_index_t* index, /*!< in: index of the record */ const upd_t* update, /*!< in: update vector for the row; NOTE: the fieldnumbersinthisMUSTbeclusteredindex
positions! */ #ifdef UNIV_DEBUG const que_thr_t*thr, /*!< in: query thread */ #endif/* UNIV_DEBUG */ const dtuple_t* row, /*!< in: old value of row, or NULL if the rowandthedatavaluesinupdatearenot knownwhenthisfunctioniscalled,e.g.,at
compile time */ const row_ext_t*ext, /*!< NULL, or prefixes of the externally
stored columns in the old row */
ulint flag) /*!< in: ROW_BUILD_NORMAL,
ROW_BUILD_FOR_PURGE or ROW_BUILD_FOR_UNDO */
{
ulint n_unique;
ulint i; const dict_index_t* clust_index;
/* Get the old mbr. */ if (dfield_is_ext(dfield)) { /* For off-page stored data, we
need to read the whole field data. */
flen = dfield_get_len(dfield);
dptr = static_cast<const byte*>(
dfield_get_data(dfield));
temp_heap = mem_heap_create(1000);
/* This treatment of column prefix indexes is loosely
based on row_build_index_entry(). */
if (UNIV_LIKELY(ind_field->prefix_len == 0)
|| dfield_is_null(dfield)) { /* do nothing special */
} elseif (ext) { /* Silence a compiler warning without
silencing a Valgrind error. */
dfield_len = 0;
MEM_UNDEFINED(&dfield_len, sizeof dfield_len); /* See if the column is stored externally. */
buf = row_ext_lookup(ext, col_no, &dfield_len);
ut_ad(col->ord_part);
if (UNIV_LIKELY_NULL(buf)) { if (UNIV_UNLIKELY(buf == field_ref_zero)) { /* The externally stored field wasnotwrittenyet.InnoDBmust haveranoutofspaceorbeenkilled
before storing the page */
ut_ad(thr);
ut_ad(thr->graph->trx->in_rollback); return(TRUE);
}
if (!dfield_datas_are_binary_equal(
dfield, &upd_field->new_val,
ind_field->prefix_len)) {
return(TRUE);
}
}
return(FALSE);
}
/***********************************************************//**
Checks if an update vector changes an ordering field of an index record.
NOTE: we compare the fields as binary strings!
@returnTRUEif update vector may change an ordering field in an index
record */
ibool
row_upd_changes_some_index_ord_field_binary( /*========================================*/ const dict_table_t* table, /*!< in: table */ const upd_t* update) /*!< in: update vector for the row */
{
upd_field_t* upd_field;
dict_index_t* index;
ulint i;
index = dict_table_get_first_index(table);
for (i = 0; i < upd_get_n_fields(update); i++) {
upd_field = upd_get_nth_field(update, i);
if (upd_fld_is_virtual_col(upd_field)) { if (dict_table_get_nth_v_col(index->table,
upd_field->field_no)
->m_col.ord_part) { return(TRUE);
}
} else { if (dict_field_get_col(dict_index_get_nth_field(
index, upd_field->field_no))->ord_part) { return(TRUE);
}
}
}
return(FALSE);
}
/***********************************************************//**
Checks if an FTS Doc ID column is affected by an UPDATE.
@return whether the Doc ID column is changed */ bool
row_upd_changes_doc_id( /*===================*/
dict_table_t* table, /*!< in: table */
upd_field_t* upd_field) /*!< in: field to check */
{
ulint col_no;
dict_index_t* clust_index;
fts_t* fts = table->fts;
ut_ad(!table->skip_alter_undo);
clust_index = dict_table_get_first_index(table);
/* Convert from index-specific column number to table-global
column number. */
col_no = dict_index_get_nth_col_no(clust_index, upd_field->field_no);
return(col_no == fts->doc_col);
} /***********************************************************//**
Checks if an FTS indexed column is affected by an UPDATE.
@return offset within fts_t::indexes if FTS indexed column updated else
ULINT_UNDEFINED */
ulint
row_upd_changes_fts_column( /*=======================*/
dict_table_t* table, /*!< in: table */
upd_field_t* upd_field) /*!< in: field to check */
{
ulint col_no;
dict_index_t* clust_index;
fts_t* fts = table->fts;
/* Convert from index-specific column number to table-global
column number. */
col_no = dict_index_get_nth_col_no(clust_index,
upd_field->field_no); return(dict_table_is_fts_column(fts->indexes, col_no, false));
}
}
/***********************************************************//**
Checks if an update vector changes some of the first ordering fields of an
index record. This is only used in foreign key checks and we can assume
that index does not contain column prefixes.
@returnTRUEif changes */ static
ibool
row_upd_changes_first_fields_binary( /*================================*/
dtuple_t* entry, /*!< in: index entry */
dict_index_t* index, /*!< in: index of entry */ const upd_t* update, /*!< in: update vector for the row */
ulint n) /*!< in: how many first fields to check */
{
ulint n_upd_fields;
ulint i, j;
dict_index_t* clust_index;
/*********************************************************************//**
Copies the column values from a record. */
UNIV_INLINE void
row_upd_copy_columns( /*=================*/
rec_t* rec, /*!< in: record in a clustered index */ const rec_offs* offsets,/*!< in: array returned by rec_get_offsets() */ const dict_index_t* index, /*!< in: index of rec */
sym_node_t* column) /*!< in: first column in a column list, or
NULL */
{
ut_ad(dict_index_is_clust(index));
const byte* data;
ulint len;
while (column) {
data = rec_get_nth_cfield(
rec, index, offsets,
column->field_nos[SYM_CLUST_FIELD_NO], &len);
eval_node_copy_and_alloc_val(column, data, len);
/*********************************************************************//**
Calculates the new values for fields to update. Note that row_upd_copy_columns
must have been called first. */
UNIV_INLINE void
row_upd_eval_new_vals( /*==================*/
upd_t* update) /*!< in/out: update vector */
{
que_node_t* exp;
upd_field_t* upd_field;
ulint n_fields;
ulint i;
n_fields = upd_get_n_fields(update);
for (i = 0; i < n_fields; i++) {
upd_field = upd_get_nth_field(update, i);
/** Stores to the heap the virtual columns that need for any indexes @param[in,out]noderowupdatenode @param[in]updateanupdatevectorifitisupdate @param[in]thdmysqlthreadhandle @param[in,out]mysql_tablemysqltableobject @returntrueifsuccess
false if virtual column value computation fails. */ static bool
row_upd_store_v_row(
upd_node_t* node, const upd_t* update,
THD* thd,
TABLE* mysql_table)
{
dict_index_t* index = dict_table_get_first_index(node->table);
ib_vcol_row vc(NULL);
for (ulint col_no = 0; col_no < dict_table_get_n_v_cols(node->table);
col_no++) {
const dict_v_col_t* col
= dict_table_get_nth_v_col(node->table, col_no);
if (col->m_col.ord_part) {
dfield_t* dfield
= dtuple_get_nth_v_field(node->row, col_no);
ulint n_upd
= update ? upd_get_n_fields(update) : 0;
ulint i = 0;
/* Check if the value is already in update vector */ for (i = 0; i < n_upd; i++) { const upd_field_t* upd_field
= upd_get_nth_field(update, i); if (!(upd_field->new_val.type.prtype
& DATA_VIRTUAL)
|| upd_field->field_no != col->v_pos) { continue;
}
/* Not updated */ if (i >= n_upd) { /* If this is an update, then the value
should be in update->old_vrow */ if (update) { if (update->old_vrow == NULL) { /* This only happens in cascadeupdate.Andvirtual columncan'tbeaffected,
so it is Ok to set it to NULL */
dfield_set_null(dfield);
} else {
dfield_t* vfield
= dtuple_get_nth_v_field(
update->old_vrow,
col_no);
dfield_copy_data(dfield, vfield);
dfield_dup(dfield, node->heap);
}
} else {
uchar *record = vc.record(thd, index,
&mysql_table); /* Need to compute, this happens when
deleting row */
dfield_t* vfield =
innobase_get_computed_value(
node->row, col, index,
&vc.heap, node->heap,
NULL, thd, mysql_table,
record, NULL, NULL); if (vfield == NULL) { returnfalse;
}
}
}
}
}
returntrue;
}
/** Stores to the heap the row on which the node->pcur is positioned. @param[in]noderowupdatenode @param[in]thdmysqlthreadhandle @param[in,out]mysql_tableNULL,ormysqltableobjectwhen userthreadinvokesdml @returnfalseifvirtualcolumnvaluecomputationfails
true otherwise. */ static bool
row_upd_store_row(
upd_node_t* node,
THD* thd,
TABLE* mysql_table)
{
dict_index_t* clust_index;
rec_t* rec;
mem_heap_t* heap = NULL;
row_ext_t** ext;
rec_offs offsets_[REC_OFFS_NORMAL_SIZE]; const rec_offs* offsets;
rec_offs_init(offsets_);
ut_ad(node->pcur->latch_mode != BTR_NO_LATCHES);
if (node->row != NULL) {
mem_heap_empty(node->heap);
}
if (dict_table_has_atomic_blobs(node->table)) { /* There is no prefix of externally stored columns in theclusteredindexrecord.Buildacacheofcolumn
prefixes. */
ext = &node->ext;
} else { /* REDUNDANT and COMPACT formats store a local 768-byteprefixofeachexternallystoredcolumn.
No cache is needed. */
ext = NULL;
node->ext = NULL;
}
if (pcur.btr_cur.rtr_info->fd_del) { /* We found the record, but a delete marked */ goto close;
}
goto not_found;
} elseif (!row_search_index_entry(entry, BTR_MODIFY_LEAF,
&pcur, &mtr)) {
not_found:
rec = btr_pcur_get_rec(&pcur);
ib::error()
<< "Record in index " << index->name
<< " of table " << index->table->name
<< " was not found on update: " << *entry
<< " at: " << rec_index_print(rec, index); #ifdef UNIV_DEBUG
mtr_commit(&mtr);
mtr_start(&mtr);
ut_ad(btr_validate_index(index, mtr.trx) == DB_SUCCESS);
ut_ad(0); #endif/* UNIV_DEBUG */
} else {
found:
ut_ad(err == DB_SUCCESS);
rec = btr_pcur_get_rec(&pcur);
/* Delete mark the old index record; it can already be deletemarkedifwereturnafteralockwaitin
row_ins_sec_index_entry() below */ if (!rec_get_deleted_flag(
rec, dict_table_is_comp(index->table))) {
err = lock_sec_rec_modify_check_and_lock(
flags,
btr_pcur_get_block(&pcur),
btr_pcur_get_rec(&pcur), index, thr, &mtr); if (err != DB_SUCCESS) { goto close;
}
/* NOTE that the following call loses
the position of pcur ! */
err = row_upd_check_references_constraints(
node, &pcur, index->table,
index, offsets, thr, &mtr);
}
}
close:
btr_pcur_close(&pcur);
mtr_commit(&mtr);
if (node->is_delete == PLAIN_DELETE || err != DB_SUCCESS) {
/* Build a new index entry */
entry = row_build_index_entry(node->upd_row, node->upd_ext,
index, heap);
ut_a(entry);
/* Insert new index entry */
err = row_ins_sec_index_entry(index, entry, thr, !node->is_delete);
func_exit:
mem_heap_free(heap);
return(err);
}
/***********************************************************//**
Updates the secondary index record if it is changed in the row update or
deletes it ifthis is a delete.
@return DB_SUCCESS if operation successfully completed, else error
code or DB_LOCK_WAIT */ static MY_ATTRIBUTE((nonnull, warn_unused_result))
dberr_t
row_upd_sec_step( /*=============*/
upd_node_t* node, /*!< in: row update node */
que_thr_t* thr) /*!< in: query thread */
{
ut_ad((node->state == UPD_NODE_UPDATE_ALL_SEC)
|| (node->state == UPD_NODE_UPDATE_SOME_SEC));
ut_ad(!dict_index_is_clust(node->index));
#ifdef UNIV_DEBUG # define row_upd_clust_rec_by_insert_inherit(rec,index,offsets,entry,update) \
row_upd_clust_rec_by_insert_inherit_func(rec,index,offsets,entry,update) #else/* UNIV_DEBUG */ # define row_upd_clust_rec_by_insert_inherit(rec,index,offsets,entry,update) \
row_upd_clust_rec_by_insert_inherit_func(rec,entry,update) #endif/* UNIV_DEBUG */ /*******************************************************************//**
Mark non-updated off-page columns inherited when the primary key is
updated. We must mark them as inherited in entry, so that they are not
freed in a rollback. A limited version of this function used to be
called btr_cur_mark_dtuple_inherited_extern().
@return whether any columns were inherited */ static bool
row_upd_clust_rec_by_insert_inherit_func( /*=====================================*/ const rec_t* rec, /*!< in: old record, or NULL */ #ifdef UNIV_DEBUG
dict_index_t* index, /*!< in: index, or NULL */ const rec_offs* offsets,/*!< in: rec_get_offsets(rec), or NULL */ #endif/* UNIV_DEBUG */
dtuple_t* entry, /*!< in/out: updated entry to be
inserted into the clustered index */ const upd_t* update) /*!< in: update vector */
{ bool inherit = false;
/* The pointer must not be zero. */
ut_ad(memcmp(rec_data, field_ref_zero,
BTR_EXTERN_FIELD_REF_SIZE)); /* The BLOB must be owned. */
ut_ad(!(rec_data[BTR_EXTERN_LEN]
& BTR_EXTERN_OWNER_FLAG));
} #endif/* UNIV_DEBUG */
len = dfield_get_len(dfield);
ut_a(len != UNIV_SQL_NULL);
ut_a(len >= BTR_EXTERN_FIELD_REF_SIZE);
data = static_cast<byte*>(dfield_get_data(dfield));
data += len - BTR_EXTERN_FIELD_REF_SIZE; /* The pointer must not be zero. */
ut_a(memcmp(data, field_ref_zero, BTR_EXTERN_FIELD_REF_SIZE));
/* The BLOB must be owned, unless we are resuming from
a lock wait and we already had disowned the BLOB. */
ut_a(rec == NULL
|| !(data[BTR_EXTERN_LEN] & BTR_EXTERN_OWNER_FLAG));
data[BTR_EXTERN_LEN] &= byte(~BTR_EXTERN_OWNER_FLAG);
data[BTR_EXTERN_LEN] |= BTR_EXTERN_INHERITED_FLAG; /* The BTR_EXTERN_INHERITED_FLAG only matters in rollbackofafreshinsert.Purgewillalwaysfree
the extern fields of a delete-marked row. */
inherit = true;
}
return(inherit);
}
/** Mark 'disowned' BLOBs as 'owned' and 'inherited' again, afterresumingfromalockwait.
@param entry clustered index entry */ static ATTRIBUTE_COLD void row_upd_reown_inherited_fields(dtuple_t *entry)
{ for (ulint i= 0; i < entry->n_fields; i++)
{ const dfield_t *dfield= dtuple_get_nth_field(entry, i); if (dfield_is_ext(dfield))
{
byte *blob_len= static_cast<byte*>(dfield->data) +
dfield->len - (BTR_EXTERN_FIELD_REF_SIZE - BTR_EXTERN_LEN);
ut_ad(*blob_len & BTR_EXTERN_OWNER_FLAG);
*blob_len= byte((*blob_len & ~BTR_EXTERN_OWNER_FLAG) |
BTR_EXTERN_INHERITED_FLAG);
}
}
}
/***********************************************************//**
Marks the clustered index record deleted and inserts the updated version
of the record to the index. This function should be used when the ordering
fields of the clustered index record change. This should be quite rare in
database applications.
@return DB_SUCCESS if operation successfully completed, else error
code or DB_LOCK_WAIT */ static MY_ATTRIBUTE((nonnull, warn_unused_result))
dberr_t
row_upd_clust_rec_by_insert( /*========================*/
upd_node_t* node, /*!< in/out: row update node */
dict_index_t* index, /*!< in: clustered index of the record */
que_thr_t* thr, /*!< in: query thread */ bool referenced,/*!< in: whether index may be referenced in
a foreign key constraint */ #ifdef WITH_WSREP bool foreign,/*!< in: whether this is a foreign key */ #endif
mtr_t* mtr) /*!< in/out: mini-transaction,
may be committed and restarted */
{
mem_heap_t* heap;
btr_pcur_t* pcur;
btr_cur_t* btr_cur;
trx_t* trx;
dict_table_t* table;
dtuple_t* entry;
dberr_t err;
rec_t* rec;
rec_offs offsets_[REC_OFFS_NORMAL_SIZE];
rec_offs* offsets = offsets_;
switch (node->state) { default:
ut_error; case UPD_NODE_INSERT_CLUSTERED: /* A lock wait occurred in row_ins_clust_index_entry() in
the previous invocation of this function. */
row_upd_clust_rec_by_insert_inherit(
NULL, NULL, NULL, entry, node->update); break; case UPD_NODE_UPDATE_CLUSTERED: /* This is the first invocation of the function where weupdatetheprimarykey.Delete-marktheoldrecord
in the clustered index and prepare to insert a new entry. */
rec = btr_cur_get_rec(btr_cur);
offsets = rec_get_offsets(rec, index, offsets,
index->n_core_fields,
ULINT_UNDEFINED, &heap);
ut_ad(page_rec_is_user_rec(rec));
if (rec_get_deleted_flag(rec, rec_offs_comp(offsets))) { /* If the clustered index record is already delete marked,thenwearehereafteraDB_LOCK_WAIT. Skipdeletemarkingclusteredindexanddisowning itsblobs.MarktheBLOBsintheindexentry (whichwecopiedfromthealready"disowned"rec) as"owned",likeitwasonthepreviouscallof
row_upd_clust_rec_by_insert(). */
ut_ad(row_get_rec_trx_id(rec, index, offsets)
== trx->id);
ut_ad(!trx_undo_roll_ptr_is_insert(
row_get_rec_roll_ptr(rec, index,
offsets)));
row_upd_reown_inherited_fields(entry); goto check_fk;
}
/* If the the new row inherits externally stored fields(off-pagecolumnsa.k.a.BLOBs)fromthe delete-markedoldrecord,markthemdisownedbythe
old record and owned by the new entry. */
if (rec_offs_any_extern(offsets)) { if (row_upd_clust_rec_by_insert_inherit(
rec, index, offsets,
entry, node->update)) { /* The blobs are disowned here, expecting the insertdownbelowtoinheritthem.Butifthe insertfails,thenthisdisownwillbeundone
when the operation is rolled back. */
btr_cur_disown_inherited_fields(
btr_cur_get_block(btr_cur),
rec, index, offsets, node->update,
mtr);
}
}
check_fk: if (referenced) { /* NOTE that the following call loses
the position of pcur ! */
switch (err) { case DB_SUCCESS: case DB_NO_REFERENCED_ROW:
err = DB_SUCCESS; break; case DB_LOCK_WAIT: case DB_DEADLOCK: case DB_LOCK_WAIT_TIMEOUT:
WSREP_DEBUG("Foreign key check fail: " "%s on table %s index %s query %s",
ut_strerr(err), index->name(), index->table->name.m_name,
wsrep_thd_query(trx->mysql_thd));
goto err_exit; default:
WSREP_ERROR("Foreign key check fail: " "%s on table %s index %s query %s",
ut_strerr(err), index->name(), index->table->name.m_name,
wsrep_thd_query(trx->mysql_thd));
if (buf_pool.running_out()) {
err = DB_LOCK_TABLE_FULL; goto func_exit;
}
/* We may have to modify the tree structure: do a pessimistic descent
down the index tree */
mtr->commit();
mtr->start();
if (index->table->is_temporary()) { /* Disable locking, because temporary tables are never
shared between transactions or connections. */
flags |= BTR_NO_LOCKING_FLAG;
mtr->set_log_mode(MTR_LOG_NO_REDO);
} else {
index->set_modified(*mtr);
}
/* NOTE: this transaction has an s-lock or x-lock on the record and thereforeothertransactionscannotmodifytherecordwhenwehaveno latchonthepage.Inaddition,weassumethatotherquerythreadsof thesametransactiondonotmodifytherecordinthemeantime.
Therefore we can assert that the restoration of the cursor succeeds. */
/* We have to restore the cursor to its position */
mtr.start();
if (node->table->is_temporary()) { /* Disable locking, because temporary tables are
private to the connection (no concurrent access). */
flags = node->table->no_rollback()
? BTR_NO_ROLLBACK
: BTR_NO_LOCKING_FLAG; /* Redo logging only matters for persistent tables. */
mtr.set_log_mode(MTR_LOG_NO_REDO);
} else {
flags = node->table->no_rollback() ? BTR_NO_ROLLBACK : 0;
index->set_modified(mtr);
}
/* If the restoration does not succeed, then the same transactionhasdeletedtherecordonwhichthecursorwas, andthatisanSQLerror.Iftherestorationsucceeds,itmay stillbethatthesametransactionhassuccessivelydeleted andinsertedarecordwiththesameorderingfields,butin thatcaseweknowthatthetransactionhasatleastan
implicit x-lock on the record. */
/* If the update is made for MySQL, we already have the update vector
ready, else we have to do some evaluation: */
if (UNIV_UNLIKELY(!node->in_mysql_interface)) { /* Copy the necessary columns from clust_rec and calculate the
new values to set */
row_upd_copy_columns(rec, offsets, index,
UT_LIST_GET_FIRST(node->columns));
row_upd_eval_new_vals(node->update);
}
exit_func:
mtr.commit(); if (UNIV_LIKELY_NULL(heap)) {
mem_heap_free(heap);
} return err;
}
/***********************************************************//**
Updates the affected index records of a row. When the control is transferred
to this node, we assume that we have a persistent cursor which was on a
record, and the position of the cursor is stored in the cursor.
@return DB_SUCCESS if operation successfully completed, else error
code or DB_LOCK_WAIT */ static
dberr_t
row_upd( /*====*/
upd_node_t* node, /*!< in: row update node */
que_thr_t* thr) /*!< in: query thread */
{
dberr_t err = DB_SUCCESS;
DBUG_ENTER("row_upd");
/***********************************************************//**
Updates a row in a table. This is a high-level function used in SQL execution
graphs.
@return query thread to run next or NULL */
que_thr_t*
row_upd_step( /*=========*/
que_thr_t* thr) /*!< in: query thread */
{
upd_node_t* node;
sel_node_t* sel_node;
que_node_t* parent;
dberr_t err = DB_SUCCESS;
trx_t* trx;
DBUG_ENTER("row_upd_step");
if (node->searched_update) { /* Reset the cursor */
sel_node->state = SEL_NODE_OPEN;
/* Fetch a row to update */
thr->run_node = sel_node;
DBUG_RETURN(thr);
}
}
/* sel_node is NULL if we are in the MySQL interface */
if (sel_node && (sel_node->state != SEL_NODE_FETCH)) {
if (!node->searched_update) { /* An explicit cursor should be positioned on a row
to update */
ut_error;
err = DB_ERROR;
goto error_handling;
}
ut_ad(sel_node->state == SEL_NODE_NO_MORE_ROWS);
/* No more rows to update, or the select node performed the
updates directly in-place */
thr->run_node = parent;
DBUG_RETURN(thr);
}
/* DO THE CHECKS OF THE CONSISTENCY CONSTRAINTS HERE */
err = row_upd(node, thr);
error_handling:
trx->error_state = err;
if (err != DB_SUCCESS) {
DBUG_RETURN(NULL);
}
/* DO THE TRIGGER ACTIONS HERE */
if (node->searched_update) { /* Fetch next row to update */
thr->run_node = sel_node;
} else { /* It was an explicit cursor update */
thr->run_node = parent;
}
node->state = UPD_NODE_UPDATE_CLUSTERED;
DBUG_RETURN(thr);
}
/** Write query start time as SQL field data to a buffer. Needed by InnoDB. @paramthdThreadobject
@param buf Buffer to hold start time data */ void thd_get_query_start_data(THD *thd, char *buf);
/** Appends row_start or row_end field to update vector and sets a CURRENT_TIMESTAMP/trx->idvaluetoit.Calledbyvers_make_update()and vers_make_delete(). @param[in]trxtransaction
@param[in] vers_sys_idx table->row_start or table->row_end */ void upd_node_t::vers_update_fields(const trx_t *trx, ulint idx)
{
ut_ad(in_mysql_interface); // otherwise needs to recalculate node->cmpl_info
ut_ad(idx == table->vers_start || idx == table->vers_end);
for (ulint i= 0; i < update->n_fields; ++i)
{ if (update->fields[i].field_no == field_no)
{
ufield= &update->fields[i]; goto skip_append;
}
}
/* row_create_update_node_for_mysql() pre-allocated this much.
At least one PK column always remains unchanged. */
ut_ad(update->n_fields < ulint(table->n_cols + table->n_v_cols));
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.93Bemerkung:
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-10-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.