Debian Codegraph @

storage/innobase/que/que0que.cc

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 que/que0que.cc
29 Query graph
30
31 Created 5/27/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include <stddef.h>
35
36 #include "dict0crea.h"
37 #include "eval0eval.h"
38 #include "eval0proc.h"
39 #include "ha_prototypes.h"
40 #include "lock0lock.h"
41 #include "log0chkp.h"
42 #include "pars0types.h"
43 #include "que0que.h"
44 #include "row0ins.h"
45 #include "row0purge.h"
46 #include "row0sel.h"
47 #include "row0undo.h"
48 #include "row0upd.h"
49 #include "trx0roll.h"
50 #include "trx0trx.h"
51 #include "usr0sess.h"
52
53 #include "my_dbug.h"
54
55 /* Short introduction to query graphs
56 ==================================
57
58 A query graph consists of nodes linked to each other in various ways. The
59 execution starts at que_run_threads() which takes a que_thr_t parameter.
60 que_thr_t contains two fields that control query graph execution: run_node
61 and prev_node. run_node is the next node to execute and prev_node is the
62 last node executed.
63
64 Each node has a pointer to a 'next' statement, i.e., its brother, and a
65 pointer to its parent node. The next pointer is NULL in the last statement
66 of a block.
67
68 Loop nodes contain a link to the first statement of the enclosed statement
69 list. While the loop runs, que_thr_step() checks if execution to the loop
70 node came from its parent or from one of the statement nodes in the loop. If
71 it came from the parent of the loop node it starts executing the first
72 statement node in the loop. If it came from one of the statement nodes in
73 the loop, then it checks if the statement node has another statement node
74 following it, and runs it if so.
75
76 To signify loop ending, the loop statements (see e.g. while_step()) set
77 que_thr_t->run_node to the loop node's parent node. This is noticed on the
78 next call of que_thr_step() and execution proceeds to the node pointed to by
79 the loop node's 'next' pointer.
80
81 For example, the code:
82
83 X := 1;
84 WHILE X < 5 LOOP
85 X := X + 1;
86 X := X + 1;
87 X := 5
88
89 will result in the following node hierarchy, with the X-axis indicating
90 'next' links and the Y-axis indicating parent/child links:
91
92 A - W - A
93 |
94 |
95 A - A
96
97 A = assign_node_t, W = while_node_t. */
98
99 /* How a stored procedure containing COMMIT or ROLLBACK commands
100 is executed?
101
102 The commit or rollback can be seen as a subprocedure call.
103
104 When the transaction starts to handle a rollback or commit.
105 It builds a query graph which, when executed, will roll back
106 or commit the incomplete transaction. The transaction
107 is moved to the TRX_QUE_ROLLING_BACK or TRX_QUE_COMMITTING state.
108 If specified, the SQL cursors opened by the transaction are closed.
109 When the execution of the graph completes, it is like returning
110 from a subprocedure: the query thread which requested the operation
111 starts running again. */
112
113 /** Moves a thread from another state to the QUE_THR_RUNNING state. Increments
114 the n_active_thrs counters of the query graph and transaction.
115 ***NOTE***: This is the only function in which such a transition is allowed
116 to happen! */
117 static void que_thr_move_to_run_state(
118 que_thr_t *thr); /*!< in: an query thread */
119
120 /** Creates a query graph fork node.
121 @return own: fork node */
122 que_fork_t *que_fork_create(
123 que_t *graph, /*!< in: graph, if NULL then this
124 fork node is assumed to be the
125 graph root */
126 que_node_t *parent, /*!< in: parent node */
127 ulint fork_type, /*!< in: fork type */
128 mem_heap_t *heap) /*!< in: memory heap where created */
129 {
130 que_fork_t *fork;
131
132 ut_ad(heap);
133
134 fork = static_cast<que_fork_t *>(mem_heap_zalloc(heap, sizeof(*fork)));
135
136 fork->heap = heap;
137
138 fork->fork_type = fork_type;
139
140 fork->common.parent = parent;
141
142 fork->common.type = QUE_NODE_FORK;
143
144 fork->state = QUE_FORK_COMMAND_WAIT;
145
146 fork->graph = (graph != nullptr) ? graph : fork;
147
148 UT_LIST_INIT(fork->thrs);
149
150 return (fork);
151 }
152
153 /** Creates a query graph thread node.
154 @param[in] parent parent node, i.e., a fork node
155 @param[in] heap memory heap where created
156 @param[in] prebuilt row prebuilt structure
157 @return own: query thread node */
158 que_thr_t *que_thr_create(que_fork_t *parent, mem_heap_t *heap,
159 row_prebuilt_t *prebuilt) {
160 que_thr_t *thr;
161
162 ut_ad(parent != nullptr);
163 ut_ad(heap != nullptr);
164
165 thr = static_cast<que_thr_t *>(mem_heap_zalloc(heap, sizeof(*thr)));
166
167 thr->graph = parent->graph;
168
169 thr->common.parent = parent;
170
171 thr->magic_n = QUE_THR_MAGIC_N;
172
173 thr->common.type = QUE_NODE_THR;
174
175 thr->state = QUE_THR_COMMAND_WAIT;
176
177 thr->lock_state = QUE_THR_LOCK_NOLOCK;
178
179 thr->prebuilt = prebuilt;
180
181 UT_LIST_ADD_LAST(parent->thrs, thr);
182
183 return (thr);
184 }
185
186 /** Moves a suspended query thread to the QUE_THR_RUNNING state and may release
187 a worker thread to execute it. This function should be used to end
188 the wait state of a query thread waiting for a lock or a stored procedure
189 completion.
190 @return the query thread that needs to be released. */
191 que_thr_t *que_thr_end_lock_wait(trx_t *trx) /*!< in: transaction with que_state
192 in QUE_THR_LOCK_WAIT */
193 {
194 ut_ad(locksys::owns_lock_shard(trx->lock.wait_lock));
195
196 ut_ad(trx_mutex_own(trx));
197
198 que_thr_t *const thr = trx->lock.wait_thr;
199
200 ut_ad(thr != nullptr);
201
202 ut_ad(trx->lock.que_state == TRX_QUE_LOCK_WAIT);
203 /* In MySQL this is the only possible state here */
204 ut_a(thr->state == QUE_THR_LOCK_WAIT);
205
206 bool const was_active = thr->is_active;
207
208 que_thr_move_to_run_state(thr);
209
210 trx->lock.que_state = TRX_QUE_RUNNING;
211
212 trx->lock.wait_thr = nullptr;
213
214 /* In MySQL we let the OS thread (not just the query thread) to wait
215 for the lock to be released: */
216
217 return !was_active ? thr : nullptr;
218 }
219
220 /** Inits a query thread for a command. */
221 static inline void que_thr_init_command(que_thr_t *thr) /*!< in: query thread */
222 {
223 thr->run_node = thr;
224 thr->prev_node = thr->common.parent;
225
226 que_thr_move_to_run_state(thr);
227 }
228
229 /** Round robin scheduler.
230 @return a query thread of the graph moved to QUE_THR_RUNNING state, or
231 NULL; the query thread should be executed by que_run_threads by the
232 caller */
233 que_thr_t *que_fork_scheduler_round_robin(
234 que_fork_t *fork, /*!< in: a query fork */
235 que_thr_t *thr) /*!< in: current pos */
236 {
237 trx_mutex_enter(fork->trx);
238
239 /* If no current, start first available. */
240 if (thr == nullptr) {
241 thr = UT_LIST_GET_FIRST(fork->thrs);
242 } else {
243 thr = UT_LIST_GET_NEXT(thrs, thr);
244 }
245
246 if (thr) {
247 fork->state = QUE_FORK_ACTIVE;
248
249 fork->last_sel_node = nullptr;
250
251 switch (thr->state) {
252 case QUE_THR_COMMAND_WAIT:
253 case QUE_THR_COMPLETED:
254 ut_a(!thr->is_active);
255 que_thr_init_command(thr);
256 break;
257
258 case QUE_THR_SUSPENDED:
259 case QUE_THR_LOCK_WAIT:
260 default:
261 ut_error;
262 }
263 }
264
265 trx_mutex_exit(fork->trx);
266
267 return (thr);
268 }
269
270 /** Starts execution of a command in a query fork. Picks a query thread which
271 is not in the QUE_THR_RUNNING state and moves it to that state. If none
272 can be chosen, a situation which may arise in parallelized fetches, NULL
273 is returned.
274 @return a query thread of the graph moved to QUE_THR_RUNNING state, or
275 NULL; the query thread should be executed by que_run_threads by the
276 caller */
277 que_thr_t *que_fork_start_command(que_fork_t *fork) /*!< in: a query fork */
278 {
279 que_thr_t *suspended_thr = nullptr;
280 que_thr_t *completed_thr = nullptr;
281
282 fork->state = QUE_FORK_ACTIVE;
283
284 fork->last_sel_node = nullptr;
285
286 suspended_thr = nullptr;
287 completed_thr = nullptr;
288
289 /* Choose the query thread to run: usually there is just one thread,
290 but in a parallelized select, which necessarily is non-scrollable,
291 there may be several to choose from */
292
293 /* First we try to find a query thread in the QUE_THR_COMMAND_WAIT
294 state. Then we try to find a query thread in the QUE_THR_SUSPENDED
295 state, finally we try to find a query thread in the QUE_THR_COMPLETED
296 state */
297
298 /* We make a single pass over the thr list within which we note which
299 threads are ready to run. */
300 for (auto thr : fork->thrs) {
301 switch (thr->state) {
302 case QUE_THR_COMMAND_WAIT:
303
304 /* We have to send the initial message to query thread
305 to start it */
306
307 que_thr_init_command(thr);
308
309 return (thr);
310
311 case QUE_THR_SUSPENDED:
312 /* In this case the execution of the thread was
313 suspended: no initial message is needed because
314 execution can continue from where it was left */
315 if (!suspended_thr) {
316 suspended_thr = thr;
317 }
318
319 break;
320
321 case QUE_THR_COMPLETED:
322 if (!completed_thr) {
323 completed_thr = thr;
324 }
325
326 break;
327
328 case QUE_THR_RUNNING:
329 case QUE_THR_LOCK_WAIT:
330 case QUE_THR_PROCEDURE_WAIT:
331 ut_error;
332 }
333 }
334 que_thr_t *thr;
335 if (suspended_thr) {
336 thr = suspended_thr;
337 que_thr_move_to_run_state(thr);
338
339 } else if (completed_thr) {
340 thr = completed_thr;
341 que_thr_init_command(thr);
342 } else {
343 ut_error;
344 }
345
346 return (thr);
347 }
348
349 /** Calls que_graph_free_recursive for statements in a statement list. */
350 static void que_graph_free_stat_list(
351 que_node_t *node) /*!< in: first query graph node in the list */
352 {
353 while (node) {
354 que_graph_free_recursive(node);
355
356 node = que_node_get_next(node);
357 }
358 }
359
360 /** Frees a query graph, but not the heap where it was created. Does not free
361 explicit cursor declarations, they are freed in que_graph_free. */
362 void que_graph_free_recursive(que_node_t *node) /*!< in: query graph node */
363 {
364 que_fork_t *fork;
365 que_thr_t *thr;
366 undo_node_t *undo;
367 sel_node_t *sel;
368 ins_node_t *ins;
369 upd_node_t *upd;
370 purge_node_t *purge;
371
372 DBUG_TRACE;
373
374 if (node == nullptr) {
375 return;
376 }
377
378 DBUG_PRINT("que_graph_free_recursive",
379 ("node: %p, type: %lu", node, que_node_get_type(node)));
380
381 switch (que_node_get_type(node)) {
382 case QUE_NODE_FORK:
383 fork = static_cast<que_fork_t *>(node);
384
385 thr = UT_LIST_GET_FIRST(fork->thrs);
386
387 while (thr) {
388 que_graph_free_recursive(thr);
389
390 thr = UT_LIST_GET_NEXT(thrs, thr);
391 }
392
393 break;
394 case QUE_NODE_THR:
395
396 thr = static_cast<que_thr_t *>(node);
397
398 ut_a(thr->magic_n == QUE_THR_MAGIC_N);
399
400 thr->magic_n = QUE_THR_MAGIC_FREED;
401
402 que_graph_free_recursive(thr->child);
403
404 break;
405
406 case QUE_NODE_UNDO:
407 undo = static_cast<undo_node_t *>(node);
408
409 /* Free the heap memory. */
410 mem_heap_free(undo->heap);
411 break;
412
413 case QUE_NODE_SELECT:
414
415 sel = static_cast<sel_node_t *>(node);
416
417 sel_node_free_private(sel);
418
419 break;
420 case QUE_NODE_INSERT:
421
422 ins = static_cast<ins_node_t *>(node);
423
424 que_graph_free_recursive(ins->select);
425 ins->select = nullptr;
426
427 if (ins->entry_sys_heap != nullptr) {
428 mem_heap_free(ins->entry_sys_heap);
429 ins->entry_sys_heap = nullptr;
430 }
431
432 break;
433 case QUE_NODE_PURGE:
434 purge = static_cast<purge_node_t *>(node);
435
436 purge->deinit();
437
438 break;
439
440 case QUE_NODE_UPDATE:
441 upd = static_cast<upd_node_t *>(node);
442
443 if (upd->update) {
444 upd->update->free_per_stmt_heap();
445 }
446
447 if (upd->in_mysql_interface) {
448 btr_pcur_t::free_for_mysql(upd->pcur);
449 upd->in_mysql_interface = false;
450 }
451
452 que_graph_free_recursive(upd->cascade_node);
453 if (upd->cascade_heap) {
454 mem_heap_free(upd->cascade_heap);
455 upd->cascade_heap = nullptr;
456 }
457
458 que_graph_free_recursive(upd->select);
459 upd->select = nullptr;
460
461 if (upd->heap != nullptr) {
462 mem_heap_free(upd->heap);
463 upd->heap = nullptr;
464 }
465
466 break;
467 case QUE_NODE_PROC:
468 que_graph_free_stat_list(((proc_node_t *)node)->stat_list);
469
470 break;
471 case QUE_NODE_IF:
472 que_graph_free_stat_list(((if_node_t *)node)->stat_list);
473 que_graph_free_stat_list(((if_node_t *)node)->else_part);
474 que_graph_free_stat_list(((if_node_t *)node)->elsif_list);
475
476 break;
477 case QUE_NODE_ELSIF:
478 que_graph_free_stat_list(((elsif_node_t *)node)->stat_list);
479
480 break;
481 case QUE_NODE_WHILE:
482 que_graph_free_stat_list(((while_node_t *)node)->stat_list);
483
484 break;
485 case QUE_NODE_FOR:
486 que_graph_free_stat_list(((for_node_t *)node)->stat_list);
487
488 break;
489
490 case QUE_NODE_ASSIGNMENT:
491 case QUE_NODE_EXIT:
492 case QUE_NODE_RETURN:
493 case QUE_NODE_COMMIT:
494 case QUE_NODE_ROLLBACK:
495 case QUE_NODE_LOCK:
496 case QUE_NODE_FUNC:
497 case QUE_NODE_ORDER:
498 case QUE_NODE_OPEN:
499 case QUE_NODE_FETCH:
500 /* No need to do anything */
501
502 break;
503 default:
504 ut_error;
505 }
506 }
507
508 /** Frees a query graph. */
509 void que_graph_free(que_t *graph) /*!< in: query graph; we assume that the
510 memory heap where this graph was created is
511 private to this graph: if not, then use
512 que_graph_free_recursive and free the heap
513 afterwards! */
514 {
515 ut_ad(graph);
516 ut_ad(!dict_sys_mutex_own());
517
518 if (graph->sym_tab) {
519 /* The following call frees dynamic memory allocated
520 for variables etc. during execution. Frees also explicit
521 cursor definitions. */
522
523 sym_tab_free_private(graph->sym_tab);
524 }
525
526 if (graph->info && graph->info->graph_owns_us) {
527 pars_info_free(graph->info);
528 }
529
530 que_graph_free_recursive(graph);
531
532 mem_heap_free(graph->heap);
533 }
534
535 /** Performs an execution step on a thr node.
536 @return query thread to run next, or NULL if none */
537 static que_thr_t *que_thr_node_step(
538 que_thr_t *thr) /*!< in: query thread where run_node must
539 be the thread node itself */
540 {
541 ut_ad(thr->run_node == thr);
542
543 if (thr->prev_node == thr->common.parent) {
544 /* If control to the node came from above, it is just passed
545 on */
546
547 thr->run_node = thr->child;
548
549 return (thr);
550 }
551
552 trx_mutex_enter(thr_get_trx(thr));
553
554 if (que_thr_peek_stop(thr)) {
555 trx_mutex_exit(thr_get_trx(thr));
556
557 return (thr);
558 }
559
560 /* Thread execution completed */
561
562 thr->state = QUE_THR_COMPLETED;
563
564 trx_mutex_exit(thr_get_trx(thr));
565
566 return (nullptr);
567 }
568
569 /** Moves a thread from another state to the QUE_THR_RUNNING state. Increments
570 the n_active_thrs counters of the query graph and transaction if thr was
571 not active.
572 ***NOTE***: This and ..._mysql are the only functions in which such a
573 transition is allowed to happen! */
574 static void que_thr_move_to_run_state(
575 que_thr_t *thr) /*!< in: an query thread */
576 {
577 ut_ad(thr->state != QUE_THR_RUNNING);
578
579 if (!thr->is_active) {
580 trx_t *trx;
581
582 trx = thr_get_trx(thr);
583
584 thr->graph->n_active_thrs++;
585
586 trx->lock.n_active_thrs++;
587
588 thr->is_active = true;
589 }
590
591 thr->state = QUE_THR_RUNNING;
592 }
593
594 bool que_thr_stop(que_thr_t *thr) {
595 que_t *graph = thr->graph;
596 trx_t *trx = thr_get_trx(thr);
597
598 ut_ad(trx_mutex_own(trx));
599
600 if (graph->state == QUE_FORK_COMMAND_WAIT) {
601 thr->state = QUE_THR_SUSPENDED;
602
603 } else if (trx->lock.que_state == TRX_QUE_LOCK_WAIT) {
604 trx->lock.wait_thr = thr;
605 thr->state = QUE_THR_LOCK_WAIT;
606
607 } else if (trx->error_state != DB_SUCCESS &&
608 trx->error_state != DB_LOCK_WAIT) {
609 /* Error handling built for the MySQL interface */
610 thr->state = QUE_THR_COMPLETED;
611
612 } else if (graph->fork_type == QUE_FORK_ROLLBACK) {
613 thr->state = QUE_THR_SUSPENDED;
614 } else {
615 ut_ad(graph->state == QUE_FORK_ACTIVE);
616
617 return false;
618 }
619
620 return true;
621 }
622
623 /** Decrements the query thread reference counts in the query graph and the
624 transaction.
625 *** NOTE ***:
626 This and que_thr_stop_for_mysql are the only functions where the reference
627 count can be decremented and this function may only be called from inside
628 que_run_threads! These restrictions exist to make the rollback code easier
629 to maintain. */
630 static void que_thr_dec_refer_count(
631 que_thr_t *thr, /*!< in: query thread */
632 que_thr_t **next_thr) /*!< in/out: next query thread to run;
633 if the value which is passed in is
634 a pointer to a NULL pointer, then the
635 calling function can start running
636 a new query thread */
637 {
638 trx_t *trx;
639 que_fork_t *fork;
640
641 trx = thr_get_trx(thr);
642
643 ut_a(thr->is_active);
644 ut_ad(trx_mutex_own(trx));
645
646 if (thr->state == QUE_THR_RUNNING) {
647 if (!que_thr_stop(thr)) {
648 ut_a(next_thr != nullptr && *next_thr == nullptr);
649
650 /* The reason for the thr suspension or wait was
651 already canceled before we came here: continue
652 running the thread.
653
654 This is also possible because in trx_commit_step() we
655 assume a single query thread. We set the query thread
656 state to QUE_THR_RUNNING. */
657
658 /* fprintf(stderr,
659 "Wait already ended: trx: %p\n", trx); */
660
661 /* Normally srv_suspend_mysql_thread resets
662 the state to DB_SUCCESS before waiting, but
663 in this case we have to do it here,
664 otherwise nobody does it. */
665
666 trx->error_state = DB_SUCCESS;
667
668 *next_thr = thr;
669
670 return;
671 }
672 }
673
674 fork = static_cast<que_fork_t *>(thr->common.parent);
675
676 --trx->lock.n_active_thrs;
677
678 --fork->n_active_thrs;
679
680 thr->is_active = false;
681 }
682
683 /** A patch for MySQL used to 'stop' a dummy query thread used in MySQL. The
684 query thread is stopped and made inactive, except in the case where
685 it was put to the lock wait state in lock0lock.cc, but the lock has already
686 been granted or the transaction chosen as a victim in deadlock resolution. */
687 void que_thr_stop_for_mysql(que_thr_t *thr) /*!< in: query thread */
688 {
689 trx_t *trx;
690
691 trx = thr_get_trx(thr);
692
693 trx_mutex_enter(trx);
694
695 if (thr->state == QUE_THR_RUNNING) {
696 if (trx->error_state != DB_SUCCESS && trx->error_state != DB_LOCK_WAIT) {
697 /* Error handling built for the MySQL interface */
698 thr->state = QUE_THR_COMPLETED;
699 } else {
700 /* It must have been a lock wait but the lock was
701 already released, or this transaction was chosen
702 as a victim in selective deadlock resolution */
703
704 trx_mutex_exit(trx);
705
706 return;
707 }
708 }
709
710 ut_ad(thr->is_active == true);
711 ut_ad(trx->lock.n_active_thrs == 1);
712 ut_ad(thr->graph->n_active_thrs == 1);
713
714 thr->is_active = false;
715 thr->graph->n_active_thrs--;
716
717 trx->lock.n_active_thrs--;
718
719 trx_mutex_exit(trx);
720 }
721
722 /** Moves a thread from another state to the QUE_THR_RUNNING state. Increments
723 the n_active_thrs counters of the query graph and transaction if thr was
724 not active. */
725 void que_thr_move_to_run_state_for_mysql(
726 que_thr_t *thr, /*!< in: an query thread */
727 trx_t *trx) /*!< in: transaction */
728 {
729 ut_a(thr->magic_n == QUE_THR_MAGIC_N);
730
731 if (!thr->is_active) {
732 thr->graph->n_active_thrs++;
733
734 trx->lock.n_active_thrs++;
735
736 thr->is_active = true;
737 }
738
739 thr->state = QUE_THR_RUNNING;
740 }
741
742 /** A patch for MySQL used to 'stop' a dummy query thread used in MySQL
743 select, when there is no error or lock wait.
744 @param[in] thr Query thread
745 @param[in] trx Transaction */
746 void que_thr_stop_for_mysql_no_error(que_thr_t *thr, trx_t *trx) {
747 ut_ad(thr->state == QUE_THR_RUNNING);
748 ut_ad(thr->is_active == true);
749 ut_ad(trx->lock.n_active_thrs == 1);
750 ut_ad(thr->graph->n_active_thrs == 1);
751 ut_a(thr->magic_n == QUE_THR_MAGIC_N);
752
753 thr->state = QUE_THR_COMPLETED;
754
755 thr->is_active = false;
756 thr->graph->n_active_thrs--;
757
758 trx->lock.n_active_thrs--;
759 }
760
761 /** Get the first containing loop node (e.g. while_node_t or for_node_t) for the
762 given node, or NULL if the node is not within a loop.
763 @return containing loop node, or NULL. */
764 que_node_t *que_node_get_containing_loop_node(que_node_t *node) /*!< in: node */
765 {
766 ut_ad(node);
767
768 for (;;) {
769 ulint type;
770
771 node = que_node_get_parent(node);
772
773 if (!node) {
774 break;
775 }
776
777 type = que_node_get_type(node);
778
779 if ((type == QUE_NODE_FOR) || (type == QUE_NODE_WHILE)) {
780 break;
781 }
782 }
783
784 return (node);
785 }
786
787 #ifdef UNIV_DEBUG
788 /** Gets information of an SQL query graph node.
789 @return type description */
790 [[nodiscard]] static const char *que_node_type_string(
791 const que_node_t *node) /*!< in: query graph node */
792 {
793 switch (que_node_get_type(node)) {
794 case QUE_NODE_SELECT:
795 return ("SELECT");
796 case QUE_NODE_INSERT:
797 return ("INSERT");
798 case QUE_NODE_UPDATE:
799 return ("UPDATE");
800 case QUE_NODE_WHILE:
801 return ("WHILE");
802 case QUE_NODE_ASSIGNMENT:
803 return ("ASSIGNMENT");
804 case QUE_NODE_IF:
805 return ("IF");
806 case QUE_NODE_FETCH:
807 return ("FETCH");
808 case QUE_NODE_OPEN:
809 return ("OPEN");
810 case QUE_NODE_PROC:
811 return ("STORED PROCEDURE");
812 case QUE_NODE_FUNC:
813 return ("FUNCTION");
814 case QUE_NODE_LOCK:
815 return ("LOCK");
816 case QUE_NODE_THR:
817 return ("QUERY THREAD");
818 case QUE_NODE_COMMIT:
819 return ("COMMIT");
820 case QUE_NODE_UNDO:
821 return ("UNDO ROW");
822 case QUE_NODE_PURGE:
823 return ("PURGE ROW");
824 case QUE_NODE_ROLLBACK:
825 return ("ROLLBACK");
826 case QUE_NODE_FOR:
827 return ("FOR LOOP");
828 case QUE_NODE_RETURN:
829 return ("RETURN");
830 case QUE_NODE_EXIT:
831 return ("EXIT");
832 default:
833 ut_d(ut_error);
834 ut_o(return ("UNKNOWN NODE TYPE"));
835 }
836 }
837 #endif /* UNIV_DEBUG */
838
839 /** Performs an execution step on a query thread.
840 @return query thread to run next: it may differ from the input
841 parameter if, e.g., a subprocedure call is made */
842 static inline que_thr_t *que_thr_step(que_thr_t *thr) /*!< in: query thread */
843 {
844 que_node_t *node;
845 que_thr_t *old_thr;
846 trx_t *trx;
847 ulint type;
848
849 trx = thr_get_trx(thr);
850
851 ut_ad(thr->state == QUE_THR_RUNNING);
852 ut_a(trx->error_state == DB_SUCCESS);
853
854 thr->resource++;
855
856 node = thr->run_node;
857 type = que_node_get_type(node);
858
859 old_thr = thr;
860
861 DBUG_PRINT("ib_que", ("Execute %u (%s) at %p", unsigned(type),
862 que_node_type_string(node), (const void *)node));
863
864 if (type & QUE_NODE_CONTROL_STAT) {
865 if ((thr->prev_node != que_node_get_parent(node)) &&
866 que_node_get_next(thr->prev_node)) {
867 /* The control statements, like WHILE, always pass the
868 control to the next child statement if there is any
869 child left */
870
871 thr->run_node = que_node_get_next(thr->prev_node);
872
873 } else if (type == QUE_NODE_IF) {
874 if_step(thr);
875 } else if (type == QUE_NODE_FOR) {
876 for_step(thr);
877 } else if (type == QUE_NODE_PROC) {
878 /* We can access trx->undo_no without reserving
879 trx->undo_mutex, because there cannot be active query
880 threads doing updating or inserting at the moment! */
881
882 if (thr->prev_node == que_node_get_parent(node)) {
883 trx->last_sql_stat_start.least_undo_no = trx->undo_no;
884 }
885
886 proc_step(thr);
887 } else if (type == QUE_NODE_WHILE) {
888 while_step(thr);
889 } else {
890 ut_error;
891 }
892 } else if (type == QUE_NODE_ASSIGNMENT) {
893 assign_step(thr);
894 } else if (type == QUE_NODE_SELECT) {
895 thr = row_sel_step(thr);
896 } else if (type == QUE_NODE_INSERT) {
897 thr = row_ins_step(thr);
898 } else if (type == QUE_NODE_UPDATE) {
899 thr = row_upd_step(thr);
900 } else if (type == QUE_NODE_FETCH) {
901 thr = fetch_step(thr);
902 } else if (type == QUE_NODE_OPEN) {
903 thr = open_step(thr);
904 } else if (type == QUE_NODE_FUNC) {
905 proc_eval_step(thr);
906
907 } else if (type == QUE_NODE_LOCK) {
908 ut_error;
909 } else if (type == QUE_NODE_THR) {
910 thr = que_thr_node_step(thr);
911 } else if (type == QUE_NODE_COMMIT) {
912 thr = trx_commit_step(thr);
913 } else if (type == QUE_NODE_UNDO) {
914 thr = row_undo_step(thr);
915 } else if (type == QUE_NODE_PURGE) {
916 thr = row_purge_step(thr);
917 } else if (type == QUE_NODE_RETURN) {
918 thr = return_step(thr);
919 } else if (type == QUE_NODE_EXIT) {
920 thr = exit_step(thr);
921 } else if (type == QUE_NODE_ROLLBACK) {
922 thr = trx_rollback_step(thr);
923 } else {
924 ut_error;
925 }
926
927 if (type == QUE_NODE_EXIT) {
928 old_thr->prev_node = que_node_get_containing_loop_node(node);
929 } else {
930 old_thr->prev_node = node;
931 }
932
933 if (thr) {
934 ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
935 }
936
937 return (thr);
938 }
939
940 /** Run a query thread until it finishes or encounters e.g. a lock wait. */
941 static void que_run_threads_low(que_thr_t *thr) /*!< in: query thread */
942 {
943 trx_t *trx;
944 que_thr_t *next_thr;
945
946 ut_ad(thr->state == QUE_THR_RUNNING);
947 ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
948 ut_ad(!trx_mutex_own(thr_get_trx(thr)));
949
950 /* cumul_resource counts how much resources the OS thread (NOT the
951 query thread) has spent in this function */
952
953 trx = thr_get_trx(thr);
954
955 do {
956 /* Check that there is enough space in the log to accommodate
957 possible log entries by this query step; if the operation can
958 touch more than about 4 pages, checks must be made also within
959 the query step! */
960
961 log_free_check();
962
963 /* Perform the actual query step: note that the query thread
964 may change if, e.g., a subprocedure call is made */
965
966 /*-------------------------*/
967 next_thr = que_thr_step(thr);
968 /*-------------------------*/
969
970 trx_mutex_enter(trx);
971
972 ut_a(next_thr == nullptr || trx->error_state == DB_SUCCESS);
973
974 if (next_thr != thr) {
975 ut_a(next_thr == nullptr);
976
977 /* This can change next_thr to a non-NULL value
978 if there was a lock wait that already completed. */
979
980 que_thr_dec_refer_count(thr, &next_thr);
981
982 if (next_thr != nullptr) {
983 thr = next_thr;
984 }
985 }
986
987 ut_ad(trx == thr_get_trx(thr));
988
989 trx_mutex_exit(trx);
990
991 } while (next_thr != nullptr);
992 }
993
994 /** Run a query thread. Handles lock waits. */
995 void que_run_threads(que_thr_t *thr) /*!< in: query thread */
996 {
997 ut_ad(!trx_mutex_own(thr_get_trx(thr)));
998
999 loop:
1000 ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
1001
1002 que_run_threads_low(thr);
1003 IF_DEBUG(if (thr->graph->sym_tab) { DEBUG_SYNC_C("after_low_with_lock"); });
1004
1005 switch (thr->state) {
1006 case QUE_THR_RUNNING:
1007 /* There probably was a lock wait, but it already ended
1008 before we came here: continue running thr */
1009
1010 ut_ad(thr_get_trx(thr)->error_state == DB_LOCK_WAIT);
1011 thr_get_trx(thr)->error_state = DB_SUCCESS;
1012 goto loop;
1013
1014 case QUE_THR_LOCK_WAIT:
1015 lock_wait_suspend_thread(thr);
1016
1017 trx_mutex_enter(thr_get_trx(thr));
1018
1019 ut_a(thr_get_trx(thr)->id != 0);
1020
1021 if (thr_get_trx(thr)->error_state != DB_SUCCESS) {
1022 /* thr was chosen as a deadlock victim or there was
1023 a lock wait timeout */
1024
1025 que_thr_dec_refer_count(thr, nullptr);
1026 trx_mutex_exit(thr_get_trx(thr));
1027 break;
1028 }
1029
1030 trx_mutex_exit(thr_get_trx(thr));
1031 goto loop;
1032
1033 case QUE_THR_COMPLETED:
1034 case QUE_THR_COMMAND_WAIT:
1035 /* Do nothing */
1036 break;
1037
1038 default:
1039 ut_error;
1040 }
1041 }
1042
1043 dberr_t que_eval_sql(pars_info_t *info, const char *sql, trx_t *trx) {
1044 que_t *graph;
1045
1046 DBUG_TRACE;
1047 DBUG_PRINT("que_eval_sql", ("query: %s", sql));
1048
1049 ut_a(trx->error_state == DB_SUCCESS);
1050
1051 mutex_enter(&pars_mutex);
1052
1053 graph = pars_sql(info, sql);
1054
1055 mutex_exit(&pars_mutex);
1056
1057 graph->trx = trx;
1058 trx->graph = nullptr;
1059
1060 graph->fork_type = QUE_FORK_MYSQL_INTERFACE;
1061
1062 auto thr = que_fork_start_command(graph);
1063 ut_a(thr);
1064
1065 que_run_threads(thr);
1066
1067 que_graph_free(graph);
1068
1069 ut_a(trx->error_state != 0);
1070
1071 return trx->error_state;
1072 }
1073
1074 /** Initialise the query sub-system. */
1075 void que_init(void) { /* No op */
1076 }
1077
1078 /** Close the query sub-system. */
1079 void que_close(void) { /* No op */
1080 }