| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1997, 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/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 |
| 43 |
class MVCC; |
| 44 |
|
| 45 |
/** Read view lists the trx ids of those transactions for which a consistent |
| 46 |
read should not see the modifications to the database. */ |
| 47 |
|
| 48 |
class 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 { |
| 52 |
typedef trx_ids_t::value_type value_type; |
| 53 |
|
| 54 |
/** |
| 55 |
Constructor */ |
| 56 |
ids_t() : m_ptr(), m_size(), m_reserved() {} |
| 57 |
|
| 58 |
/** |
| 59 |
Destructor */ |
| 60 |
~ids_t() { ut::delete_arr(m_ptr); } |
| 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. */ |
| 94 |
void insert(value_type value); |
| 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 */ |
| 115 |
void push_back(value_type value); |
| 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 &); |
| 136 |
ids_t &operator=(const ids_t &); |
| 137 |
|
| 138 |
private: |
| 139 |
/** Memory for the array */ |
| 140 |
value_type *m_ptr; |
| 141 |
|
| 142 |
/** Number of active elements in the array */ |
| 143 |
ulint m_size; |
| 144 |
|
| 145 |
/** Size of m_ptr in elements */ |
| 146 |
ulint m_reserved; |
| 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 |
|
| 171 |
check_trx_id_sanity(id, name); |
| 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() { |
| 193 |
ut_ad(m_creator_trx_id != TRX_ID_MAX); |
| 194 |
m_creator_trx_id = TRX_ID_MAX; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
@return true if the view is closed */ |
| 199 |
bool is_closed() const { return (m_closed); } |
| 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", |
| 208 |
m_low_limit_id, m_up_limit_id); |
| 209 |
} |
| 210 |
|
| 211 |
/** Check and reduce low limit number for read view. Used to |
| 212 |
block purge till GTID is persisted on disk table. |
| 213 |
@param[in] trx_no transaction number to check with */ |
| 214 |
void reduce_low_limit(trx_id_t trx_no) { |
| 215 |
if (trx_no < m_low_limit_no) { |
| 216 |
/* Save low limit number set for Read View for MVCC. */ |
| 217 |
ut_d(m_view_low_limit_no = m_low_limit_no); |
| 218 |
m_low_limit_no = trx_no; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
@return the low limit no */ |
| 224 |
trx_id_t low_limit_no() const { return (m_low_limit_no); } |
| 225 |
|
| 226 |
/** |
| 227 |
@return the low limit id */ |
| 228 |
trx_id_t low_limit_id() const { return (m_low_limit_id); } |
| 229 |
|
| 230 |
/** |
| 231 |
@return true if there are no transaction ids in the snapshot */ |
| 232 |
bool empty() const { return (m_ids.empty()); } |
| 233 |
|
| 234 |
#ifdef UNIV_DEBUG |
| 235 |
/** |
| 236 |
@return the view low limit number */ |
| 237 |
trx_id_t view_low_limit_no() const { return (m_view_low_limit_no); } |
| 238 |
|
| 239 |
/** |
| 240 |
@param rhs view to compare with |
| 241 |
@return truen if this view is less than or equal rhs */ |
| 242 |
bool le(const ReadView *rhs) const { |
| 243 |
return (m_low_limit_no <= rhs->m_low_limit_no); |
| 244 |
} |
| 245 |
#endif /* UNIV_DEBUG */ |
| 246 |
private: |
| 247 |
/** |
| 248 |
Copy the transaction ids from the source vector */ |
| 249 |
inline void copy_trx_ids(const trx_ids_t &trx_ids); |
| 250 |
|
| 251 |
/** |
| 252 |
Opens a read view where exactly the transactions serialized before this |
| 253 |
point in time are seen in the view. |
| 254 |
@param id Creator transaction id */ |
| 255 |
inline void prepare(trx_id_t id); |
| 256 |
|
| 257 |
/** |
| 258 |
Copy state from another view. Must call copy_complete() to finish. |
| 259 |
@param other view to copy from */ |
| 260 |
inline void copy_prepare(const ReadView &other); |
| 261 |
|
| 262 |
/** |
| 263 |
Complete the copy, insert the creator transaction id into the |
| 264 |
m_trx_ids too and adjust the m_up_limit_id *, if required */ |
| 265 |
inline void copy_complete(); |
| 266 |
|
| 267 |
/** |
| 268 |
Set the creator transaction id, existing id must be 0 */ |
| 269 |
void creator_trx_id(trx_id_t id) { |
| 270 |
ut_ad(m_creator_trx_id == 0); |
| 271 |
m_creator_trx_id = id; |
| 272 |
} |
| 273 |
|
| 274 |
friend class MVCC; |
| 275 |
|
| 276 |
private: |
| 277 |
// Disable copying |
| 278 |
ReadView(const ReadView &); |
| 279 |
ReadView &operator=(const ReadView &); |
| 280 |
|
| 281 |
private: |
| 282 |
/** The read should not see any transaction with trx id >= this |
| 283 |
value. In other words, this is the "high water mark". */ |
| 284 |
trx_id_t m_low_limit_id; |
| 285 |
|
| 286 |
/** The read should see all trx ids which are strictly |
| 287 |
smaller (<) than this value. In other words, this is the |
| 288 |
low water mark". */ |
| 289 |
trx_id_t m_up_limit_id; |
| 290 |
|
| 291 |
/** trx id of creating transaction, set to TRX_ID_MAX for free |
| 292 |
views. */ |
| 293 |
trx_id_t m_creator_trx_id; |
| 294 |
|
| 295 |
/** Set of RW transactions that was active when this snapshot |
| 296 |
was taken */ |
| 297 |
ids_t m_ids; |
| 298 |
|
| 299 |
/** The view does not need to see the undo logs for transactions |
| 300 |
whose transaction number is strictly smaller (<) than this value: |
| 301 |
they can be removed in purge if not needed by other views */ |
| 302 |
trx_id_t m_low_limit_no; |
| 303 |
|
| 304 |
#ifdef UNIV_DEBUG |
| 305 |
/** The low limit number up to which read views don't need to access |
| 306 |
undo log records for MVCC. This could be higher than m_low_limit_no |
| 307 |
if purge is blocked for GTID persistence. Currently used for debug |
| 308 |
variable INNODB_PURGE_VIEW_TRX_ID_AGE. */ |
| 309 |
trx_id_t m_view_low_limit_no; |
| 310 |
#endif /* UNIV_DEBUG */ |
| 311 |
|
| 312 |
/** AC-NL-RO transaction view that has been "closed". */ |
| 313 |
bool m_closed; |
| 314 |
|
| 315 |
typedef UT_LIST_NODE_T(ReadView) node_t; |
| 316 |
|
| 317 |
/** List of read views in trx_sys */ |
| 318 |
byte pad1[64 - sizeof(node_t)]; |
| 319 |
node_t m_view_list; |
| 320 |
}; |
| 321 |
|
| 322 |
#endif |