MySQL 9.5.0
Source Code Documentation
read0types.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1997, 2025, 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 designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/read0types.h
29 Cursor read
30
31 Created 2/16/1997 Heikki Tuuri
32 *******************************************************/
33
34#ifndef read0types_h
35#define read0types_h
36
37#include <algorithm>
38#include "dict0mem.h"
39
40#include "trx0types.h"
41
42// Friend declaration
43class MVCC;
44
45/** Read view lists the trx ids of those transactions for which a consistent
46read should not see the modifications to the database. */
47
48class ReadView {
49 /** This is similar to a std::vector but it is not a drop
50 in replacement. It is specific to ReadView. */
51 class ids_t {
53
54 /**
55 Constructor */
57
58 /**
59 Destructor */
61
62 /** Try and increase the size of the array. Old elements are copied across.
63 It is a no-op if n is < current size.
64 @param n Make space for n elements */
65 void reserve(ulint n);
66
67 /**
68 Resize the array, sets the current element count.
69 @param n new size of the array, in elements */
70 void resize(ulint n) {
71 ut_ad(n <= capacity());
72
73 m_size = n;
74 }
75
76 /**
77 Reset the size to 0 */
78 void clear() { resize(0); }
79
80 /**
81 @return the capacity of the array in elements */
82 ulint capacity() const { return (m_reserved); }
83
84 /**
85 Copy and overwrite the current array contents
86
87 @param start Source array
88 @param end Pointer to end of array */
89 void assign(const value_type *start, const value_type *end);
90
91 /**
92 Insert the value in the correct slot, preserving the order.
93 Doesn't check for duplicates. */
95
96 /**
97 @return the value of the first element in the array */
98 value_type front() const {
99 ut_ad(!empty());
100
101 return (m_ptr[0]);
102 }
103
104 /**
105 @return the value of the last element in the array */
106 value_type back() const {
107 ut_ad(!empty());
108
109 return (m_ptr[m_size - 1]);
110 }
111
112 /**
113 Append a value to the array.
114 @param value the value to append */
116
117 /**
118 @return a pointer to the start of the array */
119 trx_id_t *data() { return (m_ptr); }
120
121 /**
122 @return a const pointer to the start of the array */
123 const trx_id_t *data() const { return (m_ptr); }
124
125 /**
126 @return the number of elements in the array */
127 ulint size() const { return (m_size); }
128
129 /**
130 @return true if size() == 0 */
131 bool empty() const { return (size() == 0); }
132
133 private:
134 // Prevent copying
135 ids_t(const ids_t &);
137
138 private:
139 /** Memory for the array */
141
142 /** Number of active elements in the array */
144
145 /** Size of m_ptr in elements */
147
148 friend class ReadView;
149 };
150
151 public:
152 ReadView();
153 ~ReadView();
154 /** Check whether transaction id is valid.
155 @param[in] id transaction id to check
156 @param[in] name table name */
157 static void check_trx_id_sanity(trx_id_t id, const table_name_t &name);
158
159 /** Check whether the changes by id are visible.
160 @param[in] id transaction id to check against the view
161 @param[in] name table name
162 @return whether the view sees the modifications of id. */
163 [[nodiscard]] bool changes_visible(trx_id_t id,
164 const table_name_t &name) const {
165 ut_ad(id > 0);
166
167 if (id < m_up_limit_id || id == m_creator_trx_id) {
168 return (true);
169 }
170
172
173 if (id >= m_low_limit_id) {
174 return (false);
175
176 } else if (m_ids.empty()) {
177 return (true);
178 }
179
180 const ids_t::value_type *p = m_ids.data();
181
182 return (!std::binary_search(p, p + m_ids.size(), id));
183 }
184
185 /**
186 @param id transaction to check
187 @return true if view sees transaction id */
188 bool sees(trx_id_t id) const { return (id < m_up_limit_id); }
189
190 /**
191 Mark the view as closed */
192 void close() {
195 }
196
197 /**
198 @return true if the view is closed */
199 bool is_closed() const { return m_closed.load(); }
200
201 /**
202 Write the limits to the file.
203 @param file file to write to */
204 void print_limits(FILE *file) const {
205 fprintf(file,
206 "Trx read view will not see trx with"
207 " id >= " TRX_ID_FMT ", sees < " TRX_ID_FMT "\n",
209 }
210
211 /**
212 @return the low limit no */
214
215 /**
216 @return the low limit id */
218
219 /**
220 @return true if there are no transaction ids in the snapshot */
221 bool empty() const { return (m_ids.empty()); }
222
223#ifdef UNIV_DEBUG
224 /**
225 @param rhs view to compare with
226 @return truen if this view is less than or equal rhs */
227 bool le(const ReadView *rhs) const {
228 return (m_low_limit_no <= rhs->m_low_limit_no);
229 }
230#endif /* UNIV_DEBUG */
231 private:
232 /**
233 Copy the transaction ids from the source vector */
234 inline void copy_trx_ids(const trx_ids_t &trx_ids);
235
236 /**
237 Opens a read view where exactly the transactions serialized before this
238 point in time are seen in the view.
239 @param id Creator transaction id */
240 inline void prepare(trx_id_t id);
241
242 /**
243 Copy state from another view. Must call copy_complete() to finish.
244 @param other view to copy from */
245 inline void copy_prepare(const ReadView &other);
246
247 /**
248 Complete the copy, insert the creator transaction id into the
249 m_trx_ids too and adjust the m_up_limit_id *, if required */
250 inline void copy_complete();
251
252 /**
253 Set the creator transaction id, existing id must be 0 */
257 }
258
259 friend class MVCC;
260
261 private:
262 // Disable copying
265
266 private:
267 /** The read should not see any transaction with trx id >= this
268 value. In other words, this is the "high water mark". */
270
271 /** The read should see all trx ids which are strictly
272 smaller (<) than this value. In other words, this is the
273 low water mark". */
275
276 /** trx id of creating transaction, set to TRX_ID_MAX for free
277 views. */
279
280 /** Set of RW transactions that was active when this snapshot
281 was taken */
283
284 /** The view does not need to see the undo logs for transactions
285 whose transaction number is strictly smaller (<) than this value:
286 they can be removed in purge if not needed by other views */
288
289 /** AC-NL-RO transaction view that has been "closed". */
290 std::atomic_bool m_closed;
291
292 typedef UT_LIST_NODE_T(ReadView) node_t;
293
294 /** List of read views in trx_sys */
295 byte pad1[64 - sizeof(node_t)];
297};
298
299#endif
The MVCC read view manager.
Definition: read0read.h:44
This is similar to a std::vector but it is not a drop in replacement.
Definition: read0types.h:51
ulint m_size
Number of active elements in the array.
Definition: read0types.h:143
const trx_id_t * data() const
Definition: read0types.h:123
value_type * m_ptr
Memory for the array.
Definition: read0types.h:140
trx_ids_t::value_type value_type
Definition: read0types.h:52
void push_back(value_type value)
Append a value to the array.
Definition: read0read.cc:270
ids_t & operator=(const ids_t &)
value_type front() const
Definition: read0types.h:98
~ids_t()
Destructor.
Definition: read0types.h:60
ulint size() const
Definition: read0types.h:127
ids_t()
Constructor.
Definition: read0types.h:56
void insert(value_type value)
Insert the value in the correct slot, preserving the order.
Definition: read0read.cc:283
void reserve(ulint n)
Try and increase the size of the array.
Definition: read0read.cc:217
bool empty() const
Definition: read0types.h:131
value_type back() const
Definition: read0types.h:106
void clear()
Reset the size to 0.
Definition: read0types.h:78
trx_id_t * data()
Definition: read0types.h:119
ulint m_reserved
Size of m_ptr in elements.
Definition: read0types.h:146
void resize(ulint n)
Resize the array, sets the current element count.
Definition: read0types.h:70
void assign(const value_type *start, const value_type *end)
Copy and overwrite the current array contents.
Definition: read0read.cc:248
ids_t(const ids_t &)
ulint capacity() const
Definition: read0types.h:82
Read view lists the trx ids of those transactions for which a consistent read should not see the modi...
Definition: read0types.h:48
void print_limits(FILE *file) const
Write the limits to the file.
Definition: read0types.h:204
~ReadView()
ReadView destructor.
Definition: read0read.cc:326
void copy_prepare(const ReadView &other)
Copy state from another view.
Definition: read0read.cc:643
ReadView()
ReadView constructor.
Definition: read0read.cc:315
std::atomic_bool m_closed
AC-NL-RO transaction view that has been "closed".
Definition: read0types.h:290
bool le(const ReadView *rhs) const
Definition: read0types.h:227
node_t m_view_list
Definition: read0types.h:296
byte pad1[64 - sizeof(node_t)]
List of read views in trx_sys.
Definition: read0types.h:295
void copy_complete()
Complete the copy, insert the creator transaction id into the m_trx_ids too and adjust the m_up_limit...
Definition: read0read.cc:667
void creator_trx_id(trx_id_t id)
Set the creator transaction id, existing id must be 0.
Definition: read0types.h:254
trx_id_t low_limit_no() const
Definition: read0types.h:213
bool changes_visible(trx_id_t id, const table_name_t &name) const
Check whether the changes by id are visible.
Definition: read0types.h:163
trx_id_t m_low_limit_id
The read should not see any transaction with trx id >= this value.
Definition: read0types.h:269
bool is_closed() const
Definition: read0types.h:199
typedef UT_LIST_NODE_T(ReadView) node_t
bool sees(trx_id_t id) const
Definition: read0types.h:188
trx_id_t m_up_limit_id
The read should see all trx ids which are strictly smaller (<) than this value.
Definition: read0types.h:274
void close()
Mark the view as closed.
Definition: read0types.h:192
ids_t m_ids
Set of RW transactions that was active when this snapshot was taken.
Definition: read0types.h:282
trx_id_t m_creator_trx_id
trx id of creating transaction, set to TRX_ID_MAX for free views.
Definition: read0types.h:278
void copy_trx_ids(const trx_ids_t &trx_ids)
Copy the transaction ids from the source vector.
Definition: read0read.cc:353
void prepare(trx_id_t id)
Opens a read view where exactly the transactions serialized before this point in time are seen in the...
Definition: read0read.cc:446
ReadView & operator=(const ReadView &)
bool empty() const
Definition: read0types.h:221
trx_id_t m_low_limit_no
The view does not need to see the undo logs for transactions whose transaction number is strictly sma...
Definition: read0types.h:287
ReadView(const ReadView &)
static void check_trx_id_sanity(trx_id_t id, const table_name_t &name)
Check whether transaction id is valid.
Definition: trx0sys.cc:113
trx_id_t low_limit_id() const
Definition: read0types.h:217
const char * p
Definition: ctype-mb.cc:1227
Data dictionary memory object creation.
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
uint16_t value_type
Definition: vt100.h:184
const std::string FILE("FILE")
Definition: os0file.h:89
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
void delete_arr(T *ptr) noexcept
Releases storage which has been dynamically allocated through any of the ut::new_arr*() variants.
Definition: ut0new.h:1111
case opt name
Definition: sslopt-case.h:29
Table name wrapper for pretty-printing.
Definition: dict0mem.h:465
Transaction system global type definitions.
constexpr trx_id_t TRX_ID_MAX
Maximum transaction identifier.
Definition: trx0types.h:145
#define TRX_ID_FMT
printf(3) format used for printing DB_TRX_ID and other system fields
Definition: trx0types.h:49
std::vector< trx_id_t, ut::allocator< trx_id_t > > trx_ids_t
Definition: trx0types.h:633
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:138
unsigned long int ulint
Definition: univ.i:406
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:105
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510
int n
Definition: xcom_base.cc:509