MySQL 8.4.3
Source Code Documentation
dd_sql_view.cc File Reference
#include "sql/dd_sql_view.h"
#include <string.h>
#include <sys/types.h>
#include <set>
#include <vector>
#include "lex_string.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/strings/m_ctype.h"
#include "mysqld_error.h"
#include "sql/auth/auth_common.h"
#include "sql/dd/cache/dictionary_client.h"
#include "sql/dd/dd.h"
#include "sql/dd/dd_view.h"
#include "sql/dd/dictionary.h"
#include "sql/dd/object_id.h"
#include "sql/dd/string_type.h"
#include "sql/dd/types/schema.h"
#include "sql/dd/types/view.h"
#include "sql/debug_sync.h"
#include "sql/derror.h"
#include "sql/error_handler.h"
#include "sql/handler.h"
#include "sql/mdl.h"
#include "sql/mysqld.h"
#include "sql/set_var.h"
#include "sql/sp_head.h"
#include "sql/sql_alter.h"
#include "sql/sql_base.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_db.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_view.h"
#include "sql/strfunc.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/thd_raii.h"
#include "sql/transaction.h"
#include "thr_lock.h"

Classes

class  View_metadata_updater_context
 RAII class to set the context for View_metadata_updater. More...
 
class  View_metadata_updater_error_handler
 A error handler to convert all the errors except deadlock, lock wait timeout and stack overrun to ER_VIEW_INVALID while updating views metadata. More...
 

Namespaces

namespace  dd
 The version of the current data dictionary table definitions.
 

Functions

template<typename T >
static bool prepare_view_tables_list (THD *thd, const char *db, const char *tbl_or_sf_name, bool skip_same_db, MEM_ROOT *mem_root, std::vector< Table_ref * > *views)
 Prepare Table_ref object for views referencing Base Table/ View/ Stored routine "db.tbl_or_sf_name". More...
 
template<typename T >
static bool mark_all_views_invalid (THD *thd, const char *db, const char *tbl_or_sf_name, const std::vector< Table_ref * > *views_list, bool skip_same_db, bool commit_dd_changes, MEM_ROOT *mem_root)
 Helper method to mark all views state as invalid. More...
 
static bool open_views_and_update_metadata (THD *thd, const std::vector< Table_ref * > *views, bool commit_dd_changes, Uncommitted_tables_guard *uncommitted_tables)
 Helper method to. More...
 
static bool is_view_metadata_update_needed (THD *thd, const char *db, const char *name)
 Helper method to check if view metadata update is required for the DDL operation. More...
 
template<typename T >
static bool update_view_metadata (THD *thd, const char *db, const char *tbl_or_sf_name, bool commit_dd_changes, Uncommitted_tables_guard *uncommitted_tables)
 Helper method to update referencing view's metadata. More...
 
static bool update_referencing_views_metadata (THD *thd, const char *db, const char *table_name, const char *new_db, const char *new_table_name, bool commit_dd_changes, Uncommitted_tables_guard *uncommitted_tables)
 
bool update_referencing_views_metadata (THD *thd, const Table_ref *table, const char *new_db, const char *new_table_name, bool commit_dd_changes, Uncommitted_tables_guard *uncommitted_tables)
 Update metadata of views referencing "table" being renamed and views referencing (if there any) new table name "new_db.new_table_name". More...
 
bool update_referencing_views_metadata (THD *thd, const Table_ref *table, bool commit_dd_changes, Uncommitted_tables_guard *uncommitted_tables)
 Update metadata of views referencing the table. More...
 
bool update_referencing_views_metadata (THD *thd, const char *db_name, const char *table_name, bool commit_dd_changes, Uncommitted_tables_guard *uncommitted_tables)
 Update metadata of views referencing the table. More...
 
bool update_referencing_views_metadata (THD *thd, const sp_name *spname)
 Method to update metadata of views using stored function. More...
 
template<typename T >
static bool mark_referencing_views_invalid (THD *thd, const char *db, const char *tbl_or_sf_name, bool skip_same_db, bool commit_dd_changes, MEM_ROOT *mem_root)
 Helper method to mark referencing views as invalid. More...
 
bool mark_referencing_views_invalid (THD *thd, const Table_ref *table, bool skip_same_db, bool commit_dd_changes, MEM_ROOT *mem_root)
 Mark views referencing the table as invalid. More...
 
bool mark_referencing_views_invalid (THD *thd, const sp_name *spname, MEM_ROOT *mem_root)
 Mark views using stored function as invalid. More...
 
std::string push_view_warning_or_error (THD *thd, const char *db, const char *view_name)
 Push error or warnings in case a view is invalid and return the error message to the caller. More...
 

Function Documentation

◆ is_view_metadata_update_needed()

static bool is_view_metadata_update_needed ( THD thd,
const char *  db,
const char *  name 
)
static

Helper method to check if view metadata update is required for the DDL operation.

