MySQL 26.7.0
Source Code Documentation
read0read.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/read0read.h
29 Cursor read
30
31 Created 2/16/1997 Heikki Tuuri
32 *******************************************************/
33
34#ifndef read0read_h
35#define read0read_h
36
37#include <stddef.h>
38#include <algorithm>
39
40#include "read0mvcc_interface.h"
41#include "read0types.h" // ReadView
42#include "univ.i"
43#include "ut0cpu_cache.h"
44
45/** The MVCC read view manager */
46class MVCC : public MVCC_interface {
47 /* To protect cache line with vtable pointer from being dirtied via false
48 sharing with other fields of this structure, we add padding here. We don't
49 use alignas(ut::INNODB_CACHE_LINE_SIZE), as we don't know if the particular
50 allocator used by the caller respect such large alignment requirements.
51 This is not a premature optimization - false sharing caused 16% TPS drop in
52 performance on sysbench OLTP uniform 64 users in READ COMMITTED. */
54
55 public:
56 /** Constructor.
57 @param size Number of views to pre-allocate */
58 explicit MVCC(ulint size);
59
60 /** Destructor.
61 Free all the views in the m_free list */
62 ~MVCC() override;
63
64 void initialize(trx_id_t max_committed_trx_id, trx_ids_t active_ids) override;
65 void view_open(Read_view_interface *&view, trx_t *trx) override;
66 void view_close(Read_view_interface *&view, bool own_mutex) override;
67 void clone_oldest_view(Read_view_interface *&view) override;
68 void view_free(Read_view_interface *&view) override;
69 [[nodiscard]] size_t get_open_views_count() const override;
70 void undo_purge_is_starting() override;
71 void undo_purge_has_shutdown() override;
72
73 private:
74 /** A helper for the interface method with the same name, which makes it
75 cleaner to assign to the referenced pointer while using the actual
76 implementation-specific type.
77
78 If trx is auto-commit non-locking transaction and view is not-null, it
79 attempts to reopen it. This fast path succeeds if the view is still the
80 freshest possible, in which case taking trx_sys mutex is avoided. Otherwise it
81 falls back to the slow path, which requires a trx_sys mutex to (re)initialize
82 the view.
83 @see view_open(Read_view_interface*&,trx_t*) */
84 void view_open(ReadView *&view, trx_t *trx);
85
86 /** A helper for the interface method with the same name, which makes it
87 cleaner to assign to the referenced pointer while using the actual
88 implementation-specific type.
89
90 In case own_mutex is true, it will move the view to the free list and assign
91 nullptr to the argument. Otherwise it only closes the view, without freeing
92 it.
93 @see view_close(Read_view_interface*&,bool) */
94 void view_close(ReadView *&view, bool own_mutex);
95
96 /** A helper for the interface method with the same name, which makes it
97 cleaner to assign to the referenced pointer while using the actual
98 implementation-specific type.
99
100 Clones the oldest view into the provided view, unless the function
101 determines that the provided view is already a good enough lower bound.
102 The caller owns the view that is passed in, which is interpreted to be a
103 previous lower bound known to the caller.
104 No need to call view_close(view,..).
105
106 Note: This function is called by Purge to determine the purge_sys->view used
107 to distinguish which transactions are considered committed by everybody, and
108 thus their undo logs can be purged.
109 Purge mainly uses purge_sys->view->low_limit_no(), which is a safe
110 lower-bound on what can be purged based on NO, and further limits it to the
111 lowest needed NO reported by GTID Persistor. But other places like ROLLBACK
112 use purge_sys->view->changes_visible(ID,..).
113 @param[in,out] view Preallocated view, owned by the caller. Can be either
114 default constructed (m_low_limit_no is 0) or a fully
115 initialized ReadView object.
116 @see clone_oldest_view(Read_view_interface*&) */
117 void clone_oldest_view(ReadView *&view);
118
119 public:
120 [[nodiscard]] bool is_view_open(
121 const Read_view_interface *view) const override {
122 return view != nullptr && !((ReadView *)view)->is_closed();
123 }
124
126 trx_id_t id) override {
127 ut_ad(id > 0);
128
129 ((ReadView *)view)->creator_trx_id(id);
130 }
131
132 private:
133 /** Asserts the read view list is sorted. */
134 void validate() const;
135
136 /** Get a view from the free list, or allocate a new one if it's empty.
137 @return a view to use */
138 inline ReadView *get_view();
139
140 MVCC(const MVCC &) = delete;
141 MVCC &operator=(const MVCC &) = delete;
142
143 private:
144 typedef UT_LIST_BASE_NODE_T(ReadView, m_view_list) view_list_t;
145
146 /** Free views ready for reuse. */
147 view_list_t m_free;
148
149 /** Active and closed views. */
150 view_list_t m_views;
151};
152
153#endif /* read0read_h */
The MVCC read view manager.
Definition: read0mvcc_interface.h:89
The MVCC read view manager.
Definition: read0read.h:46
MVCC(const MVCC &)=delete
void set_view_creator_trx_id(Read_view_interface *&view, trx_id_t id) override
Sets the view creator transaction id, when a transaction which previously called view_open(view,...
Definition: read0read.h:125
view_list_t m_free
Free views ready for reuse.
Definition: read0read.h:147
ReadView * get_view()
Get a view from the free list, or allocate a new one if it's empty.
Definition: read0read.cc:466
~MVCC() override
Destructor.
Definition: read0read.cc:335
MVCC & operator=(const MVCC &)=delete
void view_close(Read_view_interface *&view, bool own_mutex) override
Closes a view previously opened by view_open(..).
Definition: read0read.cc:774
void view_open(Read_view_interface *&view, trx_t *trx) override
If view is nullptr then allocates a view, otherwise, reuses the one provided, in both cases opening i...
Definition: read0read.cc:485
view_list_t m_views
Active and closed views.
Definition: read0read.h:150
bool is_view_open(const Read_view_interface *view) const override
Can be used only for nullptr or view assigned by view_open(view,..) or view_close(view,...
Definition: read0read.h:120
void initialize(trx_id_t max_committed_trx_id, trx_ids_t active_ids) override
Initializes the MVCC once the max_committed_trx_id estimate is learned from reading system tablespace...
Definition: read0read.cc:330
void clone_oldest_view(Read_view_interface *&view) override
Clones the oldest open view or if there is no open view at the moment, clones one which would be crea...
Definition: read0read.cc:706
typedef UT_LIST_BASE_NODE_T(ReadView, m_view_list) view_list_t
void undo_purge_has_shutdown() override
Instructs the MVCC that the Undo Purge was shutdown and will not be asking to clone the oldest Read V...
Definition: read0read.cc:772
size_t get_open_views_count() const override
Returns the number of open views.
Definition: read0read.cc:754
void undo_purge_is_starting() override
Instructs the MVCC that the Undo Purge is about to start working and MVCC will be asked to clone the ...
Definition: read0read.cc:770
void validate() const
Asserts the read view list is sorted.
Definition: read0read.cc:200
MVCC(ulint size)
Constructor.
Definition: read0read.cc:322
char _padding[ut::INNODB_CACHE_LINE_SIZE]
Definition: read0read.h:53
void view_free(Read_view_interface *&view) override
Makes sure the view is properly freed: if view is already nullptr does nothing, otherwise it must be ...
Definition: read0read.cc:671
Read view lists the trx ids of those transactions for which a consistent read should not see the modi...
Definition: read0types.h:49
Definition: read0read_view_interface.h:33
size_t size(const char *const c)
Definition: base64.h:46
constexpr size_t INNODB_CACHE_LINE_SIZE
CPU cache line size.
Definition: ut0cpu_cache.h:41
Cursor read.
Definition: trx0trx.h:670
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
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:403
Utilities related to CPU cache.
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:109