MySQL 8.3.0
Source Code Documentation
mysql::binlog::event::Query_event Class Reference

A Query_event is created for each query that modifies the database, unless the query is logged row-based. More...

#include <statement_events.h>

Inheritance diagram for mysql::binlog::event::Query_event:
[legend]

Public Types

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
}
 query event post-header More...
 
enum  Query_event_status_vars {
  Q_FLAGS2_CODE = 0 , Q_SQL_MODE_CODE , Q_CATALOG_CODE , Q_AUTO_INCREMENT ,
  Q_CHARSET_CODE , Q_TIME_ZONE_CODE , Q_CATALOG_NZ_CODE , Q_LC_TIME_NAMES_CODE ,
  Q_CHARSET_DATABASE_CODE , Q_TABLE_MAP_FOR_UPDATE_CODE , Q_MASTER_DATA_WRITTEN_CODE , Q_INVOKER ,
  Q_UPDATED_DB_NAMES , Q_MICROSECONDS , Q_COMMIT_TS , Q_COMMIT_TS2 ,
  Q_EXPLICIT_DEFAULTS_FOR_TIMESTAMP , Q_DDL_LOGGED_WITH_XID , Q_DEFAULT_COLLATION_FOR_UTF8MB4 , Q_SQL_REQUIRE_PRIMARY_KEY ,
  Q_DEFAULT_TABLE_ENCRYPTION
}
 
enum  enum_ternary { TERNARY_UNSET , TERNARY_OFF , TERNARY_ON }
 
- Public Types inherited from mysql::binlog::event::Binary_log_event
enum  enum_post_header_length {
  QUERY_HEADER_MINIMAL_LEN = (4 + 4 + 1 + 2) , QUERY_HEADER_LEN = (QUERY_HEADER_MINIMAL_LEN + 2) , STOP_HEADER_LEN = 0 , START_V3_HEADER_LEN = (2 + ST_SERVER_VER_LEN + 4) ,
  ROTATE_HEADER_LEN = 8 , INTVAR_HEADER_LEN = 0 , APPEND_BLOCK_HEADER_LEN = 4 , DELETE_FILE_HEADER_LEN = 4 ,
  RAND_HEADER_LEN = 0 , USER_VAR_HEADER_LEN = 0 , FORMAT_DESCRIPTION_HEADER_LEN = (START_V3_HEADER_LEN + 1 + LOG_EVENT_TYPES) , XID_HEADER_LEN = 0 ,
  BEGIN_LOAD_QUERY_HEADER_LEN = APPEND_BLOCK_HEADER_LEN , ROWS_HEADER_LEN_V1 = 8 , TABLE_MAP_HEADER_LEN = 8 , EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN = (4 + 4 + 4 + 1) ,
  EXECUTE_LOAD_QUERY_HEADER_LEN , INCIDENT_HEADER_LEN = 2 , HEARTBEAT_HEADER_LEN = 0 , IGNORABLE_HEADER_LEN = 0 ,
  ROWS_HEADER_LEN_V2 = 10 , TRANSACTION_CONTEXT_HEADER_LEN = 18 , VIEW_CHANGE_HEADER_LEN = 52 , XA_PREPARE_HEADER_LEN = 0 ,
  TRANSACTION_PAYLOAD_HEADER_LEN = 0
}
 The lengths for the fixed data part of each event. More...
 

Public Member Functions

 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 will be used while creating a Query_event, to be written to the binary log. More...
 
 Query_event (const char *buf, const Format_description_event *fde, Log_event_type event_type)
 The constructor receives a buffer and instantiates a Query_event filled in with the data from the buffer. More...
 
 Query_event (Log_event_type type_arg=QUERY_EVENT)
 The simplest constructor that could possibly work. More...
 
 ~Query_event () override=default
 
