| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/row0ins.h |
| 29 |
Insert into a table |
| 30 |
|
| 31 |
Created 4/20/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef row0ins_h |
| 35 |
#define row0ins_h |
| 36 |
|
| 37 |
#include "data0data.h" |
| 38 |
#include "dict0types.h" |
| 39 |
#include "que0types.h" |
| 40 |
#include "row0types.h" |
| 41 |
#include "trx0types.h" |
| 42 |
#include "univ.i" |
| 43 |
|
| 44 |
/** Checks if foreign key constraint fails for an index entry. Sets shared locks |
| 45 |
which lock either the success or the failure of the constraint. NOTE that |
| 46 |
the caller must have a shared latch on dict_foreign_key_check_lock. |
| 47 |
@return DB_SUCCESS, DB_LOCK_WAIT, DB_NO_REFERENCED_ROW, or |
| 48 |
DB_ROW_IS_REFERENCED */ |
| 49 |
[[nodiscard]] dberr_t row_ins_check_foreign_constraint( |
| 50 |
bool check_ref, /*!< in: true If we want to check that |
| 51 |
the referenced table is ok, false if we |
| 52 |
want to check the foreign key table */ |
| 53 |
dict_foreign_t *foreign, /*!< in: foreign constraint; NOTE that the |
| 54 |
tables mentioned in it must be in the |
| 55 |
dictionary cache if they exist at all */ |
| 56 |
dict_table_t *table, /*!< in: if check_ref is true, then the foreign |
| 57 |
table, else the referenced table */ |
| 58 |
dtuple_t *entry, /*!< in: index entry for index */ |
| 59 |
que_thr_t *thr); /*!< in: query thread */ |
| 60 |
/** Creates an insert node struct. |
| 61 |
@return own: insert node struct */ |
| 62 |
ins_node_t *ins_node_create( |
| 63 |
ulint ins_type, /*!< in: INS_VALUES, ... */ |
| 64 |
dict_table_t *table, /*!< in: table where to insert */ |
| 65 |
mem_heap_t *heap); /*!< in: mem heap where created */ |
| 66 |
/** Sets a new row to insert for an INS_DIRECT node. This function is only used |
| 67 |
if we have constructed the row separately, which is a rare case; this |
| 68 |
function is quite slow. */ |
| 69 |
void ins_node_set_new_row( |
| 70 |
ins_node_t *node, /*!< in: insert node */ |
| 71 |
dtuple_t *row); /*!< in: new row (or first row) for the node */ |
| 72 |
/** Tries to insert an entry into a clustered index, ignoring foreign key |
| 73 |
constraints. If a record with the same unique key is found, the other |
| 74 |
record is necessarily marked deleted by a committed transaction, or a |
| 75 |
unique key violation error occurs. The delete marked record is then |
| 76 |
updated to an existing record, and we must write an undo log record on |
| 77 |
the delete marked record. |
| 78 |
@param[in] flags Undo logging and locking flags. |
| 79 |
@param[in] mode BTR_MODIFY_LEAF or BTR_MODIFY_TREE, depending on whether we wish |
| 80 |
optimistic or pessimistic descent down the index tree. |
| 81 |
@param[in] index Clustered index. |
| 82 |
@param[in] n_uniq 0 or index->n_uniq. |
| 83 |
@param[in] entry Index entry to insert. |
| 84 |
@param[in] thr Query thread, or nullptr if flags & (BTR_NO_LOCKING_FLAG | |
| 85 |
BTR_NO_UNDO_LOG_FLAG) and a duplicate can't occur. |
| 86 |
@param[in] dup_chk_only If true, just do duplicate check and return. don't |
| 87 |
execute actual insert. |
| 88 |
@retval DB_SUCCESS on success |
| 89 |
@retval DB_LOCK_WAIT on lock wait when !(flags & BTR_NO_LOCKING_FLAG) |
| 90 |
@retval DB_FAIL if retry with BTR_MODIFY_TREE is needed |
| 91 |
@return error code */ |
| 92 |
[[nodiscard]] dberr_t row_ins_clust_index_entry_low( |
| 93 |
uint32_t flags, ulint mode, dict_index_t *index, ulint n_uniq, |
| 94 |
dtuple_t *entry, que_thr_t *thr, bool dup_chk_only); |
| 95 |
|
| 96 |
/** Tries to insert an entry into a secondary index. If a record with exactly |
| 97 |
the same fields is found, the other record is necessarily marked deleted. |
| 98 |
It is then unmarked. Otherwise, the entry is just inserted to the index. |
| 99 |
@param[in] flags undo logging and locking flags |
| 100 |
@param[in] mode BTR_MODIFY_LEAF or BTR_MODIFY_TREE, |
| 101 |
depending on whether we wish optimistic or |
| 102 |
pessimistic descent down the index tree |
| 103 |
@param[in] index secondary index |
| 104 |
@param[in,out] offsets_heap memory heap that can be emptied |
| 105 |
@param[in,out] heap memory heap |
| 106 |
@param[in,out] entry index entry to insert |
| 107 |
@param[in] trx_id PAGE_MAX_TRX_ID during row_log_table_apply(), |
| 108 |
or trx_id when undo log is disabled during |
| 109 |
alter copy operation or 0 |
| 110 |
@param[in] thr query thread |
| 111 |
@param[in] dup_chk_only true, just do duplicate check and return. |
| 112 |
don't execute actual insert |
| 113 |
@retval DB_SUCCESS on success |
| 114 |
@retval DB_LOCK_WAIT on lock wait when !(flags & BTR_NO_LOCKING_FLAG) |
| 115 |
@retval DB_FAIL if retry with BTR_MODIFY_TREE is needed |
| 116 |
@return error code */ |
| 117 |
[[nodiscard]] dberr_t row_ins_sec_index_entry_low( |
| 118 |
uint32_t flags, ulint mode, dict_index_t *index, mem_heap_t *offsets_heap, |
| 119 |
mem_heap_t *heap, dtuple_t *entry, trx_id_t trx_id, que_thr_t *thr, |
| 120 |
bool dup_chk_only); |
| 121 |
|
| 122 |
/** Sets the values of the dtuple fields in entry from the values of appropriate |
| 123 |
columns in row. |
| 124 |
@param[in] index index handler |
| 125 |
@param[out] entry index entry to make |
| 126 |
@param[in] row row |
| 127 |
@return DB_SUCCESS if the set is successful */ |
| 128 |
dberr_t row_ins_index_entry_set_vals(const dict_index_t *index, dtuple_t *entry, |
| 129 |
const dtuple_t *row); |
| 130 |
|
| 131 |
/** Inserts an entry into a clustered index. Tries first optimistic, |
| 132 |
then pessimistic descent down the tree. If the entry matches enough |
| 133 |
to a delete marked record, performs the insert by updating or delete |
| 134 |
unmarking the delete marked record. |
| 135 |
@return DB_SUCCESS, DB_LOCK_WAIT, DB_DUPLICATE_KEY, or some other error code */ |
| 136 |
[[nodiscard]] dberr_t row_ins_clust_index_entry( |
| 137 |
dict_index_t *index, /*!< in: clustered index */ |
| 138 |
dtuple_t *entry, /*!< in/out: index entry to insert */ |
| 139 |
que_thr_t *thr, /*!< in: query thread */ |
| 140 |
bool dup_chk_only); |
| 141 |
/*!< in: if true, just do duplicate check |
| 142 |
and return. don't execute actual insert. */ |
| 143 |
/** Inserts an entry into a secondary index. Tries first optimistic, |
| 144 |
then pessimistic descent down the tree. If the entry matches enough |
| 145 |
to a delete marked record, performs the insert by updating or delete |
| 146 |
unmarking the delete marked record. |
| 147 |
@return DB_SUCCESS, DB_LOCK_WAIT, DB_DUPLICATE_KEY, or some other error code */ |
| 148 |
[[nodiscard]] dberr_t row_ins_sec_index_entry( |
| 149 |
dict_index_t *index, /*!< in: secondary index */ |
| 150 |
dtuple_t *entry, /*!< in/out: index entry to insert */ |
| 151 |
que_thr_t *thr, /*!< in: query thread */ |
| 152 |
bool dup_chk_only); |
| 153 |
/*!< in: if true, just do duplicate check |
| 154 |
and return. don't execute actual insert. */ |
| 155 |
/** Inserts a row to a table. This is a high-level function used in |
| 156 |
SQL execution graphs. |
| 157 |
@return query thread to run next or NULL */ |
| 158 |
que_thr_t *row_ins_step(que_thr_t *thr); /*!< in: query thread */ |
| 159 |
|
| 160 |
/* Insert node structure */ |
| 161 |
|
| 162 |
struct ins_node_t { |
| 163 |
que_common_t common; /*!< node type: QUE_NODE_INSERT */ |
| 164 |
ulint ins_type; /* INS_VALUES, INS_SEARCHED, or INS_DIRECT */ |
| 165 |
dtuple_t *row; /*!< row to insert */ |
| 166 |
dict_table_t *table; /*!< table where to insert */ |
| 167 |
sel_node_t *select; /*!< select in searched insert */ |
| 168 |
que_node_t *values_list; /* list of expressions to evaluate and |
| 169 |
insert in an INS_VALUES insert */ |
| 170 |
ulint state; /*!< node execution state */ |
| 171 |
dict_index_t *index; /*!< NULL, or the next index where the index |
| 172 |
entry should be inserted */ |
| 173 |
dtuple_t *entry; /*!< NULL, or entry to insert in the index; |
| 174 |
after a successful insert of the entry, |
| 175 |
this should be reset to NULL */ |
| 176 |
UT_LIST_BASE_NODE_T(dtuple_t, tuple_list) |
| 177 |
entry_list; /* list of entries, one for each index */ |
| 178 |
byte *row_id_buf; /* buffer for the row id sys field in row */ |
| 179 |
trx_id_t trx_id; /*!< trx id or the last trx which executed the |
| 180 |
node */ |
| 181 |
byte *trx_id_buf; /* buffer for the trx id sys field in row */ |
| 182 |
mem_heap_t *entry_sys_heap; |
| 183 |
/* memory heap used as auxiliary storage; |
| 184 |
entry_list and sys fields are stored here; |
| 185 |
if this is NULL, entry list should be created |
| 186 |
and buffers for sys fields in row allocated */ |
| 187 |
|
| 188 |
/** When there is a lock wait error, this remembers current position of |
| 189 |
the multi-value field, before which the values have been inserted */ |
| 190 |
uint32_t ins_multi_val_pos; |
| 191 |
|
| 192 |
ulint magic_n; |
| 193 |
}; |
| 194 |
|
| 195 |
constexpr uint32_t INS_NODE_MAGIC_N = 15849075; |
| 196 |
|
| 197 |
/* Insert node types */ |
| 198 |
/** INSERT INTO ... SELECT ... */ |
| 199 |
constexpr uint32_t INS_SEARCHED = 0; |
| 200 |
/** INSERT INTO ... VALUES ... */ |
| 201 |
constexpr uint32_t INS_VALUES = 1; |
| 202 |
/** this is for internal use in dict0crea: insert the row directly */ |
| 203 |
constexpr uint32_t INS_DIRECT = 2; |
| 204 |
|
| 205 |
/* Node execution states */ |
| 206 |
/** we should set an IX lock on table */ |
| 207 |
constexpr uint32_t INS_NODE_SET_IX_LOCK = 1; |
| 208 |
/** row id should be allocated */ |
| 209 |
constexpr uint32_t INS_NODE_ALLOC_ROW_ID = 2; |
| 210 |
/** index entries should be built and inserted */ |
| 211 |
constexpr uint32_t INS_NODE_INSERT_ENTRIES = 3; |
| 212 |
|
| 213 |
#endif |