MySQL 8.4.2
Source Code Documentation
|
sp_head represents one instance of a stored program. More...
#include <sp_head.h>
Public Types | |
enum | { HAS_RETURN = 1 , MULTI_RESULTS = 8 , CONTAINS_DYNAMIC_SQL = 16 , IS_INVOKED = 32 , HAS_SET_AUTOCOMMIT_STMT , HAS_COMMIT_OR_ROLLBACK = 128 , LOG_SLOW_STATEMENTS = 256 , LOG_GENERAL_LOG = 512 , HAS_SQLCOM_RESET = 1024 , HAS_SQLCOM_FLUSH = 2048 , MODIFIES_DATA = 4096 , HAS_TEMP_TABLE_DDL = 8192 } |
Possible values of m_flags. More... | |
Public Member Functions | |
bool | is_invoked () const |
Is this routine being executed? More... | |
bool | is_sql () const |
int64 | sp_cache_version () const |
Get the value of the SP cache version, as remembered when the routine was inserted into the cache. More... | |
void | set_sp_cache_version (int64 sp_cache_version) |
Set the value of the SP cache version. More... | |
Stored_program_creation_ctx * | get_creation_ctx () |
void | set_creation_ctx (Stored_program_creation_ctx *creation_ctx) |
void | set_body_start (THD *thd, const char *begin_ptr) |
Set the body-definition start position. More... | |
void | set_body_end (THD *thd) |
Set the statement-definition (body-definition) end position. More... | |
bool | setup_trigger_fields (THD *thd, Table_trigger_field_support *tfs, GRANT_INFO *subject_table_grant, bool need_fix_fields) |
void | mark_used_trigger_fields (TABLE *subject_table) |
bool | has_updated_trigger_fields (const MY_BITMAP *used_fields) const |
Check whether any table's fields are used in trigger. More... | |
bool | execute_trigger (THD *thd, const LEX_CSTRING &db_name, const LEX_CSTRING &table_name, GRANT_INFO *grant_info) |
Execute trigger stored program. More... | |
bool | execute_function (THD *thd, Item **args, uint argcount, Field *return_fld) |
Execute a function. More... | |
bool | execute_procedure (THD *thd, mem_root_deque< Item * > *args) |
Execute a procedure. More... | |
bool | add_instr (THD *thd, sp_instr *instr) |
Add instruction to SP. More... | |
bool | modifies_data () const |
Returns true if any substatement in the routine directly (not through another routine) modifies data/changes table. More... | |
bool | has_temp_table_ddl () const |
uint | instructions () |
sp_instr * | last_instruction () |
bool | reset_lex (THD *thd) |
Reset LEX-object during parsing, before we parse a sub statement. More... | |
bool | restore_lex (THD *thd) |
Restore LEX-object during parsing, after we have parsed a sub statement. More... | |
char * | name (uint *lenp=nullptr) const |
Field * | create_result_field (THD *thd, size_t field_max_length, const char *field_name, TABLE *table) const |
Create Field-object corresponding to the RETURN field of a stored function. More... | |
void | returns_type (THD *thd, String *result) const |
void | set_info (longlong created, longlong modified, st_sp_chistics *chistics, sql_mode_t sql_mode) |
void | set_definer (const char *definer, size_t definerlen) |
void | set_definer (const LEX_CSTRING &user_name, const LEX_CSTRING &host_name) |
void | optimize () |
Do some minimal optimization of the code: More... | |
void | add_mark_lead (uint ip, List< sp_instr > *leads) |
Helper used during flow analysis during code optimization. More... | |
sp_instr * | get_instr (uint i) |
Get SP-instruction at given index. More... | |
void | add_used_tables_to_table_list (THD *thd, Table_ref ***query_tables_last_ptr, enum_sql_command sql_command, Table_ref *belong_to_view) |
Add tables used by routine to the table list. More... | |
bool | is_not_allowed_in_function (const char *where) |
Check if this stored routine contains statements disallowed in a stored function or trigger, and set an appropriate error message if this is the case. More... | |
bool | show_routine_code (THD *thd) |
Return the routine instructions as a result set. More... | |
void | propagate_attributes (Query_tables_list *prelocking_ctx) |
sp_pcontext * | get_root_parsing_context () const |
MEM_ROOT * | get_persistent_mem_root () const |
bool | check_show_access (THD *thd, bool *full_access) |
Check if a user has access right to a SP. More... | |
bool | set_security_ctx (THD *thd, Security_context **save_ctx) |
Change routine security context, and check if there is an EXECUTE privilege in new context. More... | |
external_program_handle | get_external_program_handle () |
Get the external program handle object. More... | |
bool | set_external_program_handle (external_program_handle sp) |
Set the external program handle object. More... | |
bool | init_external_routine (my_service< const mysql_service_external_program_execution_t > &service) |
Initialize and parse an external routine. More... | |
Static Public Member Functions | |
static void | destroy (sp_head *sp) |
Public Attributes | |
enum_sp_type | m_type |
Stored program type. More... | |
uint | m_flags |
Stored program flags. More... | |
PSI_sp_share * | m_sp_share |
Instrumentation interface for SP. More... | |
Create_field | m_return_field_def |
Definition of the RETURN-field (from the RETURNS-clause). More... | |
sp_parser_data | m_parser_data |
Attributes used during the parsing stage only. More... | |
st_sp_chistics * | m_chistics |
Stored program characteristics. More... | |
LEX_CSTRING | code |
Code if language is not SQL. More... | |
sql_mode_t | m_sql_mode |
The value of sql_mode system variable at the CREATE-time. More... | |
LEX_STRING | m_qname |
Fully qualified name (<db name>.<sp name>). More... | |
bool | m_explicit_name |
Prepend the db name? */. More... | |
LEX_STRING | m_db |
LEX_STRING | m_name |
LEX_STRING | m_params |
LEX_CSTRING | m_body |
LEX_CSTRING | m_body_utf8 |
LEX_STRING | m_definer_user |
LEX_STRING | m_definer_host |
longlong | m_created |
longlong | m_modified |
ulong | m_recursion_level |
Recursion level of the current SP instance. More... | |
sp_head * | m_next_cached_sp |
A list of different recursion level instances for the same procedure. More... | |
sp_head * | m_first_instance |
Pointer to the first element of the above list. More... | |
sp_head * | m_first_free_instance |
Pointer to the first free (non-INVOKED) routine in the list of cached instances for this SP. More... | |
sp_head * | m_last_cached_sp |
Pointer to the last element in the list of instances of the SP. More... | |
malloc_unordered_map< std::string, Sroutine_hash_entry * > | m_sroutines |
Set containing names of stored routines used by this routine. More... | |
Security_context | m_security_ctx |
SQL_I_List< SQL_I_List< Item_trigger_field > > | m_list_of_trig_fields_item_lists |
List of item (Item_trigger_field objects)'s lists representing fields in old/new version of row in trigger. More... | |
SQL_I_List< Item_trigger_field > | m_cur_instr_trig_field_items |
List of all the Item_trigger_field items created while parsing sp instruction. More... | |
st_trg_chistics | m_trg_chistics |
Trigger characteristics. More... | |
class Table_trigger_dispatcher * | m_trg_list |
The Table_trigger_dispatcher instance, where this trigger belongs to. More... | |
Private Member Functions | |
sp_head (MEM_ROOT &&mem_root, enum_sp_type type) | |
Use sp_start_parsing() to create instances of sp_head. More... | |
~sp_head () | |
Use destroy() to destoy instances of sp_head. More... | |
void | init_sp_name (THD *thd, sp_name *spname) |
Copy sp name from parser. More... | |
bool | execute (THD *thd, bool merge_da_on_success) |
Execute the routine. More... | |
bool | execute_external_routine (THD *thd) |
Execute external routine. More... | |
bool | execute_external_routine_core (THD *thd) |
Core function for executing the external routine. More... | |
void | opt_mark () |
Perform a forward flow analysis in the generated code. More... | |
bool | merge_table_list (THD *thd, Table_ref *table, LEX *lex_for_tmp_check) |
Merge the list of tables used by some query into the multi-set of tables used by routine. More... | |
sp_head (const sp_head &) | |
void | operator= (sp_head &) |
Private Attributes | |
MEM_ROOT | main_mem_root |
SP-persistent memory root (for instructions and expressions). More... | |
sp_pcontext * | m_root_parsing_ctx |
Root parsing context (topmost BEGIN..END block) of this SP. More... | |
Mem_root_array< sp_instr * > | m_instructions |
The SP-instructions. More... | |
collation_unordered_map< std::string, SP_TABLE * > | m_sptabs |
Multi-set representing optimized list of tables to be locked by this routine. More... | |
std::vector< SP_TABLE * > | m_sptabs_sorted |
int64 | m_sp_cache_version |
Version of the stored routine cache at the moment when the routine was added to it. More... | |
Stored_program_creation_ctx * | m_creation_ctx |
Snapshot of several system variables at CREATE-time. More... | |
uint32 | unsafe_flags |
Flags of LEX::enum_binlog_stmt_unsafe. More... | |
external_program_handle | m_language_stored_program |
language component related state of this sp. More... | |
Friends | |
sp_head * | sp_start_parsing (THD *thd, enum_sp_type sp_type, sp_name *sp_name) |
Start parsing of a stored program. More... | |
sp_head represents one instance of a stored program.
It might be of any type (stored procedure, function, trigger, event).
anonymous enum |
Possible values of m_flags.
|
private |
Use sp_start_parsing() to create instances of sp_head.
|
private |
Use destroy() to destoy instances of sp_head.
|
private |
Add instruction to SP.
thd | Thread context. |
instr | Instruction. |
Helper used during flow analysis during code optimization.
See the implementation of opt_mark()
.
ip | the instruction to add to the leads list |
leads | the list of remaining paths to explore in the graph that represents the code, during flow analysis. |
void sp_head::add_used_tables_to_table_list | ( | THD * | thd, |
Table_ref *** | query_tables_last_ptr, | ||
enum_sql_command | sql_command, | ||
Table_ref * | belong_to_view | ||
) |
Add tables used by routine to the table list.
Converts multi-set of tables used by this routine to table list and adds this list to the end of table list specified by 'query_tables_last_ptr'.
Elements of list will be allocated in PS memroot, so this list will be persistent between PS executions.
[in] | thd | Thread context |
[in,out] | query_tables_last_ptr | Pointer to the next_global member of last element of the list where tables will be added (or to its root). |
[in] | sql_command | SQL-command for which we are adding elements to the table list. |
[in] | belong_to_view | Uppermost view which uses this routine, NULL if none. |
bool sp_head::check_show_access | ( | THD * | thd, |
bool * | full_access | ||
) |
Check if a user has access right to a SP.
thd | Thread context. | |
[out] | full_access | Set to 1 if the user is the owner of the stored program. |
Field * sp_head::create_result_field | ( | THD * | thd, |
size_t | field_max_length, | ||
const char * | field_name, | ||
TABLE * | table | ||
) | const |
Create Field-object corresponding to the RETURN field of a stored function.
This operation makes sense for stored functions only.
thd | thread context. |
field_max_length | the max length (in the sense of Item classes). |
field_name | the field name (item name). |
table | the field's table. |
|
static |
|
private |
Execute the routine.
The main instruction jump loop is there. Assume the parameters already set.
thd | Thread context. |
merge_da_on_success | Flag specifying if Warning Info should be propagated to the caller on Completion Condition or not. |
When inside a substatement (a stored function or trigger statement), clear the metadata observer in THD, if any. Remember the value of the observer here, to be able to restore it when leaving the substatement.
We reset the observer to suppress errors when a substatement uses temporary tables. If a temporary table does not exist at start of the main statement, it's not prelocked and thus is not validated with other prelocked tables.
Later on, when the temporary table is opened, metadata versions mismatch, expectedly.
The proper solution for the problem is to re-validate tables of substatements (Bug#12257, Bug#27011, Bug#32868, Bug#33000), but it's not implemented yet.
|
private |
Execute external routine.
thd | Thread context. |
|
private |
Core function for executing the external routine.
thd | Thread context. |
Execute a function.
thd | Thread context. |
args | Passed arguments (these are items from containing statement?) |
argcount | Number of passed arguments. We need to check if this is correct. |
return_fld | Save result here. |
bool sp_head::execute_procedure | ( | THD * | thd, |
mem_root_deque< Item * > * | args | ||
) |
Execute a procedure.
The function does the following steps:
thd | Thread context. |
args | List of values passed as arguments. |
bool sp_head::execute_trigger | ( | THD * | thd, |
const LEX_CSTRING & | db_name, | ||
const LEX_CSTRING & | table_name, | ||
GRANT_INFO * | grant_info | ||
) |
Execute trigger stored program.
thd | Thread context |
db_name | database name |
table_name | table name |
grant_info | GRANT_INFO structure to be filled with information about definer's privileges on subject table |
|
inline |
external_program_handle sp_head::get_external_program_handle | ( | ) |
Get the external program handle object.
|
inline |
Get SP-instruction at given index.
NOTE: it is important to have unsigned int here, sometimes we get (-1) passed here, so it gets converted to MAX_INT, and the result of the function call is NULL.
|
inline |
|
inline |
|
inline |
true | if HAS_TEMP_TABLE_DDL is set in m_flags. |
false | Otherwise. |
bool sp_head::has_updated_trigger_fields | ( | const MY_BITMAP * | used_fields | ) | const |
Check whether any table's fields are used in trigger.
[in] | used_fields | bitmap of fields to check |
true | Some table fields are used in trigger |
false | None of table fields are used in trigger |
bool sp_head::init_external_routine | ( | my_service< const mysql_service_external_program_execution_t > & | service | ) |
Initialize and parse an external routine.
If m_language_stored_program is already set, nothing will be done.
service | The_program_execution service that will be used to execute this function |
|
inline |
|
inline |
Is this routine being executed?
|
inline |
Check if this stored routine contains statements disallowed in a stored function or trigger, and set an appropriate error message if this is the case.
|
inline |
|
inline |
void sp_head::mark_used_trigger_fields | ( | TABLE * | subject_table | ) |
Merge the list of tables used by some query into the multi-set of tables used by routine.
thd | Thread context. |
table | Table list. |
lex_for_tmp_check | LEX of the query for which we are merging table list. |
|
inline |
Returns true if any substatement in the routine directly (not through another routine) modifies data/changes table.
|
inline |
|
private |
|
private |
Perform a forward flow analysis in the generated code.
Mark reachable instructions, for the optimizer.
void sp_head::optimize | ( | ) |
Do some minimal optimization of the code:
This is the main mark and move loop; it relies on the following methods in sp_instr and its subclasses:
|
inline |
bool sp_head::reset_lex | ( | THD * | thd | ) |
Reset LEX-object during parsing, before we parse a sub statement.
thd | Thread context. |
bool sp_head::restore_lex | ( | THD * | thd | ) |
Restore LEX-object during parsing, after we have parsed a sub statement.
thd | Thread context. |
void sp_head::set_body_end | ( | THD * | thd | ) |
Set the statement-definition (body-definition) end position.
void sp_head::set_body_start | ( | THD * | thd, |
const char * | begin_ptr | ||
) |
Set the body-definition start position.
|
inline |
void sp_head::set_definer | ( | const char * | definer, |
size_t | definerlen | ||
) |
void sp_head::set_definer | ( | const LEX_CSTRING & | user_name, |
const LEX_CSTRING & | host_name | ||
) |
bool sp_head::set_external_program_handle | ( | external_program_handle | sp | ) |
Set the external program handle object.
sp | The new external program handle object. Use nullptr to unset the current one. |
void sp_head::set_info | ( | longlong | created, |
longlong | modified, | ||
st_sp_chistics * | chistics, | ||
sql_mode_t | sql_mode | ||
) |
bool sp_head::set_security_ctx | ( | THD * | thd, |
Security_context ** | save_ctx | ||
) |
Change routine security context, and check if there is an EXECUTE privilege in new context.
If there is no EXECUTE privilege, change the context back and return an error.
thd | Thread context. | |
[out] | save_ctx | Where to save the old security context. |
|
inline |
Set the value of the SP cache version.
bool sp_head::setup_trigger_fields | ( | THD * | thd, |
Table_trigger_field_support * | tfs, | ||
GRANT_INFO * | subject_table_grant, | ||
bool | need_fix_fields | ||
) |
bool sp_head::show_routine_code | ( | THD * | thd | ) |
Return the routine instructions as a result set.
|
inline |
Get the value of the SP cache version, as remembered when the routine was inserted into the cache.
|
friend |
Start parsing of a stored program.
This function encapsulates all the steps necessary to initialize sp_head to start parsing SP.
Every successful call of sp_start_parsing() must finish with sp_finish_parsing().
thd | Thread context. |
sp_type | The stored program type |
sp_name | The stored program name |
LEX_CSTRING sp_head::code |
Code if language is not SQL.
LEX_CSTRING sp_head::m_body |
LEX_CSTRING sp_head::m_body_utf8 |
st_sp_chistics* sp_head::m_chistics |
Stored program characteristics.
longlong sp_head::m_created |
|
private |
Snapshot of several system variables at CREATE-time.
SQL_I_List<Item_trigger_field> sp_head::m_cur_instr_trig_field_items |
List of all the Item_trigger_field items created while parsing sp instruction.
After parsing, in add_instr method this list is moved to per instruction Item_trigger_field list "sp_lex_instr::m_trig_field_list".
LEX_STRING sp_head::m_db |
LEX_STRING sp_head::m_definer_host |
LEX_STRING sp_head::m_definer_user |
bool sp_head::m_explicit_name |
Prepend the db name? */.
sp_head* sp_head::m_first_free_instance |
Pointer to the first free (non-INVOKED) routine in the list of cached instances for this SP.
This pointer is set only for the first SP in the list of instances (see above m_first_cached_sp pointer). The pointer equal to 0 if we have no free instances. For non-first instance value of this pointer meaningless (point to itself);
sp_head* sp_head::m_first_instance |
Pointer to the first element of the above list.
uint sp_head::m_flags |
Stored program flags.
|
private |
The SP-instructions.
|
private |
language component related state of this sp.
sp_head* sp_head::m_last_cached_sp |
Pointer to the last element in the list of instances of the SP.
For non-first instance value of this pointer meaningless (point to itself);
SQL_I_List<SQL_I_List<Item_trigger_field> > sp_head::m_list_of_trig_fields_item_lists |
List of item (Item_trigger_field objects)'s lists representing fields in old/new version of row in trigger.
We use this list for checking whether all such fields are valid or not at trigger creation time and for binding these fields to TABLE object at table open (although for latter pointer to table being opened is probably enough).
longlong sp_head::m_modified |
LEX_STRING sp_head::m_name |
sp_head* sp_head::m_next_cached_sp |
A list of different recursion level instances for the same procedure.
For every recursion level we have an sp_head instance. This instances connected in the list. The list ordered by increasing recursion level (m_recursion_level).
LEX_STRING sp_head::m_params |
sp_parser_data sp_head::m_parser_data |
Attributes used during the parsing stage only.
LEX_STRING sp_head::m_qname |
Fully qualified name (<db name>.<sp name>).
ulong sp_head::m_recursion_level |
Recursion level of the current SP instance.
The levels are numbered from 0.
Create_field sp_head::m_return_field_def |
Definition of the RETURN-field (from the RETURNS-clause).
It's used (and valid) for stored functions only.
|
private |
Root parsing context (topmost BEGIN..END block) of this SP.
Security_context sp_head::m_security_ctx |
|
private |
Version of the stored routine cache at the moment when the routine was added to it.
Is used only for functions and procedures, not used for triggers or events. When sp_head is created, its version is 0. When it's added to the cache, the version is assigned the global value 'Cversion'. If later on Cversion is incremented, we know that the routine is obsolete and should not be used – sp_cache_flush_obsolete() will purge it.
PSI_sp_share* sp_head::m_sp_share |
Instrumentation interface for SP.
|
private |
Multi-set representing optimized list of tables to be locked by this routine.
Does not include tables which are used by invoked routines.
|
private |
sql_mode_t sp_head::m_sql_mode |
The value of sql_mode system variable at the CREATE-time.
It should be stored along with the character sets in the Stored_program_creation_ctx.
malloc_unordered_map<std::string, Sroutine_hash_entry *> sp_head::m_sroutines |
Set containing names of stored routines used by this routine.
Note that unlike elements of similar set for statement elements of this set are not linked in one list. Because of this we are able save memory by using for this set same objects that are used in 'sroutines' sets for statements of which this stored routine consists.
See Sroutine_hash_entry for explanation why this hash uses binary key comparison.
st_trg_chistics sp_head::m_trg_chistics |
Trigger characteristics.
class Table_trigger_dispatcher* sp_head::m_trg_list |
The Table_trigger_dispatcher instance, where this trigger belongs to.
enum_sp_type sp_head::m_type |
Stored program type.
|
private |
SP-persistent memory root (for instructions and expressions).
|
private |
Flags of LEX::enum_binlog_stmt_unsafe.