MySQL  8.0.0
Source Code Documentation
sp_head Class Reference

sp_head represents one instance of a stored program. More...

#include <sp_head.h>

Inheritance diagram for sp_head:
Query_arena

Public Types

enum  {
  HAS_RETURN = 1, MULTI_RESULTS = 8, CONTAINS_DYNAMIC_SQL = 16, IS_INVOKED = 32,
  HAS_SET_AUTOCOMMIT_STMT = 64, HAS_COMMIT_OR_ROLLBACK = 128, LOG_SLOW_STATEMENTS = 256, LOG_GENERAL_LOG = 512,
  HAS_SQLCOM_RESET = 1024, HAS_SQLCOM_FLUSH = 2048, MODIFIES_DATA = 4096
}
 Possible values of m_flags. More...
 

Public Member Functions

bool is_invoked () const
 Is this routine being executed? More...
 
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_ctxget_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, List< 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...
 
uint instructions ()
 
sp_instrlast_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=0) const
 
Fieldcreate_result_field (size_t field_max_length, const char *field_name, TABLE *table)
 Create Field-object corresponding to the RETURN field of a stored function. More...
 
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:

  1. Mark used instructions
  2. While doing this, shortcut jumps to jump instructions
  3. Compact the code, removing unused instructions.
More...
 
void add_mark_lead (uint ip, List< sp_instr > *leads)
 Helper used during flow analysis during code optimization. More...
 
sp_instrget_instr (uint i)
 Get SP-instruction at given index. More...
 
