| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2007, 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/trx0i_s.h |
| 29 |
INFORMATION SCHEMA innodb_trx, innodb_locks and |
| 30 |
innodb_lock_waits tables cache structures and public |
| 31 |
functions. |
| 32 |
|
| 33 |
Created July 17, 2007 Vasil Dimov |
| 34 |
*******************************************************/ |
| 35 |
|
| 36 |
#ifndef trx0i_s_h |
| 37 |
#define trx0i_s_h |
| 38 |
|
| 39 |
#include <optional> |
| 40 |
|
| 41 |
#include "dict0types.h" |
| 42 |
#include "trx0types.h" |
| 43 |
#include "univ.i" |
| 44 |
|
| 45 |
class PSI_server_data_lock_container; |
| 46 |
|
| 47 |
/** The maximum amount of memory that can be consumed by innodb_trx, |
| 48 |
innodb_locks and innodb_lock_waits information schema tables. */ |
| 49 |
constexpr uint32_t TRX_I_S_MEM_LIMIT = 16777216; /* 16 MiB */ |
| 50 |
|
| 51 |
/** The maximum length of a string that can be stored in |
| 52 |
i_s_locks_row_t::lock_data */ |
| 53 |
constexpr uint32_t TRX_I_S_LOCK_DATA_MAX_LEN = 8192; |
| 54 |
|
| 55 |
/** The maximum length of a string that can be stored in |
| 56 |
i_s_trx_row_t::trx_query */ |
| 57 |
constexpr uint32_t TRX_I_S_TRX_QUERY_MAX_LEN = 1024; |
| 58 |
|
| 59 |
/** The maximum length of a string that can be stored in |
| 60 |
i_s_trx_row_t::trx_operation_state */ |
| 61 |
constexpr uint32_t TRX_I_S_TRX_OP_STATE_MAX_LEN = 64; |
| 62 |
|
| 63 |
/** The maximum length of a string that can be stored in |
| 64 |
i_s_trx_row_t::trx_foreign_key_error */ |
| 65 |
constexpr uint32_t TRX_I_S_TRX_FK_ERROR_MAX_LEN = 256; |
| 66 |
|
| 67 |
/** The maximum length of a string that can be stored in |
| 68 |
i_s_trx_row_t::trx_isolation_level */ |
| 69 |
constexpr uint32_t TRX_I_S_TRX_ISOLATION_LEVEL_MAX_LEN = 16; |
| 70 |
|
| 71 |
/** Safely copy strings in to the INNODB_TRX table's |
| 72 |
string based columns */ |
| 73 |
#define TRX_I_S_STRING_COPY(data, field, constraint, tcache) \ |
| 74 |
do { \ |
| 75 |
if (strlen(data) > constraint) { \ |
| 76 |
char buff[constraint + 1]; \ |
| 77 |
strncpy(buff, data, constraint); \ |
| 78 |
buff[constraint] = '\0'; \ |
| 79 |
\ |
| 80 |
field = static_cast<const char *>( \ |
| 81 |
ha_storage_put_memlim((tcache)->storage, buff, constraint + 1, \ |
| 82 |
MAX_ALLOWED_FOR_STORAGE(tcache))); \ |
| 83 |
} else { \ |
| 84 |
field = static_cast<const char *>(ha_storage_put_str_memlim( \ |
| 85 |
(tcache)->storage, data, MAX_ALLOWED_FOR_STORAGE(tcache))); \ |
| 86 |
} \ |
| 87 |
} while (0) |
| 88 |
|
| 89 |
/** This structure represents INFORMATION_SCHEMA.innodb_locks row */ |
| 90 |
struct i_s_locks_row_t { |
| 91 |
/** The unique identifier of this lock, which does not change over time for a |
| 92 |
single lock request and no two lock requests ever have the same guid. */ |
| 93 |
lock_guid_t lock_guid; |
| 94 |
|
| 95 |
/** Information for record locks. All these are |
| 96 |
ULINT_UNDEFINED for table locks. */ |
| 97 |
/** @{ */ |
| 98 |
space_id_t lock_space; /*!< tablespace identifier */ |
| 99 |
page_no_t lock_page; /*!< page number within the_space */ |
| 100 |
ulint lock_rec; /*!< heap number of the record |
| 101 |
on the page */ |
| 102 |
/** @} */ |
| 103 |
|
| 104 |
/** The following are auxiliary and not included in the table */ |
| 105 |
/** @{ */ |
| 106 |
table_id_t lock_table_id; |
| 107 |
/*!< table identifier from |
| 108 |
lock_get_table_id */ |
| 109 |
/** @} */ |
| 110 |
}; |
| 111 |
|
| 112 |
/** This structure represents INFORMATION_SCHEMA.innodb_trx row */ |
| 113 |
struct i_s_trx_row_t { |
| 114 |
trx_id_t trx_id; /*!< transaction identifier */ |
| 115 |
const char *trx_state; /*!< transaction state from |
| 116 |
trx_get_que_state_str() */ |
| 117 |
std::chrono::system_clock::time_point trx_started; /*!< trx_t::start_time */ |
| 118 |
const i_s_locks_row_t *requested_lock_row; |
| 119 |
/*!< pointer to a row |
| 120 |
in innodb_locks if trx |
| 121 |
is waiting, or NULL */ |
| 122 |
|
| 123 |
/** The value of trx->lock.wait_started */ |
| 124 |
std::chrono::system_clock::time_point trx_wait_started; |
| 125 |
/** The value of TRX_WEIGHT(trx) */ |
| 126 |
uintmax_t trx_weight; |
| 127 |
/** If `first` is `true` then `second` is the value of the |
| 128 |
trx->lock.schedule_weight, otherwise the `second` should be ignored and |
| 129 |
displayed as NULL to the end user. |
| 130 |
(This could be std::optional once we move to C++17) */ |
| 131 |
std::pair<bool, trx_schedule_weight_t> trx_schedule_weight; |
| 132 |
ulint trx_mysql_thread_id; /*!< thd_get_thread_id() */ |
| 133 |
const char *trx_query; /*!< MySQL statement being |
| 134 |
executed in the transaction */ |
| 135 |
const CHARSET_INFO *trx_query_cs; /*!< the charset of trx_query */ |
| 136 |
const char *trx_operation_state; /*!< trx_t::op_info */ |
| 137 |
ulint trx_tables_in_use; /*!< n_mysql_tables_in_use in |
| 138 |
trx_t */ |
| 139 |
ulint trx_tables_locked; |
| 140 |
/*!< mysql_n_tables_locked in |
| 141 |
trx_t */ |
| 142 |
ulint trx_lock_structs; /*!< list len of trx_locks in |
| 143 |
trx_t */ |
| 144 |
ulint trx_lock_memory_bytes; |
| 145 |
/*!< mem_heap_get_size( |
| 146 |
trx->lock_heap) */ |
| 147 |
ulint trx_rows_locked; /*!< lock_number_of_rows_locked() */ |
| 148 |
uintmax_t trx_rows_modified; /*!< trx_t::undo_no */ |
| 149 |
ulint trx_concurrency_tickets; |
| 150 |
/*!< n_tickets_to_enter_innodb in |
| 151 |
trx_t */ |
| 152 |
const char *trx_isolation_level; |
| 153 |
/*!< isolation_level in trx_t */ |
| 154 |
bool trx_unique_checks; |
| 155 |
/*!< check_unique_secondary in trx_t*/ |
| 156 |
bool trx_foreign_key_checks; |
| 157 |
/*!< check_foreigns in trx_t */ |
| 158 |
const char *trx_foreign_key_error; |
| 159 |
/*!< detailed_error in trx_t */ |
| 160 |
bool trx_has_search_latch; |
| 161 |
/*!< has_search_latch in trx_t */ |
| 162 |
ulint trx_is_read_only; |
| 163 |
/*!< trx_t::read_only */ |
| 164 |
ulint trx_is_autocommit_non_locking; |
| 165 |
/*!< trx_is_autocommit_non_locking(trx) |
| 166 |
*/ |
| 167 |
}; |
| 168 |
|
| 169 |
/** Cache of INFORMATION_SCHEMA table data */ |
| 170 |
struct trx_i_s_cache_t; |
| 171 |
|
| 172 |
/** Auxiliary enum used by functions that need to select one of the |
| 173 |
INFORMATION_SCHEMA tables */ |
| 174 |
enum i_s_table { |
| 175 |
I_S_INNODB_TRX, /*!< INFORMATION_SCHEMA.innodb_trx */ |
| 176 |
}; |
| 177 |
|
| 178 |
/** This is the intermediate buffer where data needed to fill the |
| 179 |
INFORMATION SCHEMA tables is fetched and later retrieved by the C++ |
| 180 |
code in handler/i_s.cc. */ |
| 181 |
extern trx_i_s_cache_t *trx_i_s_cache; |
| 182 |
|
| 183 |
/** Initialize INFORMATION SCHEMA trx related cache. */ |
| 184 |
void trx_i_s_cache_init(trx_i_s_cache_t *cache); /*!< out: cache to init */ |
| 185 |
/** Free the INFORMATION SCHEMA trx related cache. */ |
| 186 |
void trx_i_s_cache_free(trx_i_s_cache_t *cache); /*!< in/out: cache to free */ |
| 187 |
|
| 188 |
/** Issue a shared/read lock on the tables cache. */ |
| 189 |
void trx_i_s_cache_start_read(trx_i_s_cache_t *cache); /*!< in: cache */ |
| 190 |
|
| 191 |
/** Release a shared/read lock on the tables cache. */ |
| 192 |
void trx_i_s_cache_end_read(trx_i_s_cache_t *cache); /*!< in: cache */ |
| 193 |
|
| 194 |
/** Issue an exclusive/write lock on the tables cache. */ |
| 195 |
void trx_i_s_cache_start_write(trx_i_s_cache_t *cache); /*!< in: cache */ |
| 196 |
|
| 197 |
/** Release an exclusive/write lock on the tables cache. */ |
| 198 |
void trx_i_s_cache_end_write(trx_i_s_cache_t *cache); /*!< in: cache */ |
| 199 |
|
| 200 |
/** Retrieves the number of used rows in the cache for a given |
| 201 |
INFORMATION SCHEMA table. |
| 202 |
@return number of rows */ |
| 203 |
ulint trx_i_s_cache_get_rows_used(trx_i_s_cache_t *cache, /*!< in: cache */ |
| 204 |
enum i_s_table table); /*!< in: which table */ |
| 205 |
|
| 206 |
/** Retrieves the nth row in the cache for a given INFORMATION SCHEMA |
| 207 |
table. |
| 208 |
@return row */ |
| 209 |
void *trx_i_s_cache_get_nth_row(trx_i_s_cache_t *cache, /*!< in: cache */ |
| 210 |
enum i_s_table table, /*!< in: which table */ |
| 211 |
ulint n); /*!< in: row number */ |
| 212 |
|
| 213 |
/** Update the transactions cache if it has not been read for some time. |
| 214 |
@return 0 - fetched, 1 - not */ |
| 215 |
int trx_i_s_possibly_fetch_data_into_cache( |
| 216 |
trx_i_s_cache_t *cache); /*!< in/out: cache */ |
| 217 |
|
| 218 |
/** Returns true if the data in the cache is truncated due to the memory |
| 219 |
limit posed by TRX_I_S_MEM_LIMIT. |
| 220 |
@param[in] cache The cache |
| 221 |
@return true if truncated */ |
| 222 |
bool trx_i_s_cache_is_truncated(trx_i_s_cache_t *cache); |
| 223 |
|
| 224 |
/** The maximum length of a resulting lock_id_size in |
| 225 |
trx_i_s_create_lock_id(), not including the terminating NUL. |
| 226 |
"%lu:%lu:%lu:%lu:%lu:%lu" -> 20*6+5 chars */ |
| 227 |
constexpr uint32_t TRX_I_S_LOCK_ID_MAX_LEN = 20 * 6 + 5; |
| 228 |
|
| 229 |
/** Crafts a lock id string from a i_s_locks_row_t object. Returns its |
| 230 |
second argument. This function aborts if there is not enough space in |
| 231 |
lock_id. Be sure to provide at least TRX_I_S_LOCK_ID_MAX_LEN + 1 if you |
| 232 |
want to be 100% sure that it will not abort. |
| 233 |
@param[in] row |
| 234 |
The description of the lock sufficient to populate a row |
| 235 |
@param[out] lock_id |
| 236 |
The buffer to store the resulting lock_id |
| 237 |
@param[in] lock_id_size |
| 238 |
The size of the lock_id buffer |
| 239 |
@return resulting lock id */ |
| 240 |
char *trx_i_s_create_lock_id(const i_s_locks_row_t &row, char *lock_id, |
| 241 |
size_t lock_id_size); |
| 242 |
|
| 243 |
/** Fill performance schema lock data. |
| 244 |
Create a string that represents the LOCK_DATA |
| 245 |
column, for a given lock record. |
| 246 |
@param[out] lock_data Lock data string |
| 247 |
@param[in] lock Lock to inspect |
| 248 |
@param[in] heap_no Lock heap number |
| 249 |
@param[in] container Data container to fill |
| 250 |
*/ |
| 251 |
void p_s_fill_lock_data(const char **lock_data, const lock_t *lock, |
| 252 |
ulint heap_no, |
| 253 |
PSI_server_data_lock_container *container); |
| 254 |
|
| 255 |
/** Fills i_s_locks_row_t object with data about the lock. |
| 256 |
@param[out] row Result object that's filled |
| 257 |
@param[in] lock Lock to get data from |
| 258 |
@param[in] heap_no Lock's record number or ULINT_UNDEFINED if the lock is a |
| 259 |
table lock */ |
| 260 |
void fill_locks_row(i_s_locks_row_t *row, const lock_t *lock, ulint heap_no); |
| 261 |
|
| 262 |
/** Parses lock id into row |
| 263 |
@param[in] lock_id Lock id generated with trx_i_s_create_lock_id |
| 264 |
@param[out] row Row to be filled in with data |
| 265 |
@return LOCK_REC, LOCK_TABLE or 0 if failed to parse */ |
| 266 |
int trx_i_s_parse_lock_id(const char *lock_id, i_s_locks_row_t *row); |
| 267 |
#endif /* trx0i_s_h */ |