1
/*****************************************************************************
3
Copyright (C) 1996, 2010, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
17
*****************************************************************************/
19
/**************************************************//**
20
@file include/que0que.h
23
Created 5/27/1996 Heikki Tuuri
24
*******************************************************/
30
#include "data0data.h"
31
#include "dict0types.h"
35
#include "usr0types.h"
36
#include "que0types.h"
37
#include "row0types.h"
38
#include "pars0types.h"
40
/* If the following flag is set TRUE, the module will print trace info
41
of SQL execution in the UNIV_SQL_DEBUG version */
42
extern ibool que_trace_on;
44
/***********************************************************************//**
45
Adds a query graph to the session's list of graphs. */
50
que_t* graph, /*!< in: graph */
51
sess_t* sess); /*!< in: session */
52
/***********************************************************************//**
53
Creates a query graph fork node.
54
@return own: fork node */
59
que_t* graph, /*!< in: graph, if NULL then this
60
fork node is assumed to be the
62
que_node_t* parent, /*!< in: parent node */
63
ulint fork_type, /*!< in: fork type */
64
mem_heap_t* heap); /*!< in: memory heap where created */
65
/***********************************************************************//**
66
Gets the first thr in a fork. */
69
que_fork_get_first_thr(
70
/*===================*/
71
que_fork_t* fork); /*!< in: query fork */
72
/***********************************************************************//**
73
Gets the child node of the first thr in a fork. */
78
que_fork_t* fork); /*!< in: query fork */
79
/***********************************************************************//**
80
Sets the parent of a graph node. */
85
que_node_t* node, /*!< in: graph node */
86
que_node_t* parent);/*!< in: parent */
87
/***********************************************************************//**
88
Creates a query graph thread node.
89
@return own: query thread node */
94
que_fork_t* parent, /*!< in: parent node, i.e., a fork node */
95
mem_heap_t* heap); /*!< in: memory heap where created */
96
/**********************************************************************//**
97
Frees a query graph, but not the heap where it was created. Does not free
98
explicit cursor declarations, they are freed in que_graph_free. */
101
que_graph_free_recursive(
102
/*=====================*/
103
que_node_t* node); /*!< in: query graph node */
104
/**********************************************************************//**
105
Frees a query graph. */
110
que_t* graph); /*!< in: query graph; we assume that the memory
111
heap where this graph was created is private
112
to this graph: if not, then use
113
que_graph_free_recursive and free the heap
115
/**********************************************************************//**
116
Stops a query thread if graph or trx is in a state requiring it. The
117
conditions are tested in the order (1) graph, (2) trx. The kernel mutex has
119
@return TRUE if stopped */
124
que_thr_t* thr); /*!< in: query thread */
125
/**********************************************************************//**
126
Moves a thread from another state to the QUE_THR_RUNNING state. Increments
127
the n_active_thrs counters of the query graph and transaction. */
130
que_thr_move_to_run_state_for_mysql(
131
/*================================*/
132
que_thr_t* thr, /*!< in: an query thread */
133
trx_t* trx); /*!< in: transaction */
134
/**********************************************************************//**
135
A patch for MySQL used to 'stop' a dummy query thread used in MySQL
136
select, when there is no error or lock wait. */
139
que_thr_stop_for_mysql_no_error(
140
/*============================*/
141
que_thr_t* thr, /*!< in: query thread */
142
trx_t* trx); /*!< in: transaction */
143
/**********************************************************************//**
144
A patch for MySQL used to 'stop' a dummy query thread used in MySQL. The
145
query thread is stopped and made inactive, except in the case where
146
it was put to the lock wait state in lock0lock.c, but the lock has already
147
been granted or the transaction chosen as a victim in deadlock resolution. */
150
que_thr_stop_for_mysql(
151
/*===================*/
152
que_thr_t* thr); /*!< in: query thread */
153
/**********************************************************************//**
154
Run a query thread. Handles lock waits. */
159
que_thr_t* thr); /*!< in: query thread */
160
/**********************************************************************//**
161
After signal handling is finished, returns control to a query graph error
162
handling routine. (Currently, just returns the control to the root of the
163
graph so that the graph can communicate an error message to the client.) */
166
que_fork_error_handle(
167
/*==================*/
168
trx_t* trx, /*!< in: trx */
169
que_t* fork); /*!< in: query graph which was run before signal
170
handling started, NULL not allowed */
171
/**********************************************************************//**
172
Moves a suspended query thread to the QUE_THR_RUNNING state and releases
173
a single worker thread to execute it. This function should be used to end
174
the wait state of a query thread waiting for a lock or a stored procedure
180
que_thr_t* thr, /*!< in: query thread in the
182
or QUE_THR_PROCEDURE_WAIT, or
183
QUE_THR_SIG_REPLY_WAIT state */
184
que_thr_t** next_thr); /*!< in/out: next query thread to run;
185
if the value which is passed in is
186
a pointer to a NULL pointer, then the
187
calling function can start running
188
a new query thread */
189
/**********************************************************************//**
190
Same as que_thr_end_wait, but no parameter next_thr available. */
193
que_thr_end_wait_no_next_thr(
194
/*=========================*/
195
que_thr_t* thr); /*!< in: query thread in the
197
or QUE_THR_PROCEDURE_WAIT, or
198
QUE_THR_SIG_REPLY_WAIT state */
199
/**********************************************************************//**
200
Starts execution of a command in a query fork. Picks a query thread which
201
is not in the QUE_THR_RUNNING state and moves it to that state. If none
202
can be chosen, a situation which may arise in parallelized fetches, NULL
204
@return a query thread of the graph moved to QUE_THR_RUNNING state, or
205
NULL; the query thread should be executed by que_run_threads by the
209
que_fork_start_command(
210
/*===================*/
211
que_fork_t* fork); /*!< in: a query fork */
212
/***********************************************************************//**
213
Gets the trx of a query thread. */
218
que_thr_t* thr); /*!< in: query thread */
219
/*******************************************************************//**
220
Determines if this thread is rolling back an incomplete transaction
222
@return TRUE if thr is rolling back an incomplete transaction in crash
228
const que_thr_t* thr); /*!< in: query thread */
229
/***********************************************************************//**
230
Gets the type of a graph node. */
235
que_node_t* node); /*!< in: graph node */
236
/***********************************************************************//**
237
Gets pointer to the value data type field of a graph node. */
240
que_node_get_data_type(
241
/*===================*/
242
que_node_t* node); /*!< in: graph node */
243
/***********************************************************************//**
244
Gets pointer to the value dfield of a graph node. */
249
que_node_t* node); /*!< in: graph node */
250
/***********************************************************************//**
251
Gets the value buffer size of a graph node.
252
@return val buffer size, not defined if val.data == NULL in node */
255
que_node_get_val_buf_size(
256
/*======================*/
257
que_node_t* node); /*!< in: graph node */
258
/***********************************************************************//**
259
Sets the value buffer size of a graph node. */
262
que_node_set_val_buf_size(
263
/*======================*/
264
que_node_t* node, /*!< in: graph node */
265
ulint size); /*!< in: size */
266
/*********************************************************************//**
267
Gets the next list node in a list of query graph nodes. */
272
que_node_t* node); /*!< in: node in a list */
273
/*********************************************************************//**
274
Gets the parent node of a query graph node.
275
@return parent node or NULL */
280
que_node_t* node); /*!< in: node */
281
/****************************************************************//**
282
Get the first containing loop node (e.g. while_node_t or for_node_t) for the
283
given node, or NULL if the node is not within a loop.
284
@return containing loop node, or NULL. */
287
que_node_get_containing_loop_node(
288
/*==============================*/
289
que_node_t* node); /*!< in: node */
290
/*********************************************************************//**
291
Catenates a query graph node to a list of them, possible empty list.
292
@return one-way list of nodes */
295
que_node_list_add_last(
296
/*===================*/
297
que_node_t* node_list, /*!< in: node list, or NULL */
298
que_node_t* node); /*!< in: node */
299
/*********************************************************************//**
300
Gets a query graph node list length.
301
@return length, for NULL list 0 */
304
que_node_list_get_len(
305
/*==================*/
306
que_node_t* node_list); /*!< in: node list, or NULL */
307
/**********************************************************************//**
308
Checks if graph, trx, or session is in a state where the query thread should
310
@return TRUE if should be stopped; NOTE that if the peek is made
311
without reserving the kernel mutex, then another peek with the mutex
312
reserved is necessary before deciding the actual stopping */
317
que_thr_t* thr); /*!< in: query thread */
318
/***********************************************************************//**
319
Returns TRUE if the query graph is for a SELECT statement.
320
@return TRUE if a select */
325
que_t* graph); /*!< in: graph */
326
/**********************************************************************//**
327
Prints info of an SQL query graph node. */
332
que_node_t* node); /*!< in: query graph node */
333
/*********************************************************************//**
334
Evaluate the given SQL
335
@return error code or DB_SUCCESS */
340
pars_info_t* info, /*!< in: info struct, or NULL */
341
const char* sql, /*!< in: SQL string */
342
ibool reserve_dict_mutex,
343
/*!< in: if TRUE, acquire/release
344
dict_sys->mutex around call to pars_sql. */
345
trx_t* trx); /*!< in: trx */
347
/* Query graph query thread node: the fields are protected by the kernel
348
mutex with the exceptions named below */
350
struct que_thr_struct{
351
que_common_t common; /*!< type: QUE_NODE_THR */
352
ulint magic_n; /*!< magic number to catch memory
354
que_node_t* child; /*!< graph child node */
355
que_t* graph; /*!< graph where this node belongs */
356
ibool is_active; /*!< TRUE if the thread has been set
358
que_thr_move_to_run_state, but not
360
que_thr_dec_reference_count */
361
ulint state; /*!< state of the query thread */
362
UT_LIST_NODE_T(que_thr_t)
363
thrs; /*!< list of thread nodes of the fork
365
UT_LIST_NODE_T(que_thr_t)
366
trx_thrs; /*!< lists of threads in wait list of
368
UT_LIST_NODE_T(que_thr_t)
369
queue; /*!< list of runnable thread nodes in
370
the server task queue */
371
/*------------------------------*/
372
/* The following fields are private to the OS thread executing the
373
query thread, and are not protected by the kernel mutex: */
375
que_node_t* run_node; /*!< pointer to the node where the
376
subgraph down from this node is
377
currently executed */
378
que_node_t* prev_node; /*!< pointer to the node from which
380
ulint resource; /*!< resource usage of the query thread
382
ulint lock_state; /*!< lock state of thread (table or
384
ulint fk_cascade_depth; /*!< maximum cascading call depth
385
supported for foreign key constraint
386
related delete/updates */
389
#define QUE_THR_MAGIC_N 8476583
390
#define QUE_THR_MAGIC_FREED 123461526
392
/* Query graph fork node: its fields are protected by the kernel mutex */
393
struct que_fork_struct{
394
que_common_t common; /*!< type: QUE_NODE_FORK */
395
que_t* graph; /*!< query graph of this node */
396
ulint fork_type; /*!< fork type */
397
ulint n_active_thrs; /*!< if this is the root of a graph, the
398
number query threads that have been
399
started in que_thr_move_to_run_state
400
but for which que_thr_dec_refer_count
401
has not yet been called */
402
trx_t* trx; /*!< transaction: this is set only in
404
ulint state; /*!< state of the fork node */
405
que_thr_t* caller; /*!< pointer to a possible calling query
407
UT_LIST_BASE_NODE_T(que_thr_t)
408
thrs; /*!< list of query threads */
409
/*------------------------------*/
410
/* The fields in this section are defined only in the root node */
411
sym_tab_t* sym_tab; /*!< symbol table of the query,
412
generated by the parser, or NULL
413
if the graph was created 'by hand' */
414
pars_info_t* info; /*!< info struct, or NULL */
415
/* The following cur_... fields are relevant only in a select graph */
417
ulint cur_end; /*!< QUE_CUR_NOT_DEFINED, QUE_CUR_START,
419
ulint cur_pos; /*!< if there are n rows in the result
420
set, values 0 and n + 1 mean before
421
first row, or after last row, depending
422
on cur_end; values 1...n mean a row
424
ibool cur_on_row; /*!< TRUE if cursor is on a row, i.e.,
425
it is not before the first row or
426
after the last row */
427
sel_node_t* last_sel_node; /*!< last executed select node, or NULL
429
UT_LIST_NODE_T(que_fork_t)
430
graphs; /*!< list of query graphs of a session
431
or a stored procedure */
432
/*------------------------------*/
433
mem_heap_t* heap; /*!< memory heap where the fork was
438
/* Query fork (or graph) types */
439
#define QUE_FORK_SELECT_NON_SCROLL 1 /* forward-only cursor */
440
#define QUE_FORK_SELECT_SCROLL 2 /* scrollable cursor */
441
#define QUE_FORK_INSERT 3
442
#define QUE_FORK_UPDATE 4
443
#define QUE_FORK_ROLLBACK 5
444
/* This is really the undo graph used in rollback,
445
no signal-sending roll_node in this graph */
446
#define QUE_FORK_PURGE 6
447
#define QUE_FORK_EXECUTE 7
448
#define QUE_FORK_PROCEDURE 8
449
#define QUE_FORK_PROCEDURE_CALL 9
450
#define QUE_FORK_MYSQL_INTERFACE 10
451
#define QUE_FORK_RECOVERY 11
453
/* Query fork (or graph) states */
454
#define QUE_FORK_ACTIVE 1
455
#define QUE_FORK_COMMAND_WAIT 2
456
#define QUE_FORK_INVALID 3
457
#define QUE_FORK_BEING_FREED 4
459
/* Flag which is ORed to control structure statement node types */
460
#define QUE_NODE_CONTROL_STAT 1024
462
/* Query graph node types */
463
#define QUE_NODE_LOCK 1
464
#define QUE_NODE_INSERT 2
465
#define QUE_NODE_UPDATE 4
466
#define QUE_NODE_CURSOR 5
467
#define QUE_NODE_SELECT 6
468
#define QUE_NODE_AGGREGATE 7
469
#define QUE_NODE_FORK 8
470
#define QUE_NODE_THR 9
471
#define QUE_NODE_UNDO 10
472
#define QUE_NODE_COMMIT 11
473
#define QUE_NODE_ROLLBACK 12
474
#define QUE_NODE_PURGE 13
475
#define QUE_NODE_CREATE_TABLE 14
476
#define QUE_NODE_CREATE_INDEX 15
477
#define QUE_NODE_SYMBOL 16
478
#define QUE_NODE_RES_WORD 17
479
#define QUE_NODE_FUNC 18
480
#define QUE_NODE_ORDER 19
481
#define QUE_NODE_PROC (20 + QUE_NODE_CONTROL_STAT)
482
#define QUE_NODE_IF (21 + QUE_NODE_CONTROL_STAT)
483
#define QUE_NODE_WHILE (22 + QUE_NODE_CONTROL_STAT)
484
#define QUE_NODE_ASSIGNMENT 23
485
#define QUE_NODE_FETCH 24
486
#define QUE_NODE_OPEN 25
487
#define QUE_NODE_COL_ASSIGNMENT 26
488
#define QUE_NODE_FOR (27 + QUE_NODE_CONTROL_STAT)
489
#define QUE_NODE_RETURN 28
490
#define QUE_NODE_ROW_PRINTF 29
491
#define QUE_NODE_ELSIF 30
492
#define QUE_NODE_CALL 31
493
#define QUE_NODE_EXIT 32
495
/* Query thread states */
496
#define QUE_THR_RUNNING 1
497
#define QUE_THR_PROCEDURE_WAIT 2
498
#define QUE_THR_COMPLETED 3 /* in selects this means that the
499
thread is at the end of its result set
500
(or start, in case of a scroll cursor);
501
in other statements, this means the
502
thread has done its task */
503
#define QUE_THR_COMMAND_WAIT 4
504
#define QUE_THR_LOCK_WAIT 5
505
#define QUE_THR_SIG_REPLY_WAIT 6
506
#define QUE_THR_SUSPENDED 7
507
#define QUE_THR_ERROR 8
509
/* Query thread lock states */
510
#define QUE_THR_LOCK_NOLOCK 0
511
#define QUE_THR_LOCK_ROW 1
512
#define QUE_THR_LOCK_TABLE 2
514
/* From where the cursor position is counted */
515
#define QUE_CUR_NOT_DEFINED 1
516
#define QUE_CUR_START 2
517
#define QUE_CUR_END 3
521
#include "que0que.ic"