MySQL 8.1.0
Source Code Documentation
row0ins.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1996, 2023, 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 also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 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 bool 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/** Creates an insert node struct.
60 @return own: insert node struct */
62 ulint ins_type, /*!< in: INS_VALUES, ... */
63 dict_table_t *table, /*!< in: table where to insert */
64 mem_heap_t *heap); /*!< in: mem heap where created */
65/** Sets a new row to insert for an INS_DIRECT node. This function is only used
66 if we have constructed the row separately, which is a rare case; this
67 function is quite slow. */
69 ins_node_t *node, /*!< in: insert node */
70 dtuple_t *row); /*!< in: new row (or first row) for the node */
71/** Tries to insert an entry into a clustered index, ignoring foreign key
72constraints. If a record with the same unique key is found, the other
73record is necessarily marked deleted by a committed transaction, or a
74unique key violation error occurs. The delete marked record is then
75updated to an existing record, and we must write an undo log record on
76the delete marked record.
77@param[in] flags Undo logging and locking flags.
78@param[in] mode BTR_MODIFY_LEAF or BTR_MODIFY_TREE, depending on whether we wish
79optimistic or pessimistic descent down the index tree.
80@param[in] index Clustered index.
81@param[in] n_uniq 0 or index->n_uniq.
82@param[in] entry Index entry to insert.
83@param[in] thr Query thread, or nullptr if flags & (BTR_NO_LOCKING_FLAG |
84BTR_NO_UNDO_LOG_FLAG) and a duplicate can't occur.
85@param[in] dup_chk_only If true, just do duplicate check and return. don't
86execute actual insert.
87@retval DB_SUCCESS on success
88@retval DB_LOCK_WAIT on lock wait when !(flags & BTR_NO_LOCKING_FLAG)
89@retval DB_FAIL if retry with BTR_MODIFY_TREE is needed
90@return error code */
92 uint32_t flags, ulint mode, dict_index_t *index, ulint n_uniq,
93 dtuple_t *entry, que_thr_t *thr, bool dup_chk_only);
94
95/** Tries to insert an entry into a secondary index. If a record with exactly
96the same fields is found, the other record is necessarily marked deleted.
97It is then unmarked. Otherwise, the entry is just inserted to the index.
98@param[in] flags undo logging and locking flags
99@param[in] mode BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
100 depending on whether we wish optimistic or
101 pessimistic descent down the index tree
102@param[in] index secondary index
103@param[in,out] offsets_heap memory heap that can be emptied
104@param[in,out] heap memory heap
105@param[in,out] entry index entry to insert
106@param[in] trx_id PAGE_MAX_TRX_ID during row_log_table_apply(),
107 or trx_id when undo log is disabled during
108 alter copy operation or 0
109@param[in] thr query thread
110@param[in] dup_chk_only true, just do duplicate check and return.
111 don't execute actual insert
112@retval DB_SUCCESS on success
113@retval DB_LOCK_WAIT on lock wait when !(flags & BTR_NO_LOCKING_FLAG)
114@retval DB_FAIL if retry with BTR_MODIFY_TREE is needed
115@return error code */
117 uint32_t flags, ulint mode, dict_index_t *index, mem_heap_t *offsets_heap,
118 mem_heap_t *heap, dtuple_t *entry, trx_id_t trx_id, que_thr_t *thr,
119 bool dup_chk_only);
120
121/** Sets the values of the dtuple fields in entry from the values of appropriate
122columns in row.
123@param[in] index index handler
124@param[out] entry index entry to make
125@param[in] row row
126@return DB_SUCCESS if the set is successful */
128 const dtuple_t *row);
129
130/** Inserts an entry into a clustered index. Tries first optimistic,
131 then pessimistic descent down the tree. If the entry matches enough
132 to a delete marked record, performs the insert by updating or delete
133 unmarking the delete marked record.
134 @return DB_SUCCESS, DB_LOCK_WAIT, DB_DUPLICATE_KEY, or some other error code */
136 dict_index_t *index, /*!< in: clustered index */
137 dtuple_t *entry, /*!< in/out: index entry to insert */
138 que_thr_t *thr, /*!< in: query thread */
139 bool dup_chk_only);
140/*!< in: if true, just do duplicate check
141and return. don't execute actual insert. */
142/** Inserts an entry into a secondary index. Tries first optimistic,
143 then pessimistic descent down the tree. If the entry matches enough
144 to a delete marked record, performs the insert by updating or delete
145 unmarking the delete marked record.
146 @return DB_SUCCESS, DB_LOCK_WAIT, DB_DUPLICATE_KEY, or some other error code */
147[[nodiscard]] dberr_t row_ins_sec_index_entry(
148 dict_index_t *index, /*!< in: secondary index */
149 dtuple_t *entry, /*!< in/out: index entry to insert */
150 que_thr_t *thr, /*!< in: query thread */
151 bool dup_chk_only);
152/*!< in: if true, just do duplicate check
153and return. don't execute actual insert. */
154/** Inserts a row to a table. This is a high-level function used in
155 SQL execution graphs.
156 @return query thread to run next or NULL */
157que_thr_t *row_ins_step(que_thr_t *thr); /*!< in: query thread */
158
159/* Insert node structure */
160
162 que_common_t common; /*!< node type: QUE_NODE_INSERT */
163 ulint ins_type; /* INS_VALUES, INS_SEARCHED, or INS_DIRECT */
164 dtuple_t *row; /*!< row to insert */
165 dict_table_t *table; /*!< table where to insert */
166 sel_node_t *select; /*!< select in searched insert */
167 que_node_t *values_list; /* list of expressions to evaluate and
168 insert in an INS_VALUES insert */
169 ulint state; /*!< node execution state */
170 dict_index_t *index; /*!< NULL, or the next index where the index
171 entry should be inserted */
172 dtuple_t *entry; /*!< NULL, or entry to insert in the index;
173 after a successful insert of the entry,
174 this should be reset to NULL */
175 UT_LIST_BASE_NODE_T(dtuple_t, tuple_list)
176 entry_list; /* list of entries, one for each index */
177 byte *row_id_buf; /* buffer for the row id sys field in row */
178 trx_id_t trx_id; /*!< trx id or the last trx which executed the
179 node */
180 byte *trx_id_buf; /* buffer for the trx id sys field in row */
182 /* memory heap used as auxiliary storage;
183 entry_list and sys fields are stored here;
184 if this is NULL, entry list should be created
185 and buffers for sys fields in row allocated */
186
187 /** When there is a lock wait error, this remembers current position of
188 the multi-value field, before which the values have been inserted */
190
192};
193
194constexpr uint32_t INS_NODE_MAGIC_N = 15849075;
195
196/* Insert node types */
197/** INSERT INTO ... SELECT ... */
198constexpr uint32_t INS_SEARCHED = 0;
199/** INSERT INTO ... VALUES ... */
200constexpr uint32_t INS_VALUES = 1;
201/** this is for internal use in dict0crea: insert the row directly */
202constexpr uint32_t INS_DIRECT = 2;
203
204/* Node execution states */
205/** we should set an IX lock on table */
206constexpr uint32_t INS_NODE_SET_IX_LOCK = 1;
207/** row id should be allocated */
208constexpr uint32_t INS_NODE_ALLOC_ROW_ID = 2;
209/** index entries should be built and inserted */
210constexpr uint32_t INS_NODE_INSERT_ENTRIES = 3;
211
212#endif
SQL data field and tuple.
dberr_t
Definition: db0err.h:38
Data dictionary global types.
static int flags[50]
Definition: hp_test1.cc:39
static PFS_engine_table_share_proxy table
Definition: pfs.cc:60
mode
Definition: file_handle.h:59
Query graph global types.
void que_node_t
Definition: que0types.h:40
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:79
constexpr uint32_t INS_NODE_MAGIC_N
Definition: row0ins.h:194
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:2347
constexpr uint32_t INS_NODE_ALLOC_ROW_ID
row id should be allocated
Definition: row0ins.h:208
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:3063
constexpr uint32_t INS_NODE_INSERT_ENTRIES
index entries should be built and inserted
Definition: row0ins.h:210
constexpr uint32_t INS_VALUES
INSERT INTO ... VALUES ...
Definition: row0ins.h:200
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:3147
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:2783
constexpr uint32_t INS_DIRECT
this is for internal use in dict0crea: insert the row directly
Definition: row0ins.h:202
constexpr uint32_t INS_NODE_SET_IX_LOCK
we should set an IX lock on table
Definition: row0ins.h:206
constexpr uint32_t INS_SEARCHED
INSERT INTO ... SELECT ...
Definition: row0ins.h:198
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:1367
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:3342
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:192
que_thr_t * row_ins_step(que_thr_t *thr)
Inserts a row to a table.
Definition: row0ins.cc:3595
Row operation global types.
Definition: completion_hash.h:34
Data structure for a foreign key constraint; an example: FOREIGN KEY (A, B) REFERENCES TABLE2 (C,...
Definition: dict0mem.h:1665
Data structure for an index.
Definition: dict0mem.h:1045
Data structure for a database table.
Definition: dict0mem.h:1908
Structure for an SQL data tuple of fields (logical record)
Definition: data0data.h:681
Definition: row0ins.h:161
dict_index_t * index
NULL, or the next index where the index entry should be inserted.
Definition: row0ins.h:170
dict_table_t * table
table where to insert
Definition: row0ins.h:165
trx_id_t trx_id
trx id or the last trx which executed the node
Definition: row0ins.h:178
byte * row_id_buf
Definition: row0ins.h:177
ulint state
node execution state
Definition: row0ins.h:169
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:189
que_common_t common
node type: QUE_NODE_INSERT
Definition: row0ins.h:162
byte * trx_id_buf
Definition: row0ins.h:180
sel_node_t * select
select in searched insert
Definition: row0ins.h:166
mem_heap_t * entry_sys_heap
Definition: row0ins.h:181
que_node_t * values_list
Definition: row0ins.h:167
ulint ins_type
Definition: row0ins.h:163
dtuple_t * row
row to insert
Definition: row0ins.h:164
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:172
ulint magic_n
Definition: row0ins.h:191
entry_list
Definition: row0ins.h:176
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:301
Definition: que0types.h:50
Definition: que0que.h:241
Select statement node.
Definition: row0sel.h:328
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:137
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:405