Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Documentation on the internal workings of MariaDB.
This category explains how we merge various source trees into MariaDB
Articles about MariaDB source code and related internals
How MariaDB uses memory
Articles about libmysqld.so, the embedded MariaDB server
Note: This page is obsolete. The information is old, outdated, or otherwise currently incorrect. We are keeping the page for historical reasons only. Do not rely on the information in this article.
We merge TokuDB from Tokutek git repositories on GutHub:
Just merge normally at release points (use tag names) and don't forget to update storage/tokudb/CMakeLists.txt, setting TOKUDB_VERSION correctly.
This page is licensed: CC BY-SA / Gnu FDL
Note: This page is obsolete. The information is old, outdated, or otherwise currently incorrect. We are keeping the page for historical reasons only. Do not rely on the information in this article.
Percona used to maintain XtraDB as a patch series against the InnoDB plugin. This affected how we started merging XtraDB in.
Now Percona maintains a normal source repository on launchpad (lp:percona-server). But we continue to merge the old way to preserve the history of our changes.
MariaDB client library (starting with version 5.5.21) and MySQL Connector/C (starting with version 2.1.0) supports _non-blocking_ operations
Storage engines can extend CREATE TABLE syntax with optional
index, field, and table attribute clauses. See Extending CREATE TABLE for more information.
Information Schema plugins can have their own and statements. See FLUSH and SHOW for Information Schema plugins.
Encryption plugins in MariaDB are used for the data at rest encryption feature. They are responsible for both key management and for the actual encryption and decryption of data.
Function plugins add new SQL functions to MariaDB. Unlike the old UDF API, function plugins can do almost anything that a built-function can.
The MariaDB plugin declaration differs from the MySQL plugin declaration in the following ways:
it has no useless 'reserved' field (the very last field in the MySQL plugin declaration)
it has a 'maturity' declaration
it has a field for a text representation of the version field
MariaDB can load plugins that only have the MySQL plugin declaration but both PLUGIN_MATURITY and PLUGIN_AUTH_VERSION will show up as 'Unknown' in the .
For compiled-in (not dynamically loaded) plugins, the presence of the MariaDB plugin declaration is mandatory.
The MariaDB plugin declaration looks like this:
This page is licensed: CC BY-SA / Gnu FDL
There used to be a lp:percona-xtradb tree, that we were merging from as:
Now we have to maintain our own XtraDB-5.5 repository to merge from. It is lp:~maria-captains/maria/xtradb-mergetree-5.5. Follow the procedures as described in Merging with a merge tree to merge from it.
This page is licensed: CC BY-SA / Gnu FDL
bzr merge lp:percona-xtradbThis article is obsolete. We don't use bzr anymore. This howto needs to be rewritten to explain how to create a merge tree in git.
Merge tree in the context of this HOWTO is a tree created specifically to simplify merges of third-party packages into MariaDB. WIth a merge tree there's a clear separation between upstream changes and our changes and in most cases bzr can do the merges automatically.
Here's how I created a merge tree for pcre:
prerequisites: we already have pcre in the MariaDB tree, together with our changes (otherwise one can trivially create a bzr repository out of source pcre tarball).
create an empty repository:
mkdir pcre
cd pcre
bzr initdownload pcre source tarball of the same version that we have in the tree — pcre-8.34.tar.bz2
unpack it in the same place where the files are in the source tree:
Add files to the repository with the same file-ids as in the MariaDB tree!
All done. Commit and push
Now null-merge that into your MariaDB tree. Note, that for the initial merge you need to specify the revision range 0..1
Remove pcre files that shouldn't be in MariaDB tree, revert all changes that came from pcre (remember — it's a null-merge, pcre-8.34 is already in MariaDB tree), rename files in place as needed, resolve conflicts:
Verify that the tree is unchanged and commit:
Congratulations, your new merge tree is ready!
Now see .
This page is licensed: CC BY-SA / Gnu FDL
/* MariaDB plugin declaration */
maria_declare_plugin(example)
{
MYSQL_STORAGE_ENGINE_PLUGIN, /* the plugin type (see include/mysql/plugin.h) */
&example_storage_engine_info, /* pointer to type-specific plugin descriptor */
"EXAMPLEDB", /* plugin name */
"John Smith", /* plugin author */
"Example of plugin interface", /* the plugin description */
PLUGIN_LICENSE_GPL, /* the plugin license (see include/mysql/plugin.h) */
example_init_func, /* Pointer to plugin initialization function */
example_deinit_func, /* Pointer to plugin deinitialization function */
0x0001 /* Numeric version 0xAABB means AA.BB version */,
example_status_variables, /* Status variables */
example_system_variables, /* System variables */
"0.1 example", /* String version representation */
MariaDB_PLUGIN_MATURITY_EXPERIMENTAL /* Maturity (see include/mysql/plugin.h)*/
}
maria_declare_plugin_end;SHOWNote that SHOW statement is a user-friendly shortcut; it's easier to type and should be easier to view — if the Information Schema table contains many columns, the SHOW statement is supposed to display only most important columns and fit nicely on the 80x25 terminal screen.
Consider an example, LOCALES plugin:
While the table has 8 columns, the statement will only display 4 of them:
To support the FLUSH statement a plugin must declare the reset_table callback. For example, in the QUERY_RESPONSE_TIME plugin:
This page is licensed: CC BY-SA / Gnu FDL
static ST_FIELD_INFO locale_info_locale_fields_info[]=
{
{"ID", 4, MYSQL_TYPE_LONGLONG, 0, 0, "Id", 0},
{"NAME", 255, MYSQL_TYPE_STRING, 0, 0, "Name", 0},
{"DESCRIPTION", 255, MYSQL_TYPE_STRING, 0, 0, "Description", 0},
{"MAX_MONTH_NAME_LENGTH", 4, MYSQL_TYPE_LONGLONG, 0, 0, 0, 0},
{"MAX_DAY_NAME_LENGTH", 4, MYSQL_TYPE_LONGLONG, 0, 0, 0, 0},
{"DECIMAL_POINT", 2, MYSQL_TYPE_STRING, 0, 0, 0, 0},
{"THOUSAND_SEP", 2, MYSQL_TYPE_STRING, 0, 0, 0, 0},
{"ERROR_MESSAGE_LANGUAGE", 64, MYSQL_TYPE_STRING, 0, 0, "Error_Message_Language", 0},
{0, 0, MYSQL_TYPE_STRING, 0, 0, 0, 0}
};We generally merge only released versions of MySQL into MariaDB trunk. This is to be able to release a well-working release of MariaDB at any time, without having to worry about including half-finished changes from MySQL. Merges of MySQL revisions in-between MySQL releases can still be done (eg. to reduce the merge task to smaller pieces), but should then be pushed to the maria-5.1-merge branch, not to the main lp:maria branch.
The merge command should thus generally be of this form:
As a general rule, when the MySQL and MariaDB side has changes with the same meaning but differing text, pick the MySQL variant when resolving this conflict. This will help reduce the number of conflicts in subsequent merges.
To assist in understanding test failures that arise during the merge, we pull the same revision to be merged into the lp:maria-captains/maria/mysql-5.1-testing tree for buildbot test. This allows to check easily if any failures introduced are also present in the vanilla MySQL tree being merged.
To help keep track of merges, we tag the result of a merge:
For example, when merging MySQL 5.1.39, the commit of the merge would be tagged like this:
The right-hand parent of tag:mariadb-merge-mysql-5.1.39 will be the revision tag:mysql-5.1.39. The left-hand parent will be a revision on the MariaDB trunk.
When merging, these tags and associated revisions can be used to generate some diffs, which are useful when resolving conflicts. Here is a diagram of the history in a merge:
Here,
'B' is the base revision when MariaDB was originally
branched from MySQL.
'A0' is the result of the last MySQL merge, eg.tag:mariadb-merge-mysql-5.1.38.
'Y0' is the MySQL revision that was last merged, eg.tag:mysql-5.1.38.
'Y1' is the MySQL revision to be merged in the new merge,
eg. tag:mysql-5.1.39.
'A1' is the result of committing the new merge, to be
tagged as eg. tag:mariadb-merge-mysql-5.1.39.
Then, these diffs can be useful:
'bzr diff -rY0..before:A1' - this is the MariaDB side of changes to be merged.
'bzr diff -rY0..Y1' - this is the MySQL side of changes to be merged.
'bzr diff -rA0..before:A1' - these are the new changes on the MariaDB side to be merged; this can be useful do separate them from other MariaDB-specific changes that have already been resolved against conflicting MySQL changes.
The documentation for MySQL is not maintained in the MySQL source bzr repository. Therefore changes to MySQL documentation needs to be merged separately.
Only some of the MySQL documentation is available under the GPL (man pages, help tables, installation instructions). Notably the MySQL manual is not available under the GPL, and so is not included in MariaDB in any form.
The man pages, help tables, and installation instruction READMEs are obtained from MySQL source tarballs and manually merged into the MariaDB source trees. The procedure for this is as follows:
There is a tree on Launchpad used for tracking merges:
(At the time of writing, this procedure only exists for the 5.1 series of MySQL and MariaDB. Additional merge base trees will be needed for other release series.)
This tree must only be used to import new documentation files from new MySQL upstream source tarballs. The procedure to import a new set of files when a new MySQL release happens is as follows:
Download the new MySQL source tarball and unpack it, say to mysql-5.1.38
run these commands:
Now do a normal merge from lp:maria-captains/maria/mysql-docs-merge-base into lp:maria
This page is licensed: CC BY-SA / Gnu FDL
tar xf ~/pcre-8.34.tar.bz2
mv pcre-8.34 pcre“Password validation” means ensuring that user passwords meet certain minimal security requirements. A dedicated plugin API allows the creation of password validation plugins that will check user passwords as they are set (in and statements) and either allow or reject them.
MariaDB comes with three password validation plugins — the simple_password_check plugin, the cracklib_password_check plugin and the password_reuse_check plugin. They are not enabled by default; use (or ) statement to install them.
When at least one password plugin is loaded, all new passwords will be validated and password-changing statements will fail if the password will not pass validation checks. Several password validation plugin can be loaded at the same time — in this case a password must pass all validation checks by all plugins.
One can use various SQL statements to change a user password:
These statements are subject to password validation. If at least one password validation plugin is loaded, plain-text passwords specified in these statements will be validated.
These statements can not possibly use password validation — there is nothing to validate, the original plain-text password is not available. MariaDB introduces a strict password validation mode — controlled by a global server variable. If the strict password validation is enabled and at least one password validation plugin is loaded then these “unvalidatable” passwords will be rejected. Otherwise they will be accepted. By default a strict password validation is enabled (but note that it has no effect if no password validation plugin is loaded).
Failed password validation:
Strict password validation:
Password validation plugin API is very simple. A plugin must implement only one method — validate_password(). This method takes two arguments — user name and the plain-text password. And it returns 0 when the password has passed the validation and 1 otherwise,
See also mysql/plugin_password_validation.h and password validation plugins in plugin/simple_password_check/ and plugins/cracklib_password_check/.
This page is licensed: CC BY-SA / Gnu FDL
If you have a merge tree, you merge into MariaDB as follows:
MariaDB merge trees are in the mergetrees repository. Add it as a new remote:
Check out the branch you want to update and merge, for example:
delete everything in the branch
download the latest released source tarball, unpack it, copy files into the repository:
for InnoDB-5.6: use the content of the storage/innobase/ of the latest MySQL 5.6 source release tarball.
for performance schema 5.6: use storage/perfschema, include/mysql/psi, mysql-test/suite/perfschema, and mysql-test/suite/perfschema_stress from the latest MySQL 5.6 source release tarball.
Now git add ., git commit (use the tarball version as a comment), git push
merge this branch into MariaDB
Sometimes after a merge, some changes may be needed:
for performance schema 5.6: update storage/perfschema/ha_perfschema.cc, plugin version under maria_declare_plugin.
for InnoDB-5.6: update storage/innobase/include/univ.i, setting INNODB_VERSION_MAJOR, INNODB_VERSION_MINOR, INNODB_VERSION_BUGFIX to whatever MySQL version you were merging from.
This page is licensed: CC BY-SA / Gnu FDL
MariaDB [test]> show locales;
+-----+-------+-------------------------------------+------------------------+
| Id | Name | Description | Error_Message_Language |
+-----+-------+-------------------------------------+------------------------+
| 0 | en_US | English - United States | english |
| 1 | en_GB | English - United Kingdom | english |
| 2 | ja_JP | Japanese - Japan | japanese |
| 3 | sv_SE | Swedish - Sweden | swedish |
...static int query_response_time_info_init(void *p)
{
ST_SCHEMA_TABLE *i_s_query_response_time= (ST_SCHEMA_TABLE *) p;
i_s_query_response_time->fields_info= query_response_time_fields_info;
i_s_query_response_time->fill_table= query_response_time_fill;
i_s_query_response_time->reset_table= query_response_time_flush;
query_response_time_init();
return 0;
}bzr merge -rtag:mysql-<MYSQL-VERSION> lp:mysql-server/5.1mariadb-merge-mysql-<MYSQL-VERSION>mariadb-merge-mysql-5.1.39B----maria------A0-------A1
\ / /
\ / /
---mysql---Y0------Y1lp:~maria-captains/maria/mysql-docs-merge-baseT=../mysql-5.1.38
bzr branch lp:~maria-captains/maria/mysql-docs-merge-base
cd mysql-docs-merge-base
for i in Docs/INSTALL-BINARY INSTALL-SOURCE INSTALL-WIN-SOURCE support-files/MacOSX/ReadMe.txt scripts/fill_help_tables.sql $(cd "$T" && find man -type f | grep '\.[0-9]$' | grep -v '^man/ndb_' | grep -v '^man/mysqlman.1$') ; do cp "$T/$i" $i; bzr add $i ; done
bzr commit -m"Imported MySQL documentation files from $T"
bzr push lp:~maria-captains/maria/mysql-docs-merge-basebzr add --file-ids-from ~/Abk/mysql/10.0bzr commit -m pcre-8.34
bzr push --remember lp:~maria-captains/maria/pcre-mergetreecd ~/Abk/mysql/10.0
bzr merge -r 0..1 ~/mergetrees/pcre/bzr rm `bzr added`
bzr revert --no-backup `bzr modified`
bzr resolve pcrebzr status
bzr commit -m 'pcre-8.34 mergetree initial merge'git remote add merge https://github.com/MariaDB/mergetreesgit checkout merge-innodb-5.6mysqlse/ subdirectory from the latest Sphinx source release tarball.for XtraDB: use the content of the storage/innobase/ of the latest Percona-Server source release tarball (5.5 or 5.6 as appropriate).
for pcre: simply unpack the latest pcre release source tarball into the repository, rename pcre-X-XX/ to pcre.
for XtraDB-5.5: update storage/xtradb/include/univ.i, setting PERCONA_INNODB_VERSION, INNODB_VERSION_STR to whatever Percona-Server version you were merging from.
for XtraDB-5.6: update storage/xtradb/include/univ.i, setting PERCONA_INNODB_VERSION, INNODB_VERSION_MAJOR, INNODB_VERSION_MINOR, INNODB_VERSION_BUGFIX to whatever Percona-Server version you were merging from.
When creating a connection, a THD object is created for that connection. This contains all connection information and also caches to speed up queries and avoid frequent malloc() calls.
When creating a new connection, the following malloc() calls are done for the THD:
The following information is the state in MariaDB 10.6.1 when compiled without debugging.
This is part of select memory_used from information_schema.processlist.
This page is licensed: CC BY-SA / Gnu FDL
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
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:
SET PASSWORD = PASSWORD('plain-text password');
SET PASSWORD FOR `user`@`host` = PASSWORD('plain-text password');
SET PASSWORD = OLD_PASSWORD('plain-text password');
SET PASSWORD FOR `user`@`host` = OLD_PASSWORD('plain-text password');
CREATE USER `user`@`host` IDENTIFIED BY 'plain-text password';
GRANT PRIVILEGES TO `user`@`host` IDENTIFIED BY 'plain-text password';SET PASSWORD = 'password hash';
SET PASSWORD FOR `user`@`host` = 'password hash';
CREATE USER `user`@`host` IDENTIFIED BY PASSWORD 'password hash';
CREATE USER `user`@`host` IDENTIFIED VIA mysql_native_password USING 'password hash';
CREATE USER `user`@`host` IDENTIFIED VIA mysql_old_password USING 'password hash';
GRANT PRIVILEGES TO `user`@`host` IDENTIFIED BY PASSWORD 'password hash';
GRANT PRIVILEGES TO `user`@`host` IDENTIFIED VIA mysql_native_password USING 'password hash';
GRANT PRIVILEGES TO `user`@`host` IDENTIFIED VIA mysql_old_password USING 'password hash';GRANT SELECT ON *.* to foobar IDENTIFIED BY 'raboof';
ERROR HY000: Your password does not satisfy the current policy requirements
SHOW WARNINGS;
+---------+------+----------------------------------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------------------------------+
| Warning | 1819 | cracklib: it is based on your username |
| Error | 1819 | Your password does not satisfy the current policy requirements |
+---------+------+----------------------------------------------------------------+GRANT SELECT ON *.* TO foo IDENTIFIED BY PASSWORD '2222222222222222';
ERROR HY000: The MariaDB server is running with the --strict-password-validation option so it cannot execute this statementTracking of changed session variables
280
THD::THD,my_hash_init(key_memory_user_var_entry,&user_vars
280
THD::THD,my_hash_init(PSI_INSTRUMENT_ME, &sequences
Cache of used sequences
1048
THD::THD, m_token_array= my_malloc(PSI_INSTRUMENT_ME, max_digest_length
16416
CONNECT::create_thd(), my_net_init(), net_allocate_new_packet()
This is for reading data from the connected user
16416
check_connection(), thd->packet.alloc()
This is for sending data to connected user
56
ACL_USER::copy(), dst->user= safe_lexcstrdup_root(root, user)
56
ACL_USER::copy()
Allocation of other connect attributes
56
ACL_USER::copy()
64
ACL_USER::copy()
64
ACL_USER::copy()
32
mysql_change_db()
Store current db in THD
48
dbname_cache->insert(db_name)
Store db name in db name cache
40
mysql_change_db(), my_register_filename(db.opt)
Store filename db.opt
8216
load_db_opt(), init_io_cache()
Disk cache for reading db.opt
1112
load_db_opts(), put_dbopts()
Cache default database parameters
26646
THD::THD
Allocation of THD object
256
Statement_map::Statement_map(), my_hash_init(key_memory_prepared_statement_map, &st_hash
Prepared statements
256
my_hash_init(key_memory_prepared_statement_map, &names_hash
Names of used prepared statements
128
wsrep_wfc(), Opt_trace_context(), dynamic_array()
1024
Diagnostics_area::init(),init_sql_alloc(PSI_INSTRUMENT_ME, &m_warn_root
120
72
send_server_handshake_packet, mpvio->cached_server_packet.pkt=
64
parse_client_handshake_packet, thd->copy_with_error(...db,db_len)
32
parse_client_handshake_packet, sctx->user=
368
ACL_USER::copy(), root=
Session_sysvars_tracker, global_system_variables.session_track_system_variables
Allocation of ACL_USER object
tests/async_queries.c(gdb) p thd->status_var.local_memory_used
$24 = 75496
(gdb) p thd->status_var.global_memory_used
$25 = 17544
(gdb) p thd->variables.query_prealloc_size
$30 = 24576
(gdb) p thd->variables.trans_prealloc_size
$37 = 4096libmysqld.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 MariaDB 10.5 the embedded server library and related test binaries are no longer part of binary tarball release archives.
This page is licensed: CC BY-SA / Gnu FDL
int mysql_library_init(int argc, char **argv, char **groups)
void mysql_library_end(void);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.
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:
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 ALGORITHM=INPLACE (which is the default in +), progress is only reported during the merge sort phase while reconstructing indexes.
mysqld (the MariaDB server) automatically sends progress report messages to clients that support the new protocol, using the value of the progress_report_time 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.
The mytop that comes with MariaDB has a '%' column which shows
the progress.
Progress reporting is enabled by default in the mariadb client. 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 progress_report_time 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.
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.
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.
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.
The progress packet is sent as an error packet with error number 65535.
It contains the following data (in addition to the error header):
1
1
Number of strings. For future
Stage
1
Stage from 1 - Max_stage
Max_stage
1
Max number of stages
Progress
3
This page is licensed: CC BY-SA / Gnu FDL
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):
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.
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.
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).
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).
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
MariaDB's requires the use of a . These plugins are responsible both for the management of encryption keys and for the actual encryption and decryption of data.
MariaDB supports the use of . Each encryption key uses a 32-bit integer as a key identifier. If the specific plugin supports , then encryption keys can also be rotated, which creates a new version of the encryption key.
See and for more information.
The Encryption plugin API was created to allow a plugin to:
ALTER TABLE my_mail ENGINE=aria;
Stage: 1 of 2 'copy to tmp table' 5.37% of stage done#ifdef CLIENT_PROGRESSmysql_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');int mysql_real_query_start(&status, MYSQL, query, query_length)
int mysql_real_query_cont(&status, MYSQL, wait_status)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);implement key management, provide encryption keys to the server on request and change them according to internal policies.
implement actual data encryption and decryption with the algorithm defined by the plugin.
This is how the API reflects that:
The first method is used for key rotation. A plugin that doesn't support key rotation — for example, file_key_management — can return a fixed version for any valid key id. Note that it still has to return an error for an invalid key id. The version ENCRYPTION_KEY_NOT_ENCRYPTED means that the data should not be encrypted.
The second method is used for key management, the server uses it to retrieve the key corresponding to a specific key identifier and a specific key version.
The last five methods deal with encryption. Note that they take the key to use and key identifier and version. This is needed because the server can derive a session-specific, user-specific, or a tablespace-specific key from the original encryption key as returned by get_key(), so the key argument doesn't have to match the encryption key as the plugin knows it. On the other hand, the encryption algorithm may depend on the key identifier and version (and in the example_key_management plugin it does) so the plugin needs to know them to be able to encrypt the data.
Encryption methods are optional — if unset (as in the debug_key_management plugin), the server will fall back to AES_CBC.
The MariaDB source tree has four encryption plugins. All these plugins are fairly simple and can serve as good examples of the Encryption plugin API.
It reads encryption keys from a plain-text file. It supports two different encryption algorithms. It supports multiple encryption keys. It does not support key rotation. See the File Key Management Plugin article for more details.
1.0
Stable
1.0
Gamma
1.0
Alpha
The AWS Key Management plugin uses the Amazon Web Services (AWS) Key Management Service (KMS) to generate and store AES keys on disk, in encrypted form, using the Customer Master Key (CMK) kept in AWS KMS. When MariaDB Server starts, the plugin will decrypt the encrypted keys, using the AWS KMS "Decrypt" API function. MariaDB data will then be encrypted and decrypted using the AES key. It supports multiple encryption keys. It supports key rotation.
See the AWS Key Management Plugin article for more details.
1.0
Stable
,
1.0
Beta
1.0
Experimental
Uses random time-based generated keys, ignores key identifiers, supports key versions and key rotation. Uses AES_ECB and AES_CBC as encryption algorithms and changes them automatically together with key versions.
1.0
Experimental
Key is generated from the version, user manually controls key rotation. Only supports key identifier 1, uses only AES_CBC.
1.0
Experimental
Encryption is generally needed on the very low level inside the storage engine. That is, the storage engine needs to support encryption and have access to the encryption and key management functionality. The usual way for a plugin to access some functionality in the server is via a service. In this case the server provides the Encryption Service for storage engines (and other interested plugins) to use. These service functions are directly hooked into encryption plugin methods (described above).
Service functions are declared as follows:
There are also convenience helpers to check for a key or key version existence and to encrypt or decrypt a block of data with one function call.
This page is licensed: CC BY-SA / Gnu FDL
Progress in % * 1000
Status_length
1-2
Packet length of string in net_field_length() format
Status
Status_length
Status / Stage name
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 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
/* Returned from get_latest_key_version() */
#define ENCRYPTION_KEY_VERSION_INVALID (~(unsigned int)0)
#define ENCRYPTION_KEY_NOT_ENCRYPTED (0)
#define ENCRYPTION_KEY_SYSTEM_DATA 1
#define ENCRYPTION_KEY_TEMPORARY_DATA 2
/* Returned from get_key() */
#define ENCRYPTION_KEY_BUFFER_TOO_SMALL (100)
#define ENCRYPTION_FLAG_DECRYPT 0
#define ENCRYPTION_FLAG_ENCRYPT 1
#define ENCRYPTION_FLAG_NOPAD 2
struct st_mariadb_encryption {
int interface_version; /**< version plugin uses */
/********************* KEY MANAGEMENT ***********************************/
/**
Function returning latest key version for a given key id.
@return A version or ENCRYPTION_KEY_VERSION_INVALID to indicate an error.
*/
unsigned int (*get_latest_key_version)(unsigned int key_id);
/**
Function returning a key for a key version
@param key_id The requested key id
@param version The requested key version
@param key The key will be stored there. Can be NULL -
in which case no key will be returned
@param key_length in: key buffer size
out: the actual length of the key
This method can be used to query the key length - the required
buffer size - by passing key==NULL.
If the buffer size is less than the key length the content of the
key buffer is undefined (the plugin is free to partially fill it with
the key data or leave it untouched).
@return 0 on success, or
ENCRYPTION_KEY_VERSION_INVALID, ENCRYPTION_KEY_BUFFER_TOO_SMALL
or any other non-zero number for errors
*/
unsigned int (*get_key)(unsigned int key_id, unsigned int version,
unsigned char *key, unsigned int *key_length);
/********************* ENCRYPTION **************************************/
/*
The caller uses encryption as follows:
1. Create the encryption context object of the crypt_ctx_size() bytes.
2. Initialize it with crypt_ctx_init().
3. Repeat crypt_ctx_update() until there are no more data to encrypt.
4. Write the remaining output bytes and destroy the context object
with crypt_ctx_finish().
*/
/**
Returns the size of the encryption context object in bytes
*/
unsigned int (*crypt_ctx_size)(unsigned int key_id, unsigned int key_version);
/**
Initializes the encryption context object.
*/
int (*crypt_ctx_init)(void *ctx, const unsigned char *key, unsigned int klen,
const unsigned char *iv, unsigned int ivlen, int flags,
unsigned int key_id, unsigned int key_version);
/**
Processes (encrypts or decrypts) a chunk of data
Writes the output to th dst buffer. note that it might write
more bytes that were in the input. or less. or none at all.
*/
int (*crypt_ctx_update)(void *ctx, const unsigned char *src,
unsigned int slen, unsigned char *dst,
unsigned int *dlen);
/**
Writes the remaining output bytes and destroys the encryption context
crypt_ctx_update might've cached part of the output in the context,
this method will flush these data out.
*/
int (*crypt_ctx_finish)(void *ctx, unsigned char *dst, unsigned int *dlen);
/**
Returns the length of the encrypted data
It returns the exact length, given only the source length.
Which means, this API only supports encryption algorithms where
the length of the encrypted data only depends on the length of the
input (a.k.a. compression is not supported).
*/
unsigned int (*encrypted_length)(unsigned int slen, unsigned int key_id,
unsigned int key_version);
};unsigned int encryption_key_get_latest_version(unsigned int key_id);
unsigned int encryption_key_get(unsigned int key_id, unsigned int key_version,
unsigned char *buffer, unsigned int *length);
unsigned int encryption_ctx_size(unsigned int key_id, unsigned int key_version);
int encryption_ctx_init(void *ctx, const unsigned char *key, unsigned int klen,
const unsigned char *iv, unsigned int ivlen, int flags,
unsigned int key_id, unsigned int key_version);
int encryption_ctx_update(void *ctx, const unsigned char *src,
unsigned int slen, unsigned char *dst,
unsigned int *dlen);
int encryption_ctx_finish(void *ctx, unsigned char *dst, unsigned int *dlen);
unsigned int encryption_encrypted_length(unsigned int slen, unsigned int key_id,
unsigned int key_version);unsigned int encryption_key_id_exists(unsigned int id);
unsigned int encryption_key_version_exists(unsigned int id,
unsigned int version);
int encryption_crypt(const unsigned char *src, unsigned int slen,
unsigned char *dst, unsigned int *dlen,
const unsigned char *key, unsigned int klen,
const unsigned char *iv, unsigned int ivlen, int flags,
unsigned int key_id, unsigned int key_version);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).
mariadb-test supports the following options:
--help, -?
Display a help message and exit.
--basedir=dir, -b dir
The base directory for tests.
--character-sets-dir=path
The directory where 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
This page is licensed: GPLv2
shell> mariadb-test **[options] [db_name]** < //test_file//--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 TLS for secure connection (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 mariadb-test-run.pl 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.
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().
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
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)In order to execute a query, the function sql_parse.cc:mysql_parse() is
called, which in turn calls the parser (yyparse()) with an updated Lex
structure as the result. mysql_parse() then calls mysql_execute_command()
which dispatches on the command code (in Lex) to the corresponding code for
executing that particular query.
There are three structures involved in the execution of a query which are of interest to the implementation:
Lex (mentioned above) is the "compiled" query, that is the output from the parser and what is then interpreted to do the actual work. It constains an enum value (sql_command) which is the query type, and all the data collected by the parser needed for the execution (table names, fields, values, etc).
THD is the "run-time" state of a connection, containing all that is needed for a particular client connection, and, among other things, the Lex structure currently being executed.
Item_*: During parsing, all data is translated into "items", objects of
the subclasses of "Item", such as Item_int, Item_real,
(More detailed APIs at the end of this page)
class sp_head (sp_head.{cc,h})
This contains, among other things, an array of "instructions" and the method for executing the procedure.
class sp_pcontext (sp_pcontext.{cc,h}
This is the parse context for the procedure. It's primarily used during parsing to keep track of local parameters, variables and labels, but it's also used at time to find the parameters mode (IN, OUT or INOUT) and type when setting up the runtime context.
class sp_instr (sp_head.{cc,h})
This is the base class for "instructions", that is, what is generated by the parser. It turns out that we only need a minimum of 5 different sub classes:
sp_instr_stmt Execute a statement. This is the "call-out" any normal SQL statement, like a , etc. It contains the Lex structure for the statement in question.
sp_instr_set Set the value of a local variable (or parameter)
sp_instr_jump An unconditional jump.
sp_instr_jump_if_not Jump if condition is not true. It turns out that the negative test is most convenient when generating the code for the flow control constructs.
class sp_rcontext (sp_rcontext.h)
This is the runtime context in the THD structure. It contains an array of items, the parameters and local variables for the currently executing stored procedure. This means that variable value lookup is in runtime is constant time, a simple index operation.
class Item_splocal (Item.{cc,h})
This is a subclass of Item. Its sole purpose is to hide the fact that the real Item is actually in the current frame (runtime context). It contains the frame offset and defers all methods to the real Item in the frame. This is what the parser generates for local variables.
Utility Functions (sp.{cc,h})
This contains functions for creating, dropping and finding a stored procedure in the (or the internal cache).
When parsing a the parser first initializes thesphead and spcont (runtime context) fields in the Lex.
The sql_command code for the result of parsing a isSQLCOM_CREATE_PROCEDURE.
The parsing of the parameter list and body is relatively straightforward:
Parameters:
name, type and mode (IN/OUT/INOUT) is pushed to spcont
Declared local variables: Same as parameters (mode is then IN)
Local Variable references:
If an identifier is found in spcont, an Item_splocal is created
with the variable's frame index, otherwise an Item_field or Item_ref
A Simple Example
Parsing the procedure:
would generate the following structures:
Note that the contents of the spcont is changing during the parsing,
at all times reflecting the state of the would-be runtime frame.
The m_instr is an array of instructions:
Here, '3', 'x>0', etc, represent the Items or Lex for the respective expressions or statements.
is essentially the same thing as for a PROCEDURE, with the addition that a FUNCTION has a return type and a RETURN statement, but no OUT or INOUT parameters.
The main difference during parsing is that we store the result type in the sp_head. However, there are big differences when it comes to invoking a FUNCTION. (See below.)
As seen above, the entired definition string, including the "CREATE PROCEDURE" (or "FUNCTION") is kept. The procedure definition string is stored in the table mysql.proc with the name and type as the key, the type being one of the enum ("procedure","function").
A PROCEDURE is just stored in the . A FUNCTION has an additional requirement. They will be called in expressions with the same syntax as UDFs, so UDFs and stored FUNCTIONs share the namespace. Thus, we must make sure that we do not have UDFs and FUNCTIONs with the same name (even if they are stored in different places).
This means that we can reparse the procedure as many time as we want. The first time, the resulting Lex is used to store the procedure in the database (using the function sp.c:sp_create_procedure()).
The simplest way would be to just leave it at that, and re-read the procedure from the database each time it is called. (And in fact, that's the way the earliest implementation will work.) However, this is not very efficient, and we can do better. The full implementation should work like this:
Upon creation time, parse and store the procedure. Note that we still need to parse it to catch syntax errors, but we can't check if called procedures exists for instance.
Upon first CALL, read from the database, parse it, and cache the resulting Lex in memory. This time we can do more error checking.
Upon subsequent CALLs, use the cached Lex.
Note that this implies that the Lex structure with its sphead must be reentrant, that is, reusable and shareable between different threads and calls. The runtime state for a procedure is kept in the sp_rcontext in THD.
The mechanisms of storing, finding, and dropping procedures are encapsulated in the files sp.{cc,h}.
A is parsed just like any statement. The resulting Lex has the sql_command SQLCOM_CALL, the procedure's name and the parameters are pushed to the Lex' value_list.
sql_parse.cc:mysql_execute_command() then uses sp.cc:sp_find() to get the sp_head for the procedure (which may have been read from the database or fetched from the in-memory cache) and calls the sp_head's method execute(). Note: It's important that substatements called by the procedure do not do send_ok(). Fortunately, there is a flag in THD->net to disable this during CALLs. If a substatement fails, it will however send an error back to the client, so the CALL mechanism must return immediately and without sending an error.
The sp_head::execute() method works as follows:
Keep a pointer to the old runtime context in THD (if any)
Create a new runtime context. The information about the required size is in sp_head's parse time context.
Push each parameter (from the CALL's Lex->value_list) to the new context. If it's an OUT or INOUT parameter, the parameter's offset in the caller's frame is set in the new context as well.
For each instruction, call its execute() method. The result is a pointer to the next instruction to execute (or NULL) if an error occurred.
Before executing the instruction we also keeps the current default database (if any). If this was changed during execution (i.e. a statement has been executed), we restore the current database to the original.
This is the most useful way to handle USE in procedures. If we didn't, the caller would find himself in a different database after calling a function, which can be confusing. Restoring the database also gives full freedom to the procedure writer:
It's possible to write "general" procedures that are independent of the actual database name.
It's possible to write procedures that work on a particular database by calling USE, without having to use fully qualified table names everywhere (which doesn't help if you want to call other, "general", procedures anyway).
There are three occasions where we need to evaluate an expression:
When SETing a variable
When CALLing a procedure
When testing an expression for a branch (in IF, WHILE, etc)
The semantics in stored procedures is "call-by-value", so we have to
evaluate any "func" Items at the point of the CALL or SET, otherwise
we would get a kind of "lazy" evaluation with unexpected results with
respect to OUT parameters for instance.
For this the support function, sp_head.cc:eval_func_item() is needed.
Functions don't have an explicit call keyword like procedures. Instead,
they appear in expressions with the conventional syntax "fun(arg, ...)".
The problem is that we already have (UDFs) which
are called the same way. A UDF is detected by the lexical analyzer (not
the parser!), in the find_keyword() function, and returns a UDF_*_FUNC
or UDA_*_SUM token with the udf_func object as the yylval.
So, stored functions must be handled in a similar way, and as a consequence, UDFs and functions must not have the same name.
The existence of UDFs are checked during the lexical analysis (in sql_lex.cc:find_keyword()). This has the drawback that they must exist before they are referred to, which was ok before SPs existed, but then it becomes a problem. The first implementation of SP FUNCTIONs will work the same way, but this should be fixed a.s.a.p. (This will required some reworking of the way UDFs are handled, which is why it's not done from the start.) For the time being, a FUNCTION is detected the same way, and returns the token SP_FUNC. During the parsing we only check for the existence of the function, we don't parse it, since wa can't call the parser recursively.
When encountering a SP_FUNC with parameters in the expression parser, an instance of the new Item_func_sp class is created. Unlike UDFs, we don't have different classes for different return types, since we at this point don't know the type.
A FUNCTION differs from a PROCEDURE in one important aspect: Whereas a PROCEDURE is CALLed as statement by itself, a FUNCTION is invoked "on-the-fly" during the execution of another statement. This makes things a lot more complicated compared to CALL:
We can't read and parse the FUNCTION from the at the point of invocation; the server requires that all tables used are opened and locked at the beginning of the query execution. One "obvious" solution would be to simply push "mysql.proc" to the list of tables used by the query, but this implies a "join" with this table if the query is a select, so it doesn't work (and we can't exclude this table easily; since a privileged used might in fact want to search the proc table). Another solution would of course be to allow the opening and closing of the during a query execution, but this it not possible at the present.
So, the solution is to collect the names of the referred FUNCTIONs during
parsing in the lex.
Then, before doing anything else in mysql_execute_command(), read all
functions from the database an keep them in the THD, where the functionsp_find_function() can find them during the execution.
Note: Even with an in-memory cache, we must still make sure that the
functions are indeed read and cached at this point.
The code that read and cache functions from the database must also be
invoked recursively for each read FUNCTION to make sure we have all the
functions we need.
The procedure name is pushed to Lex->value_list.
The sql_command code for the result of parsing a isSQLCOM_DROP_PROCEDURE/SQLCOM_DROP_FUNCTION.
Dropping is done by simply getting the procedure with the sp_find()
function and calling sp_drop() (both in sp.{cc,h}).
/ also supports the non-standard "IF EXISTS", analogous to other statements in MariaDB.
Condition names are lexical entities and are kept in the parser context just like variables. But, condition are just "aliases" for SQLSTATE strings, or mysqld error codes (which is a non-standard extension in MySQL), and are only used during parsing.
Handlers comes in three types, CONTINUE, EXIT and UNDO. The latter is like an EXIT handler with an implicit rollback, and is currently not implemented. The EXIT handler jumps to the end of its BEGIN-END block when finished. The CONTINUE handler returns to the statement following that which invoked the handler.
The handlers in effect at any point is part of each thread's runtime state, so we need to push and pop handlers in the sp_rcontext during execution. We use special instructions for this:
sp_instr_hpush_jump Push a handler. The instruction contains the necessary information, like which conditions we handle and the location of the handler. The jump takes us to the location after the handler code.
sp_instr_hpop Pop the handlers of the current frame (which we are just leaving).
It might seems strange to jump past the handlers like that, but there's no extra cost in doing this, and for technical reasons it's easiest for the parser to generate the handler instructions when they occur in the source.
When an error occurs, one of the error routines is called and an error message is normally sent back to the client immediately. Catching a condition must be done in these error routines (there are quite a few) to prevent them from doing this. We do this by calling a method in the THD's sp_rcontext (if there is one). If a handler is found, this is recorded in the context and the routine returns without sending the error message. The execution loop (sp_head::execute()) checks for this after each statement and invokes the handler that has been found. If several errors or warnings occurs during one statement, only the first is caught, the rest are ignored.
Invoking and returning from a handler is trivial in the EXIT case. We simply jump to it, and it will have an sp_instr_jump as its last instruction.
Calling and returning from a CONTINUE handler poses some special problems. Since we need to return to the point after its invocation, we push the return location on a stack in the sp_rcontext (this is done by the execution loop). The handler then ends with a special instruction, sp_instr_hreturn, which returns to this location.
CONTINUE handlers have one additional problem: They are parsed at the lexical level where they occur, so variable offsets will assume that it's actually called at that level. However, a handler might be invoked from a sub-block where additional local variables have been declared, which will then share the location of any local variables in the handler itself. So, when calling a CONTINUE handler, we need to save any local variables above the handler's frame offset, and restore them upon return. (This is not a problem for EXIT handlers, since they will leave the block anyway.) This is taken care of by the execution loop and the sp_instr_hreturn instruction.
Examples
EXIT handler:
CONTINUE handler:
For stored procedures to be really useful, you want to have cursors. MySQL doesn't yet have "real" cursor support (with API and ODBC support, allowing updating, arbitrary scrolling, etc), but a simple asensitive, non-scrolling, read-only cursor can be implemented in SPs using the class Protocol_cursor. This class intecepts the creation and sending of results sets and instead stores it in-memory, as MYSQL_FIELDS and MYSQL_ROWS (as in the client API).
To support this, we need the usual name binding support in sp_pcontext (similar to variables and conditions) to keep track on declared cursor names, and a corresponding run-time mechanism in sp_rcontext. Cursors are lexically scoped like everything with a body or BEGIN/END block, so they are pushed and poped as usual (see conditions and variables above). The basic operations on a cursor are OPEN, FETCH and CLOSE, which will each have a corresponding instruction. In addition, we need instructions to push a new cursor (this will encapsulate the LEX of the SELECT statement of the cursor), and a pop instruction:
sp_instr_cpush Push a cursor to the sp_rcontext. This instruction contains the LEX for the select statement
sp_instr_cpop Pop a number of cursors from the sp_rcontext.
sp_instr_copen Open a cursor: This will execute the select and get the result set in a sepeate memroot.
sp_instr_cfetch Fetch the next row from the in-memory result set. The instruction contains a list of the variables (frame offsets) to set.
A cursor is a separate class, sp_cursor (defined in sp_rcontex.h) which encapsulates the basic operations used by the above instructions. This class contains the LEX, Protocol_cursor object, and its memroot, as well as the cursor's current state. Compiling and executing is fairly straight-forward. sp_instr_copen is a subclass of sp_instr_stmt and uses its mechanism to execute a substatement.
Example
There are two ways to cache SPs:
one global cache, share by all threads/connections,
one cache per thread.
There are pros and cons with both methods:
Pros: Save memory, each SP only read from table once,
Cons: Needs locking (= serialization at access), requires thread-safe data structures,
Pros: Fast, no locking required (almost), limited thread-safe requirement,
Cons: Uses more memory, each SP read from table once per thread.
Unfortunately, we cannot use alternative 1 for the time being, as most of the data structures to be cached (lex and items) are not reentrant and thread-safe. (Things are modified at execution, we have THD pointers stored everywhere, etc.) This leaves us with alternative 2, one cache per thread; or actually two, since we keep FUNCTIONs and PROCEDUREs in separate caches. This is not that terrible; the only case when it will perform significantly worse than a global cache is when we have an application where new threads are connecting, calling a procedure, and disconnecting, over and over again.
The cache implementation itself is simple and straightforward, a hashtable wrapped in a class and a C API (see APIs below).
There is however one issue with multiple caches: dropping and altering procedures. Normally, this should be a very rare event in a running system; it's typically something you do during development and testing, so it's not unthinkable that we would simply ignore the issue and let any threads running with a cached version of an SP keep doing so until its disconnected. But assuming we want to keep the caches consistent with respect to drop and alter, it can be done:
A global counter is needed, initialized to 0 at start.
At each DROP or ALTER, increase the counter by one.
Each cache has its own copy of the counter, copied at the last read.
When looking up a name in the cache, first check if the global counter is larger than the local copy. If so, clear the cache and return "not found", and update the local counter; otherwise, lookup as usual.
This minimizes the cost to a single brief lock for the access of an integer when operating normally. Only in the event of an actual drop or alter, is the cache cleared. This may seem to be drastic, but since we assume that this is a rare event, it's not a problem. It would of course be possible to have a much more fine-grained solution, keeping track of each SP, but the overhead of doing so is not worth the effort.
This is an outline of the key types. Some types and other details in the actual files have been omitted for readability.
The parser context: sp_pcontext.h
Run-time context (call frame): sp_rcontext.h:
The procedure: sp_head.h:
Instructions
The base class
SET instruction
Unconditional jump
Conditional jump
Return a function value
Push a handler and jump
Pops handlers
Return from a CONTINUE handler
Push a CURSOR
Pop CURSORs
Open a CURSOR
Close a CURSOR
Fetch a row with CURSOR
Utility functions: sp.h
The cache: sp_cache.h
This is the used in :
This page is licensed: CC BY-SA / Gnu FDL
Item_stringItem_funcsp_instr_freturn Return a value from a FUNCTION and exit. For condition HANDLERs some special instructions are also needed, see that section below.
Statements:
The Lex in THD is replaced by a new Lex structure and the statement,
is parsed as usual. A sp_instr_stmt is created, containing the new
Lex, and added to the instructions in sphead.
Afterwards, the procedure's Lex is restored in THD.
SET var:
Setting a local variable generates a sp_instr_set instruction,
containing the variable's frame offset, the expression (an Item),
and the type.
Forward jumps: When jumping forward, the exact destination is not known at the time of the creation of the jump instruction. The
spheadtherefore contains a list of instruction-label pairs for each forward reference. When the position later is known, the instructions in the list are updated with the correct location.
Loop constructs have optional labels. If a loop doesn't have a label, an anonymous label is generated to simplify the parsing.
There are two types of CASE. The "simple" case is implemented with an anonymous variable bound to the value to be tested.
On success, set the new values of the OUT and INOUT parameters in the caller's frame.
sp_instr_cclose Free the result set.
CREATE PROCEDURE a(s CHAR(16))
BEGIN
DECLARE x INT;
SET x = 3;
WHILE x > 0 DO
SET x = x-1;
INSERT INTO db.tab VALUES (x, s);
END WHILE;
END______
thd: | | _________
| lex -+--->| | ___________________
|______| | spcont -+------------------->| "s",in,char(16):0 |
| sphead -+------ |("x",in,int :1)|
|_________| | |___________________|
____V__________________
| m_name: "a" |
| m_defstr: "create ..."|
| m_instr: ... |
|_______________________|Pos. Instruction
0 sp_instr_set(1, '3')
1 sp_instr_jump_if_not(5, 'x>0')
2 sp_instr_set(1, 'x-1')
3 sp_instr_stmt('insert into ...')
4 sp_instr_jump(1)
5 <end>begin
declare x int default 0;
begin
declare exit handler for 'XXXXX' set x = 1;
(statement1);
(statement2);
end;
(statement3);
endPos. Instruction
0 sp_instr_set(0, '0')
1 sp_instr_hpush_jump(4, 1) # location and frame size
2 sp_instr_set(0, '1')
3 sp_instr_jump(6)
4 sp_instr_stmt('statement1')
5 sp_instr_stmt('statement2')
6 sp_instr_hpop(1)
7 sp_instr_stmt('statement3')CREATE PROCEDURE hndlr1(val INT)
BEGIN
DECLARE x INT DEFAULT 0;
DECLARE foo CONDITION FOR 1146;
DECLARE CONTINUE HANDLER FOR foo SET x = 1;
INSERT INTO t3 VALUES ("hndlr1", val); # Non-existing table?
IF x>0 THEN
INSERT INTO t1 VALUES ("hndlr1", val); # This instead then
END IF;
END|Pos. Instruction
0 sp_instr_set(1, '0')
1 sp_instr_hpush_jump(4, 2)
2 sp_instr_set(1, '1')
3 sp_instr_hreturn(2) # frame size
4 sp_instr_stmt('insert ... t3 ...')
5 sp_instr_jump_if_not(7, 'x>0')
6 sp_instr_stmt('insert ... t1 ...')
7 sp_instr_hpop(2)begin
declare x int;
declare c cursor for select a from t1;
open c;
fetch c into x;
close c;
endPos. Instruction
0 sp_instr_cpush('select a from ...')
1 sp_instr_copen(0) # The 0'th cursor
2 sp_instr_cfetch(0) # Contains the variable list
3 sp_instr_cclose(0)
4 sp_instr_cpop(1)typedef enum
{
sp_param_in,
sp_param_out,
sp_param_inout
} sp_param_mode_t;
typedef struct
{
LEX_STRING name;
enum enum_field_types type;
sp_param_mode_t mode;
uint offset; // Offset in current frame
my_bool isset;
} sp_pvar_t;
typedef struct sp_cond_type
{
enum { number, state, warning, notfound, exception } type;
char sqlstate[6];
uint mysqlerr;
} sp_cond_type_t;
class sp_pcontext
{
sp_pcontext();
// Return the maximum frame size
uint max_framesize();
// Return the current frame size
uint current_framesize();
// Return the number of parameters
uint params();
// Set the number of parameters to the current frame size
void set_params();
// Set type of the variable at offset 'i' in the frame
void set_type(uint i, enum enum_field_types type);
// Mark the i:th variable to "set" (i.e. having a value) with
// 'val' true.
void set_isset(uint i, my_bool val);
// Push the variable 'name' to the frame.
void push_var(LEX_STRING *name,
enum enum_field_types type, sp_param_mode_t mode);
// Pop 'num' variables from the frame.
void pop_var(uint num = 1);
// Find variable by name
sp_pvar_t *find_pvar(LEX_STRING *name);
// Find variable by index
sp_pvar_t *find_pvar(uint i);
// Push label 'name' of instruction index 'ip' to the label context
sp_label_t *push_label(char *name, uint ip);
// Find label 'name' in the context
sp_label_t *find_label(char *name);
// Return the last pushed label
sp_label_t *last_label();
// Return and remove the last pushed label.
sp_label_t *pop_label();
// Push a condition to the context
void push_cond(LEX_STRING *name, sp_cond_type_t *val);
// Pop a 'num' condition from the context
void pop_cond(uint num);
// Find a condition in the context
sp_cond_type_t *find_cond(LEX_STRING *name);
// Increase the handler count
void add_handler();
// Returns the handler count
uint handlers();
// Push a cursor
void push_cursor(LEX_STRING *name);
// Find a cursor
my_bool find_cursor(LEX_STRING *name, uint *poff);
// Pop 'num' cursors
void pop_cursor(uint num);
// Return the number of cursors
uint cursors();
}#define SP_HANDLER_NONE 0
#define SP_HANDLER_EXIT 1
#define SP_HANDLER_CONTINUE 2
#define SP_HANDLER_UNDO 3
typedef struct
{
struct sp_cond_type *cond;
uint handler; // Location of handler
int type;
uint foffset; // Frame offset for the handlers declare level
} sp_handler_t;
class sp_rcontext
{
// 'fsize' is the max size of the context, 'hmax' the number of handlers,
// 'cmax' the number of cursors
sp_rcontext(uint fsize, uint hmax, , uint cmax);
// Push value (parameter) 'i' to the frame
void push_item(Item *i);
// Set slot 'idx' to value 'i'
void set_item(uint idx, Item *i);
// Return the item in slot 'idx'
Item *get_item(uint idx);
// Set the "out" index 'oidx' for slot 'idx. If it's an IN slot,
// use 'oidx' -1.
void set_oindex(uint idx, int oidx);
// Return the "out" index for slot 'idx'
int get_oindex(uint idx);
// Set the FUNCTION result
void set_result(Item *i);
// Get the FUNCTION result
Item *get_result();
// Push handler at location 'h' for condition 'cond'. 'f' is the
// current variable frame size.
void push_handler(sp_cond_type_t *cond, uint h, int type, uint f);
// Pop 'count' handlers
void pop_handlers(uint count);
// Find a handler for this error. This sets the state for a found
// handler in the context. If called repeatedly without clearing,
// only the first call's state is kept.
int find_handler(uint sql_errno);
// Returns 1 if a handler has been found, with '*ip' and '*fp' set
// to the handler location and frame size respectively.
int found_handler(uint *ip, uint *fp);
// Clear the found handler state.
void clear_handler();
// Push a return address for a CONTINUE handler
void push_hstack(uint ip);
// Pop the CONTINUE handler return stack
uint pop_hstack();
// Save variables from frame index 'fp' and up.
void save_variables(uint fp);
// Restore saved variables from to frame index 'fp' and up.
void restore_variables(uint fp);
// Push a cursor for the statement (lex)
void push_cursor(LEX *lex);
// Pop 'count' cursors
void pop_cursors(uint count);
// Pop all cursors
void pop_all_cursors();
// Get the 'i'th cursor
sp_cursor *get_cursor(uint i);
}#define TYPE_ENUM_FUNCTION 1
#define TYPE_ENUM_PROCEDURE 2
class sp_head
{
int m_type; // TYPE_ENUM_FUNCTION or TYPE_ENUM_PROCEDURE
sp_head();
void init(LEX_STRING *name, LEX *lex, LEX_STRING *comment, char suid);
// Store this procedure in the database. This is a wrapper around
// the function sp_create_procedure().
int create(THD *);
// Invoke a FUNCTION
int
execute_function(THD *thd, Item **args, uint argcount, Item **resp);
// CALL a PROCEDURE
int
execute_procedure(THD *thd, List<Item> *args);
// Add the instruction to this procedure.
void add_instr(sp_instr *);
// Returns the number of instructions.
uint instructions();
// Returns the last instruction
sp_instr *last_instruction();
// Resets lex in 'thd' and keeps a copy of the old one.
void reset_lex(THD *);
// Restores lex in 'thd' from our copy, but keeps some status from the
// one in 'thd', like ptr, tables, fields, etc.
void restore_lex(THD *);
// Put the instruction on the backpatch list, associated with
// the label.
void push_backpatch(sp_instr *, struct sp_label *);
// Update all instruction with this label in the backpatch list to
// the current position.
void backpatch(struct sp_label *);
// Returns the SP name (with optional length in '*lenp').
char *name(uint *lenp = 0);
// Returns the result type for a function
Item_result result();
// Sets various attributes
void sp_set_info(char *creator, uint creatorlen,
longlong created, longlong modified,
bool suid, char *comment, uint commentlen);
}class sp_instr
{
// 'ip' is the index of this instruction
sp_instr(uint ip);
// Execute this instrution.
// '*nextp' will be set to the index of the next instruction
// to execute. (For most instruction this will be the
// instruction following this one.)
// Returns 0 on success, non-zero if some error occurred.
virtual int execute(THD *, uint *nextp)
}
<<code>>
===== Statement instruction
<<code>>
class sp_instr_stmt : public sp_instr
{
sp_instr_stmt(uint ip);
int execute(THD *, uint *nextp);
// Set the statement's Lex
void set_lex(LEX *);
// Return the statement's Lex
LEX *get_lex();
}class sp_instr_set : public sp_instr
{
// 'offset' is the variable's frame offset, 'val' the value,
// and 'type' the variable type.
sp_instr_set(uint ip,
uint offset, Item *val, enum enum_field_types type);
int execute(THD *, uint *nextp);
}class sp_instr_jump : public sp_instr
{
// No destination, must be set.
sp_instr_jump(uint ip);
// 'dest' is the destination instruction index.
sp_instr_jump(uint ip, uint dest);
int execute(THD *, uint *nextp);
// Set the destination instruction 'dest'.
void set_destination(uint dest);
}class sp_instr_jump_if_not : public sp_instr_jump
{
// Jump if 'i' evaluates to false. Destination not set yet.
sp_instr_jump_if_not(uint ip, Item *i);
// Jump to 'dest' if 'i' evaluates to false.
sp_instr_jump_if_not(uint ip, Item *i, uint dest)
int execute(THD *, uint *nextp);
}class sp_instr_freturn : public sp_instr
{
// Return the value 'val'
sp_instr_freturn(uint ip, Item *val, enum enum_field_types type);
int execute(THD *thd, uint *nextp);
}class sp_instr_hpush_jump : public sp_instr_jump
{
// Push handler of type 'htype', with current frame size 'fp'
sp_instr_hpush_jump(uint ip, int htype, uint fp);
int execute(THD *thd, uint *nextp);
// Add condition for this handler
void add_condition(struct sp_cond_type *cond);
}class sp_instr_hpop : public sp_instr
{
// Pop 'count' handlers
sp_instr_hpop(uint ip, uint count);
int execute(THD *thd, uint *nextp);
}class sp_instr_hreturn : public sp_instr
{
// Return from handler, and restore variables to 'fp'.
sp_instr_hreturn(uint ip, uint fp);
int execute(THD *thd, uint *nextp);
}class sp_instr_cpush : public sp_instr_stmt
{
// Push a cursor for statement 'lex'
sp_instr_cpush(uint ip, LEX *lex)
int execute(THD *thd, uint *nextp);
}class sp_instr_cpop : public sp_instr_stmt
{
// Pop 'count' cursors
sp_instr_cpop(uint ip, uint count)
int execute(THD *thd, uint *nextp);
}class sp_instr_copen : public sp_instr_stmt
{
// Open the 'c'th cursor
sp_instr_copen(uint ip, uint c);
int execute(THD *thd, uint *nextp);
}class sp_instr_cclose : public sp_instr
{
// Close the 'c'th cursor
sp_instr_cclose(uint ip, uint c);
int execute(THD *thd, uint *nextp);
}class sp_instr_cfetch : public sp_instr
{
// Fetch next with the 'c'th cursor
sp_instr_cfetch(uint ip, uint c);
int execute(THD *thd, uint *nextp);
// Add a target variable for the fetch
void add_to_varlist(struct sp_pvar *var);
}#define SP_OK 0
#define SP_KEY_NOT_FOUND -1
#define SP_OPEN_TABLE_FAILED -2
#define SP_WRITE_ROW_FAILED -3
#define SP_DELETE_ROW_FAILED -4
#define SP_GET_FIELD_FAILED -5
#define SP_PARSE_ERROR -6
// Finds a stored procedure given its name. Returns NULL if not found.
sp_head *sp_find_procedure(THD *, LEX_STRING *name);
// Store the procedure 'name' in the database. 'def' is the complete
// definition string ("create procedure ...").
int sp_create_procedure(THD *,
char *name, uint namelen,
char *def, uint deflen,
char *comment, uint commentlen, bool suid);
// Drop the procedure 'name' from the database.
int sp_drop_procedure(THD *, char *name, uint namelen);
// Finds a stored function given its name. Returns NULL if not found.
sp_head *sp_find_function(THD *, LEX_STRING *name);
// Store the function 'name' in the database. 'def' is the complete
// definition string ("create function ...").
int sp_create_function(THD *,
char *name, uint namelen,
char *def, uint deflen,
char *comment, uint commentlen, bool suid);
// Drop the function 'name' from the database.
int sp_drop_function(THD *, char *name, uint namelen);/* Initialize the SP caching once at startup */
void sp_cache_init();
/* Clear the cache *cp and set *cp to NULL */
void sp_cache_clear(sp_cache **cp);
/* Insert an SP to cache. If **cp points to NULL, it's set to a
new cache */
void sp_cache_insert(sp_cache **cp, sp_head *sp);
/* Lookup an SP in cache */
sp_head *sp_cache_lookup(sp_cache **cp, char *name, uint namelen);
/* Remove an SP from cache */
void sp_cache_remove(sp_cache **cp, sp_head *sp);CREATE TABLE `proc` (
`db` char(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`name` char(64) NOT NULL DEFAULT '',
`type` enum('FUNCTION','PROCEDURE','PACKAGE','PACKAGE BODY') NOT NULL,
`specific_name` char(64) NOT NULL DEFAULT '',
`language` enum('SQL') NOT NULL DEFAULT 'SQL',
`sql_data_access` enum('CONTAINS_SQL','NO_SQL','READS_SQL_DATA','MODIFIES_SQL_DATA') NOT NULL DEFAULT 'CONTAINS_SQL',
`is_deterministic` enum('YES','NO') NOT NULL DEFAULT 'NO',
`security_type` enum('INVOKER','DEFINER') NOT NULL DEFAULT 'DEFINER',
`param_list` blob NOT NULL,
`returns` longblob NOT NULL,
`body` longblob NOT NULL,
`definer` char(141) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`created` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`sql_mode` set('REAL_AS_FLOAT','PIPES_AS_CONCAT','ANSI_QUOTES','IGNORE_SPACE','IGNORE_BAD_TABLE_OPTIONS','ONLY_FULL_GROUP_BY','NO_UNSIGNED_SUBTRACTION','NO_DIR_IN_CREATE','POSTGRESQL','ORACLE','MSSQL','DB2','MAXDB','NO_KEY_OPTIONS','NO_TABLE_OPTIONS','NO_FIELD_OPTIONS','MYSQL323','MYSQL40','ANSI','NO_AUTO_VALUE_ON_ZERO','NO_BACKSLASH_ESCAPES','STRICT_TRANS_TABLES','STRICT_ALL_TABLES','NO_ZERO_IN_DATE','NO_ZERO_DATE','INVALID_DATES','ERROR_FOR_DIVISION_BY_ZERO','TRADITIONAL','NO_AUTO_CREATE_USER','HIGH_NOT_PRECEDENCE','NO_ENGINE_SUBSTITUTION','PAD_CHAR_TO_FULL_LENGTH','EMPTY_STRING_IS_NULL','SIMULTANEOUS_ASSIGNMENT') NOT NULL DEFAULT '',
`comment` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`character_set_client` char(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`collation_connection` char(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`db_collation` char(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`body_utf8` longblob DEFAULT NULL,
`aggregate` enum('NONE','GROUP') NOT NULL DEFAULT 'NONE',
PRIMARY KEY (`db`,`name`,`type`)
) ENGINE=Aria DEFAULT CHARSET=utf8 PAGE_CHECKSUM=1 TRANSACTIONAL=1 COMMENT='Stored Procedures'