MySQL 26.7.0
Source Code Documentation
read0mvcc_interface.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2023, 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#pragma once
28
29#include "trx0types.h"
31
32/** The MVCC read view manager.
33A read view object can be in "open" or "closed" state.
34The "open" state is meant to mean that a transaction is using it at the moment.
35The "closed" state is meant to mean that it is not currently used by any
36transaction, but wasn't freed either, for performance reasons - it can be
37cheaply reopened by using view_open(view,..) on it again.
38A read view obtained by view_open(view,..) will be "open".
39It can then be closed by view_close(view,..) in which case it might also get
40freed.
41If it wasn't freed, then view will still be not nullptr, but is_view_open(view)
42will return false.
43You should free such "closed" views with view_free(view).
44All "open" read-views are tracked by MVCC_interface instance, and the oldest of
45them can be cloned using clone_oldest_view(), but the clone itself will be
46"closed". A cloned view is special in that it can't be opened with view_open(..)
47and should not be passed to is_view_open(..) - it's implicitly guaranteed to be
48always closed, so no need to check.
49The ownership of the view is passed to the caller of view_open(view,..) or
50clone_oldest_view(view,..), and the view remains in the caller ownership as long
51as the pointer is not-null. The view_close(view,...) should be called for views
52obtained with view_open(..) and it may set view to nullptr if it has chosen to
53free the object. Closed read views, such that those resulting from calling
54view_close(view,..) or clone_oldest_view(view,..), should be freed with
55view_free(view,..) which always sets view to nullptr.
56
57So, a typical usage would be something like:
58
59 trx->read_view = nullptr;
60 trx_sys->mvcc->view_open(trx->read_view, trx);
61 ut_a(trx->read_view != nullptr);
62 ut_a(trx_sys->mvcc->is_view_open(trx->read_view));
63
64 //...use the view...
65
66 trx_sys->mvcc->view_close(trx->read_view, ...);
67 // trx->read_view will now be closed, perhaps even nullptr
68 ut_a(!trx_sys->mvcc->is_view_open(trx->read_view));
69
70 // When you are sure trx will never reopen trx->read_view
71 // say, because you're going to free the trx object itself,
72 // then make sure to free the read view, after closing it.
73 // (You don't have to do that if trx->read_view is already
74 // nullptr, but it wouldn't hurt).
75 trx_sys->view_free(trx->read_view);
76 ut_a(trx->read_view == nullptr);
77
78And usage in purge is for example:
79
80 purge_sys->view = nullptr;
81 trx_sys->mvcc->clone_oldest_view(purge_sys->view);
82 ut_a(purge_sys->view != nullptr);
83
84 //...use the view...
85
86 trx_sys->mvcc->view_free(purge_sys->view);
87 ut_a(purge_sys->view == nullptr);
88*/
90 public:
91 /** Initializes the MVCC once the max_committed_trx_id estimate is learned
92 from reading system tablespace's header TRX_SYS_TRX_ID_STORE field.
93 @param[in] max_assigned_trx_id
94 The upper-bound on highest assigned trx id. No record in
95 any B-tree can currently have DB_TRX_ID larger than this.
96 No Undo Log can have TRX_UNDO_TRX_NO or TRX_UNDO_TRX_ID
97 larger than this.
98 @param[in] active_ids
99 The set of ids of currently active transactions.
100 They should all be at <= max_committed_trx_id.
101 */
102 virtual void initialize(trx_id_t max_assigned_trx_id,
103 trx_ids_t active_ids) = 0;
104
105 /** Destructor.*/
106 virtual ~MVCC_interface() = default;
107
108 /** If view is nullptr then allocates a view, otherwise, reuses the one
109 provided, in both cases opening it. That is, after the call view is not null,
110 view members can be accessed, and view->is_closed() == false.
111 It is guaranteed that clone_oldest_view(v2) calls which happen-before the call
112 to view_close(view,...) for this view, will clone a v2 which is not fresher
113 than this view.
114 @param[in,out] view
115 Must be either nullptr or a result of view_close(..), which
116 in turn should be a result of an earlier view_open(..). It
117 must not be a result of clone_oldest_view(..).
118 Upon return it will be not null and open.
119 Must be closed by calling view_close(..). If it is still
120 not nullptr after view_close(..) it must be passed to
121 view_free(..) to free it.
122 @param[in] trx
123 Transaction instance of caller */
124 virtual void view_open(Read_view_interface *&view, trx_t *trx) = 0;
125
126 /** Closes a view previously opened by view_open(..). It's safe to call it on
127 a view which was already closed. After the call, it is unsafe to access
128 view's members, because it might be freed by this function (in which case
129 view is nullptr). Even if the view is still not null after the call, it is at
130 least in the closed state - so can be reopen again with view_open(..) or freed
131 with view_free(..).
132 @param[in,out] view
133 Must not be nullptr. Must be a pointer obtained from
134 view_open(..) or view_close(..). Upon return it will be
135 either nullptr or pointing to a closed view.
136 @param[in] own_mutex
137 true if the caller owns the trx_sys->mutex */
138 virtual void view_close(Read_view_interface *&view, bool own_mutex) = 0;
139
140 /** Makes sure the view is properly freed: if view is already nullptr does
141 nothing, otherwise it must be already closed, and this function will take
142 trx_sys->mutex, free the view, and set it to nullptr.
143 @param[in,out] view
144 nullptr or a closed view. Upon return it will be nullptr.
145 */
146 virtual void view_free(Read_view_interface *&view) = 0;
147
148 /** Clones the oldest open view or if there is no open view at the moment,
149 clones one which would be created if view_open(..) was called.
150 The view provided by a caller must be either nullptr or a closed view obtained
151 from an earlier call to clone_oldest_view(..).
152 This function will either allocate a new view or clone into view provided.
153 After the call view will be considered closed.
154 It must be freed by the caller using view_free(view) when no longer needed.
155 @param[in,out] view
156 A pointer to a closed view obtained from an earlier call to
157 this method or a nullptr. It must not be a result of
158 view_close(..) or view_open(..). Upon return it will
159 point to a closed view which is a clone of the oldest open
160 read view found during the call. It should be freed with
161 view_free(..). Do not call view_open(..) on it. */
162 virtual void clone_oldest_view(Read_view_interface *&view) = 0;
163
164 /** Instructs the MVCC that the Undo Purge is about to start working and MVCC
165 will be asked to clone the oldest Read View. */
166 virtual void undo_purge_is_starting() = 0;
167
168 /** Instructs the MVCC that the Undo Purge was shutdown and will not be asking
169 to clone the oldest Read View or any other MVCC anymore. To be called before
170 trx_sys is being destroyed. */
171 virtual void undo_purge_has_shutdown() = 0;
172
173 /** Returns the number of open views. */
174 [[nodiscard]] virtual size_t get_open_views_count() const = 0;
175
176 /** Can be used only for nullptr or view assigned by view_open(view,..) or
177 view_close(view,..) or clone_oldest_view(view,..).
178 (note, though that clone_oldest_view always gives you a closed view,
179 so no need to check).
180 @param[in] view
181 The view to check. Can be nullptr.
182 @return true if the view is not nullptr and is open */
183 [[nodiscard]] virtual bool is_view_open(
184 const Read_view_interface *view) const = 0;
185
186 /** Sets the view creator transaction id, when a transaction which previously
187 called view_open(view,..) when it was read-only (and thus had trx->id==0), has
188 transitioned to a RW state and got a trx->id assigned.
189 One can imagine an implementation which could change the view instance to
190 another (say: from one shared with other read-only transactions to one
191 tailored to this particular RW transaction, which should see its own writes).
192 @param[in,out] view
193 Set the creator trx id for this view.
194 The view should be already open first by
195 view_open(view, trx) when trx->id was 0.
196 @param[in] id
197 Transaction id to set, should be the trx->id assigned to
198 the same trx for which view_open(view, trx) was called
199 earlier.
200 */
202 trx_id_t id) = 0;
203};
The MVCC read view manager.
Definition: read0mvcc_interface.h:89
virtual void view_close(Read_view_interface *&view, bool own_mutex)=0
Closes a view previously opened by view_open(..).
virtual void set_view_creator_trx_id(Read_view_interface *&view, trx_id_t id)=0
Sets the view creator transaction id, when a transaction which previously called view_open(view,...
virtual bool is_view_open(const Read_view_interface *view) const =0
Can be used only for nullptr or view assigned by view_open(view,..) or view_close(view,...
virtual void clone_oldest_view(Read_view_interface *&view)=0
Clones the oldest open view or if there is no open view at the moment, clones one which would be crea...
virtual void undo_purge_has_shutdown()=0
Instructs the MVCC that the Undo Purge was shutdown and will not be asking to clone the oldest Read V...
virtual void initialize(trx_id_t max_assigned_trx_id, trx_ids_t active_ids)=0
Initializes the MVCC once the max_committed_trx_id estimate is learned from reading system tablespace...
virtual void undo_purge_is_starting()=0
Instructs the MVCC that the Undo Purge is about to start working and MVCC will be asked to clone the ...
virtual size_t get_open_views_count() const =0
Returns the number of open views.
virtual void view_free(Read_view_interface *&view)=0
Makes sure the view is properly freed: if view is already nullptr does nothing, otherwise it must be ...
virtual ~MVCC_interface()=default
Destructor.
virtual void view_open(Read_view_interface *&view, trx_t *trx)=0
If view is nullptr then allocates a view, otherwise, reuses the one provided, in both cases opening i...
Definition: read0read_view_interface.h:33
Definition: trx0trx.h:670
Transaction system global type definitions.
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