void add_used_tables_to_table_list (THD *thd, TABLE_LIST ***query_tables_last_ptr, enum_sql_command sql_command, TABLE_LIST *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_pcontextget_root_parsing_context () const
 
MEM_ROOTget_persistent_mem_root () const
 
MEM_ROOTget_current_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...
 

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_sharem_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_chisticsm_chistics
 Stored program characteristics. 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_STRING m_body
 
LEX_STRING m_body_utf8
 
LEX_STRING m_defstr
 
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. The levels are numbered from 0. More...
 
sp_headm_next_cached_sp
 A list of diferent recursion level instances for the same procedure. More...
 
sp_headm_first_instance
 Pointer to the first element of the above list. More...
 
sp_headm_first_free_instance
 Pointer to the first free (non-INVOKED) routine in the list of cached instances for this SP. More...
 
sp_headm_last_cached_sp
 Pointer to the last element in the list of instances of the SP. More...
 
HASH 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_fieldm_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_dispatcherm_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...
 
void opt_mark ()
 Perform a forward flow analysis in the generated code. More...
 
bool merge_table_list (THD *thd, TABLE_LIST *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 Member Functions inherited from Query_arena
 Query_arena (MEM_ROOT *mem_root_arg, enum enum_state state_arg)
 
 Query_arena ()
 
virtual ~Query_arena ()
 
bool is_stmt_prepare () const
 
bool is_stmt_prepare_or_first_sp_execute () const
 
bool is_stmt_prepare_or_first_stmt_execute () const
 
bool is_conventional () const
 
void * alloc (size_t size)
 
void * mem_calloc (size_t size)
 
template<typename T >
T * alloc_typed ()
 
char * mem_strdup (const char *str)
 
char * strmake (const char *str, size_t size)
 
void * memdup (const void *str, size_t size)
 
void set_query_arena (Query_arena *set)
 
void free_items ()
 
virtual void cleanup_stmt ()
 

Private Attributes

MEM_ROOT main_mem_root
 SP-persistent memory root (for instructions and expressions). More...
 
sp_pcontextm_root_parsing_ctx
 Root parsing context (topmost BEGIN..END block) of this SP. More...
 
Mem_root_array< sp_instr *, true > m_instructions
 The SP-instructions. More...
 
HASH m_sptabs
 Multi-set representing optimized list of tables to be locked by this routine. More...
 
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_ctxm_creation_ctx
 Snapshot of several system variables at CREATE-time. More...
 
uint32 unsafe_flags
 Flags of LEX::enum_binlog_stmt_unsafe. More...
 
- Private Attributes inherited from Query_arena
Itemfree_list
 
MEM_ROOTmem_root
 
bool is_backup_arena
 
bool is_reprepared
 
enum_state state
 

Friends

sp_headsp_start_parsing (THD *thd, enum_sp_type sp_type, sp_name *sp_name)
 Start parsing of a stored program. More...
 

Additional Inherited Members

- Private Types inherited from Query_arena
enum  enum_state {
  STMT_INITIALIZED = 0, STMT_INITIALIZED_FOR_SP = 1, STMT_PREPARED = 2, STMT_CONVENTIONAL_EXECUTION = 3,
  STMT_EXECUTED = 4, STMT_ERROR = -1
}
 

Detailed Description

sp_head represents one instance of a stored program.

It might be of any type (stored procedure, function, trigger, event).

Member Enumeration Documentation

anonymous enum

Possible values of m_flags.

Enumerator
HAS_RETURN 
MULTI_RESULTS 
CONTAINS_DYNAMIC_SQL 
IS_INVOKED 
HAS_SET_AUTOCOMMIT_STMT 
HAS_COMMIT_OR_ROLLBACK 
LOG_SLOW_STATEMENTS 
LOG_GENERAL_LOG 
HAS_SQLCOM_RESET 
HAS_SQLCOM_FLUSH 
MODIFIES_DATA 

Marks routines that directly (i.e.

not by calling other routines) change tables. Note that this flag is set automatically based on type of statements used in the stored routine and is different from routine characteristic provided by user in a form of CONTAINS SQL, READS SQL DATA, MODIFIES SQL DATA clauses. The latter are accepted by parser but pretty much ignored after that. We don't rely on them: a) for compatibility reasons. b) because in CONTAINS SQL case they don't provide enough information anyway.

Constructor & Destructor Documentation

sp_head::sp_head ( MEM_ROOT  mem_root,
enum_sp_type  type 
)
private

Use sp_start_parsing() to create instances of sp_head.

sp_head::~sp_head ( )
private

Use destroy() to destoy instances of sp_head.

sp_head::sp_head ( const sp_head )
private

Member Function Documentation

bool sp_head::add_instr ( THD thd,
sp_instr instr 
)

Add instruction to SP.

Parameters
thdThread context.
instrInstruction.
Returns
Error status.
void sp_head::add_mark_lead ( uint  ip,
List< sp_instr > *  leads 
)

Helper used during flow analysis during code optimization.

See the implementation of opt_mark().

Parameters
ipthe instruction to add to the leads list
leadsthe 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_LIST ***  query_tables_last_ptr,
enum_sql_command  sql_command,
TABLE_LIST 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.

Parameters
[in]thdThread context
[in,out]query_tables_last_ptrPointer to the next_global member of last element of the list where tables will be added (or to its root).
[in]sql_commandSQL-command for which we are adding elements to the table list.
[in]belong_to_viewUppermost 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.

Parameters
thdThread context.
[out]full_accessSet to 1 if the user is the owner of the stored program.
Returns
Error status.
Field * sp_head::create_result_field ( size_t  field_max_length,
const char *  field_name,
TABLE table 
)

Create Field-object corresponding to the RETURN field of a stored function.

This operation makes sense for stored functions only.

Parameters
field_max_lengththe max length (in the sense of Item classes).
field_namethe field name (item name).
tablethe field's table.
Returns
newly created and initialized Field-instance, or NULL in case of error.
void sp_head::destroy ( sp_head sp)
static
bool sp_head::execute ( THD thd,
bool  merge_da_on_success 
)
private

Execute the routine.

The main instruction jump loop is there. Assume the parameters already set.

Parameters
thdThread context.
merge_da_on_successFlag specifying if Warning Info should be propagated to the caller on Completion Condition or not.
Returns
Error status.

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.

bool sp_head::execute_function ( THD thd,
Item **  args,
uint  argcount,
Field return_fld 
)

Execute a function.

  • evaluate parameters
  • changes security context for SUID routines
  • switch to new memroot
  • call sp_head::execute
  • restore old memroot
  • evaluate the return value
  • restores security context
Parameters
thdThread context.
argsPassed arguments (these are items from containing statement?)
argcountNumber of passed arguments. We need to check if this is correct.
return_fldSave result here.
Returns
Error status.
bool sp_head::execute_procedure ( THD thd,
List< Item > *  args 
)

Execute a procedure.

The function does the following steps:

  • Set all parameters
  • changes security context for SUID routines
  • call sp_head::execute
  • copy back values of INOUT and OUT parameters
  • restores security context
Parameters
thdThread context.
argsList of values passed as arguments.
Returns
Error status.
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.

  • changes security context for triggers
  • switch to new memroot
  • call sp_head::execute
  • restore old memroot
  • restores security context
Parameters
thdThread context
db_namedatabase name
table_nametable name
grant_infoGRANT_INFO structure to be filled with information about definer's privileges on subject table
Returns
Error status.
Stored_program_creation_ctx* sp_head::get_creation_ctx ( )
inline
MEM_ROOT* sp_head::get_current_mem_root ( ) const
inline
Returns
currently used mem-root.
sp_instr* sp_head::get_instr ( uint  i)
inline

Get SP-instruction at given index.

NOTE: it is important to have unsigned int here, sometimes we get (-1) passed here, so it get's converted to MAX_INT, and the result of the function call is NULL.

MEM_ROOT* sp_head::get_persistent_mem_root ( ) const
inline
Returns
SP-persistent mem-root. Instructions and expressions are stored in its memory between executions.
sp_pcontext* sp_head::get_root_parsing_context ( ) const
inline
Returns
root parsing context for this stored program.
bool sp_head::has_updated_trigger_fields ( const MY_BITMAP used_fields) const

Check whether any table's fields are used in trigger.

Parameters
[in]used_fieldsbitmap of fields to check
Returns
Check result
Return values
trueSome table fields are used in trigger
falseNone of table fields are used in trigger
void sp_head::init_sp_name ( THD thd,
sp_name spname 
)
private

Copy sp name from parser.

uint sp_head::instructions ( )
inline
bool sp_head::is_invoked ( ) const
inline

Is this routine being executed?

bool sp_head::is_not_allowed_in_function ( const char *  where)
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.

sp_instr* sp_head::last_instruction ( )
inline
void sp_head::mark_used_trigger_fields ( TABLE subject_table)
bool sp_head::merge_table_list ( THD thd,
TABLE_LIST table,
LEX lex_for_tmp_check 
)
private

Merge the list of tables used by some query into the multi-set of tables used by routine.

Parameters
thdThread context.
tableTable list.
lex_for_tmp_checkLEX of the query for which we are merging table list.
Note
This method will use LEX provided to check whenever we are creating temporary table and mark it as such in target multi-set.
Returns
Error status.
bool sp_head::modifies_data ( ) const
inline

Returns true if any substatement in the routine directly (not through another routine) modifies data/changes table.

See also
Comment for MODIFIES_DATA flag.
char* sp_head::name ( uint lenp = 0) const
inline
void sp_head::operator= ( sp_head )
private
void sp_head::opt_mark ( )
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:

  1. Mark used instructions
  2. While doing this, shortcut jumps to jump instructions
  3. Compact the code, removing unused instructions.

This is the main mark and move loop; it relies on the following methods in sp_instr and its subclasses:

  • opt_mark() : Mark instruction as reachable
  • opt_shortcut_jump(): Shortcut jumps to the final destination; used by opt_mark().
  • opt_move() : Update moved instruction
  • set_destination() : Set the new destination (jump instructions only)
void sp_head::propagate_attributes ( Query_tables_list prelocking_ctx)
inline
bool sp_head::reset_lex ( THD thd)

Reset LEX-object during parsing, before we parse a sub statement.

Parameters
thdThread context.
Returns
Error status.
bool sp_head::restore_lex ( THD thd)

Restore LEX-object during parsing, after we have parsed a sub statement.

Parameters
thdThread context.
Returns
Error status.
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.

void sp_head::set_creation_ctx ( Stored_program_creation_ctx creation_ctx)
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 
)
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.

Parameters
thdThread context.
[out]save_ctxWhere to save the old security context.
Returns
Error status.
void sp_head::set_sp_cache_version ( int64  sp_cache_version)
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.

Returns
Error status.
int64 sp_head::sp_cache_version ( ) const
inline

Get the value of the SP cache version, as remembered when the routine was inserted into the cache.

Friends And Related Function Documentation

sp_head* sp_start_parsing ( THD thd,
enum_sp_type  sp_type,
sp_name sp_name 
)
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().

Parameters
thdThread context.
sp_typeThe stored program type
sp_nameThe stored progam name
Returns
properly initialized sp_head-instance in case of success, or NULL is case of out-of-memory error.

Member Data Documentation

LEX_STRING sp_head::m_body
LEX_STRING sp_head::m_body_utf8
st_sp_chistics* sp_head::m_chistics

Stored program characteristics.

longlong sp_head::m_created
Stored_program_creation_ctx* sp_head::m_creation_ctx
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
LEX_STRING sp_head::m_defstr
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.

Mem_root_array<sp_instr *, true> sp_head::m_instructions
private

The SP-instructions.

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 diferent recursion level instances for the same procedure.

For every recursion level we have a 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.

sp_pcontext* sp_head::m_root_parsing_ctx
private

Root parsing context (topmost BEGIN..END block) of this SP.

Security_context sp_head::m_security_ctx
int64 sp_head::m_sp_cache_version
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.

HASH sp_head::m_sptabs
private

Multi-set representing optimized list of tables to be locked by this routine.

Does not include tables which are used by invoked routines.

Note
For prelocking-free SPs this multiset is constructed too. We do so because the same instance of sp_head may be called both in prelocked mode and in non-prelocked mode.
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.

HASH 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.

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.

MEM_ROOT sp_head::main_mem_root
private

SP-persistent memory root (for instructions and expressions).

uint32 sp_head::unsafe_flags
private

The documentation for this class was generated from the following files: