All pages
Powered by GitBook
1 of 10

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Using MariaDB with Your Programs (API)

Non-Blocking Client Library

MariaDB client library (starting with version 5.5.21) and MySQL Connector/C (starting with version 2.1.0) supports _non-blocking_ operations

Embedded MariaDB Interface

The embedded MariaDB server, libmysqld has the identical interface as the libmysqclient.

The normal usage of the embedded server is to use the normal mysql.h include file in your application and link with libmysqld instead of libmysqlclient.

The intention is that one should be able to move from a server/client version of MariaDB to a single server version of MariaDB by just changing which library you link with.

This means that the embedded C client API only changes when the normal C API changes, usually only between major releases.

The only major change required in your application if you are going to use the embedded server is that you have to call the following functions from your application:

int mysql_library_init(int argc, char **argv, char **groups)
void mysql_library_end(void);

This is also safe to do when using the standard C library.

Notes

  • libmysqld.so has many more exported symbols than the C library to allow one to expose and use more parts of MariaDB. In normal applications one should not use them, as they may change between every release.

  • Before (, , , ), the embedded server library did not support SSL when it was used to connect to remote servers.

  • Starting with the embedded server library and related test binaries are no longer part of binary tarball release archives.

See Also

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

libMariaDB

MariaDB 10.5
mariadb client with MariaDB embedded

Progress Reporting

MariaDB supports progress reporting for some long running commands.

What is Progress Reporting?

Progress reporting means that:

  • There is a Progress column in which shows the total progress (0-100%)

  • has three columns which allow you to see in which process stage we are and how much of that stage is completed:

    • STAGE

    • MAX_STAGE

    • PROGRESS (within current stage).

  • The client receives progress messages which it can display to the user to indicate how long the command will take.

We have separate progress reporting for stages because different stages take different amounts of time.

Supported Commands