Parameters
thdThread handle.
dbDatabase name.
nameBase table/ View/ Stored routine name.
Return values
trueif view metadata update is required.
falseif view metadata update is NOT required.

◆ mark_all_views_invalid()

template<typename T >
static bool mark_all_views_invalid ( THD thd,
const char *  db,
const char *  tbl_or_sf_name,
const std::vector< Table_ref * > *  views_list,
bool  skip_same_db,
bool  commit_dd_changes,
MEM_ROOT mem_root 
)
static

Helper method to mark all views state as invalid.

If operation is drop operation then view referencing it becomes invalid. This method is called to mark state of all the referencing views as invalid in such case.

Template Parameters
TType of object (View_table/View_routine) to fetch view names from.
Parameters
thdCurrent thread.
dbDatabase name.
tbl_or_sf_nameBase table/ View/ Stored function name.
views_listTable_ref objects of the referencing views.
skip_same_dbIndicates whether it is OK to skip views belonging to the same database as table (as they will be dropped anyway).
commit_dd_changesIndicates whether changes to DD need to be committed.
mem_rootMemory root for allocation of temporary objects which will be cleared after processing referenced table/view/routine.
Return values
falseSuccess.
trueFailure.

◆ mark_referencing_views_invalid() [1/3]

template<typename T >
static bool mark_referencing_views_invalid ( THD thd,
const char *  db,
const char *  tbl_or_sf_name,
bool  skip_same_db,
bool  commit_dd_changes,
MEM_ROOT mem_root 
)
static

Helper method to mark referencing views as invalid.

Template Parameters
TType of object (View_table/View_routine) to fetch referencing view names.
Parameters
thdCurrent thread.
dbDatabase name.
tbl_or_sf_nameBase table/ View/ Stored function name.
skip_same_dbIndicates whether it is OK to skip views belonging to the same database as table (as they will be dropped anyway).
commit_dd_changesIndicates whether changes to DD need to be committed.
mem_rootMemory root for allocation of temporary objects which will be cleared after each call to this function.
Return values
falseSuccess.
trueFailure.

◆ mark_referencing_views_invalid() [2/3]

bool mark_referencing_views_invalid ( THD thd,
const sp_name spname,
MEM_ROOT mem_root 
)

Mark views using stored function as invalid.

Parameters
thdThread handle.
spnameName of the stored function.
mem_rootMemory root for allocation of temporary objects which will be cleared after each call to this function.
Note
This call is a version of update_referencing_views_metadata(), which is optimized for DROP DATABASE case.
Return values
falseSuccess.
trueFailure.

◆ mark_referencing_views_invalid() [3/3]

bool mark_referencing_views_invalid ( THD thd,
const Table_ref table,
bool  skip_same_db,
bool  commit_dd_changes,
MEM_ROOT mem_root 
)

Mark views referencing the table as invalid.

Parameters
thdThread handle.
tableViews referencing this table need to be marked as invalid.
skip_same_dbIndicates whether it is OK to skip views belonging to the same database as table (as they will be dropped anyway).
commit_dd_changesIndicates whether changes to DD need to be committed.
mem_rootMemory root for allocation of temporary objects which will be cleared after each call to this function.
Note
In case when commit_dd_changes is false, the caller must rollback both statement and transaction on failure, before any further accesses to DD. This is because such a failure might be caused by a deadlock, which requires rollback before any other operations on SE (including reads using attachable transactions) can be done. If case when commit_dd_changes is true this function will handle transaction rollback itself.
This call is a version of update_referencing_views_metadata(), which is optimized for DROP DATABASE case.
Return values
falseSuccess.
trueFailure.

◆ open_views_and_update_metadata()

static bool open_views_and_update_metadata ( THD thd,
const std::vector< Table_ref * > *  views,
bool  commit_dd_changes,
Uncommitted_tables_guard uncommitted_tables 
)
static

Helper method to.

  • Mark view as invalid if DDL operation leaves the view in invalid state.
  • Open all the views from the Table_ref vector and recreates the view metadata.
Parameters
thdThread handle.
viewsTable_ref objects of the views.
commit_dd_changesIndicates whether changes to DD need to be committed.
[in,out]uncommitted_tablesHelper class to store list of views which shares need to be removed from TDC if we fail to commit changes to DD. Only used if commit_dd_changes is false.
Return values
falseSuccess.
trueFailure.
See also
Item::print_for_order()

◆ prepare_view_tables_list()

template<typename T >
static bool prepare_view_tables_list ( THD thd,
const char *  db,
const char *  tbl_or_sf_name,
bool  skip_same_db,
MEM_ROOT mem_root,
std::vector< Table_ref * > *  views 
)
static

Prepare Table_ref object for views referencing Base Table/ View/ Stored routine "db.tbl_or_sf_name".

