MySQL 9.7.0
Source Code Documentation
mysql_statement.h
Go to the documentation of this file.
1/* Copyright (c) 2010, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MYSQL_STATEMENT_H
25#define MYSQL_STATEMENT_H
26
27/**
28 @file include/mysql/psi/mysql_statement.h
29 Instrumentation helpers for statements.
30*/
31
32#include "my_compiler.h"
33
34/* HAVE_PSI_*_INTERFACE */
35#include "my_psi_config.h" // IWYU pragma: keep
36
37#include "my_inttypes.h"
38#include "mysql/psi/psi_stage.h"
40
41#if defined(MYSQL_SERVER) || defined(PFS_DIRECT_CALL)
42/* PSI_STATEMENT_CALL() as direct call. */
43/* PSI_DIGEST_CALL() as direct call. */
44#include "pfs_statement_provider.h" // IWYU pragma: keep
45/* PSI_STAGE_CALL() as direct call. */
46#include "pfs_stage_provider.h" // IWYU pragma: keep
47#endif
48
50struct CHARSET_INFO;
51
52#ifndef PSI_STATEMENT_CALL
53#define PSI_STATEMENT_CALL(M) psi_statement_service->M
54#endif
55
56#ifndef PSI_DIGEST_CALL
57#define PSI_DIGEST_CALL(M) psi_statement_service->M
58#endif
59
60#ifndef PSI_STAGE_CALL
61#define PSI_STAGE_CALL(M) psi_stage_service->M
62#endif
63
64/**
65 @defgroup psi_api_statement Statement Instrumentation (API)
66 @ingroup psi_api
67 @{
68*/
69
70/**
71 @def mysql_statement_register(P1, P2, P3)
72 Statement registration.
73*/
74#define mysql_statement_register(P1, P2, P3) \
75 inline_mysql_statement_register(P1, P2, P3)
76
77#ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
78#define MYSQL_DIGEST_START(LOCKER) inline_mysql_digest_start(LOCKER)
79#else
80#define MYSQL_DIGEST_START(LOCKER) NULL
81#endif
82
83#ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
84#define MYSQL_DIGEST_END(LOCKER, DIGEST) inline_mysql_digest_end(LOCKER, DIGEST)
85#else
86#define MYSQL_DIGEST_END(LOCKER, DIGEST) \
87 do { \
88 } while (0)
89#endif
90
91#ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
92#define MYSQL_DIGEST_SET(LOCKER, DIGEST) inline_mysql_digest_set(LOCKER, DIGEST)
93#else
94#define MYSQL_DIGEST_SET(LOCKER, DIGEST) \
95 do { \
96 } while (0)
97#endif
98
99#ifdef HAVE_PSI_STATEMENT_INTERFACE
100#define MYSQL_START_STATEMENT(STATE, K, DB, DB_LEN, CS, SPS) \
101 inline_mysql_start_statement(STATE, K, DB, DB_LEN, CS, SPS, __FILE__, \
102 __LINE__)
103#else
104#define MYSQL_START_STATEMENT(STATE, K, DB, DB_LEN, CS, SPS) NULL
105#endif
106
107#ifdef HAVE_PSI_STATEMENT_INTERFACE
108#define MYSQL_REFINE_STATEMENT(LOCKER, K) \
109 inline_mysql_refine_statement(LOCKER, K)
110#else
111#define MYSQL_REFINE_STATEMENT(LOCKER, K) NULL
112#endif
113
114#ifdef HAVE_PSI_STATEMENT_INTERFACE
115#define MYSQL_SET_STATEMENT_TEXT(LOCKER, P1, P2) \
116 inline_mysql_set_statement_text(LOCKER, P1, P2)
117#else
118#define MYSQL_SET_STATEMENT_TEXT(LOCKER, P1, P2) \
119 do { \
120 } while (0)
121#endif
122
123#ifdef HAVE_PSI_STATEMENT_INTERFACE
124#define MYSQL_SET_STATEMENT_QUERY_ID(LOCKER, P1) \
125 inline_mysql_set_statement_query_id(LOCKER, P1)
126#else
127#define MYSQL_SET_STATEMENT_QUERY_ID(LOCKER, P1) \
128 do { \
129 } while (0)
130#endif
131
132#ifdef HAVE_PSI_STATEMENT_INTERFACE
133#define MYSQL_SET_STATEMENT_LOCK_TIME(LOCKER, P1) \
134 inline_mysql_set_statement_lock_time(LOCKER, P1)
135#else
136#define MYSQL_SET_STATEMENT_LOCK_TIME(LOCKER, P1) \
137 do { \
138 } while (0)
139#endif
140
141#ifdef HAVE_PSI_STATEMENT_INTERFACE
142#define MYSQL_SET_STATEMENT_ROWS_SENT(LOCKER, P1) \
143 inline_mysql_set_statement_rows_sent(LOCKER, P1)
144#else
145#define MYSQL_SET_STATEMENT_ROWS_SENT(LOCKER, P1) \
146 do { \
147 } while (0)
148#endif
149
150#ifdef HAVE_PSI_STATEMENT_INTERFACE
151#define MYSQL_SET_STATEMENT_ROWS_EXAMINED(LOCKER, P1) \
152 inline_mysql_set_statement_rows_examined(LOCKER, P1)
153#else
154#define MYSQL_SET_STATEMENT_ROWS_EXAMINED(LOCKER, P1) \
155 do { \
156 } while (0)
157#endif
158
159#ifdef HAVE_PSI_STATEMENT_INTERFACE
160#define MYSQL_NOTIFY_STATEMENT_QUERY_ATTRIBUTES(LOCKER, P1) \
161 inline_mysql_notify_statement_query_attributes(LOCKER, P1)
162#else
163#define MYSQL_NOTIFY_STATEMENT_QUERY_ATTRIBUTES(LOCKER, P1) \
164 do { \
165 } while (0)
166#endif
167
168#ifdef HAVE_PSI_STATEMENT_INTERFACE
169#define MYSQL_END_STATEMENT(LOCKER, DA) inline_mysql_end_statement(LOCKER, DA)
170#else
171#define MYSQL_END_STATEMENT(LOCKER, DA) \
172 do { \
173 } while (0)
174#endif
175
178 const char *category, PSI_statement_info *info, int count
179#else
180 const char *category [[maybe_unused]], void *info [[maybe_unused]],
181 int count [[maybe_unused]]
182#endif
183) {
184#ifdef HAVE_PSI_STATEMENT_INTERFACE
185 PSI_STATEMENT_CALL(register_statement)(category, info, count);
186#endif
187}
188
189#ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
191 PSI_statement_locker *locker) {
192 PSI_digest_locker *digest_locker = nullptr;
193
194 if (likely(locker != nullptr)) {
195 digest_locker = PSI_DIGEST_CALL(digest_start)(locker);
196 }
197 return digest_locker;
198}
199#endif
200
201#ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
203 const sql_digest_storage *digest) {
204 if (likely(locker != nullptr)) {
205 PSI_DIGEST_CALL(digest_end)(locker, digest);
206 }
207}
208#endif
209
210#ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
212 const sql_digest_storage *digest) {
213 if (likely(locker != nullptr)) {
214 PSI_DIGEST_CALL(digest_set)(locker, digest);
215 }
216}
217#endif
218
219#ifdef HAVE_PSI_STATEMENT_INTERFACE
221 PSI_statement_locker_state *state, PSI_statement_key key, const char *db,
222 uint db_len, const CHARSET_INFO *charset, PSI_sp_share *sp_share,
223 const char *src_file, int src_line) {
224 PSI_statement_locker *locker;
225 locker = PSI_STATEMENT_CALL(get_thread_statement_locker)(state, key, charset,
226 sp_share);
227 if (likely(locker != nullptr)) {
228 PSI_STATEMENT_CALL(start_statement)(locker, db, db_len, src_file, src_line);
229 }
230 return locker;
231}
232
235 if (likely(locker != nullptr)) {
236 locker = PSI_STATEMENT_CALL(refine_statement)(locker, key);
237 }
238 return locker;
239}
240
242 const char *text,
243 uint text_len) {
244 if (likely(locker != nullptr)) {
245 PSI_STATEMENT_CALL(set_statement_text)(locker, text, text_len);
246 }
247}
248
250 PSI_statement_locker *locker, ulonglong id) {
251 if (likely(locker != nullptr)) {
252 PSI_STATEMENT_CALL(set_statement_query_id)(locker, id);
253 }
254}
255
258 if (likely(locker != nullptr)) {
259 PSI_STATEMENT_CALL(set_statement_lock_time)(locker, count);
260 }
261}
262
265 if (likely(locker != nullptr)) {
266 PSI_STATEMENT_CALL(set_statement_rows_sent)(locker, count);
267 }
268}
269
272 if (likely(locker != nullptr)) {
273 PSI_STATEMENT_CALL(set_statement_rows_examined)(locker, count);
274 }
275}
276
278 PSI_statement_locker *locker, bool with_query_attributes) {
279 if (likely(locker != nullptr)) {
280 PSI_STATEMENT_CALL(notify_statement_query_attributes)
281 (locker, with_query_attributes);
282 }
283}
284
285static inline void inline_mysql_end_statement(
286 struct PSI_statement_locker *locker, Diagnostics_area *stmt_da) {
287#ifdef HAVE_PSI_STAGE_INTERFACE
288 PSI_STAGE_CALL(end_stage)();
289#endif /* HAVE_PSI_STAGE_INTERFACE */
290 if (likely(locker != nullptr)) {
291 PSI_STATEMENT_CALL(end_statement)(locker, stmt_da);
292 }
293}
294#endif /* HAVE_PSI_STATEMENT_INTERFACE */
295
297 PSI_statement_locker *locker [[maybe_unused]],
298 bool secondary [[maybe_unused]]) {
299#ifdef HAVE_PSI_STATEMENT_INTERFACE
300 if (likely(locker != nullptr)) {
301 PSI_STATEMENT_CALL(set_statement_secondary_engine)(locker, secondary);
302 }
303#endif /* HAVE_PSI_STATEMENT_INTERFACE */
304}
305
306/** @} (end of group psi_api_statement) */
307
308#endif
Stores status of the currently executed statement.
Definition: sql_error.h:274
#define PSI_STAGE_CALL(M)
Definition: psi_stage.h:36
#define PSI_STATEMENT_CALL(M)
Definition: psi_statement.h:36
struct PSI_digest_locker PSI_digest_locker
Definition: psi_statement_bits.h:119
unsigned int PSI_statement_key
Instrumented statement key.
Definition: psi_statement_bits.h:49
struct PSI_sp_share PSI_sp_share
Definition: psi_statement_bits.h:126
struct PSI_statement_locker PSI_statement_locker
Definition: psi_statement_bits.h:105
static void inline_mysql_set_statement_query_id(PSI_statement_locker *locker, ulonglong id)
Definition: mysql_statement.h:249
static struct PSI_statement_locker * inline_mysql_refine_statement(PSI_statement_locker *locker, PSI_statement_key key)
Definition: mysql_statement.h:233
static void inline_mysql_set_statement_rows_examined(PSI_statement_locker *locker, ulonglong count)
Definition: mysql_statement.h:270
static void inline_mysql_set_statement_text(PSI_statement_locker *locker, const char *text, uint text_len)
Definition: mysql_statement.h:241
static void inline_mysql_digest_end(PSI_digest_locker *locker, const sql_digest_storage *digest)
Definition: mysql_statement.h:202
static struct PSI_digest_locker * inline_mysql_digest_start(PSI_statement_locker *locker)
Definition: mysql_statement.h:190
static void mysql_statement_set_secondary_engine(PSI_statement_locker *locker, bool secondary)
Definition: mysql_statement.h:296
static void inline_mysql_statement_register(const char *category, PSI_statement_info *info, int count)
Definition: mysql_statement.h:176
static void inline_mysql_end_statement(struct PSI_statement_locker *locker, Diagnostics_area *stmt_da)
Definition: mysql_statement.h:285
static void inline_mysql_digest_set(PSI_statement_locker *locker, const sql_digest_storage *digest)
Definition: mysql_statement.h:211
static void inline_mysql_set_statement_rows_sent(PSI_statement_locker *locker, ulonglong count)
Definition: mysql_statement.h:263
static void inline_mysql_notify_statement_query_attributes(PSI_statement_locker *locker, bool with_query_attributes)
Definition: mysql_statement.h:277
static struct PSI_statement_locker * inline_mysql_start_statement(PSI_statement_locker_state *state, PSI_statement_key key, const char *db, uint db_len, const CHARSET_INFO *charset, PSI_sp_share *sp_share, const char *src_file, int src_line)
Definition: mysql_statement.h:220
static void inline_mysql_set_statement_lock_time(PSI_statement_locker *locker, ulonglong count)
Definition: mysql_statement.h:256
Header for compiler-dependent features.
constexpr bool likely(bool expr)
Definition: my_compiler.h:57
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
#define HAVE_PSI_STATEMENT_INTERFACE
Definition: my_psi_config.h:146
static int count
Definition: myisam_ftdump.cc:45
const std::string charset("charset")
static const char * category
Definition: sha2_password.cc:190
Performance schema instrumentation (declarations).
Performance schema instrumentation (declarations).
#define PSI_DIGEST_CALL(M)
Definition: pfs_statement_provider.h:51
Performance schema instrumentation interface.
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Definition: m_ctype.h:421
Statement instrument information.
Definition: psi_statement_bits.h:140
State data storage for get_thread_statement_locker_v5_t.
Definition: psi_statement_bits.h:173
Structure to store token count/array for a statement on which digest is to be calculated.
Definition: sql_digest.h:98
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510