Post-Header for Query_log_event
| Name |
Format |
Description |
| slave_proxy_id |
4 byte unsigned integer |
An integer identifying the client thread that issued the
query. The id is unique per server. (Note, however, that two
threads on different servers may have the same slave_proxy_id.)
This is used when a client thread creates a temporary table local
to the client. The slave_proxy_id is used to distinguish
temporary tables that belong to different clients.
|
| 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, except for the error
codes ER_DB_CREATE_EXISTS == 1007 and ER_DB_DROP_EXISTS == 1008.
|
| status_vars_len |
2 byte unsigned integer |
The length of the status_vars block of the Body, in bytes. See
@ref query_log_event_status_vars "below".
|
The Body has the following components:
Status variables for Query_log_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).
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_priv.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_IGNORE_BAD_TABLE_OPTIONS==0x10
MODE_ONLY_FULL_GROUP_BY==0x20
MODE_NO_UNSIGNED_SUBTRACTION==0x40
MODE_NO_DIR_IN_CREATE==0x80
MODE_POSTGRESQL==0x100
MODE_ORACLE==0x200
MODE_MSSQL==0x400
MODE_DB2==0x800
MODE_MAXDB==0x1000
MODE_NO_KEY_OPTIONS==0x2000
MODE_NO_TABLE_OPTIONS==0x4000
MODE_NO_FIELD_OPTIONS==0x8000
MODE_MYSQL323==0x10000
MODE_MYSQL323==0x20000
MODE_MYSQL40==0x40000
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_NO_AUTO_CREATE_USER==0x20000000
MODE_HIGH_NOT_PRECEDENCE==0x40000000
MODE_PAD_CHAR_TO_FULL_LENGTH==0x80000000
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. For a rationale, see comment in
@c Query_log_event::do_apply_event in @c log_event.cc.
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.
|
@subsection Query_log_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, and update function code_name
*/
class Query_log_event: public Log_event
{
LEX_CSTRING user;
LEX_CSTRING host;
protected:
Log_event::Byte* data_buf;
public:
const char* query;
const char* catalog;
const char* db;
/*
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()
*/
uint32 q_len;
uint32 db_len;
uint16 error_code;
my_thread_id thread_id;
/*
For events created by Query_log_event::do_apply_event (and
Load_log_event::do_apply_event()) we need the *original* thread
id, to be able to log the event with the original (=master's)
thread id (fix for BUG#1686).
*/
ulong slave_proxy_id;
/*
Binlog format 3 and 4 start to differ (as far as class members are
concerned) from here.
*/
uint catalog_len; // <= 255 char; 0 means uninited
/*
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_log_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_log_event would be frozen, so no need. The
new way of logging LOAD DATA INFILE would use a derived class of
Query_log_event, so automatically benefit from the work already done for
status variables in Query_log_event.
*/
uint16 status_vars_len;
/*
'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).
*/
uint32 flags2_inited;
bool sql_mode_inited;
bool charset_inited;
uint32 flags2;
sql_mode_t sql_mode;
ulong auto_increment_increment, auto_increment_offset;
char charset[6];
uint time_zone_len; /* 0 means uninited */
const char *time_zone_str;
uint lc_time_names_number; /* 0 means en_US */
uint 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.
*/
ulonglong table_map_for_update;
/* Xid for the event, if such exists */
ulonglong xid;
/*
Holds the original length of a Query_log_event that comes from a
master of version < 5.0 (i.e., binlog_version < 4). When the IO
thread writes the relay log, it augments the Query_log_event with a
Q_MASTER_DATA_WRITTEN_CODE status_var that holds the original event
length. This field is initialized to non-zero in the SQL thread when
it reads this augmented event. SQL thread does not write
Q_MASTER_DATA_WRITTEN_CODE to the slave's server binlog.
*/
uint32 master_data_written;
/*
A copy of Gtid event's extra flags that is relevant for two-phase
logged ALTER.
*/
uchar gtid_flags_extra;
decltype(rpl_gtid::seq_no) sa_seq_no; /* data part for CA/RA flags */
#ifdef MYSQL_SERVER
Query_log_event(THD* thd_arg, const char* query_arg, size_t query_length,
bool using_trans, bool direct, bool suppress_use, int error);
const char* get_db() override { return db; }
#ifdef HAVE_REPLICATION
void pack_info(Protocol* protocol) override;
#endif /* HAVE_REPLICATION */
#else
bool print_query_header(IO_CACHE* file, PRINT_EVENT_INFO* print_event_info);
bool print_verbose(IO_CACHE* cache, PRINT_EVENT_INFO* print_event_info);
bool print(FILE* file, PRINT_EVENT_INFO* print_event_info) override;
#endif
Query_log_event();
Query_log_event(const uchar *buf, uint event_len,
const Format_description_log_event *description_event,
Log_event_type event_type);
~Query_log_event()
{
if (data_buf)
my_free(data_buf);
}
Log_event_type get_type_code() override { return QUERY_EVENT; }
static int dummy_event(String *packet, ulong ev_offset,
enum enum_binlog_checksum_alg checksum_alg);
static int begin_event(String *packet, ulong ev_offset,
enum enum_binlog_checksum_alg checksum_alg);
#ifdef MYSQL_SERVER
bool write() override;
virtual bool write_post_header_for_derived() { return FALSE; }
#endif
bool is_valid() const override { return query != 0; }
/*
Returns number of bytes additionally written to post header by derived
events (so far it is only Execute_load_query event).
*/
virtual ulong get_post_header_size_for_derived() { return 0; }
/* Writes derived event-specific part of post header. */
public: /* !!! Public in this patch to allow old usage */
#if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
enum_skip_reason do_shall_skip(rpl_group_info *rgi) override;
int do_apply_event(rpl_group_info *rgi) override;
int do_apply_event(rpl_group_info *rgi,
const char *query_arg,
uint32 q_len_arg);
static bool peek_is_commit_rollback(const uchar *event_start,
size_t event_len,
enum enum_binlog_checksum_alg
checksum_alg);
int handle_split_alter_query_log_event(rpl_group_info *rgi,
bool &skip_error_check);
#endif /* HAVE_REPLICATION */
/*
If true, the event always be applied by slave SQL thread or be printed by
mysqlbinlog
*/
bool is_trans_keyword(bool is_xa)
{
/*
Before the patch for bug#50407, The 'SAVEPOINT and ROLLBACK TO'
queries input by user was written into log events directly.
So the keywords can be written in both upper case and lower case
together, strncasecmp is used to check both cases. they also could be
binlogged with comments in the front of these keywords. for examples:
/ * bla bla * / SAVEPOINT a;
/ * bla bla * / ROLLBACK TO a;
but we don't handle these cases and after the patch, both quiries are
binlogged in upper case with no comments.
*/
return is_xa ? !strncasecmp(query, C_STRING_WITH_LEN("XA "))
: (!strncmp(query, "BEGIN", q_len) ||
!strncmp(query, "COMMIT", q_len) ||
!strncasecmp(query, "SAVEPOINT", 9) ||
!strncasecmp(query, "ROLLBACK", 8));
}
virtual bool is_begin() { return !strcmp(query, "BEGIN"); }
virtual bool is_commit() { return !strcmp(query, "COMMIT"); }
virtual bool is_rollback() { return !strcmp(query, "ROLLBACK"); }
};
class Query_compressed_log_event:public Query_log_event{
protected:
Log_event::Byte* query_buf; // point to the uncompressed query
public:
Query_compressed_log_event(const uchar *buf, uint event_len,
const Format_description_log_event *description_event,
Log_event_type event_type);
~Query_compressed_log_event()
{
if (query_buf)
my_free(query_buf);
}
Log_event_type get_type_code() override { return QUERY_COMPRESSED_EVENT; }
/*
the min length of log_bin_compress_min_len is 10,
means that Begin/Commit/Rollback would never be compressed!
*/
bool is_begin() override { return false; }
bool is_commit() override { return false; }
bool is_rollback() override { return false; }
#ifdef MYSQL_SERVER
Query_compressed_log_event(THD* thd_arg, const char* query_arg,
ulong query_length,
bool using_trans, bool direct, bool suppress_use,
int error);
bool write() override;
#endif
};
/*****************************************************************************
sql_ex_info struct
****************************************************************************/
struct sql_ex_info
{
const char* field_term;
const char* enclosed;
const char* line_term;
const char* line_start;
const char* escaped;
int cached_new_format= -1;
uint8 field_term_len= 0, enclosed_len= 0, line_term_len= 0,
line_start_len= 0, escaped_len= 0;
char opt_flags;
char empty_flags= 0;
// store in new format even if old is possible
void force_new_format() { cached_new_format = 1;}
int data_size()
{
return (new_format() ?
field_term_len + enclosed_len + line_term_len +
line_start_len + escaped_len + 6 : 7);
}
bool write_data(Log_event_writer *writer);
const uchar *init(const uchar *buf, const uchar* buf_end, bool use_new_format);
bool new_format()
{
return ((cached_new_format != -1) ? cached_new_format :
(cached_new_format=(field_term_len > 1 ||
enclosed_len > 1 ||
line_term_len > 1 || line_start_len > 1 ||
escaped_len > 1)));
}
};
/**
@class Load_log_event
This log event corresponds to a "LOAD DATA INFILE" SQL query on the
following form:
@verbatim
(1) USE db;
(2) LOAD DATA [CONCURRENT] [LOCAL] INFILE 'file_name'
(3) [REPLACE | IGNORE]
(4) INTO TABLE 'table_name'
(5) [FIELDS
(6) [TERMINATED BY 'field_term']
(7) [[OPTIONALLY] ENCLOSED BY 'enclosed']
(8) [ESCAPED BY 'escaped']
(9) ]
(10) [LINES
(11) [TERMINATED BY 'line_term']
(12) [LINES STARTING BY 'line_start']
(13) ]
(14) [IGNORE skip_lines LINES]
(15) (field_1, field_2, ..., field_n)@endverbatim
@section Load_log_event_binary_format Binary Format
The Post-Header consists of the following six components.
Post-Header for Load_log_event
| Name |
Format |
Description |
| slave_proxy_id |
4 byte unsigned integer |
An integer identifying the client thread that issued the
query. The id is unique per server. (Note, however, that two
threads on different servers may have the same slave_proxy_id.)
This is used when a client thread creates a temporary table local
to the client. The slave_proxy_id is used to distinguish
temporary tables that belong to different clients.
|
| exec_time |
4 byte unsigned integer |
The time from when the query started to when it was logged in
the binlog, in seconds. |
| skip_lines |
4 byte unsigned integer |
The number on line (14) above, if present, or 0 if line (14)
is left out.
|
| table_name_len |
1 byte unsigned integer |
The length of 'table_name' on line (4) above. |
| db_len |
1 byte unsigned integer |
The length of 'db' on line (1) above. |
| num_fields |
4 byte unsigned integer |
The number n of fields on line (15) above. |
The Body contains the following components.
Body of Load_log_event
| Name |
Format |
Description |
| sql_ex |
variable length |
Describes the part of the query on lines (3) and
(5)–(13) above. More precisely, it stores the five strings
(on lines) field_term (6), enclosed (7), escaped (8), line_term
(11), and line_start (12); as well as a bitfield indicating the
presence of the keywords REPLACE (3), IGNORE (3), and OPTIONALLY
(7).
The data is stored in one of two formats, called "old" and "new".
The type field of Common-Header determines which of these two
formats is used: type LOAD_EVENT means that the old format is
used, and type NEW_LOAD_EVENT means that the new format is used.
When MySQL writes a Load_log_event, it uses the new format if at
least one of the five strings is two or more bytes long.
Otherwise (i.e., if all strings are 0 or 1 bytes long), the old
format is used.
The new and old format differ in the way the five strings are
stored.
- In the new format, the strings are stored in the order
field_term, enclosed, escaped, line_term, line_start. Each string
consists of a length (1 byte), followed by a sequence of
characters (0-255 bytes). Finally, a boolean combination of the
following flags is stored in 1 byte: REPLACE_FLAG==0x4,
IGNORE_FLAG==0x8, and OPT_ENCLOSED_FLAG==0x2. If a flag is set,
it indicates the presence of the corresponding keyword in the SQL
query.
- In the old format, we know that each string has length 0 or
1. Therefore, only the first byte of each string is stored. The
order of the strings is the same as in the new format. These five
bytes are followed by the same 1 byte bitfield as in the new
format. Finally, a 1 byte bitfield called empty_flags is stored.
The low 5 bits of empty_flags indicate which of the five strings
have length 0. For each of the following flags that is set, the
corresponding string has length 0; for the flags that are not set,
the string has length 1: FIELD_TERM_EMPTY==0x1,
ENCLOSED_EMPTY==0x2, LINE_TERM_EMPTY==0x4, LINE_START_EMPTY==0x8,
ESCAPED_EMPTY==0x10.
Thus, the size of the new format is 6 bytes + the sum of the sizes
of the five strings. The size of the old format is always 7
bytes.
|
| field_lens |
num_fields 1 byte unsigned integers |
An array of num_fields integers representing the length of
each field in the query. (num_fields is from the Post-Header).
|
| fields |
num_fields null-terminated strings |
An array of num_fields null-terminated strings, each
representing a field in the query. (The trailing zero is
redundant, since the length are stored in the num_fields array.)
The total length of all strings equals to the sum of all
field_lens, plus num_fields bytes for all the trailing zeros.
|
| table_name |
null-terminated string of length table_len+1 bytes |
The 'table_name' from the query, as a null-terminated string.
(The trailing zero is actually redundant since the table_len is
known from Post-Header.)
|
| db |
null-terminated string of length db_len+1 bytes |
The 'db' from the query, as a null-terminated string.
(The trailing zero is actually redundant since the db_len is known
from Post-Header.)
|
| file_name |
variable length string without trailing zero, extending to the
end of the event (determined by the length field of the
Common-Header)
|
The 'file_name' from the query.
|
@subsection Load_log_event_notes_on_previous_versions Notes on Previous Versions
This event type is understood by current versions, but only
generated by MySQL 3.23 and earlier.
*/
class Load_log_event: public Log_event
{
private:
protected:
int copy_log_event(const uchar *buf, ulong event_len,
int body_offset,
const Format_description_log_event* description_event);
public:
bool print_query(THD *thd, bool need_db, const char *cs, String *buf,
my_off_t *fn_start, my_off_t *fn_end,
const char *qualify_db);
my_thread_id thread_id;
ulong slave_proxy_id;
uint32 table_name_len;
/*
No need to have a catalog, as these events can only come from 4.x.
TODO: this may become false if Dmitri pushes his new LOAD DATA INFILE in
5.0 only (not in 4.x).
*/
uint32 db_len;
uint32 fname_len;
uint32 num_fields;
const char* fields;
const uchar* field_lens;
uint32 field_block_len;
const char* table_name;
const char* db;
const char* fname;
uint32 skip_lines;
sql_ex_info sql_ex;
bool local_fname;
/**
Indicates that this event corresponds to LOAD DATA CONCURRENT,
@note Since Load_log_event event coming from the binary log
lacks information whether LOAD DATA on master was concurrent
or not, this flag is only set to TRUE for an auxiliary
Load_log_event object which is used in mysql_load() to
re-construct LOAD DATA statement from function parameters,
for logging.
*/
bool is_concurrent;
/* fname doesn't point to memory inside Log_event::temp_buf */
void set_fname_outside_temp_buf(const char *afname, size_t alen)
{
fname= afname;
fname_len= (uint)alen;
local_fname= TRUE;
}
/* fname doesn't point to memory inside Log_event::temp_buf */
int check_fname_outside_temp_buf()
{
return local_fname;
}
#ifdef MYSQL_SERVER
String field_lens_buf;
String fields_buf;
Load_log_event(THD* thd, const sql_exchange* ex, const char* db_arg,
const char* table_name_arg,
List