All pages
Powered by GitBook
1 of 7

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

4 - Server Response Packets

Understand server response packets in MariaDB's client/server protocol. This section details the various types of packets sent by the server, including OK, Error, and Result Set packets.

Result Set Row

A result set row packet contains the data for a single row in a query result, formatted as text or binary depending on the execution context.

A result set row represents a database result set unit, which is usually generated by executing a statement that queries the database. Using COM_STMT_EXECUTE, the result set row is in binary format, otherswise in text format.

Text Result Set Row

  • For each column:

    • column data.

The byte representation of the string according to .

Binary Result Set Row

  • byte<1> 0x00 header.

  • byte<(number_of_columns + 7) / 8> .

  • For each column:

    • If column value is not null:

NULL-Bitmap Values

The NULL-Bitmap indicates if a parameter for a column is null (one bit per parameter) beginning with the 3rd bit. NULL-bitmap size is (number_of_columns + 7) / 8.

DECIMAL Binary Encoding

DECIMAL has no fixed size, so will be encoded as . A DECIMAL(10,2) with a value of -15.5 is stored as:

DOUBLE Binary Encoding

DOUBLE is the IEEE 754 floating-point value in Little-endian format on 8 bytes.

BIGINT Binary Encoding

BIGINT is the value in Little-endian format on 8 bytes. Signed is defined by the .

INTEGER Binary Encoding

INTEGER is the value in Little-endian format on 4 bytes. Signed is defined by the .

MEDIUMINT Binary Encoding

MEDIUMINT is similar to INTEGER binary encoding, even if MEDIUM int is 3-bytes encoded server side. (Last byte will always be 0x00).

FLOAT Binary Encoding

FLOAT is the IEEE 754 floating-point value in Little-endian format on 4 bytes.

SMALLINT Binary Encoding

SMALLINT is the value in Little-endian format on 2 bytes. Signed is defined by the .

YEAR Binary Encoding

YEAR uses the same format as SMALLINT.

TINYINT Binary Encoding

TINYINT is the value of 1 byte. Signed is defined by the .

DATE Binary Encoding

DATE uses the same format as TIMESTAMP binary encoding, with a data length of 0 for the special '0000-00-00' value and 4 for the standard year/month/day format.

TIMESTAMP Binary Encoding

Data is encoded in 8 bytes without fractional seconds, 12 bytes with fractional seconds.

Byte Position
Description

TIME Binary Encoding

Data is encoded in 9 bytes without fractional seconds, 13 bytes with fractional seconds.

  • data length: 0 for special '00:00:00' value, 8 without fractional seconds, 12 with fractional seconds.

  • If data length > 0:

    • 0 for positive time, 1 for negative time.

