MySQL 26.10.0
Source Code Documentation
mysql_thd_attributes.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MYSQL_THD_ATTRIBUTES_H
25#define MYSQL_THD_ATTRIBUTES_H
26
29
30/**
31 @ingroup group_components_services_inventory
32
33 THD Attributes service allows to obtain data associated with the THD
34 object, which keeps various attributes of the user session.
35
36 Currently, following attributes are supported:
37
38 - Query Digest Text
39 - Query Text
40 - Host or IP
41 - Schema
42 - Command String ("command" of the return mysql_cstring_with_length type)
43 - SQL Command String ("sql_command" of the return mysql_cstring_with_length
44 type)
45 - Is a Server substatement ("is_substatement" of the returned bool type).
46 Stored functions and triggers are substatements; directly invoked stored
47 procedures are not.
48 - Query Character Set ("query_charset" of the return mysql_cstring_with_length
49 type)
50
51 @section thd_attributes_init Initialization
52
53 The service can be instantiated using the registry service with the
54 "mysql_thd_attributes" name.
55
56 @code
57 SERVICE_TYPE(registry) *registry = mysql_plugin_registry_acquire();
58 my_service<SERVICE_TYPE(mysql_thd_attributes)>
59 svc("mysql_thd_attributes", registry);
60 if (svc.is_valid()) {
61 // The service is ready to be used
62 }
63 @endcode
64
65 @section thd_attributes_query_digest_text Query Digest Text
66
67 Query Digest represents converted SQL statement to normalized form. The code
68 below demonstrates how query digest can be obtained from the service.
69
70 @code
71 my_h_string str;
72
73 mysql_thd_attributes->get(m_thd, "query_digest",
74 reinterpret_cast<void *>(&str));
75 @endcode
76
77 The buffer can be fetched using the code below:
78
79 @code
80 char buf[1024]; // buffer must be big enough to store the digest
81
82 mysql_string_converter->convert_to_buffer(str, buf, sizeof(buf), "utf8mb3");
83 @endcode
84
85 After the string content has been copied into another buffer, it must be
86 destroyed:
87
88 @code
89 mysql_string_factory->destroy(str);
90 @endcode
91
92 @section thd_attributes_query_text Query Text
93
94 Query text represents complete SQL text currently being executed within a
95 given session. The example code is identical to the one for Query Digest, the
96 only difference is that attribute name "sql_text" should be used in this
97 case.
98
99 @section thd_attributes_host_or_ip Host or IP
100
101 Host or IP text contains host or IP (if host unavailable) belonging to a
102 client associated with a given session. The example code is identical to the
103 one for Query Digest, the only difference is that attribute name "host_or_ip"
104 should be used in this case.
105
106 @section thd_attributes_schema Schema
107
108 Schema text contains a name of database currently in use by a
109 client associated with a given session. The example code is identical to the
110 one for Query Digest, the only difference is that attribute name "schema"
111 should be used in this case.
112*/
113
115
116/**
117 Get THD attribute.
118
119 Currently, following attributes are supported:
120
121 - Query Digest Text ("query_digest" of the returned my_h_string type)
122 - Query Text ("sql_text" of the returned my_h_string type)
123 - Host or IP ("host_or_ip" of the returned my_h_string type)
124 - Schema ("schema" of the returned my_h_string type)
125 - Is Upgrade Thread ("is_upgrade_thread" of the returned bool type)
126 - Is Init File System Thread ("is_init_file_thread" of the returned bool type)
127 - Command String ("command" of the return mysql_cstring_with_length type)
128 - SQL Command String ("sql_command" of the return mysql_cstring_with_length
129 type)
130 - Is a Server substatement ("is_substatement" of the returned bool type).
131 Stored functions and triggers are substatements; directly invoked stored
132 procedures are not.
133 - Query Character Set ("query_charset" of the return mysql_cstring_with_length
134 type)
135 - status variable ("thd_status" of the returned uint16 type). if session is
136 OK, session is killed, query is killed or timeout
137 - time-zone name variable ("time_zone_name" of the returned
138 mysql_cstring_with_length type)
139 - Query execution status ("da_status" of the returned uint16 type).
140
141 @param thd Session THD object.
142 @param name Name of the attribute to be set.
143 @param[out] inout_pvalue Iterator pointer.
144
145 @return
146 @retval FALSE Succeeded.
147 @retval TRUE Failed.
148*/
149DECLARE_BOOL_METHOD(get, (MYSQL_THD thd, const char *name, void *inout_pvalue));
150
151/**
152 Set THD attribute.
153
154 Currently the implementation does not support setting of any attribute.
155
156 @param thd Session THD object.
157 @param name Name of the attribute to be set.
158 @param[out] inout_pvalue Iterator pointer.
159
160 @return The function always fail and return TRUE value.
161*/
162DECLARE_BOOL_METHOD(set, (MYSQL_THD thd, const char *name, void *inout_pvalue));
163
165
166#endif /* MYSQL_THD_ATTRIBUTES_H */
#define MYSQL_THD
Definition: backup_page_tracker.h:38
static mysql_service_status_t get(THD **thd) noexcept
Definition: mysql_current_thread_reader_all_empty.cc:31
std::set< Key, Compare, ut::allocator< Key > > set
Specialization of set which uses ut_allocator.
Definition: ut0new.h:2732
const mysql_service_mysql_thd_attributes_t * mysql_thd_attributes
Definition: rewriter_plugin.cc:59
#define END_SERVICE_DEFINITION(name)
A macro to end the last Service definition started with the BEGIN_SERVICE_DEFINITION macro.
Definition: service.h:91
#define BEGIN_SERVICE_DEFINITION(name)
Declares a new Service.
Definition: service.h:86
#define DECLARE_BOOL_METHOD(name, args)
Declares a method that returns bool as a part of the Service definition.
Definition: service.h:112
case opt name
Definition: sslopt-case.h:29