~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/******************************************************
2
Query graph
3
4
(c) 1996 Innobase Oy
5
6
Created 5/27/1996 Heikki Tuuri
7
*******************************************************/
8
9
#ifndef que0que_h
10
#define que0que_h
11
12
#include "univ.i"
13
#include "data0data.h"
14
#include "dict0types.h"
15
#include "trx0trx.h"
16
#include "srv0srv.h"
17
#include "usr0types.h"
18
#include "que0types.h"
19
#include "row0types.h"
20
#include "pars0types.h"
21
22
/* If the following flag is set TRUE, the module will print trace info
23
of SQL execution in the UNIV_SQL_DEBUG version */
24
extern ibool	que_trace_on;
25
26
/***************************************************************************
27
Adds a query graph to the session's list of graphs. */
28
29
void
30
que_graph_publish(
31
/*==============*/
32
	que_t*	graph,	/* in: graph */
33
	sess_t*	sess);	/* in: session */
34
/***************************************************************************
35
Creates a query graph fork node. */
36
37
que_fork_t*
38
que_fork_create(
39
/*============*/
40
					/* out, own: fork node */
41
	que_t*		graph,		/* in: graph, if NULL then this
42
					fork node is assumed to be the
43
					graph root */
44
	que_node_t*	parent,		/* in: parent node */
45
	ulint		fork_type,	/* in: fork type */
46
	mem_heap_t*	heap);		/* in: memory heap where created */
47
/***************************************************************************
48
Gets the first thr in a fork. */
49
UNIV_INLINE
50
que_thr_t*
51
que_fork_get_first_thr(
52
/*===================*/
53
	que_fork_t*	fork);	/* in: query fork */
54
/***************************************************************************
55
Gets the child node of the first thr in a fork. */
56
UNIV_INLINE
57
que_node_t*
58
que_fork_get_child(
59
/*===============*/
60
	que_fork_t*	fork);	/* in: query fork */
61
/***************************************************************************
62
Sets the parent of a graph node. */
63
UNIV_INLINE
64
void
65
que_node_set_parent(
66
/*================*/
67
	que_node_t*	node,	/* in: graph node */
68
	que_node_t*	parent);/* in: parent */
69
/***************************************************************************
70
Creates a query graph thread node. */
71
72
que_thr_t*
73
que_thr_create(
74
/*===========*/
75
				/* out, own: query thread node */
76
	que_fork_t*	parent,	/* in: parent node, i.e., a fork node */
77
	mem_heap_t*	heap);	/* in: memory heap where created */
78
/**************************************************************************
79
Checks if the query graph is in a state where it should be freed, and
80
frees it in that case. If the session is in a state where it should be
81
closed, also this is done. */
82
83
ibool
84
que_graph_try_free(
85
/*===============*/
86
			/* out: TRUE if freed */
87
	que_t*	graph);	/* in: query graph */
88
/**************************************************************************
89
Frees a query graph, but not the heap where it was created. Does not free
90
explicit cursor declarations, they are freed in que_graph_free. */
91
92
void
93
que_graph_free_recursive(
94
/*=====================*/
95
	que_node_t*	node);	/* in: query graph node */
96
/**************************************************************************
97
Frees a query graph. */
98
99
void
100
que_graph_free(
101
/*===========*/
102
	que_t*	graph);	/* in: query graph; we assume that the memory
103
			heap where this graph was created is private
104
			to this graph: if not, then use
105
			que_graph_free_recursive and free the heap
106
			afterwards! */
107
/**************************************************************************
108
Stops a query thread if graph or trx is in a state requiring it. The
109
conditions are tested in the order (1) graph, (2) trx. The kernel mutex has
110
to be reserved. */
111
112
ibool
113
que_thr_stop(
114
/*=========*/
115
				/* out: TRUE if stopped */
116
	que_thr_t*	thr);	/* in: query thread */
