MySQL 8.3.0
Source Code Documentation
trx0i_s.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2007, 2023, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/trx0i_s.h
28 INFORMATION SCHEMA innodb_trx, innodb_locks and
29 innodb_lock_waits tables cache structures and public
30 functions.
31
32 Created July 17, 2007 Vasil Dimov
33 *******************************************************/
34
35#ifndef trx0i_s_h
36#define trx0i_s_h
37
38#include <optional>
39
40#include "dict0types.h"
41#include "lock0types.h"
42#include "trx0types.h"
43#include "univ.i"
44
46
47struct CHARSET_INFO;
48
49/** The maximum amount of memory that can be consumed by innodb_trx,
50innodb_locks and innodb_lock_waits information schema tables. */
51constexpr uint32_t TRX_I_S_MEM_LIMIT = 16777216; /* 16 MiB */
52
53/** The maximum length of a string that can be stored in
54i_s_locks_row_t::lock_data */
55constexpr uint32_t TRX_I_S_LOCK_DATA_MAX_LEN = 8192;
56
57/** The maximum length of a string that can be stored in
58i_s_trx_row_t::trx_query */
59constexpr uint32_t TRX_I_S_TRX_QUERY_MAX_LEN = 1024;
60
61/** The maximum length of a string that can be stored in
62i_s_trx_row_t::trx_operation_state */
63constexpr uint32_t TRX_I_S_TRX_OP_STATE_MAX_LEN = 64;
64
65/** The maximum length of a string that can be stored in
66i_s_trx_row_t::trx_foreign_key_error */
67constexpr uint32_t TRX_I_S_TRX_FK_ERROR_MAX_LEN = 256;
68
69/** The maximum length of a string that can be stored in
70i_s_trx_row_t::trx_isolation_level */
72
73/** Safely copy strings in to the INNODB_TRX table's
74string based columns */
75#define TRX_I_S_STRING_COPY(data, field, constraint, tcache) \
76 do { \
77 if (strlen(data) > constraint) { \
78 char buff[constraint + 1]; \
79 strncpy(buff, data, constraint); \
80 buff[constraint] = '\0'; \
81 \
82 field = static_cast<const char *>( \
83 ha_storage_put_memlim((tcache)->storage, buff, constraint + 1, \
84 MAX_ALLOWED_FOR_STORAGE(tcache))); \
85 } else { \
86 field = static_cast<const char *>(ha_storage_put_str_memlim( \
87 (tcache)->storage, data, MAX_ALLOWED_FOR_STORAGE(tcache))); \
88 } \
89 } while (0)
90
91/** This structure represents INFORMATION_SCHEMA.innodb_locks row */
93 uint64_t lock_trx_immutable_id; /*!< transaction address as integer. We need
94 an id which is unique and does not change over time.
95 Unfortunately trx->id is initially equal to 0 for
96 all trxs which still appear to be read only, and it
97 changes to non-zero, once trx needs to perform write.
98 For this reason trx->id is not good enough for our
99 purpose. */
100 uint64_t lock_immutable_id; /*!< lock address as integer. We need to identify
101 the lock in unique way. Specifying space, page and heap_no
102 and trx is not enough, because there could be locks with
103 different modes. Using mode as part of id is not good,
104 because we sometimes change the mode of the lock (for
105 example when granting the lock we drop LOCK_WAITING flag
106 and in lock_trx_release_read_locks we add LOCK_REC_NOT_GAP
107 flag). The only permanent thing is then the address.
108 We use both lock_immutable_id and lock_trx_immutable_id
109 even though lock_immutable_id is unique, because we need
110 to be able to locate the row in PERFORMANCE_SCHEMA based
111 on the id, and we need a way to verify that the
112 lock_immutable_id is safe to dereference. Simplest way to
113 do that is to check that trx still has the lock on its
114 list of locks. */
115
116 /** Information for record locks. All these are
117 ULINT_UNDEFINED for table locks. */
118 /** @{ */
119 space_id_t lock_space; /*!< tablespace identifier */
120 page_no_t lock_page; /*!< page number within the_space */
121 ulint lock_rec; /*!< heap number of the record
122 on the page */
123 /** @} */
124
125 /** The following are auxiliary and not included in the table */
126 /** @{ */
128 /*!< table identifier from
129 lock_get_table_id */
130 /** @} */
131};
132
133/** This structure represents INFORMATION_SCHEMA.innodb_trx row */
135 trx_id_t trx_id; /*!< transaction identifier */
136 const char *trx_state; /*!< transaction state from
137 trx_get_que_state_str() */
138 std::chrono::system_clock::time_point trx_started; /*!< trx_t::start_time */
140 /*!< pointer to a row
141 in innodb_locks if trx
142 is waiting, or NULL */
143
144 /** The value of trx->lock.wait_started */
145 std::chrono::system_clock::time_point trx_wait_started;
146 /** The value of TRX_WEIGHT(trx) */
147 uintmax_t trx_weight;
148 /** If `first` is `true` then `second` is the value of the
149 trx->lock.schedule_weight, otherwise the `second` should be ignored and
150 displayed as NULL to the end user.
151 (This could be std::optional once we move to C++17) */
152 std::pair<bool, trx_schedule_weight_t> trx_schedule_weight;
153 ulint trx_mysql_thread_id; /*!< thd_get_thread_id() */
154 const char *trx_query; /*!< MySQL statement being
155 executed in the transaction */
156 const CHARSET_INFO *trx_query_cs; /*!< the charset of trx_query */
157 const char *trx_operation_state; /*!< trx_t::op_info */
158 ulint trx_tables_in_use; /*!< n_mysql_tables_in_use in
159 trx_t */
161 /*!< mysql_n_tables_locked in
162 trx_t */
163 ulint trx_lock_structs; /*!< list len of trx_locks in
164 trx_t */
166 /*!< mem_heap_get_size(
167 trx->lock_heap) */
168 ulint trx_rows_locked; /*!< lock_number_of_rows_locked() */
169 uintmax_t trx_rows_modified; /*!< trx_t::undo_no */
171 /*!< n_tickets_to_enter_innodb in
172 trx_t */
174 /*!< isolation_level in trx_t */
176 /*!< check_unique_secondary in trx_t*/
178 /*!< check_foreigns in trx_t */
180 /*!< detailed_error in trx_t */
182 /*!< has_search_latch in trx_t */
184 /*!< trx_t::read_only */
186 /*!< trx_is_autocommit_non_locking(trx)
187 */
188};
189
190/** Cache of INFORMATION_SCHEMA table data */
191struct trx_i_s_cache_t;
192
193/** Auxiliary enum used by functions that need to select one of the
194INFORMATION_SCHEMA tables */
196 I_S_INNODB_TRX, /*!< INFORMATION_SCHEMA.innodb_trx */
197};
198
199/** This is the intermediate buffer where data needed to fill the
200INFORMATION SCHEMA tables is fetched and later retrieved by the C++
201code in handler/i_s.cc. */
203
204/** Initialize INFORMATION SCHEMA trx related cache. */
205void trx_i_s_cache_init(trx_i_s_cache_t *cache); /*!< out: cache to init */
206/** Free the INFORMATION SCHEMA trx related cache. */
207void trx_i_s_cache_free(trx_i_s_cache_t *cache); /*!< in/out: cache to free */
208
209/** Issue a shared/read lock on the tables cache. */
210void trx_i_s_cache_start_read(trx_i_s_cache_t *cache); /*!< in: cache */
211
212/** Release a shared/read lock on the tables cache. */
213void trx_i_s_cache_end_read(trx_i_s_cache_t *cache); /*!< in: cache */
214
215/** Issue an exclusive/write lock on the tables cache. */
216void trx_i_s_cache_start_write(trx_i_s_cache_t *cache); /*!< in: cache */
217
218/** Release an exclusive/write lock on the tables cache. */
219void trx_i_s_cache_end_write(trx_i_s_cache_t *cache); /*!< in: cache */
220
221/** Retrieves the number of used rows in the cache for a given
222 INFORMATION SCHEMA table.
223 @return number of rows */
224ulint trx_i_s_cache_get_rows_used(trx_i_s_cache_t *cache, /*!< in: cache */
225 enum i_s_table table); /*!< in: which table */
226
227/** Retrieves the nth row in the cache for a given INFORMATION SCHEMA
228 table.
229 @return row */
230void *trx_i_s_cache_get_nth_row(trx_i_s_cache_t *cache, /*!< in: cache */
231 enum i_s_table table, /*!< in: which table */
232 ulint n); /*!< in: row number */
233
234/** Update the transactions cache if it has not been read for some time.
235 @return 0 - fetched, 1 - not */
237 trx_i_s_cache_t *cache); /*!< in/out: cache */
238
239/** Returns true if the data in the cache is truncated due to the memory
240 limit posed by TRX_I_S_MEM_LIMIT.
241 @param[in] cache The cache
242 @return true if truncated */
244
245/** The maximum length of a resulting lock_id_size in
246trx_i_s_create_lock_id(), not including the terminating NUL.
247"%lu:%lu:%lu:%lu:%lu" -> 20*5+4 chars */
248constexpr uint32_t TRX_I_S_LOCK_ID_MAX_LEN = 20 * 5 + 4;
249
250/** Crafts a lock id string from a i_s_locks_row_t object. Returns its
251 second argument. This function aborts if there is not enough space in
252 lock_id. Be sure to provide at least TRX_I_S_LOCK_ID_MAX_LEN + 1 if you
253 want to be 100% sure that it will not abort.
254 @return resulting lock id */
256 const i_s_locks_row_t *row, /*!< in: innodb_locks row */
257 char *lock_id, /*!< out: resulting lock_id */
258 ulint lock_id_size); /*!< in: size of the lock id
259 buffer */
260
261/** Fill performance schema lock data.
262Create a string that represents the LOCK_DATA
263column, for a given lock record.
264@param[out] lock_data Lock data string
265@param[in] lock Lock to inspect
266@param[in] heap_no Lock heap number
267@param[in] container Data container to fill
268*/
269void p_s_fill_lock_data(const char **lock_data, const lock_t *lock,
270 ulint heap_no,
272
273/** Fills i_s_locks_row_t object with data about the lock.
274@param[out] row Result object that's filled
275@param[in] lock Lock to get data from
276@param[in] heap_no Lock's record number or ULINT_UNDEFINED if the lock is a
277 table lock */
278void fill_locks_row(i_s_locks_row_t *row, const lock_t *lock, ulint heap_no);
279
280/** Parses lock id into row
281@param[in] lock_id Lock id generated with trx_i_s_create_lock_id
282@param[out] row Row to be filled in with data
283@return LOCK_REC, LOCK_TABLE or 0 if failed to parse */
284int trx_i_s_parse_lock_id(const char *lock_id, i_s_locks_row_t *row);
285#endif /* trx0i_s_h */
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:46
uint32_t page_no_t
Page number.
Definition: api0api.h:44
Server interface, row lock container.
Definition: psi_data_lock.h:116
Data dictionary global types.
ib_id_t table_id_t
Table or partition identifier (unique within an InnoDB instance).
Definition: dict0types.h:217
The transaction lock system global types.
static PFS_engine_table_share_proxy table
Definition: pfs.cc:60
Definition: atomics_array.h:38
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:78
Definition: m_ctype.h:422
This structure represents INFORMATION_SCHEMA.innodb_locks row.
Definition: trx0i_s.h:92
uint64_t lock_immutable_id
lock address as integer.
Definition: trx0i_s.h:100
space_id_t lock_space
Information for record locks.
Definition: trx0i_s.h:119
ulint lock_rec
heap number of the record on the page
Definition: trx0i_s.h:121
table_id_t lock_table_id
The following are auxiliary and not included in the table.
Definition: trx0i_s.h:127
uint64_t lock_trx_immutable_id
transaction address as integer.
Definition: trx0i_s.h:93
page_no_t lock_page
page number within the_space
Definition: trx0i_s.h:120
This structure represents INFORMATION_SCHEMA.innodb_trx row.
Definition: trx0i_s.h:134
ulint trx_lock_memory_bytes
mem_heap_get_size( trx->lock_heap)
Definition: trx0i_s.h:165
ulint trx_rows_locked
lock_number_of_rows_locked()
Definition: trx0i_s.h:168
std::chrono::system_clock::time_point trx_started
trx_t::start_time
Definition: trx0i_s.h:138
const char * trx_isolation_level
isolation_level in trx_t
Definition: trx0i_s.h:173
bool trx_unique_checks
check_unique_secondary in trx_t
Definition: trx0i_s.h:175
std::pair< bool, trx_schedule_weight_t > trx_schedule_weight
If first is true then second is the value of the trx->lock.schedule_weight, otherwise the second shou...
Definition: trx0i_s.h:152
ulint trx_tables_locked
mysql_n_tables_locked in trx_t
Definition: trx0i_s.h:160
const char * trx_state
transaction state from trx_get_que_state_str()
Definition: trx0i_s.h:136
ulint trx_concurrency_tickets
n_tickets_to_enter_innodb in trx_t
Definition: trx0i_s.h:170
bool trx_has_search_latch
has_search_latch in trx_t
Definition: trx0i_s.h:181
const char * trx_foreign_key_error
detailed_error in trx_t
Definition: trx0i_s.h:179
const CHARSET_INFO * trx_query_cs
the charset of trx_query
Definition: trx0i_s.h:156
trx_id_t trx_id
transaction identifier
Definition: trx0i_s.h:135
const char * trx_operation_state
trx_t::op_info
Definition: trx0i_s.h:157
ulint trx_mysql_thread_id
thd_get_thread_id()
Definition: trx0i_s.h:153
ulint trx_is_read_only
trx_t::read_only
Definition: trx0i_s.h:183
ulint trx_lock_structs
list len of trx_locks in trx_t
Definition: trx0i_s.h:163
ulint trx_tables_in_use
n_mysql_tables_in_use in trx_t
Definition: trx0i_s.h:158
bool trx_foreign_key_checks
check_foreigns in trx_t
Definition: trx0i_s.h:177
ulint trx_is_autocommit_non_locking
trx_is_autocommit_non_locking(trx)
Definition: trx0i_s.h:185
const char * trx_query
MySQL statement being executed in the transaction.
Definition: trx0i_s.h:154
std::chrono::system_clock::time_point trx_wait_started
The value of trx->lock.wait_started.
Definition: trx0i_s.h:145
const i_s_locks_row_t * requested_lock_row
pointer to a row in innodb_locks if trx is waiting, or NULL
Definition: trx0i_s.h:139
uintmax_t trx_weight
The value of TRX_WEIGHT(trx)
Definition: trx0i_s.h:147
uintmax_t trx_rows_modified
trx_t::undo_no
Definition: trx0i_s.h:169
Lock struct; protected by lock_sys latches.
Definition: lock0priv.h:135
This structure describes the intermediate buffer.
Definition: trx0i_s.cc:130
void trx_i_s_cache_end_read(trx_i_s_cache_t *cache)
Release a shared/read lock on the tables cache.
Definition: trx0i_s.cc:994
void trx_i_s_cache_start_read(trx_i_s_cache_t *cache)
Issue a shared/read lock on the tables cache.
Definition: trx0i_s.cc:988
char * trx_i_s_create_lock_id(const i_s_locks_row_t *row, char *lock_id, ulint lock_id_size)
Crafts a lock id string from a i_s_locks_row_t object.
Definition: trx0i_s.cc:1091
constexpr uint32_t TRX_I_S_MEM_LIMIT
The maximum amount of memory that can be consumed by innodb_trx, innodb_locks and innodb_lock_waits i...
Definition: trx0i_s.h:51
void * trx_i_s_cache_get_nth_row(trx_i_s_cache_t *cache, enum i_s_table table, ulint n)
Retrieves the nth row in the cache for a given INFORMATION SCHEMA table.
Definition: trx0i_s.cc:1056
void trx_i_s_cache_start_write(trx_i_s_cache_t *cache)
Issue an exclusive/write lock on the tables cache.
Definition: trx0i_s.cc:1005
void fill_locks_row(i_s_locks_row_t *row, const lock_t *lock, ulint heap_no)
Fills i_s_locks_row_t object with data about the lock.
Definition: trx0i_s.cc:647
bool trx_i_s_cache_is_truncated(trx_i_s_cache_t *cache)
Returns true if the data in the cache is truncated due to the memory limit posed by TRX_I_S_MEM_LIMIT...
Definition: trx0i_s.cc:939
constexpr uint32_t TRX_I_S_TRX_FK_ERROR_MAX_LEN
The maximum length of a string that can be stored in i_s_trx_row_t::trx_foreign_key_error.
Definition: trx0i_s.h:67
trx_i_s_cache_t * trx_i_s_cache
This is the intermediate buffer where data needed to fill the INFORMATION SCHEMA tables is fetched an...
Definition: trx0i_s.cc:160
constexpr uint32_t TRX_I_S_TRX_ISOLATION_LEVEL_MAX_LEN
The maximum length of a string that can be stored in i_s_trx_row_t::trx_isolation_level.
Definition: trx0i_s.h:71
ulint trx_i_s_cache_get_rows_used(trx_i_s_cache_t *cache, enum i_s_table table)
Retrieves the number of used rows in the cache for a given INFORMATION SCHEMA table.
Definition: trx0i_s.cc:1043
int trx_i_s_possibly_fetch_data_into_cache(trx_i_s_cache_t *cache)
Update the transactions cache if it has not been read for some time.
Definition: trx0i_s.cc:918
constexpr uint32_t TRX_I_S_TRX_OP_STATE_MAX_LEN
The maximum length of a string that can be stored in i_s_trx_row_t::trx_operation_state.
Definition: trx0i_s.h:63
constexpr uint32_t TRX_I_S_TRX_QUERY_MAX_LEN
The maximum length of a string that can be stored in i_s_trx_row_t::trx_query.
Definition: trx0i_s.h:59
void trx_i_s_cache_end_write(trx_i_s_cache_t *cache)
Release an exclusive/write lock on the tables cache.
Definition: trx0i_s.cc:1011
constexpr uint32_t TRX_I_S_LOCK_ID_MAX_LEN
The maximum length of a resulting lock_id_size in trx_i_s_create_lock_id(), not including the termina...
Definition: trx0i_s.h:248
void trx_i_s_cache_free(trx_i_s_cache_t *cache)
Free the INFORMATION SCHEMA trx related cache.
Definition: trx0i_s.cc:976
int trx_i_s_parse_lock_id(const char *lock_id, i_s_locks_row_t *row)
Parses lock id into row.
Definition: trx0i_s.cc:1121
i_s_table
Auxiliary enum used by functions that need to select one of the INFORMATION_SCHEMA tables.
Definition: trx0i_s.h:195
@ I_S_INNODB_TRX
INFORMATION_SCHEMA.innodb_trx.
Definition: trx0i_s.h:196
constexpr uint32_t TRX_I_S_LOCK_DATA_MAX_LEN
The maximum length of a string that can be stored in i_s_locks_row_t::lock_data.
Definition: trx0i_s.h:55
void p_s_fill_lock_data(const char **lock_data, const lock_t *lock, ulint heap_no, PSI_server_data_lock_container *container)
Fill performance schema lock data.
Definition: trx0i_s.cc:583
void trx_i_s_cache_init(trx_i_s_cache_t *cache)
Initialize INFORMATION SCHEMA trx related cache.
Definition: trx0i_s.cc:944
Transaction system global type definitions.
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:137
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:405
int n
Definition: xcom_base.cc:508