MySQL 9.0.0
Source Code Documentation
row0ins.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1996, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 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 */
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 */
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
73constraints. If a record with the same unique key is found, the other
74record is necessarily marked deleted by a committed transaction, or a
75unique key violation error occurs. The delete marked record is then
76updated to an existing record, and we must write an undo log record on
77the 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
80optimistic 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 |
85BTR_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
87execute 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 */
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
97the same fields is found, the other record is necessarily marked deleted.
98It 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 */
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
123columns 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 */
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 */
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
142and 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
154and 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 */
158que_thr_t *row_ins_step(que_thr_t *thr); /*!< in: query thread */
159
160/* Insert node structure */
161
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 */
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 */
191
193};
194
195constexpr uint32_t INS_NODE_MAGIC_N = 15849075;
196
197/* Insert node types */
198/** INSERT INTO ... SELECT ... */
199constexpr uint32_t INS_SEARCHED = 0;
200/** INSERT INTO ... VALUES ... */
201constexpr uint32_t INS_VALUES = 1;
202/** this is for internal use in dict0crea: insert the row directly */
203constexpr uint32_t INS_DIRECT = 2;
204
205/* Node execution states */
206/** we should set an IX lock on table */
207constexpr uint32_t INS_NODE_SET_IX_LOCK = 1;
208/** row id should be allocated */
209constexpr uint32_t INS_NODE_ALLOC_ROW_ID = 2;
210/** index entries should be built and inserted */
211constexpr uint32_t INS_NODE_INSERT_ENTRIES = 3;
212
213#endif
SQL data field and tuple.
dberr_t
Definition: db0err.h:39
Data dictionary global types.
static int flags[50]
Definition: hp_test1.cc:40
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
mode
Definition: file_handle.h:61
Query graph global types.
void que_node_t
Definition: que0types.h:41
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:80
constexpr uint32_t INS_NODE_MAGIC_N
Definition: row0ins.h:195
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:2348
constexpr uint32_t INS_NODE_ALLOC_ROW_ID
row id should be allocated
Definition: row0ins.h:209
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:3064
constexpr uint32_t INS_NODE_INSERT_ENTRIES
index entries should be built and inserted
Definition: row0ins.h:211
constexpr uint32_t INS_VALUES
INSERT INTO ... VALUES ...
Definition: row0ins.h:201
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:3148
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:2784
constexpr uint32_t INS_DIRECT
this is for internal use in dict0crea: insert the row directly
Definition: row0ins.h:203
constexpr uint32_t INS_NODE_SET_IX_LOCK
we should set an IX lock on table
Definition: row0ins.h:207
constexpr uint32_t INS_SEARCHED
INSERT INTO ... SELECT ...
Definition: row0ins.h:199
dberr_t row_ins_check_foreign_constraint(bool 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:1368
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:3343
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:193
que_thr_t * row_ins_step(que_thr_t *thr)
Inserts a row to a table.
Definition: row0ins.cc:3596
Row operation global types.
Definition: completion_hash.h:35
Data structure for a foreign key constraint; an example: FOREIGN KEY (A, B) REFERENCES TABLE2 (C,...
Definition: dict0mem.h:1666
Data structure for an index.
Definition: dict0mem.h:1046
Data structure for a database table.
Definition: dict0mem.h:1909
Structure for an SQL data tuple of fields (logical record)
Definition: data0data.h:684
Definition: row0ins.h:162
dict_index_t * index
NULL, or the next index where the index entry should be inserted.
Definition: row0ins.h:171
dict_table_t * table
table where to insert
Definition: row0ins.h:166
trx_id_t trx_id
trx id or the last trx which executed the node
Definition: row0ins.h:179
byte * row_id_buf
Definition: row0ins.h:178
ulint state
node execution state
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:190
que_common_t common
node type: QUE_NODE_INSERT
Definition: row0ins.h:163
byte * trx_id_buf
Definition: row0ins.h:181
sel_node_t * select
select in searched insert
Definition: row0ins.h:167
mem_heap_t * entry_sys_heap
Definition: row0ins.h:182
que_node_t * values_list
Definition: row0ins.h:168
ulint ins_type
Definition: row0ins.h:164
dtuple_t * row
row to insert
Definition: row0ins.h:165
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:173
ulint magic_n
Definition: row0ins.h:192
entry_list
Definition: row0ins.h:177
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:302
Definition: que0types.h:51
Definition: que0que.h:242
Select statement node.
Definition: row0sel.h:329
typedef UT_LIST_BASE_NODE_T(rw_lock_t, list) rw_lock_list_t
Transaction system global type definitions.
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:138
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:406