117
/**************************************************************************
118
Moves a thread from another state to the QUE_THR_RUNNING state. Increments
119
the n_active_thrs counters of the query graph and transaction. */
120
121
void
122
que_thr_move_to_run_state_for_mysql(
123
/*================================*/
124
	que_thr_t*	thr,	/* in: an query thread */
125
	trx_t*		trx);	/* in: transaction */
126
/**************************************************************************
127
A patch for MySQL used to 'stop' a dummy query thread used in MySQL
128
select, when there is no error or lock wait. */
129
130
void
131
que_thr_stop_for_mysql_no_error(
132
/*============================*/
133
	que_thr_t*	thr,	/* in: query thread */
134
	trx_t*		trx);	/* in: transaction */
135
/**************************************************************************
136
A patch for MySQL used to 'stop' a dummy query thread used in MySQL. The
137
query thread is stopped and made inactive, except in the case where
138
it was put to the lock wait state in lock0lock.c, but the lock has already
139
been granted or the transaction chosen as a victim in deadlock resolution. */
140
141
void
142
que_thr_stop_for_mysql(
143
/*===================*/
144
	que_thr_t*	thr);	/* in: query thread */
145
/**************************************************************************
146
Run a query thread. Handles lock waits. */
147
148
void
149
que_run_threads(
150
/*============*/
151
	que_thr_t*	thr);	/* in: query thread */
152
/**************************************************************************
153
After signal handling is finished, returns control to a query graph error
154
handling routine. (Currently, just returns the control to the root of the
155
graph so that the graph can communicate an error message to the client.) */
156
157
void
158
que_fork_error_handle(
159
/*==================*/
160
	trx_t*	trx,	/* in: trx */
161
	que_t*	fork);	/* in: query graph which was run before signal
162
			handling started, NULL not allowed */
163
/**************************************************************************
164
Moves a suspended query thread to the QUE_THR_RUNNING state and releases
165
a single worker thread to execute it. This function should be used to end
166
the wait state of a query thread waiting for a lock or a stored procedure
167
completion. */
168
169
void
170
que_thr_end_wait(
171
/*=============*/
172
	que_thr_t*	thr,		/* in: query thread in the
173
					QUE_THR_LOCK_WAIT,
174
					or QUE_THR_PROCEDURE_WAIT, or
175
					QUE_THR_SIG_REPLY_WAIT state */
176
	que_thr_t**	next_thr);	/* in/out: next query thread to run;
177
					if the value which is passed in is
178
					a pointer to a NULL pointer, then the
179
					calling function can start running
180
					a new query thread */
181
/**************************************************************************
182
Same as que_thr_end_wait, but no parameter next_thr available. */
183
184
void
185
que_thr_end_wait_no_next_thr(
186
/*=========================*/
187
	que_thr_t*	thr);		/* in: query thread in the
188
					QUE_THR_LOCK_WAIT,
189
					or QUE_THR_PROCEDURE_WAIT, or
190
					QUE_THR_SIG_REPLY_WAIT state */
191
/**************************************************************************
192
Starts execution of a command in a query fork. Picks a query thread which
193
is not in the QUE_THR_RUNNING state and moves it to that state. If none
194
can be chosen, a situation which may arise in parallelized fetches, NULL
195
is returned. */
196
197
que_thr_t*
198
que_fork_start_command(
199
/*===================*/
200
				/* out: a query thread of the graph moved to
201
				QUE_THR_RUNNING state, or NULL; the query
202
				thread should be executed by que_run_threads
203
				by the caller */
204
	que_fork_t*	fork);	/* in: a query fork */
205
/***************************************************************************
206
Gets the trx of a query thread. */
207
UNIV_INLINE
208
trx_t*
209
thr_get_trx(
210
/*========*/
211
	que_thr_t*	thr);	/* in: query thread */
212
/***************************************************************************
213
Gets the type of a graph node. */
214
UNIV_INLINE
215
ulint
216
que_node_get_type(
217
/*==============*/
218
	que_node_t*	node);	/* in: graph node */
219
/***************************************************************************
220
Gets pointer to the value data type field of a graph node. */
221
UNIV_INLINE
222
dtype_t*
223
que_node_get_data_type(
224
/*===================*/
225
	que_node_t*	node);	/* in: graph node */
