All pages
Powered by GitBook
1 of 7

4 - Server Response Packets

For most commands which the client sends to the server, the server returns the following response packets

EOF_Packet

The eof packet marks the end of a resultset and returns status and warnings.

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

Fields

  • int<1> 0xfe : EOF header

  • int<2> warning count

  • int<2> server status

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

ERR_Packet

ERR_Packet

ERR_Packet indicates that an error occured.

Fields

  • int<1> ERR_Packet header = 0xFF

  • int<2> error code. see error list

  • if (errorcode == 0xFFFF) /* progress reporting */

    • int<1> stage

    • int<1> max_stage

    • int<3> progress

    • string progress_info

  • else

    • if (next byte = '#')

      • string<1> sql state marker '#'

      • string<5>sql state

      • string human-readable error message

    • else

      • string human-readable error message

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

"Progress reporting"

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

OK_Packet

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

  • int<1> 0x00 : OK_Packet header or (0xFE if CLIENT_DEPRECATE_EOF is set)

  • int affected rows

  • int last insert id

  • 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)

      • string session state info

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 SSL certificate verification signature. For the following OK_Packets it contains (if present) various human-readable information.

Server status flag

Values of server status flag

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

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 reprepare has been executed)

SERVER_QUERY_WAS_SLOW

1<<11

The query was slower than long_query_time

SERVER_PS_OUT_PARAMS

1<<12

This resultset contain 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 Session change type for more information

Session state info

  • while packet has remaining data

    • int<1> session change type

    • if (session-change-type != SESSION_TRACK_STATE_CHANGE)

      • int total length

    • string session data's change

Session change type

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

session data's change

Each type of data has his own kind of format :

SESSION_TRACK_SCHEMA

  • string new current schema

SESSION_TRACK_SYSTEM_VARIABLES

while there is remaining data :

  • string variable data

for each variable data :

  • string variable name

  • string variable value

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

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

    • ttl : cache timeout in ms to remember redirection, in order to reconnect directly to new host. 0=no caching

SESSION_TRACK_STATE_CHANGE

indicates if session state changes occured. The value is represented as "1".

  • string "1" if session state tracking was enabled

SESSION_TRACK_GTIDS

This tracker is not implemented by MariaDB.

SESSION_TRACK_TRANSACTION_CHARACTERISTICS

  • string Transaction characteristics

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

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>;

SESSION_TRACK_TRANSACTION_STATE

  • string 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: _ Read-only transaction: R

  4. Transaction write safe: _ Transaction write unsafe: w

  5. Unknown transaction type: _ Read-write transaction: W

  6. Transaction statement safe: _ Transaction statement unsafe: s

  7. Transaction does not have resultsets: _ Transaction with resultsets: S

  8. No locked tables: _ Tables have been locked: L

Notes

  • Session tracking is supported from MariaDB 10.2. To determine if session tracking is enabled, check if the CLIENT_SESSION_TRACK flag is set in server_capabilities.

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

LOCAL_INFILE Packet

LOCAL_INFILE Packet

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

Fields

  • int<1> 0xFB : LOCAL_INFILE header

  • string 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 OK_packet or an ERR_packet.

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

Result Set Packets

MariaDB Server sends the following packets as part of a result set: A resultset consists of different packets:

  • Resultset metadata

    • 1 Column count packet

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

    • for each column (i.e column_count times)

      • Column Definition packet

  • if not (CLIENT_DEPRECATE_EOF capability set) EOF_Packet

  • n resultset row

  • if error

    • ERR_Packet

  • else

    • if CLIENT_DEPRECATE_EOF capability

      • OK_Packet with a 0xFE header

    • else EOF_Packet

It would be unsafe to assume that any packet with a 0xFE header is an OK packet (OK_Packet) or an EOF packet (EOF_Packet), because result-set row packets (ResultsetRow) 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 OK packet (OK_Packet) or an EOF packet (EOF_Packet), 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:

  • int 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 will follow the column definitions. If the metadata byte is set to 0, the Column Count Packet is immediately followed by the second EOF packet (EOF_Packet) or the resultset 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:

  • string catalog (always 'def')

  • string schema

  • string table alias

  • string table

  • 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 -

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 resultset rows.

Value
Protocol Column Type
Encoding

0

MYSQL_TYPE_DECIMAL

byte encoding

1