Currently, the following commands can send progress report messages to the client:

  • (not LOAD DATA LOCAL INFILE, as in that case we don't know the size of the file).

Some Aria storage engine operations also support progress messages:

Limitations

Although the above commands support progress reporting, there are some limitations to what progress is reported. To be specific, when executing one of these commands against an InnoDB table with (which is the default in +), progress is only reported during the merge sort phase while reconstructing indexes.

Enabling and Disabling Progress Reporting

mysqld (the MariaDB server) automatically sends progress report messages to clients that support the new protocol, using the value of the variable. They are sent every max(global.progress_report_time , progress_report_time) seconds (by default 5). You can disable the sending of progress report messages to the client by setting either the local variable (affects only the current connection) or the global variable (affects all connections) to 0.

If the extra column in SHOW PROCESSLIST gives you a compatibility problem, you can disable it by starting mysqld with the --old flag.

Clients Which Support Progress Reporting

  • The

  • The mytop that comes with MariaDB has a '%' column which shows the progress.

Progress Reporting in the mysql Command Line Client

Progress reporting is enabled by default in the . You can disable it with --disable-progress-reports. It is automatically disabled in batch mode.

When enabled, for every supported command you get a progress report like:

This is updated every seconds (the default is 5). If the global progress_report_time is higher, this will be used. You can also disable error reporting by setting the variable to 0.

How to Add Support for Progress Reporting to a Client

You need to use the or later client library. You can check that the library supports progress reporting by doing:

To enable progress reporting to the client you need to addCLIENT_PROGRESS to the connect_flag in mysql_real_connect():

Then you need to provide a callback function for progress reports:

The above report_progress function will be called for each progress message.

This is the implementation used by mysql.cc:

If you want only one number for the total progress, you can calculate it with:

Note: proc_info is totally independent of stage. You can have many different proc_info values within a stage. The idea behind proc_info is to give the user more information about what the server is doing.

How to Add Support for Progress Reporting to a Storage Engine

The functions to use for progress reporting are:

Initialize progress reporting with stages. This is mainly used for commands that are totally executed within the engine, like CHECK TABLE. You should not use this for operations that could be called by, for example,ALTER TABLE as this has already called the function.

max_stage is the number of stages your storage engine will have.

The above is used for reporting progress.

  • progress is how much of the file/rows/keys you have gone through.

  • max_progress is the max number of rows you will go through.

You can call this with varying numbers, but normally the ratioprogress/max_progress should be increasing.

This function can be called even if you are not using stages, for example when enabling keys as part of ALTER TABLE or ADD INDEX.

To go to the next stage in a multi-stage process initiated bythd_progress_init():

End progress reporting; Sets 'Progress' back to 0 in SHOW PROCESSLIST.

This sets the name of the current status/stage that is displayed inSHOW PROCESSLIST and in the client. It's recommended that you call this between stages and thus before thd_progress_report() andthd_progress_next_stage().

This functions returns the last used proc_info. It's recommended that you restore proc_info to its original value when you are done processing.

Note: thd_proc_info() is totally independent of stage. You can have many different proc_info values within a stage to give the user more information about what is going on.

Examples to Look at in the MariaDB Source:

  • client/mysql.cc for an example of how to use reporting.

  • libmysql/client.c:cli_safe_read() to see how progress packets are handled in client

  • sql/protocol.cc::net_send_progress_packet() for how progress packets are handled in server.

Format of Progress Packets

The progress packet is sent as an error packet with error number 65535.

It contains the following data (in addition to the error header):

Option
Number of bytes
Other info

See Also

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

About Non-blocking Operation in the Client Library

MariaDB, starting with version 5.5.21 supports non-blocking operations in the client-library. This allows an application to start a query or other operation against the database, and then continue to do other work (in the same thread) while the request is sent over the network, the query is processed in the server, and the result travels back. As parts of the result become ready, the application can — at its leisure — call back into the library to continue processing, repeating this until the operation is completed.

Non-blocking operation is implemented entirely within the client library. This means no special server support is necessary and non-blocking operation works with any version of the MariaDB or MySQL server, the same as the normal blocking API. It also means that it is not possible to have two queries running at the same time on the same connection (this is a protocol limitation). But a single thread can have any number of non-blocking queries running at the same time, each using its own MYSQL connection object.

Non-blocking operation is useful when an application needs to run a number of independent queries in parallel at the same time, to speed up operation compared to running them sequentially one after the other. This could be multiple queries against a single server (to better utilize multiple CPU cores and/or a high-capacity I/O system on the server), or it could be queries against multiple servers (e.g. SHOW STATUS against all running servers for monitoring, or a map/reduce-like operation against a big sharded database).

Non-blocking operation is also very useful in applications that are already written in a non-blocking style, for example using a framework like libevent, or, for example, a GUI-application using an event loop. Using the non-blocking client library allows the integrations of database queries into such applications, without the risk of long-running queries "hanging" the user interface or stalling the event loop, and without having to manually spawn separate threads to run the queries and re-synchronize with the threads to get the results back.

In this context, "blocking" means the situation where communication on the network socket to the server has to wait while processing the query. Waiting can be necessary because the server has not yet had time to process the query, or because the data needs to travel over the network from the server, or even because the first part of a large request needs to be sent out on the network before local socket buffers can accept the last part. Whenever such a wait is necessary, control returns to the application. The application will then runselect() or poll() (or something similar) to detect when any wait condition is satisfied, and then call back into the library to continue processing.

An example program is available in the MariaDB source tree:

It uses libevent to run a set of queries in parallel from within a single thread / event loop. This is a good example of how to integrate non-blocking query processing into an event-based framework.

The non-blocking API in the client library is entirely optional. The new library is completely ABI- and source-compatible with existing applications. Also, applications not using non-blocking operations are not affected, nor is there any significant performance penalty for having support for non-blocking operations in the library for applications which do not use them.

The library internally uses co-routines, and requires a co-routine implementation to work. Native implementations are included for i386, amd64, and (since Connector/C version 3.3.12) aarch64 architectures. For other architectures, a fallback to ucontext is automatically used if available. An alternate fallback boost::context can also be used instead of ucontext by building with -DWITH_BOOST_CONTEXT=ON (boost::context is not used by default). If no co-routine implementation is available the non-blocking operations are disabled and will not work.

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

Status_length

1-2

Packet length of string in net_field_length() format

Status

Status_length

Status / Stage name

1

1

Number of strings. For future

Stage

1

Stage from 1 - Max_stage

Max_stage

1

Max number of stages

Progress

3

OPTIMIZE TABLE
ALGORITHM=INPLACE
progress_report_time
mariadb command line client
mariadb client
progress_report_time

Progress in % * 1000

tests/async_queries.c
ALTER TABLE my_mail ENGINE=aria;
Stage: 1 of 2 'copy to tmp table'  5.37% of stage done
#ifdef CLIENT_PROGRESS
mysql_real_connect(mysql, host, user, password,
                   database, opt_mysql_port, opt_mysql_unix_port,
                   connect_flag | CLIENT_PROGRESS);
static void report_progress(const MYSQL *mysql, uint stage, uint max_stage,
                            double progress, const char *proc_info,
                            uint proc_info_length);

mysql_options(&mysql, MYSQL_PROGRESS_CALLBACK, (void*) report_progress);
uint last_progress_report_length;

static void report_progress(const MYSQL *mysql, uint stage, uint max_stage,
                            double progress, const char *proc_info,
                            uint proc_info_length)
{
  uint length= printf("Stage: %d of %d '%.*s' %6.3g%% of stage done",
                      stage, max_stage, proc_info_length, proc_info, 
                      progress);
  if (length < last_progress_report_length)
    printf("%*s", last_progress_report_length - length, "");
  putc('\r', stdout);
  fflush(stdout);
  last_progress_report_length= length;
}
double total_progress=
 ((stage -1) / (double) max_stage * 100.00 + progress / max_stage);
void thd_progress_init(MYSQL_THD thd, unsigned int max_stage);
void thd_progress_report(MYSQL_THD thd, unsigned long long progress,
                         unsigned long long max_progress);
void thd_progress_next_stage(MYSQL_THD thd);
void thd_progress_end(MYSQL_THD thd);
const char *thd_proc_info(thd, 'stage name');

libmysqld

Articles about libmysqld.so, the embedded MariaDB server

The mariadb-test and mariadb-test-embedded Programs

The mariadb-test program runs a test case against a MariaDB or MySQL server and optionally compares the output with a result file. This program reads input written in a special test language. Typically, you invokemariadb-test via mariadb-test-run.pl rather than invoking it directly.

mariadb-test_embedded is similar but is built with support for the libmariadbd embedded server.

Features of mariadb-test:

  • Can send SQL statements to the server for execution

  • Can execute external shell commands

  • Can test whether the result from an SQL statement or shell command is as expected

  • Can connect to one or more standalone mariadbd servers and switch between connections

  • Can connect to an embedded server (libmariadbd), if MariaDB is compiled with support for libmariadbd. (In this case, the executable is named mariadb-test_embedded rather than mariadb-test.)

By default, mariadb-test reads the test case on the standard input. To run mariadb-test this way, you normally invoke it like this:

You can also name the test case file with a--test-file=file_name option.

The exit value from mariadb-test is 0 for success, 1 for failure, and 62 if it skips the test case (for example, if after checking some preconditions it decides not to run the test).

Options

mariadb-test supports the following options:

Option
Description

See Also

This page is licensed: GPLv2

--cursor-protocol

Use cursors for prepared statements.

--database=db_name, -D db_name

The default database to use.

--debug[=debug_options], -#[debug_options]

Write a debugging log if MariaDB is built with debugging support. The default debug_options value is d:t:S:i:O,/tmp/mysqltest.trace on Unix and d:t:i:O,\mysqld.trace on Windows.

--debug-check

Print some debugging information when the program exits.

--debug-info

Print debugging information and memory and CPU usage statistics when the program exits.

--host=host_name, -h host_name

Connect to the server on the given host.

--logdir=dir_name

The directory to use for log files.

--mark-progress

Write the line number and elapsed time to test_file.progress.

--max-connect-retries=num

The maximum number of connection attempts when connecting to server.

--max-connections=num

The maximum number of simultaneous server connections per client (that is, per test). If not set, the maximum is 128. Minimum allowed limit is 8, maximum is 5120.

--no-defaults

Do not read default options from any option files. If used, this must be the first option.

--non-blocking-api

Use the non-blocking client API for communication.

--overlay-dir=name

Overlay directory.

--password[=password], -p[password]

The password to use when connecting to the server. If you use the short option form (-p), you cannot have a space between the option and the password. If you omit the password value following the --password or -p option on the command line, you are prompted for one.

plugin-dir

Directory for client-side plugins.

--port=port_num, -P port_num

The TCP/IP port number to use for the connection, or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306).