226
/***************************************************************************
227
Gets pointer to the value dfield of a graph node. */
228
UNIV_INLINE
229
dfield_t*
230
que_node_get_val(
231
/*=============*/
232
	que_node_t*	node);	/* in: graph node */
233
/***************************************************************************
234
Gets the value buffer size of a graph node. */
235
UNIV_INLINE
236
ulint
237
que_node_get_val_buf_size(
238
/*======================*/
239
				/* out: val buffer size, not defined if
240
				val.data == NULL in node */
241
	que_node_t*	node);	/* in: graph node */
242
/***************************************************************************
243
Sets the value buffer size of a graph node. */
244
UNIV_INLINE
245
void
246
que_node_set_val_buf_size(
247
/*======================*/
248
	que_node_t*	node,	/* in: graph node */
249
	ulint		size);	/* in: size */
250
/*************************************************************************
251
Gets the next list node in a list of query graph nodes. */
252
UNIV_INLINE
253
que_node_t*
254
que_node_get_next(
255
/*==============*/
256
	que_node_t*	node);	/* in: node in a list */
257
/*************************************************************************
258
Gets the parent node of a query graph node. */
259
UNIV_INLINE
260
que_node_t*
261
que_node_get_parent(
262
/*================*/
263
				/* out: parent node or NULL */
264
	que_node_t*	node);	/* in: node */
265
/********************************************************************
266
Get the first containing loop node (e.g. while_node_t or for_node_t) for the
267
given node, or NULL if the node is not within a loop. */
268
269
que_node_t*
270
que_node_get_containing_loop_node(
271
/*==============================*/
272
				/* out: containing loop node, or NULL. */
273
	que_node_t*	node);	/* in: node */
274
/*************************************************************************
275
Catenates a query graph node to a list of them, possible empty list. */
276
UNIV_INLINE
277
que_node_t*
278
que_node_list_add_last(
279
/*===================*/
280
					/* out: one-way list of nodes */
281
	que_node_t*	node_list,	/* in: node list, or NULL */
282
	que_node_t*	node);		/* in: node */
283
/*************************************************************************
284
Gets a query graph node list length. */
285
UNIV_INLINE
286
ulint
287
que_node_list_get_len(
288
/*==================*/
289
					/* out: length, for NULL list 0 */
290
	que_node_t*	node_list);	/* in: node list, or NULL */
291
/**************************************************************************
292
Checks if graph, trx, or session is in a state where the query thread should
293
be stopped. */
294
UNIV_INLINE
295
ibool
296
que_thr_peek_stop(
297
/*==============*/
298
				/* out: TRUE if should be stopped; NOTE that
299
				if the peek is made without reserving the
300
				kernel mutex, then another peek with the
301
				mutex reserved is necessary before deciding
302
				the actual stopping */
303
	que_thr_t*	thr);	/* in: query thread */
304
/***************************************************************************
305
Returns TRUE if the query graph is for a SELECT statement. */
306
UNIV_INLINE
307
ibool
308
que_graph_is_select(
309
/*================*/
310
					/* out: TRUE if a select */
311
	que_t*		graph);		/* in: graph */
312
/**************************************************************************
313
Prints info of an SQL query graph node. */
314
315
void
316
que_node_print_info(
317
/*================*/
318
	que_node_t*	node);	/* in: query graph node */
319
/*************************************************************************
320
Evaluate the given SQL */
321
322
ulint
323
que_eval_sql(
324
/*=========*/
325
				/* out: error code or DB_SUCCESS */
326
	pars_info_t*	info,	/* in: info struct, or NULL */
327
	const char*	sql,	/* in: SQL string */
328
	ibool		reserve_dict_mutex,
329
				/* in: if TRUE, acquire/release
330
				dict_sys->mutex around call to pars_sql. */
331
	trx_t*		trx);	/* in: trx */
