MySQL  8.0.16
Source Code Documentation
dictionary.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef DD__DICTIONARY_INCLUDED
24 #define DD__DICTIONARY_INCLUDED
25 
26 #include "my_compiler.h"
27 #include "sql/dd/string_type.h" // dd::String_type
29 
30 class THD;
31 class MDL_ticket;
32 class Plugin_table;
33 // class Tablespace;
34 
35 namespace dd {
36 
37 ///////////////////////////////////////////////////////////////////////////
38 
39 class Collation;
40 class Object_table;
41 class Schema;
42 class Tablespace;
43 
44 namespace cache {
45 class Dictionary_client;
46 }
47 
48 ///////////////////////////////////////////////////////////////////////////
49 
50 /// Main interface class enabling users to operate on data dictionary.
51 class Dictionary {
52  public:
53  /**
54  Get dictionary object for a given dictionary table name.
55  If the given schema_name and table_name is not a dictionary
56  table name, then the function returns NULL.
57 
58  @returns Pointer to dictionary object for the given
59  dictionary table name, else NULL.
60  */
61  virtual const Object_table *get_dd_table(
62  const String_type &schema_name, const String_type &table_name) const = 0;
63 
64  public:
65  /////////////////////////////////////////////////////////////////////////
66  // Auxiliary operations.
67  /////////////////////////////////////////////////////////////////////////
68 
69  /**
70  Check if the given schema name is 'mysql', which where
71  the DD tables are stored.
72 
73  @param schema_name Schema name to check.
74 
75  @returns true - If schema_name is 'mysql'
76  @returns false - If schema_name is not 'mysql'
77  */
78  virtual bool is_dd_schema_name(const String_type &schema_name) const = 0;
79 
80  /**
81  Check if given table name is a dictionary table name.
82 
83  @param schema_name Schema name to check.
84  @param table_name Table name to check.
85 
86  @returns true - If given table name is a dictionary table.
87  @returns false - If table name is not a dictionary table.
88  */
89  virtual bool is_dd_table_name(const String_type &schema_name,
90  const String_type &table_name) const = 0;
91 
92  /**
93  Check if given table name is a system table name.
94 
95  @param schema_name Schema name to check.
96  @param table_name Table name to check.
97 
98  @returns true - If given table name is a system table.
99  @returns false - If table name is not a system table.
100  */
101  virtual bool is_system_table_name(const String_type &schema_name,
102  const String_type &table_name) const = 0;
103 
104  /**
105  Get the error code representing the type name string for a dictionary
106  or system table.
107 
108  Necessary to support localization of error messages.
109 
110  @param schema_name Schema name to check.
111  @param table_name Table name to check.
112 
113  @returns The error code representing the type name associated with the
114  table, for being used in error messages.
115  */
116  virtual int table_type_error_code(const String_type &schema_name,
117  const String_type &table_name) const = 0;
118 
119  /**
120  Check if given table name can be accessed by the given thread type.
121 
122  @param is_dd_internal_thread 'true' if this is a DD internal
123  thread.
124  @param is_ddl_statement 'true' if this is a DDL statement.
125  @param schema_name Schema name to check.
126  @param schema_length Length of schema name to check.
127  @param table_name Table name to check.
128 
129  @returns true - If given table name is accessible by the thread type.
130  @returns false - If table name is not accessible.
131  */
132  virtual bool is_dd_table_access_allowed(bool is_dd_internal_thread,
133  bool is_ddl_statement,
134  const char *schema_name,
135  size_t schema_length,
136  const char *table_name) const = 0;
137 
138  /**
139  Check if given table name is a system view name.
140 
141  @param schema_name Schema name to check.
142  @param table_name Table name to check.
143  @param[out] hidden Pointer to boolean flag indicating
144  if the object is hidden.
145 
146  @returns true - If given table name is a system view.
147  @returns false - If table name is not a system view.
148  */
149  virtual bool is_system_view_name(const char *schema_name,
150  const char *table_name,
151  bool *hidden) const = 0;
152 
153  /**
154  Check if given table name is a system view name.
155 
156  @param schema_name Schema name to check.
157  @param table_name Table name to check.
158 
159  @returns true - If given table name is a system view.
160  @returns false - If table name is not a system view.
161  */
162  virtual bool is_system_view_name(const char *schema_name,
163  const char *table_name) const = 0;
164 
165  public:
166  // Destructor to cleanup data dictionary instance upon server shutdown.
167  virtual ~Dictionary() {}
168 };
169 
170 ///////////////////////////////////////////////////////////////////////////
171 
172 //
173 // MDL wrapper functions
174 //
175 
176 /**
177  @brief
178  Acquire shared metadata lock on the given table name with
179  explicit duration.
180 
181  @param thd - THD to which lock belongs to.
182  @param schema_name - Schema name
183  @param table_name - Table name
184  @param no_wait - Use try_acquire_lock() if no_wait is true.
185  else use acquire_lock() with
186  thd->variables.lock_wait_timeout timeout value.
187  @param out_mdl_ticket - This is a OUT parameter, a pointer to MDL_ticket
188  upon successful lock attempt.
189 
190 */
191 bool acquire_shared_table_mdl(THD *thd, const char *schema_name,
192  const char *table_name, bool no_wait,
193  MDL_ticket **out_mdl_ticket)
194  MY_ATTRIBUTE((warn_unused_result));
195 
196 /**
197  Predicate to check if we have a shared meta data lock on the
198  submitted schema qualified table name.
199 
200  @param thd Thread context.
201  @param schema_name Schema name.
202  @param table_name Table name.
203 
204  @retval true The thread context has a lock.
205  @retval false The thread context does not have a lock.
206 */
207 
208 bool has_shared_table_mdl(THD *thd, const char *schema_name,
209  const char *table_name);
210 
211 /**
212  Predicate to check if we have an exclusive meta data lock on the
213  submitted schema qualified table name.
214 
215  @param thd Thread context.
216  @param schema_name Schema name.
217  @param table_name Table name.
218 
219  @retval true The thread context has a lock.
220  @retval false The thread context does not have a lock.
221 */
222 
223 bool has_exclusive_table_mdl(THD *thd, const char *schema_name,
224  const char *table_name);
225 
226 /**
227  Acquire an exclusive metadata lock on the given tablespace name with
228  transaction duration.
229 
230  @param thd THD to which lock belongs.
231  @param tablespace_name Tablespace name
232  @param no_wait Use try_acquire_lock() if no_wait is true,
233  else use acquire_lock() with
234  thd->variables.lock_wait_timeout timeout value.
235  @param ticket ticket for request (optional out parameter)
236  @param for_trx true if MDL duration is MDL_TRANSACTION
237  false if MDL duration is MDL_EXPLICIT
238 
239  @retval true Failure, e.g. a lock wait timeout.
240  @retval false Successful lock acquisition.
241 */
242 
243 bool acquire_exclusive_tablespace_mdl(THD *thd, const char *tablespace_name,
244  bool no_wait,
245  MDL_ticket **ticket = nullptr,
246  bool for_trx = true)
247  MY_ATTRIBUTE((warn_unused_result));
248 
249 /**
250  Acquire a shared metadata lock on the given tablespace name with
251  transaction duration.
252 
253  @param thd THD to which lock belongs.
254  @param tablespace_name Tablespace name
255  @param no_wait Use try_acquire_lock() if no_wait is true,
256  else use acquire_lock() with
257  thd->variables.lock_wait_timeout timeout value.
258  @param ticket ticket for request (optional out parameter)
259  @param for_trx true if MDL duration is MDL_TRANSACTION
260  false if MDL duration is MDL_EXPLICIT
261 
262  @retval true Failure, e.g. a lock wait timeout.
263  @retval false Successful lock acquisition.
264 */
265 bool acquire_shared_tablespace_mdl(THD *thd, const char *tablespace_name,
266  bool no_wait, MDL_ticket **ticket = nullptr,
267  bool for_trx = true)
268  MY_ATTRIBUTE((warn_unused_result));
269 
270 /**
271  Predicate to check if we have a shared meta data lock on the
272  submitted tablespace name.
273 
274  @param thd Thread context.
275  @param tablespace_name Tablespace name.
276 
277  @retval true The thread context has a lock.
278  @retval false The thread context does not have a lock.
279 */
280 
281 bool has_shared_tablespace_mdl(THD *thd, const char *tablespace_name);
282 
283 /**
284  Predicate to check if we have an exclusive meta data lock on the
285  submitted tablespace name.
286 
287  @param thd Thread context.
288  @param tablespace_name Tablespace name.
289 
290  @retval true The thread context has a lock.
291  @retval false The thread context does not have a lock.
292 */
293 
294 bool has_exclusive_tablespace_mdl(THD *thd, const char *tablespace_name);
295 
296 /**
297  Acquire exclusive metadata lock on the given table name with
298  TRANSACTIONAL duration.
299 
300  @param[in] thd THD to which lock belongs to.
301  @param[in] schema_name Schema name
302  @param[in] table_name Table name
303  @param[in] no_wait Use try_acquire_lock() if no_wait is true.
304  else use acquire_lock() with
305  thd->variables.lock_wait_timeout timeout value.
306  @param[out] out_mdl_ticket A pointer to MDL_ticket upon successful lock
307  attempt.
308 */
309 
310 bool acquire_exclusive_table_mdl(THD *thd, const char *schema_name,
311  const char *table_name, bool no_wait,
312  MDL_ticket **out_mdl_ticket)
313  MY_ATTRIBUTE((warn_unused_result));
314 
315 /**
316  Acquire exclusive metadata lock on the given table name with
317  TRANSACTIONAL duration.
318 
319  @param[in] thd THD to which lock belongs to.
320  @param[in] schema_name Schema name
321  @param[in] table_name Table name
322  @param[in] lock_wait_timeout Time to wait.
323  @param[out] out_mdl_ticket A pointer to MDL_ticket upon successful lock
324  attempt.
325 */
326 
327 bool acquire_exclusive_table_mdl(THD *thd, const char *schema_name,
328  const char *table_name,
329  unsigned long int lock_wait_timeout,
330  MDL_ticket **out_mdl_ticket)
331  MY_ATTRIBUTE((warn_unused_result));
332 
333 /**
334  Acquire exclusive metadata lock on the given schema name with
335  explicit duration.
336 
337  @param[in] thd THD to which lock belongs to.
338  @param[in] schema_name Schema name
339  @param[in] no_wait Use try_acquire_lock() if no_wait is true.
340  else use acquire_lock() with
341  thd->variables.lock_wait_timeout timeout value.
342  @param[out] out_mdl_ticket A pointer to MDL_ticket upon successful lock
343  attempt.
344 */
345 
346 bool acquire_exclusive_schema_mdl(THD *thd, const char *schema_name,
347  bool no_wait, MDL_ticket **out_mdl_ticket)
348  MY_ATTRIBUTE((warn_unused_result));
349 
350 /**
351  @brief
352  Release MDL_EXPLICIT lock held by a ticket
353 
354  @param thd - THD to which lock belongs to.
355  @param mdl_ticket - Lock ticket.
356 
357 */
358 void release_mdl(THD *thd, MDL_ticket *mdl_ticket);
359 
360 /** Get Dictionary_client from THD object (the latter is opaque * in SEs). */
361 cache::Dictionary_client *get_dd_client(THD *thd);
362 
363 /**
364  Create plugin native table. The API would only write metadata to DD
365  and skip calling handler::create().
366 
367  @param[in] thd THD to which lock belongs to.
368  @param[in] pt Plugin_table* contain metadata of table to
369  be created.
370 
371  @returns false on success, otherwise true.
372 */
373 
374 bool create_native_table(THD *thd, const Plugin_table *pt);
375 
376 /**
377  Remove plugin native table from DD. The API would only update
378  metadata to DD and skip calling handler::drop().
379 
380  @param[in] thd THD to which lock belongs to.
381  @param[in] schema_name schema name which the table belongs to.
382  @param[in] table_name table name to be dropped.
383 
384  @returns false on success, otherwise true.
385 */
386 
387 bool drop_native_table(THD *thd, const char *schema_name,
388  const char *table_name);
389 
390 /**
391  Reset the tables and tablespace partitions in the DD cache,
392  and invalidate the entries in the DDSE cache.
393 
394  @note This is a temporary workaround to support proper recovery
395  after ha_recover().
396 
397  @returns false on success, otherwise true.
398 */
400 
401 /**
402  Update a tablespace change, commit and release transactional MDL.
403 
404  @param[in,out] thd Current thread context.
405  @param[in,out] space Tablespace to update and commit.
406  @param[in] error true for failure: Do rollback.
407  false for success: Do commit.
408  @param[in] release_mdl_on_commit_only release MDLs only on commit
409 
410  @retval true If error is true, or if failure in update or in commit.
411  @retval false Otherwise.
412 */
413 
415  THD *thd, dd::Tablespace *space, bool error,
416  bool release_mdl_on_commit_only = false);
417 
418 /**
419  Get the Object_table instance storing the given entity object type.
420 
421  We can return this as a reference since all relevant types for which
422  this template is used will indeed have a corresponding object table.
423 
424  @tparam Entity_object_type Type for which to get the object table.
425 
426  @returns reference to Object_table instance.
427 */
428 template <typename Entity_object_type>
429 const Object_table &get_dd_table();
430 
431 /**
432  Implicit tablespaces are renamed inside SE. But it is necessary to inform the
433  server layer about the rename, specifically which MDLs have been taken, so
434  that it can perform the necessary adjustment of MDLs when running in LOCK
435  TABLES mode.
436 
437  @param thd thread context
438  @param src ticket for old name
439  @param dst ticket for new name
440 */
441 void rename_tablespace_mdl_hook(THD *thd, MDL_ticket *src, MDL_ticket *dst);
442 
443 } // namespace dd
444 
445 #endif // DD__DICTIONARY_INCLUDED
cache::Dictionary_client * get_dd_client(THD *thd)
Get Dictionary_client from THD object (the latter is opaque * in SEs).
Definition: dictionary_impl.cc:536
A granted metadata lock.
Definition: mdl.h:962
bool acquire_exclusive_table_mdl(THD *thd, const char *schema_name, const char *table_name, bool no_wait, MDL_ticket **out_mdl_ticket)
Acquire exclusive metadata lock on the given table name with TRANSACTIONAL duration.
Definition: dictionary_impl.cc:503
virtual bool is_dd_table_name(const String_type &schema_name, const String_type &table_name) const =0
Check if given table name is a dictionary table name.
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:50
bool commit_or_rollback_tablespace_change(THD *thd, dd::Tablespace *space, bool error, bool release_mdl_on_commit_only=false)
Update a tablespace change, commit and release transactional MDL.
Definition: dictionary_impl.cc:654
void rename_tablespace_mdl_hook(THD *thd, MDL_ticket *src, MDL_ticket *dst)
Implicit tablespaces are renamed inside SE.
Definition: dictionary_impl.cc:688
bool acquire_shared_table_mdl(THD *thd, const char *schema_name, const char *table_name, bool no_wait, MDL_ticket **out_mdl_ticket)
Acquire shared metadata lock on the given table name with explicit duration.
Definition: dictionary_impl.cc:452
bool acquire_exclusive_tablespace_mdl(THD *thd, const char *tablespace_name, bool no_wait, MDL_ticket **ticket=nullptr, bool for_trx=true)
Acquire an exclusive metadata lock on the given tablespace name with transaction duration.
Definition: dictionary_impl.cc:472
virtual ~Dictionary()
Definition: dictionary.h:167
virtual int table_type_error_code(const String_type &schema_name, const String_type &table_name) const =0
Get the error code representing the type name string for a dictionary or system table.
virtual bool is_system_view_name(const char *schema_name, const char *table_name, bool *hidden) const =0
Check if given table name is a system view name.
bool reset_tables_and_tablespaces()
Reset the tables and tablespace partitions in the DD cache, and invalidate the entries in the DDSE ca...
Definition: dictionary_impl.cc:634
void release_mdl(THD *thd, MDL_ticket *mdl_ticket)
Release MDL_EXPLICIT lock held by a ticket.
Definition: dictionary_impl.cc:527
bool acquire_exclusive_schema_mdl(THD *thd, const char *schema_name, bool no_wait, MDL_ticket **out_mdl_ticket)
Acquire exclusive metadata lock on the given schema name with explicit duration.
Definition: dictionary_impl.cc:520
bool create_native_table(THD *thd, const Plugin_table *pt)
Create plugin native table.
Definition: dictionary_impl.cc:539
Main interface class enabling users to operate on data dictionary.
Definition: dictionary.h:51
Header for compiler-dependent features.
Class to hold information regarding a table to be created on behalf of a plugin.
Definition: plugin_table.h:39
#define true
Definition: config_static.h:44
virtual bool is_system_table_name(const String_type &schema_name, const String_type &table_name) const =0
Check if given table name is a system table name.
bool has_shared_table_mdl(THD *thd, const char *schema_name, const char *table_name)
Predicate to check if we have a shared meta data lock on the submitted schema qualified table name...
Definition: dictionary_impl.cc:460
This class represents all data dictionary table like mysql.tables, mysql.columns and more...
Definition: object_table.h:71
virtual bool is_dd_schema_name(const String_type &schema_name) const =0
Check if the given schema name is &#39;mysql&#39;, which where the DD tables are stored.
bool drop_native_table(THD *thd, const char *schema_name, const char *table_name)
Remove plugin native table from DD.
Definition: dictionary_impl.cc:598
virtual const Object_table * get_dd_table(const String_type &schema_name, const String_type &table_name) const =0
Get dictionary object for a given dictionary table name.
bool has_shared_tablespace_mdl(THD *thd, const char *tablespace_name)
Predicate to check if we have a shared meta data lock on the submitted tablespace name...
Definition: dictionary_impl.cc:491
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:39
Data structure that contains the information about shared tablespaces.
Definition: fsp0space.h:46
bool has_exclusive_table_mdl(THD *thd, const char *schema_name, const char *table_name)
Predicate to check if we have an exclusive meta data lock on the submitted schema qualified table nam...
Definition: dictionary_impl.cc:466
#define false
Definition: config_static.h:43
virtual bool is_dd_table_access_allowed(bool is_dd_internal_thread, bool is_ddl_statement, const char *schema_name, size_t schema_length, const char *table_name) const =0
Check if given table name can be accessed by the given thread type.
const Object_table & get_dd_table()
Get the Object_table instance storing the given entity object type.
Definition: dictionary_impl.cc:678
bool has_exclusive_tablespace_mdl(THD *thd, const char *tablespace_name)
Predicate to check if we have an exclusive meta data lock on the submitted tablespace name...
Definition: dictionary_impl.cc:497
bool acquire_shared_tablespace_mdl(THD *thd, const char *tablespace_name, bool no_wait, MDL_ticket **ticket=nullptr, bool for_trx=true)
Acquire a shared metadata lock on the given tablespace name with transaction duration.
Definition: dictionary_impl.cc:481
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:776
const char * table_name
Definition: rules_table_service.cc:55
Log error(cerr, "ERROR")