--prologue=name

Include the contents of the given file before processing the contents of the test file. The included file should have the same format as other mariadb-test test files. This option has the same effect as putting a --source file_name command as the first line of the test file.

--protocol=name

The protocol to use for connection (tcp, socket, pipe, memory).

--ps-protocol

Use the prepared-statement protocol for communication.

--quiet

Suppress all normal output. This is a synonym for --silent.

--record, -r

Record the output that results from running the test file into the file named by the --result-file option, if that option is given. It is an error to use this option without also using --result-file.

--result-file=file_name, -R file_name

This option specifies the file for test case expected results. --result-file, together with --record, determines how mariadb-test treats the test actual and expected results for a test case:If the test produces no results, mariadb-test exits with an error message to that effect, unless --result-file is given and the named file is an empty file.Otherwise, if --result-file is not given, mariadb-test sends test results to the standard output. With --result-file but not --record, mariadb-test reads the expected results from the given file and compares them with the actual results. If the results do not match, mariadb-test writes a reject file in the same directory as the result file, outputs a diff of the two files, and exits with an error. With both --result-file and --record, mariadb-test updates the given file by writing the actual test results to it.

--result-format-version=#

Version of the result file format to use.

--server-arg=value, -A value

Pass the argument as an argument to the embedded server. For example, --server-arg=--tmpdir=/tmp or --server-arg=--core. Up to 64 arguments can be given.

--server-file=file_name, -F file_name

Read arguments for the embedded server from the given file. The file should contain one argument per line.

--shared-memory-base-name

Shared-memory name to use for Windows connections using shared memory to a local server (started with the --shared-memory option). Case-sensitive.

--silent, -s

Suppress all normal output.

--sleep=num, -T num

