Debian Codegraph @

storage/innobase/include/que0que.h

raw
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/que0que.h
29 Query graph
30
31 Created 5/27/1996 Heikki Tuuri
32 *******************************************************/
33
34 #ifndef que0que_h
35 #define que0que_h
36
37 #include "data0data.h"
38 #include "dict0types.h"
39 #include "pars0types.h"
40 #include "que0types.h"
41 #include "row0types.h"
42 #include "srv0srv.h"
43 #include "trx0roll.h"
44 #include "trx0trx.h"
45 #include "univ.i"
46 #include "usr0types.h"
47
48 /** Mutex protecting the query threads. */
49 extern ib_mutex_t que_thr_mutex;
50
51 /** Creates a query graph fork node.
52 @return own: fork node */
53 que_fork_t *que_fork_create(
54 que_t *graph, /*!< in: graph, if NULL then this
55 fork node is assumed to be the
56 graph root */
57 que_node_t *parent, /*!< in: parent node */
58 ulint fork_type, /*!< in: fork type */
59 mem_heap_t *heap); /*!< in: memory heap where created */
60 /** Gets the first thr in a fork. */
61 static inline que_thr_t *que_fork_get_first_thr(
62 que_fork_t *fork); /*!< in: query fork */
63 /** Gets the child node of the first thr in a fork. */
64 static inline que_node_t *que_fork_get_child(
65 que_fork_t *fork); /*!< in: query fork */
66
67 /** Sets the parent of a graph node.
68 @param[in] node graph node
69 @param[in] parent parent */
70 static inline void que_node_set_parent(que_node_t *node, que_node_t *parent);
71
72 /** Creates a query graph thread node.
73 @param[in] parent parent node, i.e., a fork node
74 @param[in] heap memory heap where created
75 @param[in] prebuilt row prebuilt structure
76 @return own: query thread node */
77 que_thr_t *que_thr_create(que_fork_t *parent, mem_heap_t *heap,
78 row_prebuilt_t *prebuilt);
79 /** Frees a query graph, but not the heap where it was created. Does not free
80 explicit cursor declarations, they are freed in que_graph_free. */
81 void que_graph_free_recursive(que_node_t *node); /*!< in: query graph node */
82 /** Frees a query graph. */
83 void que_graph_free(que_t *graph); /*!< in: query graph; we assume that the
84 memory heap where this graph was created is
85 private to this graph: if not, then use
86 que_graph_free_recursive and free the heap
87 afterwards! */
88 /** Stops a query thread if graph or trx is in a state requiring it. The
89 conditions are tested in the order (1) graph, (2) trx.
90 Caller must hold the trx mutex.
91 @param[in,out] thr query thread
92 @return true if stopped */
93 bool que_thr_stop(que_thr_t *thr);
94
95 /** Moves a thread from another state to the QUE_THR_RUNNING state. Increments
96 the n_active_thrs counters of the query graph and transaction. */
97 void que_thr_move_to_run_state_for_mysql(
98 que_thr_t *thr, /*!< in: an query thread */
99 trx_t *trx); /*!< in: transaction */
100
101 /** A patch for MySQL used to 'stop' a dummy query thread used in MySQL
102 select, when there is no error or lock wait.
103 @param[in] thr Query thread
104 @param[in] trx Transaction */
105 void que_thr_stop_for_mysql_no_error(que_thr_t *thr, trx_t *trx);
106
107 /** A patch for MySQL used to 'stop' a dummy query thread used in MySQL. The
108 query thread is stopped and made inactive, except in the case where
109 it was put to the lock wait state in lock0lock.cc, but the lock has already
110 been granted or the transaction chosen as a victim in deadlock resolution. */
111 void que_thr_stop_for_mysql(que_thr_t *thr); /*!< in: query thread */
112 /** Run a query thread. Handles lock waits. */
113 void que_run_threads(que_thr_t *thr); /*!< in: query thread */
114 /** Moves a suspended query thread to the QUE_THR_RUNNING state and release
115 a worker thread to execute it. This function should be used to end
116 the wait state of a query thread waiting for a lock or a stored procedure
117 completion.
118 @return query thread instance of thread to wakeup or NULL */
119 que_thr_t *que_thr_end_lock_wait(trx_t *trx); /*!< in: transaction in the
120 QUE_THR_LOCK_WAIT state */
121 /** Starts execution of a command in a query fork. Picks a query thread which
122 is not in the QUE_THR_RUNNING state and moves it to that state. If none
123 can be chosen, a situation which may arise in parallelized fetches, NULL
124 is returned.
125 @return a query thread of the graph moved to QUE_THR_RUNNING state, or
126 NULL; the query thread should be executed by que_run_threads by the
127 caller */
128 que_thr_t *que_fork_start_command(que_fork_t *fork); /*!< in: a query fork */
129 /** Gets the trx of a query thread. */
130 static inline trx_t *thr_get_trx(que_thr_t *thr); /*!< in: query thread */
131 /** Determines if this thread is rolling back an incomplete transaction
132 in crash recovery.
133 @return true if thr is rolling back an incomplete transaction in crash
134 recovery */
135 static inline bool thr_is_recv(const que_thr_t *thr); /*!< in: query thread */
136 /** Gets the type of a graph node. */
137 static inline ulint que_node_get_type(
138 const que_node_t *node); /*!< in: graph node */
139 /** Gets pointer to the value data type field of a graph node. */
140 static inline dtype_t *que_node_get_data_type(
141 que_node_t *node); /*!< in: graph node */
142 /** Gets pointer to the value dfield of a graph node. */
143 static inline dfield_t *que_node_get_val(
144 que_node_t *node); /*!< in: graph node */
145 /** Gets the value buffer size of a graph node.
146 @return val buffer size, not defined if val.data == NULL in node */
147 static inline ulint que_node_get_val_buf_size(
148 que_node_t *node); /*!< in: graph node */
149
150 /** Sets the value buffer size of a graph node.
151 @param[in] node graph node
152 @param[in] size size */
153 static inline void que_node_set_val_buf_size(que_node_t *node, ulint size);
154
155 /** Gets the next list node in a list of query graph nodes. */
156 static inline que_node_t *que_node_get_next(
157 que_node_t *node); /*!< in: node in a list */
158 /** Gets the parent node of a query graph node.
159 @return parent node or NULL */
160 static inline que_node_t *que_node_get_parent(
161 que_node_t *node); /*!< in: node */
162 /** Get the first containing loop node (e.g. while_node_t or for_node_t) for the
163 given node, or NULL if the node is not within a loop.
164 @return containing loop node, or NULL. */
165 que_node_t *que_node_get_containing_loop_node(
166 que_node_t *node); /*!< in: node */
167
168 /** Catenates a query graph node to a list of them, possible empty list.
169 @param[in] node_list node list, or NULL
170 @param[in] node node
171 @return one-way list of nodes */
172 static inline que_node_t *que_node_list_add_last(que_node_t *node_list,
173 que_node_t *node);
174
175 /*************************************************************************
176 Get the last node from the list.*/
177 static inline que_node_t *que_node_list_get_last(
178 /* out: node last node from list.*/
179 que_node_t *node_list); /* in: node list, or NULL */
180 /** Gets a query graph node list length.
181 @return length, for NULL list 0 */
182 static inline ulint que_node_list_get_len(
183 que_node_t *node_list); /*!< in: node list, or NULL */
184 /** Checks if graph, trx, or session is in a state where the query thread should
185 be stopped.
186 @return true if should be stopped; NOTE that if the peek is made
187 without reserving the trx_t::mutex, then another peek with the mutex
188 reserved is necessary before deciding the actual stopping */
189 static inline bool que_thr_peek_stop(que_thr_t *thr); /*!< in: query thread */
190 /** Returns true if the query graph is for a SELECT statement.
191 @return true if a select */
192 static inline bool que_graph_is_select(que_t *graph); /*!< in: graph */
193 /** Prints info of an SQL query graph node. */
194 void que_node_print_info(que_node_t *node); /*!< in: query graph node */
195 /** Evaluate the given SQL
196 @param[in] info Info struct, or nullptr.
197 @param[in] sql SQL string.
198 @param[in] trx Transaction.
199 @return error code or DB_SUCCESS */
200 dberr_t que_eval_sql(pars_info_t *info, const char *sql, trx_t *trx);
201
202 /** Round robin scheduler.
203 @return a query thread of the graph moved to QUE_THR_RUNNING state, or
204 NULL; the query thread should be executed by que_run_threads by the
205 caller */
206 que_thr_t *que_fork_scheduler_round_robin(
207 que_fork_t *fork, /*!< in: a query fork */
208 que_thr_t *thr); /*!< in: current pos */
209
210 /** Initialise the query sub-system. */
211 void que_init(void);
212
213 /** Close the query sub-system. */
214 void que_close(void);
215
216 /** Query thread states */
217 enum que_thr_state_t {
218 QUE_THR_RUNNING,
219 QUE_THR_PROCEDURE_WAIT,
220 /** in selects this means that the thread is at the end of its
221 result set (or start, in case of a scroll cursor); in other
222 statements, this means the thread has done its task */
223 QUE_THR_COMPLETED,
224 QUE_THR_COMMAND_WAIT,
225 QUE_THR_LOCK_WAIT,
226 QUE_THR_SUSPENDED
227 };
228
229 /** Query thread lock states */
230 enum que_thr_lock_t {
231 QUE_THR_LOCK_NOLOCK,
232 QUE_THR_LOCK_ROW,
233 QUE_THR_LOCK_TABLE
234 };
235
236 /** From where the cursor position is counted */
237 enum que_cur_t { QUE_CUR_NOT_DEFINED, QUE_CUR_START, QUE_CUR_END };
238
239 /* Query graph query thread node: the fields are protected by the
240 trx_t::mutex with the exceptions named below */
241
242 struct que_thr_t {
243 que_common_t common; /*!< type: QUE_NODE_THR */
244 ulint magic_n; /*!< magic number to catch memory
245 corruption */
246 que_node_t *child; /*!< graph child node */
247 que_t *graph; /*!< graph where this node belongs */
248 que_thr_state_t state; /*!< state of the query thread */
249 bool is_active; /*!< true if the thread has been set
250 to the run state in
251 que_thr_move_to_run_state, but not
252 deactivated in
253 que_thr_dec_reference_count */
254 /*------------------------------*/
255 /* The following fields are private to the OS thread executing the
256 query thread, and are not protected by any mutex: */
257
258 que_node_t *run_node; /*!< pointer to the node where the
259 subgraph down from this node is
260 currently executed */
261 que_node_t *prev_node; /*!< pointer to the node from which
262 the control came */
263 ulint resource; /*!< resource usage of the query thread
264 thus far */
265 ulint lock_state; /*!< lock state of thread (table or
266 row) */
267
268 /** The thread slot in the lock_sys->waiting_threads array protected by
269 lock_sys->wait_mutex when writing to it, and also by trx->mutex when changing
270 from null to non-null.
271 While reading, one can either hold the lock_sys->wait_mutex, or hold the
272 trx->mutex and a proof that no one has woken the trx yet,
273 so the slot is either still null (if trx hadn't yet started the sleep), or
274 already non-null (if it already started sleep), but definitely not
275 changing from null to non-null (as it requires trx->mutex) nor
276 changing from non-null to null (as it happens after wake up). */
277 struct srv_slot_t *slot;
278 /*------------------------------*/
279 /* The following fields are links for the various lists that
280 this type can be on. */
281 UT_LIST_NODE_T(que_thr_t)
282 thrs; /*!< list of thread nodes of the fork
283 node */
284 UT_LIST_NODE_T(que_thr_t)
285 trx_thrs; /*!< lists of threads in wait list of
286 the trx */
287 UT_LIST_NODE_T(que_thr_t)
288 queue; /*!< list of runnable thread nodes in
289 the server task queue */
290 ulint fk_cascade_depth; /*!< maximum cascading call depth
291 supported for foreign key constraint
292 related delete/updates */
293 row_prebuilt_t *prebuilt; /*!< prebuilt structure processed by
294 the query thread */
295 };
296
297 constexpr uint32_t QUE_THR_MAGIC_N = 8476583;
298 constexpr uint32_t QUE_THR_MAGIC_FREED = 123461526;
299
300 /* Query graph fork node: its fields are protected by the query thread mutex */
301 struct que_fork_t {
302 que_common_t common; /*!< type: QUE_NODE_FORK */
303 que_t *graph; /*!< query graph of this node */
304 ulint fork_type; /*!< fork type */
305 ulint n_active_thrs; /*!< if this is the root of a graph, the
306 number query threads that have been
307 started in que_thr_move_to_run_state
308 but for which que_thr_dec_refer_count
309 has not yet been called */
310 trx_t *trx; /*!< transaction: this is set only in
311 the root node */
312 ulint state; /*!< state of the fork node */
313 que_thr_t *caller; /*!< pointer to a possible calling query
314 thread */
315 UT_LIST_BASE_NODE_T(que_thr_t, thrs)
316 thrs; /*!< list of query threads */
317 /*------------------------------*/
318 /* The fields in this section are defined only in the root node */
319 sym_tab_t *sym_tab; /*!< symbol table of the query,
320 generated by the parser, or NULL
321 if the graph was created 'by hand' */
322 pars_info_t *info; /*!< info struct, or NULL */
323 /* The following cur_... fields are relevant only in a select graph */
324
325 ulint cur_end; /*!< QUE_CUR_NOT_DEFINED, QUE_CUR_START,
326 QUE_CUR_END */
327 ulint cur_pos; /*!< if there are n rows in the result
328 set, values 0 and n + 1 mean before
329 first row, or after last row, depending
330 on cur_end; values 1...n mean a row
331 index */
332 bool cur_on_row; /*!< true if cursor is on a row, i.e.,
333 it is not before the first row or
334 after the last row */
335 sel_node_t *last_sel_node; /*!< last executed select node, or NULL
336 if none */
337 UT_LIST_NODE_T(que_fork_t)
338 graphs; /*!< list of query graphs of a session
339 or a stored procedure */
340 /*------------------------------*/
341 mem_heap_t *heap; /*!< memory heap where the fork was
342 created */
343 };
344
345 /* Query fork (or graph) types */
346 constexpr uint32_t QUE_FORK_SELECT_NON_SCROLL = 1; /* forward-only cursor */
347 constexpr uint32_t QUE_FORK_SELECT_SCROLL = 2; /* scrollable cursor */
348 constexpr uint32_t QUE_FORK_INSERT = 3;
349 constexpr uint32_t QUE_FORK_UPDATE = 4;
350 constexpr uint32_t QUE_FORK_ROLLBACK = 5;
351 /* This is really the undo graph used in rollback,
352 no signal-sending roll_node in this graph */
353 constexpr uint32_t QUE_FORK_PURGE = 6;
354 constexpr uint32_t QUE_FORK_EXECUTE = 7;
355 constexpr uint32_t QUE_FORK_PROCEDURE = 8;
356 constexpr uint32_t QUE_FORK_PROCEDURE_CALL = 9;
357 constexpr uint32_t QUE_FORK_MYSQL_INTERFACE = 10;
358 constexpr uint32_t QUE_FORK_RECOVERY = 11;
359
360 /* Query fork (or graph) states */
361 constexpr uint32_t QUE_FORK_ACTIVE = 1;
362 constexpr uint32_t QUE_FORK_COMMAND_WAIT = 2;
363 constexpr uint32_t QUE_FORK_INVALID = 3;
364 constexpr uint32_t QUE_FORK_BEING_FREED = 4;
365
366 /* Flag which is ORed to control structure statement node types */
367 constexpr uint32_t QUE_NODE_CONTROL_STAT = 1024;
368
369 /* Query graph node types */
370 constexpr uint32_t QUE_NODE_LOCK = 1;
371 constexpr uint32_t QUE_NODE_INSERT = 2;
372 constexpr uint32_t QUE_NODE_UPDATE = 4;
373 constexpr uint32_t QUE_NODE_CURSOR = 5;
374 constexpr uint32_t QUE_NODE_SELECT = 6;
375 constexpr uint32_t QUE_NODE_AGGREGATE = 7;
376 constexpr uint32_t QUE_NODE_FORK = 8;
377 constexpr uint32_t QUE_NODE_THR = 9;
378 constexpr uint32_t QUE_NODE_UNDO = 10;
379 constexpr uint32_t QUE_NODE_COMMIT = 11;
380 constexpr uint32_t QUE_NODE_ROLLBACK = 12;
381 constexpr uint32_t QUE_NODE_PURGE = 13;
382 constexpr uint32_t QUE_NODE_SYMBOL = 14;
383 constexpr uint32_t QUE_NODE_RES_WORD = 15;
384 constexpr uint32_t QUE_NODE_FUNC = 16;
385 constexpr uint32_t QUE_NODE_ORDER = 17;
386 constexpr uint32_t QUE_NODE_PROC = 18 + QUE_NODE_CONTROL_STAT;
387 constexpr uint32_t QUE_NODE_IF = 19 + QUE_NODE_CONTROL_STAT;
388 constexpr uint32_t QUE_NODE_WHILE = 20 + QUE_NODE_CONTROL_STAT;
389 constexpr uint32_t QUE_NODE_ASSIGNMENT = 21;
390 constexpr uint32_t QUE_NODE_FETCH = 22;
391 constexpr uint32_t QUE_NODE_OPEN = 23;
392 constexpr uint32_t QUE_NODE_COL_ASSIGNMENT = 24;
393 constexpr uint32_t QUE_NODE_FOR = 25 + QUE_NODE_CONTROL_STAT;
394 constexpr uint32_t QUE_NODE_RETURN = 26;
395 constexpr uint32_t QUE_NODE_ELSIF = 27;
396 constexpr uint32_t QUE_NODE_CALL = 28;
397 constexpr uint32_t QUE_NODE_EXIT = 29;
398
399 #include "que0que.ic"
400
401 #endif