Template Parameters
TType of object (View_table/View_routine) to fetch view names from.
Parameters
thdCurrent thread.
dbDatabase name.
tbl_or_sf_nameBase table/ View/ Stored function name.
skip_same_dbIndicates whether it is OK to skip views belonging to the same database as table (as they will be dropped anyway).
mem_rootMemory root for allocation of temporary objects which will be cleared after processing this table/view/routine.
[out]viewsTable_ref objects for views.
Return values
falseSuccess.
trueFailure.

◆ push_view_warning_or_error()

std::string push_view_warning_or_error ( THD thd,
const char *  db,
const char *  view_name 
)

Push error or warnings in case a view is invalid and return the error message to the caller.

Parameters
thdThread handle.
dbDatabase name.
view_nameView name.

returns The error/warning message string.

◆ update_referencing_views_metadata() [1/5]

static bool update_referencing_views_metadata ( THD thd,
const char *  db,
const char *  table_name,
const char *  new_db,
const char *  new_table_name,
bool  commit_dd_changes,
Uncommitted_tables_guard uncommitted_tables 
)
static

◆ update_referencing_views_metadata() [2/5]

bool update_referencing_views_metadata ( THD thd,
const char *  db_name,
const char *  table_name,
bool  commit_dd_changes,
Uncommitted_tables_guard uncommitted_tables 
)

Update metadata of views referencing the table.

Parameters
thdThread handle.
db_nameDatabase name.
table_nameUpdate metadata of views referencing this table.
commit_dd_changesIndicates whether changes to DD need to be committed.
[in,out]uncommitted_tablesHelper class to store list of views which shares need to be removed from TDC if we fail to commit changes to DD. Only used if commit_dd_changes is false.
Note
In case when commit_dd_changes is false, the caller must rollback both statement and transaction on failure, before any further accesses to DD. This is because such a failure might be caused by a deadlock, which requires rollback before any other operations on SE (including reads using attachable transactions) can be done. If case when commit_dd_changes is true this function will handle transaction rollback itself.
Return values
falseSuccess.
trueFailure.

◆ update_referencing_views_metadata() [3/5]

bool update_referencing_views_metadata ( THD thd,
const sp_name spname 
)

Method to update metadata of views using stored function.

Parameters
thdThread handle.
spnameName of the stored function.
Return values
falseSuccess.
trueFailure.

◆ update_referencing_views_metadata() [4/5]

bool update_referencing_views_metadata ( THD thd,
const Table_ref table,
bool  commit_dd_changes,
Uncommitted_tables_guard uncommitted_tables 
)

Update metadata of views referencing the table.

Parameters
thdThread handle.
tableUpdate metadata of views referencing this table.
commit_dd_changesIndicates whether changes to DD need to be committed.
[in,out]uncommitted_tablesHelper class to store list of views which shares need to be removed from TDC if we fail to commit changes to DD. Only used if commit_dd_changes is false.
Note
In case when commit_dd_changes is false, the caller must rollback both statement and transaction on failure, before any further accesses to DD. This is because such a failure might be caused by a deadlock, which requires rollback before any other operations on SE (including reads using attachable transactions) can be done. If case when commit_dd_changes is true this function will handle transaction rollback itself.
Return values
falseSuccess.
trueFailure.

◆ update_referencing_views_metadata() [5/5]

bool update_referencing_views_metadata ( THD thd,
const Table_ref table,
const char *  new_db,
const char *  new_table_name,
bool  commit_dd_changes,
Uncommitted_tables_guard uncommitted_tables 
)

Update metadata of views referencing "table" being renamed and views referencing (if there any) new table name "new_db.new_table_name".

Parameters
thdThread handle.
tableUpdate metadata of views referencing this table.
new_dbNew db name set in the rename operation.
new_table_nameNew table name set in the rename
commit_dd_changesIndicates whether changes to DD need to be committed.
[in,out]uncommitted_tablesHelper class to store list of views which shares need to be removed from TDC if we fail to commit changes to DD. Only used if commit_dd_changes is false.
Note
In case when commit_dd_changes is false, the caller must rollback both statement and transaction on failure, before any further accesses to DD. This is because such a failure might be caused by a deadlock, which requires rollback before any other operations on SE (including reads using attachable transactions) can be done. If case when commit_dd_changes is true this function will handle transaction rollback itself. operation.
Return values
falseSuccess.
trueFailure.

◆ update_view_metadata()

template<typename T >
static bool update_view_metadata ( THD thd,
const char *  db,
const char *  tbl_or_sf_name,
bool  commit_dd_changes,
Uncommitted_tables_guard uncommitted_tables 
)
static

Helper method to update referencing view's metadata.

Template Parameters
TType of object (View_table/View_routine) to fetch referencing view names.
Parameters
thdCurrent thread.
dbDatabase name.
tbl_or_sf_nameBase table/ View/ Stored function name.
commit_dd_changesIndicates whether changes to DD need to be committed.
[in,out]uncommitted_tablesHelper class to store list of views which shares need to be removed from TDC if we fail to commit changes to DD. Only used if commit_dd_changes is false.
Return values
falseSuccess.
trueFailure.