Cause all sleep commands in the test case file to sleep num seconds. This option does not affect real_sleep commands. An option value of 0 can be used, which effectively disables sleep commands in the test case.

--socket=path, -S path

The socket file to use when connecting to localhost (which is the default host).

--sp-protocol

Execute DML statements within a stored procedure. For every DML statement, mariadb-test creates and invokes a stored procedure that executes the statement rather than executing the statement directly.

--ssl

Enable (automatically enabled with other flags). Disable with --skip-ssl.

--ssl-ca=name

CA file in PEM format (check OpenSSL docs, implies --ssl).

--ssl-capath=name

CA directory (check OpenSSL docs, implies --ssl).

--ssl-cert=name

X509 cert in PEM format (implies --ssl).

--ssl-cipher=name

SSL cipher to use (implies --ssl).

--ssl-key=name

X509 key in PEM format (implies --ssl).

--ssl-crl=name

Certificate revocation list (implies --ssl).

--ssl-crlpath=name

Certificate revocation list path (implies --ssl).

--ssl-verify-server-cert

Verify server's "Common Name" in its cert against hostname used when connecting. This option is disabled by default.

--suite-dir=name

Suite directory.

--tail-lines=nn

Specify how many lines of the result to include in the output if the test fails because an SQL statement fails. The default is 0, meaning no lines of result printed.

--test-file=file_name, -x file_name

Read test input from this file. The default is to read from the standard input.

--timer-file=file_name, -m file_name

If given, the number of microseconds spent running the test will be written to this file. This is used by for its reporting.

--tmpdir=dir_name, -t dir_name

The temporary directory where socket files are created.

--user=user_name, -u user_name

The user name to use when connecting to the server.

--verbose, -v

Verbose mode. Print out more information about what the program does.

--version, -V

Display version information and exit.

--view-protocol

Every SELECT statement is wrapped inside a view.

--wait-longer-for-timeouts

Wait longer for timeouts. Useful when running under valgrind.

--help, -?

Display a help message and exit.

--basedir=dir, -b dir

The base directory for tests.

--character-sets-dir=path

The directory where character sets are installed.

--compress, -C

Compress all information sent between the client and the server if both support compression.

--connect-timeout=N

This can be used to set the MYSQL_OPT_CONNECT_TIMEOUT parameter of mysql_options to change the number of seconds before an unsuccessful connection attempt times out.

--continue-on-error

Continue test even if we got an error. This is mostly useful when testing a storage engine to see what from a test file it can execute, or to find all syntax errors in a newly created big test file

New Features for mysqltest in MariaDB
shell> mariadb-test **[options] [db_name]** < //test_file//
TLS for secure connection
mariadb-test-run.pl

Using the Non-blocking Library

The MariaDB non-blocking client API is modelled after the normal blocking library calls. This makes it easy to learn and remember. It makes it easier to translate code from using the blocking API to using the non-blocking API (or vice versa). And it also makes it simple to mix blocking and non-blocking calls in the same code path.

For every library call that may block on socket I/O, such as 'int mysql_real_query(MYSQL, query, query_length)', two additional non-blocking calls are introduced:

To do non-blocking operation, an application first callsmysql_real_query_start() instead of mysql_real_query(), passing the same parameters.

If mysql_real_query_start() returns zero, then the operation completed without blocking, and 'status' is set to the value that would normally be returned from mysql_real_query().

Else, the return value from mysql_real_query_start() is a bitmask of events that the library is waiting on. This can be MYSQL_WAIT_READ,MYSQL_WAIT_WRITE, or MYSQL_WAIT_EXCEPT, corresponding to the similar flags for select() or poll(); and it can include MYSQL_WAIT_TIMEOUT when waiting for a timeout to occur (e.g. a connection timeout).

In this case, the application continues other processing and eventually checks for the appropriate condition(s) to occur on the socket (or for timeout). When this occurs, the application can resume the operation by callingmysql_real_query_cont(), passing in 'wait_status' a bitmask of the events which actually occurred.

Just like mysql_real_query_start(), mysql_real_query_cont() returns zero when done, or a bitmask of events it needs to wait on. Thus the application continues to repeatedly call mysql_real_query_cont(), intermixed with other processing of its choice; until zero is returned, after which the result of the operation is stored in 'status'.

Some calls, like mysql_option(), do not do any socket I/O, and so can never block. For these, there are no separate _start() or _cont() calls. See the "" page for a full list of what functions can and can not block.

The checking for events on the socket / timeout can be done with select() or poll() or a similar mechanism. Though often it will be done using a higher-level framework (such as libevent), which supplies facilities for registering and acting on such conditions.

The descriptor of the socket on which to check for events can be obtained by calling mysql_get_socket(). The duration of any timeout can be obtained from mysql_get_timeout_value().

Here is a trivial (but full) example of running a query with the non-blocking API. The example is found in the MariaDB source tree asclient/async_example.c. (A larger, more realistic example using libevent is found as tests/async_queries.c in the source):

