/* Copyright (c) 2014, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file statement_events.h
@brief Contains the classes representing statement events occurring in the
replication stream. Each event is represented as a byte sequence with logical
divisions as event header, event specific data and event footer. The header
and footer are common to all the events and are represented as two different
subclasses.
*/
#ifndef MYSQL_BINLOG_EVENT_STATEMENT_EVENTS_H
#define MYSQL_BINLOG_EVENT_STATEMENT_EVENTS_H
#include "mysql/binlog/event/control_events.h"
#include "mysql/udf_registration_types.h"
/// @addtogroup GroupLibsMysqlBinlogEvent
/// @{
namespace mysql::binlog::event {
/**
The following constant represents the maximum of MYSQL_XID domain.
The maximum XID value practically is never supposed to grow beyond UINT64
range.
*/
const uint64_t INVALID_XID = 0xffffffffffffffffULL;
/**
@class Query_event
A @c Query_event is created for each query that modifies the
database, unless the query is logged row-based.
@section Query_event_binary_format Binary format
See @ref Binary_log_event_binary_format "Binary format for log events" for
a general discussion and introduction to the binary format of binlog
events.
The Post-Header has five components:
Status variables for Query_event
Status variable |
1 byte identifier |
Format |
Description |
flags2 |
Q_FLAGS2_CODE == 0 |
4 byte bitfield |
The flags in @c thd->options, binary AND-ed with @c
OPTIONS_WRITTEN_TO_BIN_LOG. The @c thd->options bitfield contains
options for "SELECT". @c OPTIONS_WRITTEN identifies those options
that need to be written to the binlog (not all do). Specifically,
@c OPTIONS_WRITTEN_TO_BIN_LOG equals (@c OPTION_AUTO_IS_NULL | @c
OPTION_NO_FOREIGN_KEY_CHECKS | @c OPTION_RELAXED_UNIQUE_CHECKS |
@c OPTION_NOT_AUTOCOMMIT), or 0x0c084000 in hex.
These flags correspond to the SQL variables SQL_AUTO_IS_NULL,
FOREIGN_KEY_CHECKS, UNIQUE_CHECKS, and AUTOCOMMIT, documented in
the "SET Syntax" section of the MySQL Manual.
This field is always written to the binlog in version >= 5.0, and
never written in version < 5.0.
|
sql_mode |
Q_SQL_MODE_CODE == 1 |
8 byte bitfield |
The @c sql_mode variable. See the section "SQL Modes" in the
MySQL manual, and see sql_class.h for a list of the possible
flags. Currently (2007-10-04), the following flags are available:
MODE_REAL_AS_FLOAT==0x1
MODE_PIPES_AS_CONCAT==0x2
MODE_ANSI_QUOTES==0x4
MODE_IGNORE_SPACE==0x8
MODE_NOT_USED==0x10
MODE_ONLY_FULL_GROUP_BY==0x20
MODE_NO_UNSIGNED_SUBTRACTION==0x40
MODE_NO_DIR_IN_CREATE==0x80
MODE_ANSI==0x80000
MODE_NO_AUTO_VALUE_ON_ZERO==0x100000
MODE_NO_BACKSLASH_ESCAPES==0x200000
MODE_STRICT_TRANS_TABLES==0x400000
MODE_STRICT_ALL_TABLES==0x800000
MODE_NO_ZERO_IN_DATE==0x1000000
MODE_NO_ZERO_DATE==0x2000000
MODE_INVALID_DATES==0x4000000
MODE_ERROR_FOR_DIVISION_BY_ZERO==0x8000000
MODE_TRADITIONAL==0x10000000
MODE_HIGH_NOT_PRECEDENCE==0x40000000
MODE_PAD_CHAR_TO_FULL_LENGTH==0x80000000
MODE_TIME_TRUNCATE_FRACTIONAL==0x100000000
All these flags are replicated from the server. However, all
flags except @c MODE_NO_DIR_IN_CREATE are honored by the slave;
the slave always preserves its old value of @c
MODE_NO_DIR_IN_CREATE.
This field is always written to the binlog.
|
catalog |
Q_CATALOG_NZ_CODE == 6 |
Variable-length string: the length in bytes (1 byte) followed
by the characters (at most 255 bytes)
|
Stores the client's current catalog. Every database belongs
to a catalog, the same way that every table belongs to a
database. Currently, there is only one catalog, "std".
This field is written if the length of the catalog is > 0;
otherwise it is not written.
|
auto_increment |
Q_AUTO_INCREMENT == 3 |
two 2 byte unsigned integers, totally 2+2=4 bytes |
The two variables auto_increment_increment and
auto_increment_offset, in that order. For more information, see
"System variables" in the MySQL manual.
This field is written if auto_increment > 1. Otherwise, it is not
written.
|
charset |
Q_CHARSET_CODE == 4 |
three 2 byte unsigned integers, totally 2+2+2=6 bytes |
The three variables character_set_client,
collation_connection, and collation_server, in that order.
character_set_client is a code identifying the character set and
collation used by the client to encode the query.
collation_connection identifies the character set and collation
that the master converts the query to when it receives it; this is
useful when comparing literal strings. collation_server is the
default character set and collation used when a new database is
created.
See also "Connection Character Sets and Collations" in the MySQL
5.1 manual.
All three variables are codes identifying a (character set,
collation) pair. To see which codes map to which pairs, run the
query "SELECT id, character_set_name, collation_name FROM
COLLATIONS".
Cf. Q_CHARSET_DATABASE_CODE below.
This field is always written.
|
time_zone |
Q_TIME_ZONE_CODE == 5 |
Variable-length string: the length in bytes (1 byte) followed
by the characters (at most 255 bytes).
| The time_zone of the master.
See also "System Variables" and "MySQL Server Time Zone Support"
in the MySQL manual.
This field is written if the length of the time zone string is >
0; otherwise, it is not written.
|
lc_time_names_number |
Q_LC_TIME_NAMES_CODE == 7 |
2 byte integer |
A code identifying a table of month and day names. The
mapping from codes to languages is defined in @c sql_locale.cc.
This field is written if it is not 0, i.e., if the locale is not
en_US.
|
charset_database_number |
Q_CHARSET_DATABASE_CODE == 8 |
2 byte integer |
The value of the collation_database system variable (in the
source code stored in @c thd->variables.collation_database), which
holds the code for a (character set, collation) pair as described
above (see Q_CHARSET_CODE).
collation_database was used in old versions (???WHEN). Its value
was loaded when issuing a "use db" query and could be changed by
issuing a "SET collation_database=xxx" query. It used to affect
the "LOAD DATA INFILE" and "CREATE TABLE" commands.
In newer versions, "CREATE TABLE" has been changed to take the
character set from the database of the created table, rather than
the character set of the current database. This makes a
difference when creating a table in another database than the
current one. "LOAD DATA INFILE" has not yet changed to do this,
but there are plans to eventually do it, and to make
collation_database read-only.
This field is written if it is not 0.
|
table_map_for_update |
Q_TABLE_MAP_FOR_UPDATE_CODE == 9 |
8 byte integer |
The value of the table map that is to be updated by the
multi-table update query statement. Every bit of this variable
represents a table, and is set to 1 if the corresponding table is
to be updated by this statement.
The value of this variable is set when executing a multi-table update
statement and used by slave to apply filter rules without opening
all the tables on slave. This is required because some tables may
not exist on slave because of the filter rules.
|
master_data_written |
Q_MASTER_DATA_WRITTEN_CODE == 10 |
4 byte bitfield |
The value of the original length of a Query_event that comes from a
master. Master's event is relay-logged with storing the original size of
event in this field by the IO thread. The size is to be restored by reading
Q_MASTER_DATA_WRITTEN_CODE-marked event from the relay log.
This field is not written to slave's server binlog by the SQL thread.
This field only exists in relay logs where master has binlog_version<4 i.e.
server_version < 5.0 and the slave has binlog_version=4.
|
binlog_invoker |
Q_INVOKER == 11 |
2 Variable-length strings: the length in bytes (1 byte) followed
by characters (user), again followed by length in bytes (1 byte) followed
by characters(host) |
The value of boolean variable m_binlog_invoker is set TRUE if
CURRENT_USER() is called in account management statements. SQL thread
uses it as a default definer in CREATE/ALTER SP, SF, Event, TRIGGER or
VIEW statements.
The field Q_INVOKER has length of user stored in 1 byte followed by the
user string which is assigned to 'user' and the length of host stored in
1 byte followed by host string which is assigned to 'host'.
|
mts_accessed_dbs |
Q_UPDATED_DB_NAMES == 12 |
1 byte character, and a 2-D array |
The total number and the names to of the databases accessed is stored,
to be propagated to the slave in order to facilitate the parallel
applying of the Query events.
|
explicit_defaults_ts |
Q_EXPLICIT_DEFAULTS_FOR_TIMESTAMP |
1 byte boolean |
Stores master connection @@session.explicit_defaults_for_timestamp when
CREATE and ALTER operate on a table with a TIMESTAMP column. |
ddl_xid |
Q_DDL_LOGGED_WITH_XID |
8 byte integer |
Stores variable carrying xid info of 2pc-aware (recoverable) DDL
queries. |
default_collation_for_utf8mb4_number |
Q_DEFAULT_COLLATION_FOR_UTF8MB4 |
2 byte integer |
Stores variable carrying the the default collation for the utf8mb4
character set. Mainly used to support replication 5.7- master to a 8.0+
slave.
|
sql_require_primary_key |
Q_SQL_REQUIRE_PRIMARY_KEY |
2 byte integer |
Value of the config variable sql_require_primary_key |
default_table_encryption |
Q_DEFAULT_TABLE_ENCRYPTION |
2 byte integer |
Value of the config variable default_table_encryption |
@subsection Query_event_notes_on_previous_versions Notes on Previous Versions
* Status vars were introduced in version 5.0. To read earlier
versions correctly, check the length of the Post-Header.
* The status variable Q_CATALOG_CODE == 2 existed in MySQL 5.0.x,
where 0<=x<=3. It was identical to Q_CATALOG_CODE, except that the
string had a trailing '\0'. The '\0' was removed in 5.0.4 since it
was redundant (the string length is stored before the string). The
Q_CATALOG_CODE will never be written by a new master, but can still
be understood by a new slave.
* See Q_CHARSET_DATABASE_CODE in the table above.
* When adding new status vars, please don't forget to update the
MAX_SIZE_LOG_EVENT_STATUS.
*/
class Query_event : public Binary_log_event {
public:
/** query event post-header */
enum Query_event_post_header_offset {
Q_THREAD_ID_OFFSET = 0,
Q_EXEC_TIME_OFFSET = 4,
Q_DB_LEN_OFFSET = 8,
Q_ERR_CODE_OFFSET = 9,
Q_STATUS_VARS_LEN_OFFSET = 11,
Q_DATA_OFFSET = QUERY_HEADER_LEN
};
/* these are codes, not offsets; not more than 256 values (1 byte). */
enum Query_event_status_vars {
Q_FLAGS2_CODE = 0,
Q_SQL_MODE_CODE,
/*
Q_CATALOG_CODE is catalog with end zero stored; it is used only by MySQL
5.0.x where 0<=x<=3. We have to keep it to be able to replicate these
old masters.
*/
Q_CATALOG_CODE,
Q_AUTO_INCREMENT,
Q_CHARSET_CODE,
Q_TIME_ZONE_CODE,
/*
Q_CATALOG_NZ_CODE is catalog withOUT end zero stored; it is used by MySQL
5.0.x where x>=4. Saves one byte in every Query_event in binlog,
compared to Q_CATALOG_CODE. The reason we didn't simply re-use
Q_CATALOG_CODE is that then a 5.0.3 slave of this 5.0.x (x>=4)
master would crash (segfault etc) because it would expect a 0 when there
is none.
*/
Q_CATALOG_NZ_CODE,
Q_LC_TIME_NAMES_CODE,
Q_CHARSET_DATABASE_CODE,
Q_TABLE_MAP_FOR_UPDATE_CODE,
/* It is just a placeholder after 8.0.2*/
Q_MASTER_DATA_WRITTEN_CODE,
Q_INVOKER,
/*
Q_UPDATED_DB_NAMES status variable collects information of accessed
databases i.e. the total number and the names to be propagated to the
slave in order to facilitate the parallel applying of the Query events.
*/
Q_UPDATED_DB_NAMES,
Q_MICROSECONDS,
/*
A old (unused now) code for Query_log_event status similar to G_COMMIT_TS.
*/
Q_COMMIT_TS,
/*
An old (unused after migration to Gtid_event) code for
Query_log_event status, similar to G_COMMIT_TS2.
*/
Q_COMMIT_TS2,
/*
The master connection @@session.explicit_defaults_for_timestamp which
is recorded for queries, CREATE and ALTER table that is defined with
a TIMESTAMP column, that are dependent on that feature.
For pre-WL6292 master's the associated with this code value is zero.
*/
Q_EXPLICIT_DEFAULTS_FOR_TIMESTAMP,
/*
The variable carries xid info of 2pc-aware (recoverable) DDL queries.
*/
Q_DDL_LOGGED_WITH_XID,
/*
This variable stores the default collation for the utf8mb4 character set.
Used to support cross-version replication.
*/
Q_DEFAULT_COLLATION_FOR_UTF8MB4,
/*
Replicate sql_require_primary_key.
*/
Q_SQL_REQUIRE_PRIMARY_KEY,
/*
Replicate default_table_encryption.
*/
Q_DEFAULT_TABLE_ENCRYPTION
};
const char *query;
const char *db;
const char *catalog;
const char *time_zone_str;
protected:
const char *user;
size_t user_len;
const char *host;
size_t host_len;
/* Required by the MySQL server class Log_event::Query_event */
unsigned long data_len;
/*
Copies data into the buffer in the following fashion
+--------+-----------+------+------+---------+----+-------+----+
| catlog | time_zone | user | host | db name | \0 | Query | \0 |
+--------+-----------+------+------+---------+----+-------+----+
*/
int fill_data_buf(unsigned char *dest, unsigned long len);
public:
/* data members defined in order they are packed and written into the log */
uint32_t thread_id;
uint32_t query_exec_time;
size_t db_len;
uint16_t error_code;
/*
We want to be able to store a variable number of N-bit status vars:
(generally N=32; but N=64 for SQL_MODE) a user may want to log the number
of affected rows (for debugging) while another does not want to lose 4
bytes in this.
The storage on disk is the following:
status_vars_len is part of the post-header,
status_vars are in the variable-length part, after the post-header, before
the db & query.
status_vars on disk is a sequence of pairs (code, value) where 'code' means
'sql_mode', 'affected' etc. Sometimes 'value' must be a short string, so
its first byte is its length. For now the order of status vars is:
flags2 - sql_mode - catalog - autoinc - charset
We should add the same thing to Load_event, but in fact
LOAD DATA INFILE is going to be logged with a new type of event (logging of
the plain text query), so Load_event would be frozen, so no need. The
new way of logging LOAD DATA INFILE would use a derived class of
Query_event, so automatically benefit from the work already done for
status variables in Query_event.
*/
uint16_t status_vars_len;
/*
If we already know the length of the query string
we pass it with q_len, so we would not have to call strlen()
otherwise, set it to 0, in which case, we compute it with strlen()
*/
size_t q_len;
/* The members below represent the status variable block */
/*
'flags2' is a second set of flags (on top of those in Log_event), for
session variables. These are thd->options which is & against a mask
(OPTIONS_WRITTEN_TO_BIN_LOG).
flags2_inited helps make a difference between flags2==0 (3.23 or 4.x
master, we don't know flags2, so use the slave server's global options) and
flags2==0 (5.0 master, we know this has a meaning of flags all down which
must influence the query).
*/
bool flags2_inited;
bool sql_mode_inited;
bool charset_inited;
uint32_t flags2;
/* In connections sql_mode is 32 bits now but will be 64 bits soon */
uint64_t sql_mode;
uint16_t auto_increment_increment, auto_increment_offset;
char charset[6];
size_t time_zone_len; /* 0 means uninited */
/*
Binlog format 3 and 4 start to differ (as far as class members are
concerned) from here.
*/
size_t catalog_len; // <= 255 char; 0 means uninited
uint16_t lc_time_names_number; /* 0 means en_US */
uint16_t charset_database_number;
/*
map for tables that will be updated for a multi-table update query
statement, for other query statements, this will be zero.
*/
uint64_t table_map_for_update;
/*
The following member gets set to OFF or ON value when the
Query-log-event is marked as dependent on
@@explicit_defaults_for_timestamp. That is the member is relevant
to queries that declare TIMESTAMP column attribute, like CREATE
and ALTER.
The value is set to @c TERNARY_OFF when @@explicit_defaults_for_timestamp
encoded value is zero, otherwise TERNARY_ON.
*/
enum enum_ternary {
TERNARY_UNSET,
TERNARY_OFF,
TERNARY_ON
} explicit_defaults_ts;
/*
number of updated databases by the query and their names. This info
is requested by both Coordinator and Worker.
*/
unsigned char mts_accessed_dbs;
char mts_accessed_db_names[MAX_DBS_IN_EVENT_MTS][NAME_LEN];
/* XID value when the event is a 2pc-capable DDL */
uint64_t ddl_xid;
/* Default collation for the utf8mb4 set. Used in cross-version replication */
uint16_t default_collation_for_utf8mb4_number;
uint8_t sql_require_primary_key;
uint8_t default_table_encryption;
/**
The constructor will be used while creating a Query_event, to be
written to the binary log.
*/
Query_event(const char *query_arg, const char *catalog_arg,
const char *db_arg, uint32_t query_length,
unsigned long thread_id_arg, unsigned long long sql_mode_arg,
unsigned long auto_increment_increment_arg,
unsigned long auto_increment_offset_arg, unsigned int number,
unsigned long long table_map_for_update_arg, int errcode);
/**
The constructor receives a buffer and instantiates a Query_event filled in
with the data from the buffer