| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/dict0load.h |
| 29 |
Loads to the memory cache database object definitions |
| 30 |
from dictionary tables |
| 31 |
|
| 32 |
Created 4/24/1996 Heikki Tuuri |
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#ifndef dict0load_h |
| 36 |
#define dict0load_h |
| 37 |
|
| 38 |
#include "btr0types.h" |
| 39 |
#include "dict0types.h" |
| 40 |
#include "fil0fil.h" |
| 41 |
#include "mem0mem.h" |
| 42 |
#include "trx0types.h" |
| 43 |
#include "univ.i" |
| 44 |
#include "ut0byte.h" |
| 45 |
#include "ut0new.h" |
| 46 |
|
| 47 |
#include <deque> |
| 48 |
|
| 49 |
/** A stack of table names related through foreign key constraints */ |
| 50 |
typedef std::deque<const char *, ut::allocator<const char *>> dict_names_t; |
| 51 |
|
| 52 |
/** enum that defines all system table IDs. @see SYSTEM_TABLE_NAME[] */ |
| 53 |
enum dict_system_id_t { |
| 54 |
SYS_TABLES = 0, |
| 55 |
SYS_INDEXES, |
| 56 |
SYS_COLUMNS, |
| 57 |
SYS_FIELDS, |
| 58 |
SYS_FOREIGN, |
| 59 |
SYS_FOREIGN_COLS, |
| 60 |
SYS_TABLESPACES, |
| 61 |
SYS_DATAFILES, |
| 62 |
SYS_VIRTUAL, |
| 63 |
|
| 64 |
/* This must be last item. Defines the number of system tables. */ |
| 65 |
SYS_NUM_SYSTEM_TABLES |
| 66 |
}; |
| 67 |
|
| 68 |
/** Status bit for dict_process_sys_tables_rec_and_mtr_commit() */ |
| 69 |
enum dict_table_info_t { |
| 70 |
DICT_TABLE_LOAD_FROM_RECORD = 0, /*!< Directly populate a dict_table_t |
| 71 |
structure with information from |
| 72 |
a SYS_TABLES record */ |
| 73 |
DICT_TABLE_LOAD_FROM_CACHE = 1 /*!< Check first whether dict_table_t |
| 74 |
is in the cache, if so, return it */ |
| 75 |
}; |
| 76 |
|
| 77 |
extern const char *SYSTEM_TABLE_NAME[]; |
| 78 |
|
| 79 |
/** Finds the first table name in the given database. |
| 80 |
@return own: table name, NULL if does not exist; the caller must free |
| 81 |
the memory in the string! */ |
| 82 |
char *dict_get_first_table_name_in_db( |
| 83 |
const char *name); /*!< in: database name which ends to '/' */ |
| 84 |
|
| 85 |
/** Get the first filepath from SYS_DATAFILES for a given space_id. |
| 86 |
@param[in] space_id Tablespace ID |
| 87 |
@return First filepath (caller must invoke ut::free() on it) |
| 88 |
@retval NULL if no SYS_DATAFILES entry was found. */ |
| 89 |
char *dict_get_first_path(ulint space_id); |
| 90 |
|
| 91 |
/** Make sure the data_dir_path is saved in dict_table_t if DATA DIRECTORY |
| 92 |
was used. Try to read it from the fil_system first, then from SYS_DATAFILES. |
| 93 |
@param[in] table Table object |
| 94 |
@param[in] dict_mutex_own true if dict_sys->mutex is owned already */ |
| 95 |
void dict_get_and_save_data_dir_path(dict_table_t *table, bool dict_mutex_own); |
| 96 |
|
| 97 |
/** Make sure the tablespace name is saved in dict_table_t if the table |
| 98 |
uses a general tablespace. |
| 99 |
Try to read it from the fil_system_t first, then from SYS_TABLESPACES. |
| 100 |
@param[in] table Table object */ |
| 101 |
void dict_get_and_save_space_name(dict_table_t *table); |
| 102 |
|
| 103 |
/** Loads a table definition and also all its index definitions, and also |
| 104 |
the cluster definition if the table is a member in a cluster. Also loads |
| 105 |
all foreign key constraints where the foreign key is in the table or where |
| 106 |
a foreign key references columns in this table. |
| 107 |
@param[in] name Table name in the dbname/tablename format |
| 108 |
@param[in] cached true=add to cache, false=do not |
| 109 |
@param[in] ignore_err Error to be ignored when loading |
| 110 |
table and its index definition |
| 111 |
@param[in] prev_table previous table name. The current table load |
| 112 |
is happening because of the load of the |
| 113 |
previous table name. This parameter is used |
| 114 |
to check for cyclic calls. |
| 115 |
@return table, NULL if does not exist; if the table is stored in an |
| 116 |
.ibd file, but the file does not exist, then we set the ibd_file_missing |
| 117 |
flag in the table object we return. */ |
| 118 |
dict_table_t *dict_load_table(const char *name, bool cached, |
| 119 |
dict_err_ignore_t ignore_err, |
| 120 |
const std::string *prev_table = nullptr); |
| 121 |
|
| 122 |
/** This function is called when the database is booted. |
| 123 |
Loads system table index definitions except for the clustered index which |
| 124 |
is added to the dictionary cache at booting before calling this function. */ |
| 125 |
void dict_load_sys_table(dict_table_t *table); /*!< in: system table */ |
| 126 |
/** Loads foreign key constraints where the table is either the foreign key |
| 127 |
holder or where the table is referenced by a foreign key. Adds these |
| 128 |
constraints to the data dictionary. |
| 129 |
|
| 130 |
The foreign key constraint is loaded only if the referenced table is also |
| 131 |
in the dictionary cache. If the referenced table is not in dictionary |
| 132 |
cache, then it is added to the output parameter (fk_tables). |
| 133 |
|
| 134 |
@return DB_SUCCESS or error code */ |
| 135 |
[[nodiscard]] dberr_t dict_load_foreigns( |
| 136 |
const char *table_name, /*!< in: table name */ |
| 137 |
const char **col_names, /*!< in: column names, or NULL |
| 138 |
to use table->col_names */ |
| 139 |
bool check_recursive, /*!< in: Whether to check |
| 140 |
recursive load of tables |
| 141 |
chained by FK */ |
| 142 |
bool check_charsets, /*!< in: whether to check |
| 143 |
charset compatibility */ |
| 144 |
dict_err_ignore_t ignore_err, /*!< in: error to be ignored */ |
| 145 |
dict_names_t &fk_tables); /*!< out: stack of table names |
| 146 |
which must be loaded |
| 147 |
subsequently to load all the |
| 148 |
foreign key constraints. */ |
| 149 |
|
| 150 |
/** This function opens a system table, and return the first record. |
| 151 |
@return first record of the system table */ |
| 152 |
const rec_t *dict_startscan_system( |
| 153 |
btr_pcur_t *pcur, /*!< out: persistent cursor to |
| 154 |
the record */ |
| 155 |
mtr_t *mtr, /*!< in: the mini-transaction */ |
| 156 |
dict_system_id_t system_id); /*!< in: which system table to open */ |
| 157 |
/** This function get the next system table record as we scan the table. |
| 158 |
@return the record if found, NULL if end of scan. */ |
| 159 |
const rec_t *dict_getnext_system(btr_pcur_t *pcur, /*!< in/out: persistent |
| 160 |
cursor to the record */ |
| 161 |
mtr_t *mtr); /*!< in: the mini-transaction */ |
| 162 |
|
| 163 |
/** This function parses a SYS_TABLESPACES record, extracts necessary |
| 164 |
information from the record and returns to caller. |
| 165 |
@return error message, or NULL on success */ |
| 166 |
const char *dict_process_sys_tablespaces( |
| 167 |
mem_heap_t *heap, /*!< in/out: heap memory */ |
| 168 |
const rec_t *rec, /*!< in: current SYS_TABLESPACES rec */ |
| 169 |
space_id_t *space, /*!< out: space id */ |
| 170 |
const char **name, /*!< out: tablespace name */ |
| 171 |
uint32_t *flags); /*!< out: tablespace flags */ |
| 172 |
|
| 173 |
/** Opens a tablespace for dict_load_table_one() |
| 174 |
@param[in,out] table A table that refers to the tablespace to open |
| 175 |
@param[in] ignore_err Whether to ignore an error. */ |
| 176 |
void dict_load_tablespace(dict_table_t *table, dict_err_ignore_t ignore_err); |
| 177 |
|
| 178 |
/** Using the table->heap, copy the null-terminated filepath into |
| 179 |
table->data_dir_path. The data directory path is derived from the |
| 180 |
filepath by stripping the the table->name.m_name component suffix. |
| 181 |
If the filepath is not of the correct form (".../db/table.ibd"), |
| 182 |
then table->data_dir_path will remain nullptr. |
| 183 |
@param[in,out] table table instance |
| 184 |
@param[in] filepath filepath of tablespace */ |
| 185 |
void dict_save_data_dir_path(dict_table_t *table, char *filepath); |
| 186 |
|
| 187 |
/** Load all tablespaces during upgrade */ |
| 188 |
void dict_load_tablespaces_for_upgrade(); |
| 189 |
|
| 190 |
/* Comparator for missing_spaces. */ |
| 191 |
struct space_compare { |
| 192 |
bool operator()(const fil_space_t *lhs, const fil_space_t *rhs) const { |
| 193 |
return (lhs->id < rhs->id); |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
/* This is set of tablespaces that are not found in SYS_TABLESPACES. |
| 198 |
InnoDB tablespaces before 5.6 are not registered in SYS_TABLESPACES. |
| 199 |
So we maintain a std::set, which is later used to register the |
| 200 |
tablespaces to dictionary table mysql.tablespaces */ |
| 201 |
using missing_sys_tblsp_t = std::set<fil_space_t *, space_compare>; |
| 202 |
extern missing_sys_tblsp_t missing_spaces; |
| 203 |
|
| 204 |
/** This bool denotes if we found a Table or Partition with discarded Tablespace |
| 205 |
during load of SYS_TABLES (in dict_check_sys_tables). |
| 206 |
|
| 207 |
We use it to stop upgrade from 5.7 to 8.0 if there are discarded Tablespaces. */ |
| 208 |
extern bool has_discarded_tablespaces; |
| 209 |
|
| 210 |
#endif |