MySQL  8.0.21
Source Code Documentation
row0ins.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1996, 2019, Oracle and/or its affiliates. All Rights Reserved.
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 also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 
25 *****************************************************************************/
26 
27 /** @file include/row0ins.h
28  Insert into a table
29 
30  Created 4/20/1996 Heikki Tuuri
31  *******************************************************/
32 
33 #ifndef row0ins_h
34 #define row0ins_h
35 
36 #include "data0data.h"
37 #include "dict0types.h"
38 #include "que0types.h"
39 #include "row0types.h"
40 #include "trx0types.h"
41 #include "univ.i"
42 
43 /** Checks if foreign key constraint fails for an index entry. Sets shared locks
44  which lock either the success or the failure of the constraint. NOTE that
45  the caller must have a shared latch on dict_foreign_key_check_lock.
46  @return DB_SUCCESS, DB_LOCK_WAIT, DB_NO_REFERENCED_ROW, or
47  DB_ROW_IS_REFERENCED */
49  ibool check_ref, /*!< in: TRUE If we want to check that
50  the referenced table is ok, FALSE if we
51  want to check the foreign key table */
52  dict_foreign_t *foreign, /*!< in: foreign constraint; NOTE that the
53  tables mentioned in it must be in the
54  dictionary cache if they exist at all */
55  dict_table_t *table, /*!< in: if check_ref is TRUE, then the foreign
56  table, else the referenced table */
57  dtuple_t *entry, /*!< in: index entry for index */
58  que_thr_t *thr) /*!< in: query thread */
59  MY_ATTRIBUTE((warn_unused_result));
60 /** Creates an insert node struct.
61  @return own: insert node struct */
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. */
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  @retval DB_SUCCESS on success
79  @retval DB_LOCK_WAIT on lock wait when !(flags & BTR_NO_LOCKING_FLAG)
80  @retval DB_FAIL if retry with BTR_MODIFY_TREE is needed
81  @return error code */
83  uint32_t flags, /*!< in: undo logging and locking flags */
84  ulint mode, /*!< in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
85  depending on whether we wish optimistic or
86  pessimistic descent down the index tree */
87  dict_index_t *index, /*!< in: clustered index */
88  ulint n_uniq, /*!< in: 0 or index->n_uniq */
89  dtuple_t *entry, /*!< in/out: index entry to insert */
90  que_thr_t *thr, /*!< in: query thread, or NULL if
91  flags & (BTR_NO_LOCKING_FLAG
92  | BTR_NO_UNDO_LOG_FLAG) and a duplicate
93  can't occur */
94  bool dup_chk_only)
95  /*!< in: if true, just do duplicate check
96  and return. don't execute actual insert. */
97  MY_ATTRIBUTE((warn_unused_result));
98 
99 /** Tries to insert an entry into a secondary index. If a record with exactly
100 the same fields is found, the other record is necessarily marked deleted.
101 It is then unmarked. Otherwise, the entry is just inserted to the index.
102 @param[in] flags undo logging and locking flags
103 @param[in] mode BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
104  depending on whether we wish optimistic or
105  pessimistic descent down the index tree
106 @param[in] index secondary index
107 @param[in,out] offsets_heap memory heap that can be emptied
108 @param[in,out] heap memory heap
109 @param[in,out] entry index entry to insert
110 @param[in] trx_id PAGE_MAX_TRX_ID during row_log_table_apply(),
111  or trx_id when undo log is disabled during
112  alter copy operation or 0
113 @param[in] thr query thread
114 @param[in] dup_chk_only TRUE, just do duplicate check and return.
115  don't execute actual insert
116 @retval DB_SUCCESS on success
117 @retval DB_LOCK_WAIT on lock wait when !(flags & BTR_NO_LOCKING_FLAG)
118 @retval DB_FAIL if retry with BTR_MODIFY_TREE is needed
119 @return error code */
122  mem_heap_t *offsets_heap, mem_heap_t *heap,
124  que_thr_t *thr, bool dup_chk_only)
125  MY_ATTRIBUTE((warn_unused_result));
126 
127 /** Sets the values of the dtuple fields in entry from the values of appropriate
128 columns in row.
129 @param[in] index index handler
130 @param[out] entry index entry to make
131 @param[in] row row */
133  const dtuple_t *row);
134 
135 /** Inserts an entry into a clustered index. Tries first optimistic,
136  then pessimistic descent down the tree. If the entry matches enough
137  to a delete marked record, performs the insert by updating or delete
138  unmarking the delete marked record.
139  @return DB_SUCCESS, DB_LOCK_WAIT, DB_DUPLICATE_KEY, or some other error code */
141  dict_index_t *index, /*!< in: clustered index */
142  dtuple_t *entry, /*!< in/out: index entry to insert */
143  que_thr_t *thr, /*!< in: query thread */
144  bool dup_chk_only)
145  /*!< in: if true, just do duplicate check
146  and return. don't execute actual insert. */
147  MY_ATTRIBUTE((warn_unused_result));
148 /** Inserts an entry into a secondary index. Tries first optimistic,
149  then pessimistic descent down the tree. If the entry matches enough
150  to a delete marked record, performs the insert by updating or delete
151  unmarking the delete marked record.
152  @return DB_SUCCESS, DB_LOCK_WAIT, DB_DUPLICATE_KEY, or some other error code */
154  dict_index_t *index, /*!< in: secondary index */
155  dtuple_t *entry, /*!< in/out: index entry to insert */
156  que_thr_t *thr, /*!< in: query thread */
157  bool dup_chk_only)
158  /*!< in: if true, just do duplicate check
159  and return. don't execute actual insert. */
160  MY_ATTRIBUTE((warn_unused_result));
161 /** Inserts a row to a table. This is a high-level function used in
162  SQL execution graphs.
163  @return query thread to run next or NULL */
164 que_thr_t *row_ins_step(que_thr_t *thr); /*!< in: query thread */
165 
166 /* Insert node structure */
167 
168 struct ins_node_t {
169  que_common_t common; /*!< node type: QUE_NODE_INSERT */
170  ulint ins_type; /* INS_VALUES, INS_SEARCHED, or INS_DIRECT */
171  dtuple_t *row; /*!< row to insert */
172  dict_table_t *table; /*!< table where to insert */
173  sel_node_t *select; /*!< select in searched insert */
174  que_node_t *values_list; /* list of expressions to evaluate and
175  insert in an INS_VALUES insert */
176  ulint state; /*!< node execution state */
177  dict_index_t *index; /*!< NULL, or the next index where the index
178  entry should be inserted */
179  dtuple_t *entry; /*!< NULL, or entry to insert in the index;
180  after a successful insert of the entry,
181  this should be reset to NULL */
183  entry_list; /* list of entries, one for each index */
184  byte *row_id_buf; /* buffer for the row id sys field in row */
185  trx_id_t trx_id; /*!< trx id or the last trx which executed the
186  node */
187  byte *trx_id_buf; /* buffer for the trx id sys field in row */
189  /* memory heap used as auxiliary storage;
190  entry_list and sys fields are stored here;
191  if this is NULL, entry list should be created
192  and buffers for sys fields in row allocated */
193 
194  /** When there is a lock wait error, this remembers current position of
195  the multi-value field, before which the values have been inserted */
197 
198  ulint magic_n;
199 };
200 
201 #define INS_NODE_MAGIC_N 15849075
202 
203 /* Insert node types */
204 #define INS_SEARCHED 0 /* INSERT INTO ... SELECT ... */
205 #define INS_VALUES 1 /* INSERT INTO ... VALUES ... */
206 #define INS_DIRECT \
207  2 /* this is for internal use in dict0crea: \
208  insert the row directly */
209 
210 /* Node execution states */
211 #define INS_NODE_SET_IX_LOCK 1 /* we should set an IX lock on table */
212 #define INS_NODE_ALLOC_ROW_ID 2 /* row id should be allocated */
213 #define INS_NODE_INSERT_ENTRIES \
214  3 /* index entries should be built and \
215  inserted */
216 
217 #endif
ins_node_t * ins_node_create(ulint ins_type, dict_table_t *table, mem_heap_t *heap)
Creates an insert node struct.
Definition: row0ins.cc:78
entry_list
Definition: row0ins.h:183
que_node_t * values_list
Definition: row0ins.h:174
dict_table_t * table
table where to insert
Definition: row0ins.h:172
ulint state
node execution state
Definition: row0ins.h:176
dict_index_t * index
current index for a search, if any
Definition: row0mysql.h:545
dberr_t row_ins_clust_index_entry_low(uint32_t flags, ulint mode, dict_index_t *index, ulint n_uniq, dtuple_t *entry, que_thr_t *thr, bool dup_chk_only)
Tries to insert an entry into a clustered index, ignoring foreign key constraints.
Definition: row0ins.cc:2336
mode
Definition: file_handle.h:58
void ins_node_set_new_row(ins_node_t *node, dtuple_t *row)
Sets a new row to insert for an INS_DIRECT node.
Definition: row0ins.cc:191
dberr_t row_ins_check_foreign_constraint(ibool check_ref, dict_foreign_t *foreign, dict_table_t *table, dtuple_t *entry, que_thr_t *thr)
Checks if foreign key constraint fails for an index entry.
Definition: row0ins.cc:1348
que_thr_t * row_ins_step(que_thr_t *thr)
Inserts a row to a table.
Definition: row0ins.cc:3585
Data structure for a database table.
Definition: dict0mem.h:1524
Definition: row0ins.h:168
dict_table_t * table
Innobase table handle.
Definition: row0mysql.h:544
Data structure for a foreign key constraint; an example: FOREIGN KEY (A, B) REFERENCES TABLE2 (C...
Definition: dict0mem.h:1291
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:347
SQL data field and tuple.
dict_index_t * index
NULL, or the next index where the index entry should be inserted.
Definition: row0ins.h:177
Query graph global types.
byte * row_id_buf
Definition: row0ins.h:184
Data dictionary global types.
dberr_t row_ins_sec_index_entry_low(uint32_t flags, ulint mode, dict_index_t *index, mem_heap_t *offsets_heap, mem_heap_t *heap, dtuple_t *entry, trx_id_t trx_id, que_thr_t *thr, bool dup_chk_only)
Tries to insert an entry into a secondary index.
Definition: row0ins.cc:2772
mem_heap_t * heap
memory heap from which these auxiliary structures are allocated when needed
Definition: row0mysql.h:616
Structure for an SQL data tuple of fields (logical record)
Definition: data0data.h:711
dberr_t
Definition: db0err.h:38
Row operation global types.
Definition: que0que.h:248
dtuple_t * entry
NULL, or entry to insert in the index; after a successful insert of the entry, this should be reset t...
Definition: row0ins.h:179
sel_node_t * select
select in searched insert
Definition: row0ins.h:173
Transaction system global type definitions.
mem_heap_t * entry_sys_heap
Definition: row0ins.h:188
dberr_t row_ins_index_entry_set_vals(const dict_index_t *index, dtuple_t *entry, const dtuple_t *row)
Sets the values of the dtuple fields in entry from the values of appropriate columns in row...
Definition: row0ins.cc:3332
void que_node_t
Definition: que0types.h:40
Definition: que0types.h:50
Select statement node.
Definition: row0sel.h:341
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:144
#define UT_LIST_BASE_NODE_T(TYPE)
Definition: innodb_utility.h:47
dberr_t row_ins_clust_index_entry(dict_index_t *index, dtuple_t *entry, que_thr_t *thr, bool dup_chk_only)
Inserts an entry into a clustered index.
Definition: row0ins.cc:3052
dtuple_t * row
row to insert
Definition: row0ins.h:171
que_common_t common
node type: QUE_NODE_INSERT
Definition: row0ins.h:169
ulint magic_n
Definition: row0ins.h:198
byte * trx_id_buf
Definition: row0ins.h:187
trx_id_t trx_id
The table->def_trx_id when ins_graph was built.
Definition: row0mysql.h:643
ulint ins_type
Definition: row0ins.h:170
uint32_t ins_multi_val_pos
When there is a lock wait error, this remembers current position of the multi-value field...
Definition: row0ins.h:196
static int flags[50]
Definition: hp_test1.cc:39
unsigned char byte
Blob class.
Definition: common.h:159
Definition: completion_hash.h:34
dberr_t row_ins_sec_index_entry(dict_index_t *index, dtuple_t *entry, que_thr_t *thr, bool dup_chk_only)
Inserts an entry into a secondary index.
Definition: row0ins.cc:3136
trx_id_t trx_id
trx id or the last trx which executed the node
Definition: row0ins.h:185
Data structure for an index.
Definition: dict0mem.h:883