This page is licensed: CC BY-SA / Gnu FDL

  • If MYSQL_TYPE_DOUBLE type : DOUBLE Binary encoding

  • If MYSQL_TYPE_LONGLONG type : BIGINT Binary encoding

  • If MYSQL_TYPE_INTEGER type : INTEGER Binary encoding

  • If MYSQL_TYPE_MEDIUMINT type :

  • If MYSQL_TYPE_FLOAT type :

  • If MYSQL_TYPE_SMALLINT type :

  • If MYSQL_TYPE_YEAR type :

  • If MYSQL_TYPE_TINYINT type :

  • If MYSQL_TYPE_DATE type :

  • If MYSQL_TYPE_TIMESTAMP type :

  • If MYSQL_TYPE_DATETIME type :

  • If MYSQL_TYPE_TIME type :

  • If MYSQL_TYPE_NEWDECIMAL type :

  • If MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_BLOB, MYSQL_TYPE_GEOMETRY, MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VAR_STRING): value

  • 8

    seconds (0 if DATE type)

    9-12

    micro-second on 4 bytes little-endian format (only if data-length is > 7)

    int<4> days.

  • int<1> hours.

  • int<1> minutes.

  • int<1> seconds.

  • if data length > 8 :

    • int<4> microseconds.

  • 1

    data length : 0 for special '0000-00-00 00:00:00' value. 4 with year + month + day of month only 7 for timestamps without fractional seconds 11 with fractional seconds

    2-3

    year on 2 bytes little-endian format

    4

    Month ( 1=january)

    5

    days of month

    6

    hour of day (0 if DATE type)

    7

    minutes (0 if DATE type)

    string
    client character collation
    NULL-Bitmap
    string
    Column field detail flag
    Column field detail flag
    Column field detail flag
    Column field detail flag
    int<1>
    int<1>

    ERR_Packet

    The ERR_Packet is sent by the server to report an error, containing a numeric error code, SQL state marker, and a human-readable error message.

    ERR_Packet indicates that an error occurred.

    Fields

    • int<1> ERR_Packet header = 0xFF.

    • error code. see .

    • If (error code == 0xFFFF) /* progress reporting */:

      • stage.

      • max_stage.

      • progress.

    • Else:

      • If (next byte = '#'):

        • sql state marker '#'.

        • sql state.

    Note that the ERR_Packet is supposed to send a server error to the client. In particular, all error codes in the range 2000 to 2999 and 5000 to 5999 (inclusive) are reserved for client errors and an ERR packet with such an error code will be considered malformed.

    See also

    This page is licensed: CC BY-SA / Gnu FDL

    06 45 49 53 46 53 48      . - 1 5 . 5 0

    string progress_info.

    string human-readable error message.

  • Else:

    • string human-readable error message.

  • int<2>
    error list
    int<1>
    int<1>
    int<3>
    string<1>
    string<5>
    MEDIUMINT Binary encoding
    FLOAT Binary encoding
    SMALLINTBinary encoding
    YEAR Binary encoding
    TINYINT Binary encoding
    DATE Binary encoding
    TIMESTAMP Binary encoding
    TIMESTAMP Binary encoding
    TIME Binary encoding
    DECIMAL Binary encoding
    byte

    EOF_Packet

    The EOF_Packet marks the end of a result set or a sequence of packets, containing warning counts and server status flags.

    The EOF_Packet marks the end of a result set, and returns status and warnings.

    When testing for an EOF packet, the packet size must be less than 9 bytes in length. The result set can send data that begin with a 0xfe byte, but then the packet length will be greater than 9.

    Fields

    • 0xfe : EOF header.

    • warning count.

    • .

    This page is licensed: CC BY-SA / Gnu FDL

    LOCAL_INFILE_Packet

    The LOCAL_INFILE_Packet is sent by the server to request a file from the client during the execution of a LOAD DATA LOCAL INFILE statement.

    If the client sends a LOAD DATA LOCAL INFILE statement via com_query, the server responds with LOCAL_INFILE_Packet to tell the client to send a specified file to the server.

    Fields

    • 0xFB : LOCAL_INFILE header.

    • filename.

    Client Response

    The client sends the file as the packet body. If the file is large, the contents are sent in multiple separate packets. After the file is sent, the client must send an empty packet to indicate that no more data will follow.

    Once the client has finished sending the file, the server will respond with an or an .

    This page is licensed: CC BY-SA / Gnu FDL

    int<1>
    int<2>
    int<2>
    server status
    int<1>
    string
    OK_packet
    ERR_packet

    OK_Packet

    The OK_Packet is sent by the server to indicate the successful completion of a command, conveying affected row counts and server status flags.

    OK_Packet is sent by the server to the client and indicates a successful completion of a command sent by the client before.

    Direction

    Server to client.

    Fields

    • 0x00 : OK_Packet header or (0xFE if CLIENT_DEPRECATE_EOF is set).

    • affected rows.

    • last insert id.

    The length-encoded info string is not always included in the packet. Check the length of the packet to detect if there is data after the warning count. For the first OK_Packet in the connection it contains (if present) the . For the following OK_Packets it contains (if present) various human-readable information.

    Server status flag

    Values of server status flags:

    Flag
    Value
    Details

    Session state info

    • While packet has remaining data:

      • .

      • If (session-change-type != SESSION_TRACK_STATE_CHANGE):

    Session change type

    Change Type
    Flag

    Session Data Change

    Each type of data has its own kind of format:

    SESSION_TRACK_SCHEMA

    • new current schema.

    SESSION_TRACK_SYSTEM_VARIABLES

    While there is remaining data:

    • variable data.

    For each variable data:

    • variable name.

    • variable value.

    Possible tracked variables list is tracked by special variable value description:

    • : format is mariadb/mysql:[<user>[:<password>]@]<host>[:<port>]/[<db>[?<opt1>=<value1>[&<opt2>=<value2>]]]. Possible options:

      • ttl

    SESSION_TRACK_STATE_CHANGE

    Indicates if session state changes occurred. The value is represented as 1.

    • 1 if session state tracking was enabled.

    SESSION_TRACK_GTIDS

    This tracker is not implemented by MariaDB.

    SESSION_TRACK_TRANSACTION_CHARACTERISTICS

    • Transaction characteristics.

    Transaction characteristics is the set of SQL statements that reproduce the type and state of the current transaction. It can consist of the following SQL statements:

    SESSION_TRACK_TRANSACTION_STATE

    • Transaction state string.

    The transaction state string is always 8 characters long. The characters, in order, are:

    1. No transaction: _ Explicit transaction: T Implicit transaction: I

    2. Transaction read safe: _ Transaction read unsafe: r

    3. Unknown transaction type: _

    Notes

    • To determine if session tracking is enabled, check if the flag is set in server_capabilities.

    This page is licensed: CC BY-SA / Gnu FDL

    int<2> server status.

  • int<2> warning count.

  • If packet has more data:

    • string info.

    • If (status flags & SERVER_SESSION_STATE_CHANGED) and session_tracking_supported (see CLIENT_SESSION_TRACK):

      • .

  • SERVER_QUERY_NO_INDEX_USED

    32

    The query did not use an index.

    SERVER_STATUS_CURSOR_EXISTS

    64

    When using COM_STMT_FETCH, indicate that current cursor still has result.

    SERVER_STATUS_LAST_ROW_SENT

    128

    When using COM_STMT_FETCH, indicate that current cursor has finished to send results.

    SERVER_STATUS_DB_DROPPED

    1<<8

    Database has been dropped.

    SERVER_STATUS_NO_BACKSLASH_ESCAPES

    1<<9

    Current escape mode is "no backslash escape".

    SERVER_STATUS_METADATA_CHANGED

    1<<10

    A DDL change did have an impact on an existing PREPARE (an automatic re-prepare has been executed).

    SERVER_QUERY_WAS_SLOW

    1<<11

    The query was slower than .

    SERVER_PS_OUT_PARAMS

    1<<12

    This result set contains stored procedure output parameter.

    SERVER_STATUS_IN_TRANS_READONLY

    1<<13

    Current transaction is a read-only transaction.

    SERVER_SESSION_STATE_CHANGED

    1<<14

    Session state change. See for more information.

    int total length.
  • string session data change.

  • : cache timeout in ms to remember redirection, in order to reconnect directly to new host. 0=no caching
    Read-only transaction:
    R
  • Transaction write safe: _ Transaction write unsafe: w

  • Unknown transaction type: _ Read-write transaction: W

  • Transaction statement safe: _ Transaction statement unsafe: s

  • Transaction does not have resultsets: _ Transaction with result sets: S

  • No locked tables: _ Tables have been locked: L

  • SERVER_STATUS_IN_TRANS

    1

    A transaction is currently active.

    SERVER_STATUS_AUTOCOMMIT

    2

    Autocommit mode is set.

    SERVER_MORE_RESULTS_EXISTS

    8

    More results exists (more packets will follow).

    SERVER_QUERY_NO_GOOD_INDEX_USED

    16

    Set if EXPLAIN would've shown Range checked for each record.

    0

    SESSION_TRACK_SYSTEM_VARIABLES

    1

    SESSION_TRACK_SCHEMA

    2

    SESSION_TRACK_STATE_CHANGE

    3

    SESSION_TRACK_GTIDS

    4

    SESSION_TRACK_TRANSACTION_CHARACTERISTICS

    5

    SESSION_TRACK_TRANSACTION_STATE

    int<1>
    int
    int
    SSL certificate verification signature
    int<1>
    session change type
    string
    string
    string
    string
    session_track_system_variables
    redirect_url
    string
    string
    string
    CLIENT_SESSION_TRACK

    Result Set Packets

    A result set consists of a sequence of packets including column counts, column definitions, and row data, terminated by an EOF or OK packet.

    MariaDB Server sends the following packets as part of a result set.

    A result set consists of different packets:

    • Result set metadata.

      • 1 column count packet.

    • If not (MARIADB_CLIENT_CACHE_METADATA capability set) OR (send metadata == 1):

      • For each column (for instance, column_count times):

        • .

    • If not (CLIENT_DEPRECATE_EOF capability set) .

    • n .

    • If error:

      • .

    • Else:

      • If CLIENT_DEPRECATE_EOF capability:

        • with a 0xFE header.

    It would be unsafe to assume that any packet with a 0xFE header is an or an , because can also begin with 0xFE when using the text protocol with a field length greater than 0xFFFFFF. To safely confirm that a packet with a 0xFE header is an or an , you must also check that the packet length is less than 0xFFFFFF.

    Column Count Packet

    The column count packet describes the number of columns in the result set. It uses the following format:

    • column count.

    • If (MARIADB_CLIENT_CACHE_METADATA capability set):

      • int<1> metadata follows (0 / 1).

    The metadata indicator byte is only present if both the client and the server declare the MARIADB_CLIENT_CACHE_METADATA capability.

    If the metadata byte is set to 1, the normal metadata follows the column definitions. If the metadata byte is set to 0, the Column Count Packet is immediately followed by the second or the result set rows if the CLIENT_DEPRECATE_EOF capability is set.

    Column Definition Packet

    A column definition packet describes a column in the result set. It uses the following format:

    • catalog (always 'def').

    • schema.

    • table alias.

    • table.

    Field types

    The column type field in the column definition packet describes the base type of the column. It also indicates how the values are encoded for COM_STMT_EXECUTE parameters and binary result set rows.

    Value
    Protocol Column Type
    Encoding

    Field Details Flag

    The column details flag describes certain column attributes and whether certain column options are set.

    It is a bitmask with the following flags:

    Flag Value
    Flag Name
    Flag Description

    The BLOB flag cannot be used to determine if a column has binary data, because and columns are treated as strings, instead of blobs.

    The BINARY_COLLATION flag can be used to determine if a string column has binary data.

    Extended Metadata

    This extended column type information can be used to find out more specific details about the column type:

    • For a column, the column type field is MYSQL_TYPE_GEOMETRY, but the extended type indicates 'point'.

    • For a column, the column type field is MYSQL_TYPE_STRING, but the extended type indicates 'json'.

    • While string has data:

    This page is licensed: CC BY-SA / Gnu FDL

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
    SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
    SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
    START TRANSACTION;
    START TRANSACTION WITH CONSISTENT SNAPSHOT;
    START TRANSACTION WITH CONSISTENT SNAPSHOT, READ ONLY;
    START TRANSACTION WITH CONSISTENT SNAPSHOT, READ WRITE;
    SET TRANSACTION READ ONLY;
    SET TRANSACTION READ WRITE;
    XA START <XA specification>;
    string
    session state info
    long_query_time
    session change type
    Else EOF_Packet.

    string column alias.

  • string column.

  • If extended type supported (see MARIADB_CLIENT_EXTENDED_METADATA ):

    • string extended metadata.

  • int length of fixed fields (=0xC).

  • int<2> character set number.

  • int<4> max. column size.

  • int<1> Field types.

  • int<2> Field detail flag.

  • int<1> decimals.

  • int<2> - unused -

  • 4

    MYSQL_TYPE_FLOAT

    5

    MYSQL_TYPE_DOUBLE

    6

    MYSQL_TYPE_NULL

    Not used, nullness is indicated by the NULL-bitmap in the result

    7

    MYSQL_TYPE_TIMESTAMP

    8

    MYSQL_TYPE_LONGLONG

    9

    MYSQL_TYPE_INT24

    10

    MYSQL_TYPE_DATE

    11

    MYSQL_TYPE_TIME

    12

    MYSQL_TYPE_DATETIME

    13

    MYSQL_TYPE_YEAR

    14

    MYSQL_TYPE_NEWDATE

    15

    MYSQL_TYPE_VARCHAR

    16

    MYSQL_TYPE_BIT

    17

    MYSQL_TYPE_TIMESTAMP2

    Used only in the replication protocol

    18

    MYSQL_TYPE_DATETIME2

    Used only in the replication protocol

    19

    MYSQL_TYPE_TIME2

    Used only in the replication protocol

    245

    MYSQL_TYPE_JSON

    (only used with MySQL, MariaDB uses MYSQL_TYPE_STRING for JSON)

    246

    MYSQL_TYPE_NEWDECIMAL

    247

    MYSQL_TYPE_ENUM

    248

    MYSQL_TYPE_SET

    249

    MYSQL_TYPE_TINY_BLOB

    250

    MYSQL_TYPE_MEDIUM_BLOB

    251

    MYSQL_TYPE_LONG_BLOB

    252

    MYSQL_TYPE_BLOB

    253

    MYSQL_TYPE_VAR_STRING

    254

    MYSQL_TYPE_STRING

    255

    MYSQL_TYPE_GEOMETRY

    16

    BLOB

    is this field a Blob

    32

    UNSIGNED

    is this field unsigned

    64

    ZEROFILL_FLAG

    is this field a zerofill

    128

    BINARY_COLLATION

    whether this field has a binary collation

    256

    ENUM

    Field is an enumeration

    512

    AUTO_INCREMENT

    field auto-increment

    1024

    TIMESTAMP

    field is a timestamp value

    2048

    SET

    field is a SET

    4096

    NO_DEFAULT_VALUE_FLAG

    field doesn't have default value

    8192

    ON_UPDATE_NOW_FLAG

    field is set to NOW on UPDATE

    32768

    NUM_FLAG

    field is num

    int<1> data type: 0x00:type, 0x01: format.

  • string value.

  • 0

    MYSQL_TYPE_DECIMAL

    byte encoding

    1

    MYSQL_TYPE_TINY

    TINYINT Binary encoding

    2

    MYSQL_TYPE_SHORT

    SMALLINT Binary encoding

    3

    MYSQL_TYPE_LONG

    1

    NOT_NULL

    field cannot be null

    2

    PRIMARY_KEY

    field is a primary key

    4

    UNIQUE_KEY

    field is unique

    8

    MULTIPLE_KEY

    Column Definition packet
    EOF_Packet
    result set row
    ERR_Packet
    OK_Packet
    OK packet (OK_Packet)
    EOF packet (EOF_Packet)
    result-set row packets (ResultsetRow)
    OK packet (OK_Packet)
    EOF packet (EOF_Packet)
    int
    EOF packet (EOF_Packet)
    string
    string
    string
    string
    BINARY
    VARBINARY
    POINT
    JSON

    field is in a multiple key

    INTEGER Binary encoding
    FLOAT Binary encoding
    DOUBLE Binary encoding
    TIMESTAMP Binary encoding
    BIGINT Binary encoding
    INTEGER Binary encoding
    TIMESTAMP Binary encoding
    TIME Binary encoding
    TIMESTAMP Binary encoding
    SMALLINT Binary encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    byte encoding
    "Progress reporting"