332
333
/* Query graph query thread node: the fields are protected by the kernel
334
mutex with the exceptions named below */
335
336
struct que_thr_struct{
337
	que_common_t	common;		/* type: QUE_NODE_THR */
338
	ulint		magic_n;	/* magic number to catch memory
339
					corruption */
340
	que_node_t*	child;		/* graph child node */
341
	que_t*		graph;		/* graph where this node belongs */
342
	ibool		is_active;	/* TRUE if the thread has been set
343
					to the run state in
344
					que_thr_move_to_run_state, but not
345
					deactivated in
346
					que_thr_dec_reference_count */
347
	ulint		state;		/* state of the query thread */
348
	UT_LIST_NODE_T(que_thr_t)
349
			thrs;		/* list of thread nodes of the fork
350
					node */
351
	UT_LIST_NODE_T(que_thr_t)
352
			trx_thrs;	/* lists of threads in wait list of
353
					the trx */
354
	UT_LIST_NODE_T(que_thr_t)
355
			queue;		/* list of runnable thread nodes in
356
					the server task queue */
357
	/*------------------------------*/
358
	/* The following fields are private to the OS thread executing the
359
	query thread, and are not protected by the kernel mutex: */
360
361
	que_node_t*	run_node;	/* pointer to the node where the
362
					subgraph down from this node is
363
					currently executed */
364
	que_node_t*	prev_node;	/* pointer to the node from which
365
					the control came */
366
	ulint		resource;	/* resource usage of the query thread
367
					thus far */
368
	ulint		lock_state;	/* lock state of thread (table or
369
					row) */
370
};
371
372
#define QUE_THR_MAGIC_N		8476583
373
#define QUE_THR_MAGIC_FREED	123461526
374
375
/* Query graph fork node: its fields are protected by the kernel mutex */
376
struct que_fork_struct{
377
	que_common_t	common;		/* type: QUE_NODE_FORK */
378
	que_t*		graph;		/* query graph of this node */
379
	ulint		fork_type;	/* fork type */
380
	ulint		n_active_thrs;	/* if this is the root of a graph, the
381
					number query threads that have been
382
					started in que_thr_move_to_run_state
383
					but for which que_thr_dec_refer_count
384
					has not yet been called */
385
	trx_t*		trx;		/* transaction: this is set only in
386
					the root node */
387
	ulint		state;		/* state of the fork node */
388
	que_thr_t*	caller;		/* pointer to a possible calling query
389
					thread */
390
	UT_LIST_BASE_NODE_T(que_thr_t)
391
			thrs;		/* list of query threads */
392
	/*------------------------------*/
393
	/* The fields in this section are defined only in the root node */
394
	sym_tab_t*	sym_tab;	/* symbol table of the query,
395
					generated by the parser, or NULL
396
					if the graph was created 'by hand' */
397
	pars_info_t*	info;		/* in: info struct, or NULL */
398
	/* The following cur_... fields are relevant only in a select graph */
399
400
	ulint		cur_end;	/* QUE_CUR_NOT_DEFINED, QUE_CUR_START,
401
					QUE_CUR_END */
402
	ulint		cur_pos;	/* if there are n rows in the result
403
					set, values 0 and n + 1 mean before
404
					first row, or after last row, depending
405
					on cur_end; values 1...n mean a row
406
					index */
407
	ibool		cur_on_row;	/* TRUE if cursor is on a row, i.e.,
408
					it is not before the first row or
409
					after the last row */
410
	dulint		n_inserts;	/* number of rows inserted */
411
	dulint		n_updates;	/* number of rows updated */
412
	dulint		n_deletes;	/* number of rows deleted */
413
	sel_node_t*	last_sel_node;	/* last executed select node, or NULL
414
					if none */
415
	UT_LIST_NODE_T(que_fork_t)
416
			graphs;		/* list of query graphs of a session
417
					or a stored procedure */
418
	/*------------------------------*/
419
	mem_heap_t*	heap;		/* memory heap where the fork was
420
					created */
421
422
};
423
424
/* Query fork (or graph) types */
425
#define QUE_FORK_SELECT_NON_SCROLL	1	/* forward-only cursor */
426
#define QUE_FORK_SELECT_SCROLL		2	/* scrollable cursor */
427
#define QUE_FORK_INSERT			3
428
#define QUE_FORK_UPDATE			4
429
#define QUE_FORK_ROLLBACK		5
430
			/* This is really the undo graph used in rollback,
431
			no signal-sending roll_node in this graph */