- Public Member Functions inherited from mysql::binlog::event::Binary_log_event
virtual ~Binary_log_event ()=0
 
 Binary_log_event (const Binary_log_event &)=default
 
 Binary_log_event (Binary_log_event &&)=default
 
Binary_log_eventoperator= (const Binary_log_event &)=default
 
Binary_log_eventoperator= (Binary_log_event &&)=default
 
enum Log_event_type get_event_type () const
 Helper method. More...
 
const Log_event_headerheader () const
 Return a const pointer to the header of the log event. More...
 
Log_event_headerheader ()
 Return a non-const pointer to the header of the log event. More...
 
const Log_event_footerfooter () const
 Return a const pointer to the footer of the log event. More...
 
Log_event_footerfooter ()
 Return a non-const pointer to the footer of the log event. More...
 
Event_readerreader ()
 Returns a reference to the event Event_reader object. More...
 

Public Attributes

const char * query
 
const char * db
 
const char * catalog
 
const char * time_zone_str
 
uint32_t thread_id
 
uint32_t query_exec_time
 
size_t db_len
 
uint16_t error_code
 
uint16_t status_vars_len
 
size_t q_len
 
bool flags2_inited
 
bool sql_mode_inited
 
bool charset_inited
 
uint32_t flags2
 
uint64_t sql_mode
 
uint16_t auto_increment_increment
 
uint16_t auto_increment_offset
 
char charset [6]
 
size_t time_zone_len
 
size_t catalog_len
 
uint16_t lc_time_names_number
 
uint16_t charset_database_number
 
uint64_t table_map_for_update
 
enum mysql::binlog::event::Query_event::enum_ternary explicit_defaults_ts
 
unsigned char mts_accessed_dbs
 
char mts_accessed_db_names [MAX_DBS_IN_EVENT_MTS][NAME_LEN]
 
uint64_t ddl_xid
 
uint16_t default_collation_for_utf8mb4_number
 
uint8_t sql_require_primary_key
 
uint8_t default_table_encryption
 

Protected Member Functions

int fill_data_buf (unsigned char *dest, unsigned long len)
 Layout for the data buffer is as follows. More...
 
- Protected Member Functions inherited from mysql::binlog::event::Binary_log_event
 Binary_log_event (Log_event_type type_code)
 This constructor is used to initialize the type_code of header object m_header. More...
 
 Binary_log_event (const char **buf, const Format_description_event *fde)
 This constructor will create a new object of Log_event_header and initialize the variable m_header, which in turn will be used to initialize Log_event's member common_header. More...
 

Protected Attributes

const char * user
 
size_t user_len
 
const char * host
 
size_t host_len
 
unsigned long data_len
 

Additional Inherited Members

- Static Public Attributes inherited from mysql::binlog::event::Binary_log_event
static const int LOG_EVENT_TYPES = (ENUM_END_EVENT - 1)
 

Detailed Description

A Query_event is created for each query that modifies the database, unless the query is logged row-based.

Binary format

See Binary format for log events for a general discussion and introduction to the binary format of binlog events.

The Post-Header has five components:

Post-Header for Query_event
Name Format

Description

thread_id 4 byte unsigned integer

The ID of the thread that issued this statement. It is needed for temporary tables.

query_exec_time 4 byte unsigned integer

The time from when the query started to when it was logged in the binlog, in seconds.

db_len 1 byte integer

The length of the name of the currently selected database.

error_code 2 byte unsigned integer

Error code generated by the master. If the master fails, the slave will fail with the same error code.

status_vars_len 2 byte unsigned integer The length of the status_vars block of the Body, in bytes. This is not present for binlog version 1 and 3. See below.

The Body has the following components:

Body for Query_event
Name Format

Description

status_vars status_vars_len bytes

Zero or more status variables. Each status variable consists of one byte identifying the variable stored, followed by the value of the variable. The possible variables are listed separately in the table below. MySQL always writes events in the order defined below; however, it is capable of reading them in any order.

m_db db_len + 1

