MySQL Internals Manual  /  The Binary Log  /  Event Structure

20.7 Event Structure

This section describes the general properties of events as byte sequences as they are written to binary or relay log files.

All events have a common general structure consisting of an event header followed by event data:

+===================+
| event header      |
+===================+
| event data        |
+===================+

The details about what goes in the header and data parts have changed over time, which gives rise to different versions of the binary log format:

  • v1: Used in MySQL 3.23

  • v3: Used in MySQL 4.0.2 though 4.1

  • v4: Used in MySQL 5.0 and up

A v2 format was used briefly (in early MySQL 4.0.x versions), but it is obsolete and no longer supported.

Some details of event structure are invariant across binary log versions; others depend on the version. Within any given version, different types of events vary in the structure of the data part.

The first event in a log file is special. It is a descriptor event that provides information such as the binary log version and the server version. The information in the descriptor event enables programs to determine which version of the binary log format applies to the file so that the remaining events in the file can be properly read and interpreted.

For details about the initial descriptor event and how to use it to determine the format of a binary log file, see Binary Log Versions. For additional information about other types of events, see Event Data for Specific Event Types.

The following event diagrams contain field descriptions written using these conventions:

  • A field line has a name describing the contents of the field.

  • The name is followed by two numbers in offset : length format, where offset is the 0-based offset (position) of the field within the event and length is the length of the field. Both values are given in bytes.

The overall structure for events in the different binary versions is shown here. The following sections describe the header and data parts in more detail.

v1 event structure:

+=====================================+
| event  | timestamp         0 : 4    |
| header +----------------------------+
|        | type_code         4 : 1    |
|        +----------------------------+
|        | server_id         5 : 4    |
|        +----------------------------+
|        | event_length      9 : 4    |
+=====================================+
| event  | fixed part       13 : y    |
| data   +----------------------------+
|        | variable part              |
+=====================================+

header length = 13 bytes

data length = (event_length - 13) bytes

y is specific to the event type.

v3 event structure:

+=====================================+
| event  | timestamp         0 : 4    |
| header +----------------------------+
|        | type_code         4 : 1    |
|        +----------------------------+
|        | server_id         5 : 4    |
|        +----------------------------+
|        | event_length      9 : 4    |
|        +----------------------------+
|        | next_position    13 : 4    |
|        +----------------------------+
|        | flags            17 : 2    |
+=====================================+
| event  | fixed part       19 : y    |
| data   +----------------------------+
|        | variable part              |
+=====================================+

header length = 19 bytes

data length = (event_length - 19) bytes

y is specific to the event type.

v4 event structure:

+=====================================+
| event  | timestamp         0 : 4    |
| header +----------------------------+
|        | type_code         4 : 1    |
|        +----------------------------+
|        | server_id         5 : 4    |
|        +----------------------------+
|        | event_length      9 : 4    |
|        +----------------------------+
|        | next_position    13 : 4    |
|        +----------------------------+
|        | flags            17 : 2    |
|        +----------------------------+
|        | extra_headers    19 : x-19 |
+=====================================+
| event  | fixed part        x : y    |
| data   +----------------------------+
|        | variable part              |
+=====================================+

header length = x bytes

data length = (event_length - x) bytes

fixed data length = y bytes variable data length = (event_length - (x + y)) bytes

x is given by the header_length field in the format description event (FDE). Currently, x is 19, so the extra_headers field is empty.

y is specific to the event type, and is given by the FDE. The fixed-part length is the same for all events of a given type, but may vary for different event types.

The fixed part of the event data is sometimes referred to as the "post-header" part. The variable part is sometimes referred to as the "payload" or "body."

For information about how to use the FDE to interpret v4 events, see Binary Log Formats.


User Comments
Sign Up Login You must be logged in to post a comment.