432
#define QUE_FORK_PURGE			6
433
#define	QUE_FORK_EXECUTE		7
434
#define QUE_FORK_PROCEDURE		8
435
#define QUE_FORK_PROCEDURE_CALL		9
436
#define QUE_FORK_MYSQL_INTERFACE	10
437
#define	QUE_FORK_RECOVERY		11
438
439
/* Query fork (or graph) states */
440
#define QUE_FORK_ACTIVE		1
441
#define QUE_FORK_COMMAND_WAIT	2
442
#define QUE_FORK_INVALID	3
443
#define QUE_FORK_BEING_FREED	4
444
445
/* Flag which is ORed to control structure statement node types */
446
#define QUE_NODE_CONTROL_STAT	1024
447
448
/* Query graph node types */
449
#define	QUE_NODE_LOCK		1
450
#define	QUE_NODE_INSERT		2
451
#define QUE_NODE_UPDATE		4
452
#define	QUE_NODE_CURSOR		5
453
#define	QUE_NODE_SELECT		6
454
#define	QUE_NODE_AGGREGATE	7
455
#define QUE_NODE_FORK		8
456
#define QUE_NODE_THR		9
457
#define QUE_NODE_UNDO		10
458
#define QUE_NODE_COMMIT		11
459
#define QUE_NODE_ROLLBACK	12
460
#define QUE_NODE_PURGE		13
461
#define QUE_NODE_CREATE_TABLE	14
462
#define QUE_NODE_CREATE_INDEX	15
463
#define QUE_NODE_SYMBOL		16
464
#define QUE_NODE_RES_WORD	17
465
#define QUE_NODE_FUNC		18
466
#define QUE_NODE_ORDER		19
467
#define QUE_NODE_PROC		(20 + QUE_NODE_CONTROL_STAT)
468
#define QUE_NODE_IF		(21 + QUE_NODE_CONTROL_STAT)
469
#define QUE_NODE_WHILE		(22 + QUE_NODE_CONTROL_STAT)
470
#define QUE_NODE_ASSIGNMENT	23
471
#define QUE_NODE_FETCH		24
472
#define QUE_NODE_OPEN		25
473
#define QUE_NODE_COL_ASSIGNMENT	26
474
#define QUE_NODE_FOR		(27 + QUE_NODE_CONTROL_STAT)
475
#define QUE_NODE_RETURN		28
476
#define QUE_NODE_ROW_PRINTF	29
477
#define QUE_NODE_ELSIF		30
478
#define QUE_NODE_CALL		31
479
#define QUE_NODE_EXIT		32
480
481
/* Query thread states */
482
#define QUE_THR_RUNNING		1
483
#define QUE_THR_PROCEDURE_WAIT	2
484
#define	QUE_THR_COMPLETED	3	/* in selects this means that the
485
					thread is at the end of its result set
486
					(or start, in case of a scroll cursor);
487
					in other statements, this means the
488
					thread has done its task */
489
#define QUE_THR_COMMAND_WAIT	4
490
#define QUE_THR_LOCK_WAIT	5
491
#define QUE_THR_SIG_REPLY_WAIT	6
492
#define QUE_THR_SUSPENDED	7
493
#define QUE_THR_ERROR		8
494
495
/* Query thread lock states */
496
#define QUE_THR_LOCK_NOLOCK	0
497
#define QUE_THR_LOCK_ROW	1
498
#define QUE_THR_LOCK_TABLE	2
499
500
/* From where the cursor position is counted */
501
#define QUE_CUR_NOT_DEFINED	1
502
#define QUE_CUR_START		2
503
#define	QUE_CUR_END		3
504
505
506
#ifndef UNIV_NONINL
507
#include "que0que.ic"
508
#endif
509
510
#endif