The currently selected database, as a null-terminated string.

(The trailing zero is redundant since the length is already known; it is db_len from Post-Header.)

m_query variable length string without trailing zero, extending to the end of the event (determined by the length field of the Common-Header) The SQL query.

The following table lists the status variables that may appear in the status_vars field.

Status variables for Query_event
Status variable 1 byte identifier Format

Description

flags2 Q_FLAGS2_CODE == 0 4 byte bitfield

The flags in thd->options, binary AND-ed with OPTIONS_WRITTEN_TO_BIN_LOG. The thd->options bitfield contains options for "SELECT". OPTIONS_WRITTEN identifies those options that need to be written to the binlog (not all do). Specifically, OPTIONS_WRITTEN_TO_BIN_LOG equals (OPTION_AUTO_IS_NULL | OPTION_NO_FOREIGN_KEY_CHECKS | OPTION_RELAXED_UNIQUE_CHECKS | 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 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 MODE_NO_DIR_IN_CREATE are honored by the slave; the slave always preserves its old value of 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 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 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

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.

Member Enumeration Documentation

◆ enum_ternary

Enumerator
TERNARY_UNSET 
TERNARY_OFF 
TERNARY_ON 

◆ Query_event_post_header_offset

query event post-header

Enumerator
Q_THREAD_ID_OFFSET 
Q_EXEC_TIME_OFFSET 
Q_DB_LEN_OFFSET 
Q_ERR_CODE_OFFSET 
Q_STATUS_VARS_LEN_OFFSET 
Q_DATA_OFFSET 

◆ Query_event_status_vars

Enumerator
Q_FLAGS2_CODE 
Q_SQL_MODE_CODE 
Q_CATALOG_CODE 
Q_AUTO_INCREMENT 
Q_CHARSET_CODE 
Q_TIME_ZONE_CODE 
Q_CATALOG_NZ_CODE 
Q_LC_TIME_NAMES_CODE 
Q_CHARSET_DATABASE_CODE 
Q_TABLE_MAP_FOR_UPDATE_CODE 
Q_MASTER_DATA_WRITTEN_CODE 
Q_INVOKER 
Q_UPDATED_DB_NAMES 
Q_MICROSECONDS 
Q_COMMIT_TS 
Q_COMMIT_TS2 
Q_EXPLICIT_DEFAULTS_FOR_TIMESTAMP 
Q_DDL_LOGGED_WITH_XID 
Q_DEFAULT_COLLATION_FOR_UTF8MB4 
Q_SQL_REQUIRE_PRIMARY_KEY 
Q_DEFAULT_TABLE_ENCRYPTION 

Constructor & Destructor Documentation

◆ Query_event() [1/3]

mysql::binlog::event::Query_event::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 will be used while creating a Query_event, to be written to the binary log.

The constructor used by MySQL master to create a query event, to be written to the binary log.

◆ Query_event() [2/3]

mysql::binlog::event::Query_event::Query_event ( const char *  buf,
const Format_description_event fde,
Log_event_type  event_type 
)

The constructor receives a buffer and instantiates a Query_event filled in with the data from the buffer.

The event occurs when an updating statement is done.

The fixed event data part buffer layout is as follows:
+---------------------------------------------------------------------+
| thread_id | query_exec_time | db_len | error_code | status_vars_len |
+---------------------------------------------------------------------+
The fixed event data part buffer layout is as follows:
+--------------------------------------------+
| Zero or more status variables | db | query |
+--------------------------------------------+
Parameters
bufContains the serialized event.
fdeAn FDE event (see Rotate_event constructor for more info).
event_typeRequired to determine whether the event type is QUERY_EVENT or EXECUTE_LOAD_QUERY_EVENT

◆ Query_event() [3/3]

mysql::binlog::event::Query_event::Query_event ( Log_event_type  type_arg = QUERY_EVENT)

The simplest constructor that could possibly work.

This is used for creating static objects that have a special meaning and are invisible to the log.

◆ ~Query_event()

mysql::binlog::event::Query_event::~Query_event ( )
overridedefault

Member Function Documentation

◆ fill_data_buf()

int mysql::binlog::event::Query_event::fill_data_buf ( unsigned char *  dest,
unsigned long  buf_len 
)
protected

Layout for the data buffer is as follows.

+--------+-----------+------+------+---------+----+-------+----+
| catlog | time_zone | user | host | db name | \0 | Query | \0 |
+--------+-----------+------+------+---------+----+-------+----+

Member Data Documentation

◆ auto_increment_increment

uint16_t mysql::binlog::event::Query_event::auto_increment_increment

◆ auto_increment_offset

uint16_t mysql::binlog::event::Query_event::auto_increment_offset

◆ catalog

const char* mysql::binlog::event::Query_event::catalog

◆ catalog_len

size_t mysql::binlog::event::Query_event::catalog_len

◆ charset

char mysql::binlog::event::Query_event::charset[6]

◆ charset_database_number

uint16_t mysql::binlog::event::Query_event::charset_database_number

◆ charset_inited

bool mysql::binlog::event::Query_event::charset_inited

◆ data_len

unsigned long mysql::binlog::event::Query_event::data_len
protected

◆ db

const char* mysql::binlog::event::Query_event::db

◆ db_len

size_t mysql::binlog::event::Query_event::db_len

◆ ddl_xid

uint64_t mysql::binlog::event::Query_event::ddl_xid

◆ default_collation_for_utf8mb4_number

uint16_t mysql::binlog::event::Query_event::default_collation_for_utf8mb4_number

◆ default_table_encryption

uint8_t mysql::binlog::event::Query_event::default_table_encryption

◆ error_code

uint16_t mysql::binlog::event::Query_event::error_code

◆ explicit_defaults_ts

enum mysql::binlog::event::Query_event::enum_ternary mysql::binlog::event::Query_event::explicit_defaults_ts

◆ flags2

uint32_t mysql::binlog::event::Query_event::flags2

◆ flags2_inited

bool mysql::binlog::event::Query_event::flags2_inited

◆ host

const char* mysql::binlog::event::Query_event::host
protected

◆ host_len

size_t mysql::binlog::event::Query_event::host_len
protected

◆ lc_time_names_number

uint16_t mysql::binlog::event::Query_event::lc_time_names_number

◆ mts_accessed_db_names

char mysql::binlog::event::Query_event::mts_accessed_db_names[MAX_DBS_IN_EVENT_MTS][NAME_LEN]

◆ mts_accessed_dbs

unsigned char mysql::binlog::event::Query_event::mts_accessed_dbs

◆ q_len

size_t mysql::binlog::event::Query_event::q_len

◆ query

const char* mysql::binlog::event::Query_event::query

◆ query_exec_time

uint32_t mysql::binlog::event::Query_event::query_exec_time

◆ sql_mode

uint64_t mysql::binlog::event::Query_event::sql_mode

◆ sql_mode_inited

bool mysql::binlog::event::Query_event::sql_mode_inited

◆ sql_require_primary_key

uint8_t mysql::binlog::event::Query_event::sql_require_primary_key

◆ status_vars_len

uint16_t mysql::binlog::event::Query_event::status_vars_len

◆ table_map_for_update

uint64_t mysql::binlog::event::Query_event::table_map_for_update

◆ thread_id

uint32_t mysql::binlog::event::Query_event::thread_id

◆ time_zone_len

size_t mysql::binlog::event::Query_event::time_zone_len

◆ time_zone_str

const char* mysql::binlog::event::Query_event::time_zone_str

◆ user

const char* mysql::binlog::event::Query_event::user
protected

◆ user_len

size_t mysql::binlog::event::Query_event::user_len
protected

The documentation for this class was generated from the following files: