MySQL 8.0.31
Source Code Documentation
trx0i_s.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2007, 2022, 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 "trx0types.h"
42#include "univ.i"
43
45
46/** The maximum amount of memory that can be consumed by innodb_trx,
47innodb_locks and innodb_lock_waits information schema tables. */
48constexpr uint32_t TRX_I_S_MEM_LIMIT = 16777216; /* 16 MiB */
49
50/** The maximum length of a string that can be stored in
51i_s_locks_row_t::lock_data */
52constexpr uint32_t TRX_I_S_LOCK_DATA_MAX_LEN = 8192;
53
54/** The maximum length of a string that can be stored in
55i_s_trx_row_t::trx_query */
56constexpr uint32_t TRX_I_S_TRX_QUERY_MAX_LEN = 1024;
57
58/** The maximum length of a string that can be stored in
59i_s_trx_row_t::trx_operation_state */
60constexpr uint32_t TRX_I_S_TRX_OP_STATE_MAX_LEN = 64;
61
62/** The maximum length of a string that can be stored in
63i_s_trx_row_t::trx_foreign_key_error */
64constexpr uint32_t TRX_I_S_TRX_FK_ERROR_MAX_LEN = 256;
65
66/** The maximum length of a string that can be stored in
67i_s_trx_row_t::trx_isolation_level */
69
70/** Safely copy strings in to the INNODB_TRX table's
71string based columns */
72#define TRX_I_S_STRING_COPY(data, field, constraint, tcache) \
73 do { \
74 if (strlen(data) > constraint) { \
75 char buff[constraint + 1]; \
76 strncpy(buff, data, constraint); \
77 buff[constraint] = '\0'; \
78 \
79 field = static_cast<const char *>( \
80 ha_storage_put_memlim((tcache)->storage, buff, constraint + 1, \
81 MAX_ALLOWED_FOR_STORAGE(tcache))); \
82 } else { \
83 field = static_cast<const char *>(ha_storage_put_str_memlim( \
84 (tcache)->storage, data, MAX_ALLOWED_FOR_STORAGE(tcache))); \
85 } \
86 } while (0)
87
88/** This structure represents INFORMATION_SCHEMA.innodb_locks row */
90 uint64_t lock_trx_immutable_id; /*!< transaction address as integer. We need
91 an id which is unique and does not change over time.
92 Unfortunately trx->id is initially equal to 0 for
93 all trxs which still appear to be read only, and it
94 changes to non-zero, once trx needs to perform write.
95 For this reason trx->id is not good enough for our
96 purpose. */
97 uint64_t lock_immutable_id; /*!< lock address as integer. We need to identify
98 the lock in unique way. Specifying space, page and heap_no
99 and trx is not enough, because there could be locks with
100 different modes. Using mode as part of id is not good,
101 because we sometimes change the mode of the lock (for
102 example when granting the lock we drop LOCK_WAITING flag
103 and in lock_trx_release_read_locks we add LOCK_REC_NOT_GAP
104 flag). The only permanent thing is then the address.
105 We use both lock_immutable_id and lock_trx_immutable_id
106 even though lock_immutable_id is unique, because we need
107 to be able to locate the row in PERFORMANCE_SCHEMA based
108 on the id, and we need a way to verify that the
109 lock_immutable_id is safe to dereference. Simplest way to
110 do that is to check that trx still has the lock on its
111 list of locks. */
112
113 /** Information for record locks. All these are
114 ULINT_UNDEFINED for table locks. */
115 /** @{ */
116 space_id_t lock_space; /*!< tablespace identifier */
117 page_no_t lock_page; /*!< page number within the_space */
118 ulint lock_rec; /*!< heap number of the record
119 on the page */
120 /** @} */
121
122 /** The following are auxiliary and not included in the table */
123 /** @{ */
125 /*!< table identifier from
126 lock_get_table_id */
127 /** @} */
128};
129
130/** This structure represents INFORMATION_SCHEMA.innodb_trx row */
132 trx_id_t trx_id; /*!< transaction identifier */
133 const char *trx_state; /*!< transaction state from
134 trx_get_que_state_str() */
135 std::chrono::system_clock::time_point trx_started; /*!< trx_t::start_time */
137 /*!< pointer to a row
138 in innodb_locks if trx
139 is waiting, or NULL */
140
141 /** The value of trx->lock.wait_started */
142 std::chrono::system_clock::time_point trx_wait_started;
143 /** The value of TRX_WEIGHT(trx) */
144 uintmax_t trx_weight;
145 /** If `first` is `true` then `second` is the value of the
146 trx->lock.schedule_weight, otherwise the `second` should be ignored and
147 displayed as NULL to the end user.
148 (This could be std::optional once we move to C++17) */
149 std::pair<bool, trx_schedule_weight_t> trx_schedule_weight;
150 ulint trx_mysql_thread_id; /*!< thd_get_thread_id() */
151 const char *trx_query; /*!< MySQL statement being
152 executed in the transaction */
153 const CHARSET_INFO *trx_query_cs; /*!< the charset of trx_query */
154 const char *trx_operation_state; /*!< trx_t::op_info */
155 ulint trx_tables_in_use; /*!< n_mysql_tables_in_use in
156 trx_t */
158 /*!< mysql_n_tables_locked in
159 trx_t */
160 ulint trx_lock_structs; /*!< list len of trx_locks in
161 trx_t */
163 /*!< mem_heap_get_size(
164 trx->lock_heap) */
165 ulint trx_rows_locked; /*!< lock_number_of_rows_locked() */
166 uintmax_t trx_rows_modified; /*!< trx_t::undo_no */
168 /*!< n_tickets_to_enter_innodb in
169 trx_t */
171 /*!< isolation_level in trx_t */
173 /*!< check_unique_secondary in trx_t*/
175 /*!< check_foreigns in trx_t */
177 /*!< detailed_error in trx_t */
179 /*!< has_search_latch in trx_t */
181 /*!< trx_t::read_only */
183 /*!< trx_is_autocommit_non_locking(trx)
184 */
185};
186
187/** Cache of INFORMATION_SCHEMA table data */
188struct trx_i_s_cache_t;
189
190/** Auxiliary enum used by functions that need to select one of the
191INFORMATION_SCHEMA tables */
193 I_S_INNODB_TRX, /*!< INFORMATION_SCHEMA.innodb_trx */
194};
195
196/** This is the intermediate buffer where data needed to fill the
197INFORMATION SCHEMA tables is fetched and later retrieved by the C++
198code in handler/i_s.cc. */
200
201/** Initialize INFORMATION SCHEMA trx related cache. */
202void trx_i_s_cache_init(trx_i_s_cache_t *cache); /*!< out: cache to init */
203/** Free the INFORMATION SCHEMA trx related cache. */
204void trx_i_s_cache_free(trx_i_s_cache_t *cache); /*!< in/out: cache to free */
205
206/** Issue a shared/read lock on the tables cache. */
207void trx_i_s_cache_start_read(trx_i_s_cache_t *cache); /*!< in: cache */
208
209/** Release a shared/read lock on the tables cache. */
210void trx_i_s_cache_end_read(trx_i_s_cache_t *cache); /*!< in: cache */
211
212/** Issue an exclusive/write lock on the tables cache. */
213void trx_i_s_cache_start_write(trx_i_s_cache_t *cache); /*!< in: cache */
214
215/** Release an exclusive/write lock on the tables cache. */
216void trx_i_s_cache_end_write(trx_i_s_cache_t *cache); /*!< in: cache */
217
218/** Retrieves the number of used rows in the cache for a given
219 INFORMATION SCHEMA table.
220 @return number of rows */
221ulint trx_i_s_cache_get_rows_used(trx_i_s_cache_t *cache, /*!< in: cache */
222 enum i_s_table table); /*!< in: which table */
223
224/** Retrieves the nth row in the cache for a given INFORMATION SCHEMA
225 table.
226 @return row */
227void *trx_i_s_cache_get_nth_row(trx_i_s_cache_t *cache, /*!< in: cache */
228 enum i_s_table table, /*!< in: which table */
229 ulint n); /*!< in: row number */
230
231/** Update the transactions cache if it has not been read for some time.
232 @return 0 - fetched, 1 - not */
234 trx_i_s_cache_t *cache); /*!< in/out: cache */
235
236/** Returns true if the data in the cache is truncated due to the memory
237 limit posed by TRX_I_S_MEM_LIMIT.
238 @param[in] cache The cache
239 @return true if truncated */
241
242/** The maximum length of a resulting lock_id_size in
243trx_i_s_create_lock_id(), not including the terminating NUL.
244"%lu:%lu:%lu:%lu:%lu" -> 20*5+4 chars */
245constexpr uint32_t TRX_I_S_LOCK_ID_MAX_LEN = 20 * 5 + 4;
246
247/** Crafts a lock id string from a i_s_locks_row_t object. Returns its
248 second argument. This function aborts if there is not enough space in
249 lock_id. Be sure to provide at least TRX_I_S_LOCK_ID_MAX_LEN + 1 if you
250 want to be 100% sure that it will not abort.
251 @return resulting lock id */
253 const i_s_locks_row_t *row, /*!< in: innodb_locks row */
254 char *lock_id, /*!< out: resulting lock_id */
255 ulint lock_id_size); /*!< in: size of the lock id
256 buffer */
257
258/** Fill performance schema lock data.
259Create a string that represents the LOCK_DATA
260column, for a given lock record.
261@param[out] lock_data Lock data string
262@param[in] lock Lock to inspect
263@param[in] heap_no Lock heap number
264@param[in] container Data container to fill
265*/
266void p_s_fill_lock_data(const char **lock_data, const lock_t *lock,
267 ulint heap_no,
269
270/** Fills i_s_locks_row_t object with data about the lock.
271@param[out] row Result object that's filled
272@param[in] lock Lock to get data from
273@param[in] heap_no Lock's record number or ULINT_UNDEFINED if the lock is a
274 table lock */
275void fill_locks_row(i_s_locks_row_t *row, const lock_t *lock, ulint heap_no);
276
277/** Parses lock id into row
278@param[in] lock_id Lock id generated with trx_i_s_create_lock_id
279@param[out] row Row to be filled in with data
280@return LOCK_REC, LOCK_TABLE or 0 if failed to parse */
281int trx_i_s_parse_lock_id(const char *lock_id, i_s_locks_row_t *row);
282#endif /* trx0i_s_h */
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:50
uint32_t page_no_t
Page number.
Definition: api0api.h:48
Server interface, row lock container.
Definition: psi_data_lock.h:107
Data dictionary global types.
ib_id_t table_id_t
Table or partition identifier (unique within an InnoDB instance).
Definition: dict0types.h:217
Definition: atomics_array.h:38
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:78
Definition: m_ctype.h:382
This structure represents INFORMATION_SCHEMA.innodb_locks row.
Definition: trx0i_s.h:89
uint64_t lock_immutable_id
lock address as integer.
Definition: trx0i_s.h:97
space_id_t lock_space
Information for record locks.
Definition: trx0i_s.h:116
ulint lock_rec
heap number of the record on the page
Definition: trx0i_s.h:118
table_id_t lock_table_id
The following are auxiliary and not included in the table.
Definition: trx0i_s.h:124
uint64_t lock_trx_immutable_id
transaction address as integer.
Definition: trx0i_s.h:90
page_no_t lock_page
page number within the_space
Definition: trx0i_s.h:117
This structure represents INFORMATION_SCHEMA.innodb_trx row.
Definition: trx0i_s.h:131
ulint trx_lock_memory_bytes
mem_heap_get_size( trx->lock_heap)
Definition: trx0i_s.h:162
ulint trx_rows_locked
lock_number_of_rows_locked()
Definition: trx0i_s.h:165
std::chrono::system_clock::time_point trx_started
trx_t::start_time
Definition: trx0i_s.h:135
const char * trx_isolation_level
isolation_level in trx_t
Definition: trx0i_s.h:170
bool trx_unique_checks
check_unique_secondary in trx_t
Definition: trx0i_s.h:172
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:149
ulint trx_tables_locked
mysql_n_tables_locked in trx_t
Definition: trx0i_s.h:157
const char * trx_state
transaction state from trx_get_que_state_str()
Definition: trx0i_s.h:133
ulint trx_concurrency_tickets
n_tickets_to_enter_innodb in trx_t
Definition: trx0i_s.h:167
bool trx_has_search_latch
has_search_latch in trx_t
Definition: trx0i_s.h:178
const char * trx_foreign_key_error
detailed_error in trx_t
Definition: trx0i_s.h:176
const CHARSET_INFO * trx_query_cs
the charset of trx_query
Definition: trx0i_s.h:153
trx_id_t trx_id
transaction identifier
Definition: trx0i_s.h:132
const char * trx_operation_state
trx_t::op_info
Definition: trx0i_s.h:154
ulint trx_mysql_thread_id
thd_get_thread_id()
Definition: trx0i_s.h:150
ulint trx_is_read_only
trx_t::read_only
Definition: trx0i_s.h:180
ulint trx_lock_structs
list len of trx_locks in trx_t
Definition: trx0i_s.h:160
ulint trx_tables_in_use
n_mysql_tables_in_use in trx_t
Definition: trx0i_s.h:155
bool trx_foreign_key_checks
check_foreigns in trx_t
Definition: trx0i_s.h:174
ulint trx_is_autocommit_non_locking
trx_is_autocommit_non_locking(trx)
Definition: trx0i_s.h:182
const char * trx_query
MySQL statement being executed in the transaction.
Definition: trx0i_s.h:151
std::chrono::system_clock::time_point trx_wait_started
The value of trx->lock.wait_started.
Definition: trx0i_s.h:142
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:136
uintmax_t trx_weight
The value of TRX_WEIGHT(trx)
Definition: trx0i_s.h:144
uintmax_t trx_rows_modified
trx_t::undo_no
Definition: trx0i_s.h:166
Lock struct; protected by lock_sys latches.
Definition: lock0priv.h:135
This structure describes the intermediate buffer.
Definition: trx0i_s.cc:133
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:996
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:990
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:1093
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:48
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:1058
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:1007
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:650
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:942
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:64
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:163
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:68
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:1045
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:921
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:60
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:56
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:1013
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:245
void trx_i_s_cache_free(trx_i_s_cache_t *cache)
Free the INFORMATION SCHEMA trx related cache.
Definition: trx0i_s.cc:978
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:1123
i_s_table
Auxiliary enum used by functions that need to select one of the INFORMATION_SCHEMA tables.
Definition: trx0i_s.h:192
@ I_S_INNODB_TRX
INFORMATION_SCHEMA.innodb_trx.
Definition: trx0i_s.h:193
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:52
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:586
void trx_i_s_cache_init(trx_i_s_cache_t *cache)
Initialize INFORMATION SCHEMA trx related cache.
Definition: trx0i_s.cc:947
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:407
int n
Definition: xcom_base.cc:505