Setting MYSQL_OPT_NONBLOCK

Before using any non-blocking operation, it is necessary to enable it first by setting the MYSQL_OPT_NONBLOCK option:

This call can be made at any time — typically it will be done at the start, before mysql_real_connect(), but it can be done at any time to start using non-blocking operations.

If a non-blocking operation is attempted without setting theMYSQL_OPT_NONBLOCK option, the program will typically crash with a NULL pointer exception.

The argument for MYSQL_OPT_NONBLOCK is the size of the stack used to save the state of a non-blocking operation while it is waiting for I/O and the application is doing other processing. Normally, applications will not have to change this, and it can be passed as zero to use the default value.

Mixing blocking and non-blocking operation

It is possible to freely mix blocking and non-blocking calls on the sameMYSQL connection.

Thus, an application can do a normal blocking mysql_real_connect() and subsequently do a non-blocking mysql_real_query_start(). Or vice versa, do a non-blocking mysql_real_connect_start(), and later do a blockingmysql_real_query() on the resulting connection.

Mixing can be useful to allow code to use the simpler blocking API in parts of the program where waiting is not a problem. For example establishing the connection(s) at program startup, or doing small quick queries between large, long-running ones.

The only restriction is that any previous non-blocking operation must have finished before starting a new blocking (or non-blocking) operation, see the next section: "Terminating a non-blocking operation early" below.

Terminating a non-blocking operation early

When a non-blocking operation is started with mysql_real_query_start() or another _start() function, it must be allowed to finish before starting a new operation. Thus, the application must continue calling mysql_real_query_cont() until zero is returned, indicating that the operation is completed. It is not allowed to leave one operation "hanging" in the middle of processing and then start a new one on top of it.

It is, however, permissible to terminate the connection completely withmysql_close() in the middle of processing a non-blocking call. A new connection must then be initiated with mysql_real_connect before new queries can be run, either with a new MYSQL object or re-using the old one.

In the future, we may implement an abort facility to force an on-going operation to terminate as quickly as possible (but it will still be necessary to call mysql_real_query_cont() one last time after abort, allowing it to clean up the operation and return immediately with an appropriate error code).

Restrictions

DNS

