MySQL 26.7.0
Source Code Documentation
read0types.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1997, 2026, 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"
40#include "trx0types.h"
41#include "ut0cpu_cache.h"
42
43// Friend declaration
44class MVCC;
45
46/** Read view lists the trx ids of those transactions for which a consistent
47read should not see the modifications to the database. */
48
50 /** This is similar to a std::vector but it is not a drop
51 in replacement. It is specific to ReadView. */
52 class ids_t {
54
55 /**
56 Constructor */
58
59 /**
60 Destructor */
62
63 /** Try and increase the size of the array. Old elements are copied across.
64 It is a no-op if n is < current size.
65 @param n Make space for n elements */
66 void reserve(ulint n);
67
68 /**
69 Resize the array, sets the current element count.
70 @param n new size of the array, in elements */
71 void resize(ulint n) {
72 ut_ad(n <= capacity());
73
74 m_size = n;
75 }
76
77 /**
78 Reset the size to 0 */
79 void clear() { resize(0); }
80
81 /**
82 @return the capacity of the array in elements */
83 ulint capacity() const { return (m_reserved); }
84
85 /**
86 Copy and overwrite this array contents
87
88 @param start Source array
89 @param end Pointer to end of array */
90 void assign(const value_type *start, const value_type *end);
91
92 /**
93 Insert the value in the correct slot, preserving the order.
94 Doesn't check for duplicates. */
96
97 /**
98 @return the value of the first element in the array */
99 value_type front() const {
100 ut_ad(!empty());
101
102 return (m_ptr[0]);
103 }
104
105 /**
106 @return the value of the last element in the array */
107 value_type back() const {
108 ut_ad(!empty());
109
110 return (m_ptr[m_size - 1]);
111 }
112
113 /**
114 Append a value to the array.
115 @param value the value to append */
117
118 /**
119 @return a pointer to the start of the array */
120 trx_id_t *data() { return (m_ptr); }
121
122 /**
123 @return a const pointer to the start of the array */
124 const trx_id_t *data() const { return (m_ptr); }
125
126 /**
127 @return the number of elements in the array */
128 ulint size() const { return (m_size); }
129
130 /**
131 @return true if size() == 0 */
132 bool empty() const { return (size() == 0); }
133
134 private:
135 // Prevent copying
136 ids_t(const ids_t &);
138
139 private:
140 /** Memory for the array */
142
143 /** Number of active elements in the array */
145
146 /** Size of m_ptr in elements */
148
149 friend class ReadView;
150 };
151
152 public:
153 ReadView();
154 ~ReadView() override;
155
156 /** Check whether the changes by id are visible.
157 @param[in] id transaction id to check against the view
158 @return whether the view sees the modifications of id. */
159 [[nodiscard]] bool changes_visible(trx_id_t id) const override {
160 ut_ad(id > 0);
161
162 if (id < m_up_limit_id || id == m_creator_trx_id) {
163 return true;
164 }
165 if (id >= m_low_limit_id) {
166 return false;
167 }
168 if (m_ids.empty()) {
169 return true;
170 }
171
172 const ids_t::value_type *p = m_ids.data();
173
174 return !std::binary_search(p, p + m_ids.size(), id);
175 }
176
178 trx_id_t id) const override {
179 return id < m_up_limit_id;
180 }
181
182 /**
183 @return true if the view is closed */
184 [[nodiscard]] bool is_closed() const { return m_closed.load(); }
185
186 void print(FILE *file) const override {
187 fprintf(file,
188 "Trx read view will not see trx with"
189 " id >= " TRX_ID_FMT ", sees < " TRX_ID_FMT "\n",
191 }
192
193 [[nodiscard]] trx_id_t get_lowest_needed_trx_no() const override {
194 return m_low_limit_no;
195 }
196
197 /**
198 @return true if there are no transaction ids in the snapshot */
199 [[nodiscard]] bool empty() const { return (m_ids.empty()); }
200
201#ifdef UNIV_DEBUG
202 /**
203 @param rhs view to compare with
204 @return true if this view is less than or equal rhs */
205 [[nodiscard]] bool le(const ReadView *rhs) const {
206 return (m_low_limit_no <= rhs->m_low_limit_no);
207 }
208#endif /* UNIV_DEBUG */
209 private:
210 /**
211 Copy the transaction ids from the source vector */
212 inline void copy_trx_ids(const trx_ids_t &trx_ids);
213
214 /**
215 Opens a read view where exactly the transactions serialized before this
216 point in time are seen in the view.
217 @param id Creator transaction id */
218 inline void prepare(trx_id_t id);
219
220 /**
221 Copy state from another view. Must call copy_complete() to finish.
222 @param other view to copy from */
223 inline void copy_prepare(const ReadView &other);
224
225 /**
226 Complete the copy, insert the creator transaction id into the
227 m_trx_ids too and adjust the m_up_limit_id *, if required */
228 inline void copy_complete();
229
230 /**
231 Set the creator transaction id, existing id must be 0 */
235 }
236
237 friend class MVCC;
238
239 private:
240 // Disable copying
243
244 private:
245 /** The read should not see any transaction with trx id >= this
246 value. In other words, this is the "high water mark". */
248
249 /** The read should see all trx ids which are strictly
250 smaller (<) than this value. In other words, this is the
251 low water mark". */
253
254 /** If the view is open, then this is a trx->id of the transaction which has
255 created this view, used to let this view see the changes of this transaction.
256 Note that a transaction might have no trx->id assigned in which case this
257 will be 0. A transaction may also get trx->id assigned after it has already
258 created a read view, in which case it should call set_view_creator_trx_id to
259 update this field.
260 It is 0 for read views cloned by clone_oldest_view.
261 Otherwise its value doesn't matter. */
263
264 /** Set of RW transactions that was active when this snapshot
265 was taken */
267
268 /** The view does not need to see the undo logs for transactions
269 whose transaction number is strictly smaller (<) than this value:
270 they can be removed in purge if not needed by other views */
272
273 /** False iff this view is in use by a transaction at the moment (is open).*/
274 std::atomic_bool m_closed{true};
275
276 typedef UT_LIST_NODE_T(ReadView) node_t;
277
278 /** List of read views in trx_sys */
280 node_t m_view_list{};
281};
282
283#endif
The MVCC read view manager.
Definition: read0read.h:46
This is similar to a std::vector but it is not a drop in replacement.
Definition: read0types.h:52
ulint m_size
Number of active elements in the array.
Definition: read0types.h:144
const trx_id_t * data() const
Definition: read0types.h:124
value_type * m_ptr
Memory for the array.
Definition: read0types.h:141
trx_ids_t::value_type value_type
Definition: read0types.h:53
void push_back(value_type value)
Append a value to the array.
Definition: read0read.cc:260
ids_t & operator=(const ids_t &)
value_type front() const
Definition: read0types.h:99
~ids_t()
Destructor.
Definition: read0types.h:61
ulint size() const
Definition: read0types.h:128
ids_t()
Constructor.
Definition: read0types.h:57
void insert(value_type value)
Insert the value in the correct slot, preserving the order.
Definition: read0read.cc:273
void reserve(ulint n)
Try and increase the size of the array.
Definition: read0read.cc:212
bool empty() const
Definition: read0types.h:132
value_type back() const
Definition: read0types.h:107
void clear()
Reset the size to 0.
Definition: read0types.h:79
trx_id_t * data()
Definition: read0types.h:120
ulint m_reserved
Size of m_ptr in elements.
Definition: read0types.h:147
void resize(ulint n)
Resize the array, sets the current element count.
Definition: read0types.h:71
void assign(const value_type *start, const value_type *end)
Copy and overwrite this array contents.
Definition: read0read.cc:238
ids_t(const ids_t &)
ulint capacity() const
Definition: read0types.h:83
Read view lists the trx ids of those transactions for which a consistent read should not see the modi...
Definition: read0types.h:49
void copy_prepare(const ReadView &other)
Copy state from another view.
Definition: read0read.cc:629
ReadView()
ReadView constructor.
Definition: read0read.cc:305
std::atomic_bool m_closed
False iff this view is in use by a transaction at the moment (is open).
Definition: read0types.h:274
~ReadView() override
ReadView destructor.
Definition: read0read.cc:316
bool le(const ReadView *rhs) const
Definition: read0types.h:205
node_t m_view_list
Definition: read0types.h:280
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:653
void creator_trx_id(trx_id_t id)
Set the creator transaction id, existing id must be 0.
Definition: read0types.h:232
trx_id_t m_low_limit_id
The read should not see any transaction with trx id >= this value.
Definition: read0types.h:247
byte pad1[ut::INNODB_CACHE_LINE_SIZE]
List of read views in trx_sys.
Definition: read0types.h:279
bool is_closed() const
Definition: read0types.h:184
void print(FILE *file) const override
Describe the read-view.
Definition: read0types.h:186
typedef UT_LIST_NODE_T(ReadView) node_t
trx_id_t m_up_limit_id
The read should see all trx ids which are strictly smaller (<) than this value.
Definition: read0types.h:252
ids_t m_ids
Set of RW transactions that was active when this snapshot was taken.
Definition: read0types.h:266
trx_id_t m_creator_trx_id
If the view is open, then this is a trx->id of the transaction which has created this view,...
Definition: read0types.h:262
bool sees_all_trxs_with_id_smaller_or_equal_to(trx_id_t id) const override
Checks if changes made by each trx with trx->id smaller or equal to id, should be visible to this rea...
Definition: read0types.h:177
void copy_trx_ids(const trx_ids_t &trx_ids)
Copy the transaction ids from the source vector.
Definition: read0read.cc:348
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:441
ReadView & operator=(const ReadView &)
bool empty() const
Definition: read0types.h:199
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:271
ReadView(const ReadView &)
trx_id_t get_lowest_needed_trx_no() const override
This read view does not need Undo Logs generated by transactions with trx->no strictly smaller than t...
Definition: read0types.h:193
bool changes_visible(trx_id_t id) const override
Check whether the changes by id are visible.
Definition: read0types.h:159
Definition: read0read_view_interface.h:33
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
constexpr size_t INNODB_CACHE_LINE_SIZE
CPU cache line size.
Definition: ut0cpu_cache.h:41
void delete_arr(T *ptr) noexcept
Releases storage which has been dynamically allocated through any of the ut::new_arr*() variants.
Definition: ut0new.h:951
Transaction system global type definitions.
#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:403
Utilities related to CPU cache.
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:109
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510
int n
Definition: xcom_base.cc:509