MYSQL_TYPE_TINY

TINYINT Binary encoding

2

MYSQL_TYPE_SHORT

SMALLINT Binary encoding

3

MYSQL_TYPE_LONG

INTEGER Binary encoding

4

MYSQL_TYPE_FLOAT

FLOAT Binary encoding

5

MYSQL_TYPE_DOUBLE

DOUBLE Binary encoding

6

MYSQL_TYPE_NULL

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

7

MYSQL_TYPE_TIMESTAMP

TIMESTAMP Binary encoding

8

MYSQL_TYPE_LONGLONG

BIGINT Binary encoding

9

MYSQL_TYPE_INT24

INTEGER Binary encoding

10

MYSQL_TYPE_DATE

TIMESTAMP Binary encoding

11

MYSQL_TYPE_TIME

TIME Binary encoding

12

MYSQL_TYPE_DATETIME

TIMESTAMP Binary encoding

13

MYSQL_TYPE_YEAR

SMALLINT Binary encoding

14

MYSQL_TYPE_NEWDATE

byte encoding

15

MYSQL_TYPE_VARCHAR

byte encoding

16

MYSQL_TYPE_BIT

byte encoding

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

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

246

MYSQL_TYPE_NEWDECIMAL

byte encoding

247

MYSQL_TYPE_ENUM

byte encoding

248

MYSQL_TYPE_SET

byte encoding

249

MYSQL_TYPE_TINY_BLOB

byte encoding

250

MYSQL_TYPE_MEDIUM_BLOB

byte encoding

251

MYSQL_TYPE_LONG_BLOB

byte encoding

252

MYSQL_TYPE_BLOB

byte encoding

253

MYSQL_TYPE_VAR_STRING

byte encoding

254

MYSQL_TYPE_STRING

byte encoding

255

MYSQL_TYPE_GEOMETRY

byte 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

1

NOT_NULL

field cannot be null

2

PRIMARY_KEY

field is a primary key

4

UNIQUE_KEY

field is unique

8

MULTIPLE_KEY

field is in a multiple key

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

The BLOB flag cannot be used to determine if a column has binary data, because BINARY and VARBINARY 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 example:

  • For a POINT column, the column type field will be MYSQL_TYPE_GEOMETRY, but the extended type will indicate 'point'.

  • For a JSON column, the column type field will be MYSQL_TYPE_STRING, but the extended type will indicate 'json'.

  • while string has data

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

    • string value

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

Resultset row

A resultset row represents a database resultset unit, which is usually generated by executing a statement that queries the database. Using COM_STMT_EXECUTE the resultsetrow will be in binary format, others in text format.

Text resultset row

  • for each column

    • string column data

The byte representation of the string according to client character collation.

Binary resultset row

  • byte<1> 0x00 header

  • byte<(number_of_columns + 7) / 8> NULL-Bitmap

  • for each column

    • if column value is not null

      • 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 : MEDIUMINT Binary encoding

      • if MYSQL_TYPE_FLOAT type : FLOAT Binary encoding

      • if MYSQL_TYPE_SMALLINT type : SMALLINTBinary encoding

      • if MYSQL_TYPE_YEAR type : YEAR Binary encoding

      • if MYSQL_TYPE_TINYINT type : TINYINT Binary encoding

      • if MYSQL_TYPE_DATE type : DATE Binary encoding

      • if MYSQL_TYPE_TIMESTAMP type : TIMESTAMP Binary encoding

      • if MYSQL_TYPE_DATETIME type : TIMESTAMP Binary encoding

      • if MYSQL_TYPE_TIME type : TIME Binary encoding

      • if MYSQL_TYPE_NEWDECIMAL type : DECIMAL Binary encoding

      • 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): byte value

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 string. An DECIMAL(10,2) with a value of -15.5 is stored as

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

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 Column field detail flag.

INTEGER binary encoding

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

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 Column field detail flag.

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 Column field detail flag.

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

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)

8

secondes (0 if DATE type)

9-12

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

TIME binary encoding

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

  • int<1> data length: 0 for special '00:00:00' value, 8 without fractional seconds, 12 with fractional seconds

  • if data length > 0

    • int<1> 0 for positive time, 1 for negative time

    • int<4> days

    • int<1> hours

    • int<1> minutes

    • int<1> seconds

    • if data length > 8

      • int<4> microseconds

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