When mysql_real_connect_start() is passed a hostname (as opposed to a local unix socket or an IP address, it may need to look up the hostname in DNS, depending on local host configuration (e.g. if the name is not in/etc/hosts or cached). Such DNS lookups do not happen in a non-blocking way. This means that mysql_real_connect_start() will not return control to the application while waiting for the DNS response. Thus the application may "hang" for some time if DNS is slow or non-functional.

If this is a problem, the application can pass an IP address tomysql_real_connect_start() instead of a hostname, which avoids the problem. The IP address can be obtained by the application with whatever non-blocking DNS loopup operation is available to it from the operating system or event framework used. Alternatively, a simple solution may be to just add the hostname to the local host lookup file (/etc/hosts on Posix/Unix/Linux machines).

Windows Named Pipes and Shared Memory connections

There is no support in the non-blocking API for connections using Windows named pipes or shared memory

Named pipes and shared memory can still be used, using either the blocking or the non-blocking API. However, operations that need to wait on I/O on the named pipe will not return control to the application; instead they will "hang" waiting for the operation to complete, just like the normal blocking API calls.

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

int mysql_real_query_start(&status, MYSQL, query, query_length)
int mysql_real_query_cont(&status, MYSQL, wait_status)
Non-blocking API reference
static void run_query(const char *host, const char *user, const char *password) {
  int err, status;
  MYSQL mysql, *ret;
  MYSQL_RES *res;
  MYSQL_ROW row;

  mysql_init(&mysql);
  mysql_options(&mysql, MYSQL_OPT_NONBLOCK, 0);

  status = mysql_real_connect_start(&ret, &mysql, host, user, password, NULL, 0, NULL, 0);
  while (status) {
    status = wait_for_mysql(&mysql, status);
    status = mysql_real_connect_cont(&ret, &mysql, status);
  }

  if (!ret)
    fatal(&mysql, "Failed to mysql_real_connect()");

  status = mysql_real_query_start(&err, &mysql, SL("SHOW STATUS"));
  while (status) {
    status = wait_for_mysql(&mysql, status);
    status = mysql_real_query_cont(&err, &mysql, status);
  }
  if (err)
    fatal(&mysql, "mysql_real_query() returns error");

  /* This method cannot block. */
  res= mysql_use_result(&mysql);
  if (!res)
    fatal(&mysql, "mysql_use_result() returns error");

  for (;;) {
    status= mysql_fetch_row_start(&row, res);
    while (status) {
      status= wait_for_mysql(&mysql, status);
      status= mysql_fetch_row_cont(&row, res, status);
    }
    if (!row)
      break;
    printf("%s: %s\n", row[0], row[1]);
  }
  if (mysql_errno(&mysql))
    fatal(&mysql, "Got error while retrieving rows");
  mysql_free_result(res);
  mysql_close(&mysql);
}

/* Helper function to do the waiting for events on the socket. */
static int wait_for_mysql(MYSQL *mysql, int status) {
  struct pollfd pfd;
  int timeout, res;

  pfd.fd = mysql_get_socket(mysql);
  pfd.events =
    (status & MYSQL_WAIT_READ ? POLLIN : 0) |
    (status & MYSQL_WAIT_WRITE ? POLLOUT : 0) |
    (status & MYSQL_WAIT_EXCEPT ? POLLPRI : 0);
  if (status & MYSQL_WAIT_TIMEOUT)
    timeout = 1000*mysql_get_timeout_value(mysql);
  else
    timeout = -1;
  res = poll(&pfd, 1, timeout);
  if (res == 0)
    return MYSQL_WAIT_TIMEOUT;
  else if (res < 0)
    return MYSQL_WAIT_TIMEOUT;
  else {
    int status = 0;
    if (pfd.revents & POLLIN) status |= MYSQL_WAIT_READ;
    if (pfd.revents & POLLOUT) status |= MYSQL_WAIT_WRITE;
    if (pfd.revents & POLLPRI) status |= MYSQL_WAIT_EXCEPT;
    return status;
  }
}
mysql_options(&mysql, MYSQL_OPT_NONBLOCK, 0);

Non-blocking API Reference

Here is a list of all functions in the non-blocking client API and their parameters. Apart from operating in a non-blocking way, they all work exactly the same as their blocking counterparts, so their exact semantics can be obtained from the documentation of the normal client API.

The API also contains the following three functions which are used to get the socket fd and timeout values when waiting for events to occur:

Return the descriptor of the socket used for the connection.

This should only be called when a _start() or _cont() function returns a value with the MYSQL_WAIT_TIMEOUT flag set. In this case, it returns the value, in seconds, after which a timeout has occured and the application should call the appropriate _cont() function passingMYSQL_WAIT_TIMEOUT as the event that occured.

This is used to handle connection and read timeouts.

This function is available starting from and .

Like mysql_get_timeout_value(), this should only be called when a _start() or _cont() function returns a value with the MYSQL_WAIT_TIMEOUT flag set. In this case, it returns the value, in millisecondsseconds, after which a timeout has occured and the application should call the appropriate _cont() function passing MYSQL_WAIT_TIMEOUT as the event that occured.

The difference to mysql_get_timeout_value() is that this provides millisecond resolution for timeouts, rather than just whole seconds. In , internal timeouts can now be in milliseconds, while in 5.5 and below it was only whole seconds.

This milliseconds version is provided also in (from 5.5.28 onwards) to make it easier for applications to work with either library version. However, in 5.5 it always returns a multiple of 1000 milliseconds.

is a list of all functions from the normal API which can be used safely in a non-blocking program, since they never need to block.

mysql_real_connect_start() initiates a non-blocking connection request to a server.

When mysql_real_connect_start() or mysql_real_connect_cont() returns zero, a copy of the passed 'mysql' argument is stored in *ret.

Initiate fetch of another row from a SELECT query.

If the MYSQL_RES was obtained from mysql_use_result(), then this function allows stream processing, where initial rows are returned to the application while the server is still sending subsequent rows. When no more data is available on the socket, mysql_fetch_row_start() ormysql_fetch_row_cont() will return MYSQL_WAIT_READ (or possiblyMYSQL_WAIT_WRITE if using TLS and TLS re-negotiation is needed; alsoMYSQL_WAIT_TIMEOUT may be set if read timeout is enabled). When data becomes available, more rows can be fetched with mysql_fetch_row_cont().

If the MYSQL_RES was obtained from mysql_store_result() /mysql_store_result_start() / mysql_store_result_cont(), then this function cannot block — mysql_fetch_row_start() will always return 0 (and if desired, plain mysql_fetch_row() may be used instead with equivalent effect).

This function can need to wait if not all rows were fetched before it was called (then it needs to consume any pending rows sent from the server so they do not interfere with any subsequent queries sent).

If all rows were already fetched, then this function will not need to wait.mysql_free_result_start() will return zero (or if so desired, plainmysql_free_result() may be used instead).

Note that mysql_free_result() returns no value, so there is no extra 'ret' parameter for mysql_free_result_start() ormysql_free_result_cont().

mysql_close() sends a COM_QUIT request to the server, though it does not wait for any reply.

Thus teoretically it can block (if the socket buffer is full), though in practise it is probably unlikely to occur frequently.

The non-blocking version of mysql_close() is provided for completeness; for many applications using the normal mysql_close() is probably sufficient (and may be simpler).

Note that mysql_close() returns no value, so there is no extra 'ret' parameter for mysql_close_start() or mysql_close_cont().

Client API functions which never block

The following client API functions never need to do I/O and thus can never block. Therefore, they can be used as normal in programs using non-blocking operations; no need to call any special _start() variant. (Even if a_start() variant was available, it would always return zero, so no_cont() call would ever be needed).

  • mysql_eof()``

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

my_socket mysql_get_socket(const MYSQL *mysql)
unsigned int STDCALL mysql_get_timeout_value(const MYSQL *mysql)
  • mysql_set_local_infile_handler()

  • mysql_set_local_infile_default()

  • mysql_get_server_name()

  • myodbc_remove_escape()

  • mysql_thread_safe()

  • mysql_embedded()

  • mariadb_connection()

  • mysql_get_socket()

  • mysql_get_timeout_value

  • mysql_get_timeout_value_ms

  • At the end
    unsigned int STDCALL mysql_get_timeout_value_ms(const MYSQL *mysql)
    int mysql_real_connect_start(MYSQL **ret, MYSQL *mysql, const char *host,
                             const char *user, const char *passwd, const char *db,
                             unsigned int port, const char *unix_socket,
                             unsigned long client_flags)
    int mysql_real_connect_cont(MYSQL **ret, MYSQL *mysql, int ready_status)
    int mysql_real_query_start(int *ret, MYSQL *mysql, const char *stmt_str,
    unsigned long length)
    int mysql_real_query_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_fetch_row_start(MYSQL_ROW *ret, MYSQL_RES *result)
    int mysql_fetch_row_cont(MYSQL_ROW *ret, MYSQL_RES *result, int ready_status)
    int mysql_set_character_set_start(int *ret, MYSQL *mysql, const char *csname)
    int mysql_set_character_set_cont(int *ret, MYSQL *mysql, int ready_status)
    mysql_select_db_start(int *ret, MYSQL *mysql, const char *db)
    int mysql_select_db_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_send_query_start(int *ret, MYSQL *mysql, const char *q, unsigned long length)
    int mysql_send_query_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_store_result_start(MYSQL_RES **ret, MYSQL *mysql)
    int mysql_store_result_cont(MYSQL_RES **ret, MYSQL *mysql, int ready_status)
    int mysql_free_result_start(MYSQL_RES *result)
    int mysql_free_result_cont(MYSQL_RES *result, int ready_status)
    int mysql_close_start(MYSQL *sock)
    int mysql_close_cont(MYSQL *sock, int ready_status)
    int mysql_change_user_start(my_bool *ret, MYSQL *mysql, const char *user, const
                                char *passwd, const char *db)
    int mysql_change_user_cont(my_bool *ret, MYSQL *mysql, int ready_status)
    int mysql_query_start(int *ret, MYSQL *mysql, const char *q)
    int mysql_query_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_shutdown_start(int *ret, MYSQL *mysql, enum mysql_enum_shutdown_level
                            shutdown_level)
    int mysql_shutdown_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_dump_debug_info_start(int *ret, MYSQL *mysql)
    int mysql_dump_debug_info_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_refresh_start(int *ret, MYSQL *mysql, unsigned int refresh_options)
    int mysql_refresh_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_kill_start(int *ret, MYSQL *mysql, unsigned long pid)
    int mysql_kill_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_set_server_option_start(int *ret, MYSQL *mysql,
                                  enum enum_mysql_set_option option)
    int mysql_set_server_option_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_ping_start(int *ret, MYSQL *mysql)
    int mysql_ping_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_stat_start(const char **ret, MYSQL *mysql)
    int mysql_stat_cont(const char **ret, MYSQL *mysql, int ready_status)
    int mysql_list_dbs_start(MYSQL_RES **ret, MYSQL *mysql, const char *wild)
    int mysql_list_dbs_cont(MYSQL_RES **ret, MYSQL *mysql, int ready_status)
    int mysql_list_tables_start(MYSQL_RES **ret, MYSQL *mysql, const char *wild)
    int mysql_list_tables_cont(MYSQL_RES **ret, MYSQL *mysql, int ready_status)
    int mysql_list_processes_start(MYSQL_RES **ret, MYSQL *mysql)
    int mysql_list_processes_cont(MYSQL_RES **ret, MYSQL *mysql, int ready_status)
    int mysql_list_fields_start(MYSQL_RES **ret, MYSQL *mysql, const char *table,
                            const char *wild)
    int mysql_list_fields_cont(MYSQL_RES **ret, MYSQL *mysql, int ready_status)
    int mysql_read_query_result_start(my_bool *ret, MYSQL *mysql)
    int mysql_read_query_result_cont(my_bool *ret, MYSQL *mysql, int ready_status)
    int mysql_stmt_prepare_start(int *ret, MYSQL_STMT *stmt, const char *query,
                             unsigned long length)
    int mysql_stmt_prepare_cont(int *ret, MYSQL_STMT *stmt, int ready_status)
    int mysql_stmt_execute_start(int *ret, MYSQL_STMT *stmt)
    int mysql_stmt_execute_cont(int *ret, MYSQL_STMT *stmt, int ready_status)
    int mysql_stmt_fetch_start(int *ret, MYSQL_STMT *stmt)
    int mysql_stmt_fetch_cont(int *ret, MYSQL_STMT *stmt, int ready_status)
    int mysql_stmt_store_result_start(int *ret, MYSQL_STMT *stmt)
    int mysql_stmt_store_result_cont(int *ret, MYSQL_STMT *stmt, int ready_status)
    int mysql_stmt_close_start(my_bool *ret, MYSQL_STMT *stmt)
    int mysql_stmt_close_cont(my_bool *ret, MYSQL_STMT *stmt, int ready_status)
    int mysql_stmt_reset_start(my_bool *ret, MYSQL_STMT *stmt)
    int mysql_stmt_reset_cont(my_bool *ret, MYSQL_STMT *stmt, int ready_status)
    int mysql_stmt_free_result_start(my_bool *ret, MYSQL_STMT *stmt)
    int mysql_stmt_free_result_cont(my_bool *ret, MYSQL_STMT *stmt, int ready_status)
    int mysql_stmt_send_long_data_start(my_bool *ret, MYSQL_STMT *stmt,
                                    unsigned int param_number,
                                    const char *data, unsigned long length)
    int mysql_stmt_send_long_data_cont(my_bool *ret, MYSQL_STMT *stmt, int ready_status)
    int mysql_commit_start(my_bool *ret, MYSQL *mysql)
    int mysql_commit_cont(my_bool *ret, MYSQL *mysql, int ready_status)
    int mysql_rollback_start(my_bool *ret, MYSQL *mysql)
    int mysql_rollback_cont(my_bool *ret, MYSQL *mysql, int ready_status)
    int mysql_autocommit_start(my_bool *ret, MYSQL *mysql, my_bool auto_mode)
    int mysql_autocommit_cont(my_bool *ret, MYSQL *mysql, int ready_status)
    int mysql_next_result_start(int *ret, MYSQL *mysql)
    int mysql_next_result_cont(int *ret, MYSQL *mysql, int ready_status)
    int mysql_stmt_next_result_start(int *ret, MYSQL_STMT *stmt)
    int mysql_stmt_next_result_cont(int *ret, MYSQL_STMT *stmt, int ready_status)
    C client library
    mysql_library_init
    mysql_num_rows()
    mysql_num_fields()
    mysql_fetch_field_direct()
    mysql_fetch_fields()
    mysql_row_tell
    mysql_field_tell()
    mysql_field_count()
    mysql_affected_rows()
    mysql_insert_id()
    mysql_errno()
    mysql_error()
    mysql_sqlstate()
    mysql_warning_count()
    mysql_info()
    mysql_thread_id()
    mysql_character_set_name()
    mysql_init()
    mysql_ssl_set()
    mysql_get_ssl_cipher()
    mysql_use_result()
    mysql_get_character_set_info()
    mysql_get_server_info()
    mysql_get_client_info()
    mysql_get_client_version()
    mysql_get_host_info()
    mysql_get_server_version()
    mysql_get_proto_info()
    mysql_options()
    mysql_data_seek()
    mysql_row_seek()
    mysql_field_seek()
    mysql_fetch_lengths()
    mysql_fetch_field()
    mysql_escape_string()
    mysql_hex_string()
    mysql_real_escape_string()
    mysql_debug()
    mysql_stmt_init()
    mysql_stmt_fetch_column()
    mysql_stmt_param_count()
    mysql_stmt_attr_set()
    mysql_stmt_attr_get()
    mysql_stmt_bind_param()
    mysql_stmt_bind_result()
    mysql_stmt_result_metadata()
    mysql_stmt_param_metadata()
    mysql_stmt_errno()
    mysql_stmt_error()
    mysql_stmt_sqlstate()
    mysql_stmt_row_seek()
    mysql_stmt_row_tell()
    mysql_stmt_data_seek()
    mysql_stmt_num_rows()
    mysql_stmt_affected_rows()
    mysql_stmt_insert_id()
    mysql_stmt_field_count()
    mysql_more_results()
    MariaDB 5.5.60
    MariaDB 10.0.35
    MariaDB 10.1.33
    MariaDB 10.2.15
    MariaDB 10.3.7
    MariaDB 5.5.28
    MariaDB 10.0.0
    MariaDB 10.0
    MariaDB 5.5
    MariaDB 10.0
    MariaDB 5.3
    What is MariaDB 5.3
    SHOW PROCESSLIST
    INFORMATION_SCHEMA.PROCESSLIST
    ALTER TABLE
    CREATE INDEX
    DROP INDEX
    LOAD DATA INFILE
    CHECK TABLE
    REPAIR TABLE
    ANALYZE TABLE