MySQL  8.0.19
Source Code Documentation
read0types.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1997, 2019, Oracle and/or its affiliates. All Rights Reserved.
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 also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 
25 *****************************************************************************/
26 
27 /** @file include/read0types.h
28  Cursor read
29 
30  Created 2/16/1997 Heikki Tuuri
31  *******************************************************/
32 
33 #ifndef read0types_h
34 #define read0types_h
35 
36 #include <algorithm>
37 #include "dict0mem.h"
38 
39 #include "trx0types.h"
40 
41 // Friend declaration
42 class MVCC;
43 
44 /** Read view lists the trx ids of those transactions for which a consistent
45 read should not see the modifications to the database. */
46 
47 class ReadView {
48  /** This is similar to a std::vector but it is not a drop
49  in replacement. It is specific to ReadView. */
50  class ids_t {
52 
53  /**
54  Constructor */
55  ids_t() : m_ptr(), m_size(), m_reserved() {}
56 
57  /**
58  Destructor */
60 
61  /**
62  Try and increase the size of the array. Old elements are
63  copied across. It is a no-op if n is < current size.
64 
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 the current 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. */
95  void insert(value_type value);
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 */
116  void push_back(value_type value);
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 &);
137  ids_t &operator=(const ids_t &);
138 
139  private:
140  /** Memory for the array */
142 
143  /** Number of active elements in the array */
144  ulint m_size;
145 
146  /** Size of m_ptr in elements */
147  ulint m_reserved;
148 
149  friend class ReadView;
150  };
151 
152  public:
153  ReadView();
154  ~ReadView();
155  /** Check whether transaction id is valid.
156  @param[in] id transaction id to check
157  @param[in] name table name */
158  static void check_trx_id_sanity(trx_id_t id, const table_name_t &name);
159 
160  /** Check whether the changes by id are visible.
161  @param[in] id transaction id to check against the view
162  @param[in] name table name
163  @return whether the view sees the modifications of id. */
164  bool changes_visible(trx_id_t id, const table_name_t &name) const
165  MY_ATTRIBUTE((warn_unused_result)) {
166  ut_ad(id > 0);
167 
168  if (id < m_up_limit_id || id == m_creator_trx_id) {
169  return (true);
170  }
171 
173 
174  if (id >= m_low_limit_id) {
175  return (false);
176 
177  } else if (m_ids.empty()) {
178  return (true);
179  }
180 
181  const ids_t::value_type *p = m_ids.data();
182 
183  return (!std::binary_search(p, p + m_ids.size(), id));
184  }
185 
186  /**
187  @param id transaction to check
188  @return true if view sees transaction id */
189  bool sees(trx_id_t id) const { return (id < m_up_limit_id); }
190 
191  /**
192  Mark the view as closed */
193  void close() {
196  }
197 
198  /**
199  @return true if the view is closed */
200  bool is_closed() const { return (m_closed); }
201 
202  /**
203  Write the limits to the file.
204  @param file file to write to */
205  void print_limits(FILE *file) const {
206  fprintf(file,
207  "Trx read view will not see trx with"
208  " id >= " TRX_ID_FMT ", sees < " TRX_ID_FMT "\n",
210  }
211 
212  /** Check and reduce low limit number for read view. Used to
213  block purge till GTID is persisted on disk table.
214  @param[in] trx_no transaction number to check with */
215  void reduce_low_limit(trx_id_t trx_no) {
216  if (trx_no < m_low_limit_no) {
217  /* Save low limit number set for Read View for MVCC. */
219  m_low_limit_no = trx_no;
220  }
221  }
222 
223  /**
224  @return the low limit no */
225  trx_id_t low_limit_no() const { return (m_low_limit_no); }
226 
227  /**
228  @return the low limit id */
229  trx_id_t low_limit_id() const { return (m_low_limit_id); }
230 
231  /**
232  @return true if there are no transaction ids in the snapshot */
233  bool empty() const { return (m_ids.empty()); }
234 
235 #ifdef UNIV_DEBUG
236  /**
237  @return the view low limit number */
239 
240  /**
241  @param rhs view to compare with
242  @return truen if this view is less than or equal rhs */
243  bool le(const ReadView *rhs) const {
244  return (m_low_limit_no <= rhs->m_low_limit_no);
245  }
246 #endif /* UNIV_DEBUG */
247  private:
248  /**
249  Copy the transaction ids from the source vector */
250  inline void copy_trx_ids(const trx_ids_t &trx_ids);
251 
252  /**
253  Opens a read view where exactly the transactions serialized before this
254  point in time are seen in the view.
255  @param id Creator transaction id */
256  inline void prepare(trx_id_t id);
257 
258  /**
259  Copy state from another view. Must call copy_complete() to finish.
260  @param other view to copy from */
261  inline void copy_prepare(const ReadView &other);
262 
263  /**
264  Complete the copy, insert the creator transaction id into the
265  m_trx_ids too and adjust the m_up_limit_id *, if required */
266  inline void copy_complete();
267 
268  /**
269  Set the creator transaction id, existing id must be 0 */
271  ut_ad(m_creator_trx_id == 0);
273  }
274 
275  friend class MVCC;
276 
277  private:
278  // Disable copying
279  ReadView(const ReadView &);
280  ReadView &operator=(const ReadView &);
281 
282  private:
283  /** The read should not see any transaction with trx id >= this
284  value. In other words, this is the "high water mark". */
286 
287  /** The read should see all trx ids which are strictly
288  smaller (<) than this value. In other words, this is the
289  low water mark". */
291 
292  /** trx id of creating transaction, set to TRX_ID_MAX for free
293  views. */
295 
296  /** Set of RW transactions that was active when this snapshot
297  was taken */
299 
300  /** The view does not need to see the undo logs for transactions
301  whose transaction number is strictly smaller (<) than this value:
302  they can be removed in purge if not needed by other views */
304 
305 #ifdef UNIV_DEBUG
306  /** The low limit number up to which read views don't need to access
307  undo log records for MVCC. This could be higher than m_low_limit_no
308  if purge is blocked for GTID persistence. Currently used for debug
309  variable INNODB_PURGE_VIEW_TRX_ID_AGE. */
311 #endif /* UNIV_DEBUG */
312 
313  /** AC-NL-RO transaction view that has been "closed". */
314  bool m_closed;
315 
316  typedef UT_LIST_NODE_T(ReadView) node_t;
317 
318  /** List of read views in trx_sys */
319  byte pad1[64 - sizeof(node_t)];
320  node_t m_view_list;
321 };
322 
323 #endif
ReadView::copy_trx_ids
void copy_trx_ids(const trx_ids_t &trx_ids)
Copy the transaction ids from the source vector.
Definition: read0read.cc:358
ReadView::ids_t::push_back
void push_back(value_type value)
Append a value to the array.
Definition: read0read.cc:270
ReadView::prepare
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:428
ReadView::creator_trx_id
void creator_trx_id(trx_id_t id)
Set the creator transaction id, existing id must be 0.
Definition: read0types.h:270
ReadView::~ReadView
~ReadView()
ReadView destructor.
Definition: read0read.cc:327
trx_id_t
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:148
ReadView::m_creator_trx_id
trx_id_t m_creator_trx_id
trx id of creating transaction, set to TRX_ID_MAX for free views.
Definition: read0types.h:294
ReadView::ids_t::resize
void resize(ulint n)
Resize the array, sets the current element count.
Definition: read0types.h:71
ut_ad
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:65
ReadView::ids_t::~ids_t
~ids_t()
Destructor.
Definition: read0types.h:59
dict0mem.h
ReadView::low_limit_id
trx_id_t low_limit_id() const
Definition: read0types.h:229
ReadView::m_low_limit_no
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:303
ReadView::sees
bool sees(trx_id_t id) const
Definition: read0types.h:189
ReadView::low_limit_no
trx_id_t low_limit_no() const
Definition: read0types.h:225
ReadView::operator=
ReadView & operator=(const ReadView &)
ReadView::m_up_limit_id
trx_id_t m_up_limit_id
The read should see all trx ids which are strictly smaller (<) than this value.
Definition: read0types.h:290
ReadView::copy_complete
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:635
ReadView::copy_prepare
void copy_prepare(const ReadView &other)
Copy state from another view.
Definition: read0read.cc:609
value
const string value("\"Value\"")
ReadView::ids_t::back
value_type back() const
Definition: read0types.h:107
ReadView::check_trx_id_sanity
static void check_trx_id_sanity(trx_id_t id, const table_name_t &name)
Check whether transaction id is valid.
Definition: trx0sys.cc:62
ReadView::ids_t::size
ulint size() const
Definition: read0types.h:128
ReadView::m_closed
bool m_closed
AC-NL-RO transaction view that has been "closed".
Definition: read0types.h:314
TRX_ID_FMT
#define TRX_ID_FMT
printf(3) format used for printing DB_TRX_ID and other system fields
Definition: trx0types.h:50
ReadView::ids_t::capacity
ulint capacity() const
Definition: read0types.h:83
ReadView::m_view_low_limit_no
trx_id_t m_view_low_limit_no
The low limit number up to which read views don't need to access undo log records for MVCC.
Definition: read0types.h:310
ReadView::view_low_limit_no
trx_id_t view_low_limit_no() const
Definition: read0types.h:238
ReadView::print_limits
void print_limits(FILE *file) const
Write the limits to the file.
Definition: read0types.h:205
ReadView::ids_t::empty
bool empty() const
Definition: read0types.h:132
UT_DELETE_ARRAY
#define UT_DELETE_ARRAY(ptr)
Destroy, deallocate and trace the deallocation of an array created by UT_NEW_ARRAY() or UT_NEW_ARRAY_...
Definition: ut0new.h:1057
ReadView::ids_t::assign
void assign(const value_type *start, const value_type *end)
Copy and overwrite the current array contents.
Definition: read0read.cc:248
table_name_t
Table name wrapper for pretty-printing.
Definition: dict0mem.h:439
ReadView::ids_t::front
value_type front() const
Definition: read0types.h:99
ReadView::ids_t::m_reserved
ulint m_reserved
Size of m_ptr in elements.
Definition: read0types.h:147
MVCC
The MVCC read view manager.
Definition: read0read.h:43
ReadView::is_closed
bool is_closed() const
Definition: read0types.h:200
ReadView::reduce_low_limit
void reduce_low_limit(trx_id_t trx_no)
Check and reduce low limit number for read view.
Definition: read0types.h:215
ReadView::ids_t::value_type
trx_ids_t::value_type value_type
Definition: read0types.h:51
TRX_ID_MAX
#define TRX_ID_MAX
Maximum transaction identifier.
Definition: trx0types.h:155
ReadView::ids_t::ids_t
ids_t()
Constructor.
Definition: read0types.h:55
ReadView::ids_t::clear
void clear()
Reset the size to 0.
Definition: read0types.h:79
ReadView::ids_t::data
const trx_id_t * data() const
Definition: read0types.h:124
Vt100::value_type
uint16_t value_type
Definition: vt100.h:182
ReadView::ids_t
This is similar to a std::vector but it is not a drop in replacement.
Definition: read0types.h:50
trx0types.h
rules_table_service::end
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:188
ReadView::ids_t::data
trx_id_t * data()
Definition: read0types.h:120
ReadView::ids_t::operator=
ids_t & operator=(const ids_t &)
ReadView::ids_t::m_size
ulint m_size
Number of active elements in the array.
Definition: read0types.h:144
consts::FILE
const std::string FILE("FILE")
ReadView::m_low_limit_id
trx_id_t m_low_limit_id
The read should not see any transaction with trx id >= this value.
Definition: read0types.h:285
ReadView::ids_t::insert
void insert(value_type value)
Insert the value in the correct slot, preserving the order.
Definition: read0read.cc:283
ReadView::ids_t::m_ptr
value_type * m_ptr
Memory for the array.
Definition: read0types.h:141
ReadView::empty
bool empty() const
Definition: read0types.h:233
name
const string name("\"Name\"")
ut_d
#define ut_d(EXPR)
Debug statement.
Definition: ut0dbg.h:67
ReadView::le
bool le(const ReadView *rhs) const
Definition: read0types.h:243
ReadView
Read view lists the trx ids of those transactions for which a consistent read should not see the modi...
Definition: read0types.h:47
ReadView::ReadView
ReadView()
ReadView constructor.
Definition: read0read.cc:315
trx_ids_t
std::vector< trx_id_t, ut_allocator< trx_id_t > > trx_ids_t
Definition: trx0types.h:538
ReadView::m_ids
ids_t m_ids
Set of RW transactions that was active when this snapshot was taken.
Definition: read0types.h:298
ReadView::ids_t::reserve
void reserve(ulint n)
Try and increase the size of the array.
Definition: read0read.cc:218
ReadView::UT_LIST_NODE_T
typedef UT_LIST_NODE_T(ReadView) node_t
start
static void start(PluginFuncEnv *env)
Definition: http_server_plugin.cc:572
ReadView::m_view_list
node_t m_view_list
Definition: read0types.h:320
n
int n
Definition: xcom_base.c:425
ReadView::close
void close()
Mark the view as closed.
Definition: read0types.h:193
p
const char * p
Definition: ctype-mb.cc:1233
id
unsigned long id[MAX_DEAD]
Definition: xcom_base.c:426
ReadView::changes_visible
bool changes_visible(trx_id_t id, const table_name_t &name) const
Check whether the changes by id are visible.
Definition: read0types.h:164
ReadView::pad1
byte pad1[64 - sizeof(node_t)]
List of read views in trx_sys.
Definition: read0types.h:319