Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Plugin API Implementation

Plugin API reference: Plugin API Implementation . Generated from the MariaDB server headers by doxygen and moxygen.

This is how the individual abstract concepts are implemented and interact with each other.

The Server Plugin API implementation

The server is the bedrock of the plugin infrastructure and contains the base implementation to enable plugin usage. Plugins can theoretically be loaded into other binaries, but chances are that they won't operate all that well (if at all) in this case since they heavily depend on server's infrastructure.

Here's what the server implements to support plugins:

The Global Plugin List

The server maintains a global in-memory list of all available plugins (plugin_array), mirrored into a set of hash tables (plugin_hash) and guarded by LOCK_plugin.

The plugin libraries containing the available plugins can be:

  • statically linked into the server binary

  • dynamically loaded at startup via the –plugin-load... command line arguments

  • dynamically loaded just prior to the server's commencement of normal operations from the mysql.plugin table.

  • dynamically loaded at runtime via the INSTALL PLUGIN SQL command.

  • unloaded at runtime via the UNINSTALL PLUGIN SQL command

  • unloaded at server shutdown

The server implements all of the above possibilities in its code.

It also implements and provides a list of interfaces the plugins can use to call back and interact with the server. These are called Plugin Services Implementation.

The list and some basic attributes for each plugin in it are visible via the INFORMATION_SCHEMA.plugins table.

How Does The Server Call a Plugin API.

At certain opportune stages of server's normal operation the server might decide to "reach out" to a plugin by calling a plugin API. Since the plugins are named it can call any individual plugin by name. E.g. when executing CREATE TABLE engine=foo, the server will:

  • look "foo" into the cache of plugin "references" (plugin_ref) for that plugin type, if available.

  • If no cached reference is found:

    • Take LOCK_plugin

    • Look "foo" up into the global plugin_array for implementations of the specific plugin API (storage engine in this case)

    • If found, create a reference (plugin_ref) to the plugin.

  • call API "method" (function pointers) through the plugin_ref, if found.

  • when done, either store the plugin_ref into a cache for reuse or release it.

Plugin Services Implementation

Every dynamic plugin exposes one symbol for every service it knows about. These are implemented by the libservices static library that is linked into every plugin.

Initially, these global pointers are not valid pointers: when the plugin is compiled, they are set to the service version (e.g. VERSION_base64) that the plugin expects. The service header also contains a definition of a more concrete data structure (e.g. base64_service_st) that this global pointer should be pointing to.

The server maintains a global named list of plugin service API implementations. Every time a Plugin Library is initialized, the server gets its global service API implementation list, looks up all the symbols exported by the Plugin Library, and, for each symbol:

  • checks the versions by comparing the symbol's value to the server version.

  • sets Plugin Library 's symbol value to the actual server service pointer.

Each service header also contains "syntax sugar" macros, e.g.

that allow successful compilation of

as a function pointer call through the Plugin Library 's service pointer.

See also: Plugin Services for a list of the services provided by the server.

The Plugin library Implementation

A Plugin Library is executable code, either a part of the server itself or bundled as a shared library.

Each plugin library implements the plugin library API. It exposes that implementation via a set of dynamic library symbols.

Statically Linked Plugin Libraries.

Plugins linked statically to the server are collected into a global server "plugin library" and "installed" (and initialized) at server startup.

Dynamic Plugin Libraries

A Plugin Library is usually compiled as OS dynamic library: DLLs on MS Windows, .so on Unixes. What differentiates a Plugin Library from a regular OS shared library is that it exposes a specific public symbol maria_plugin_declarations that the server recognizes. In fact it also exposes maria_plugin_interface_version and maria_sizeof_struct_st_plugin, but these are auxiliary.

See maria_declare_plugin for details on how this is implemented.

The Lifecycle Of a Plugin Library

A plugin library always provides an array of st_maria_plugin structures, each describing a Plugin this Plugin Library provides. This contains everything that goes between maria_declare_plugin and maria_declare_plugin_end in plugin declaration.

When the Plugin Library is loaded into the server:

  • the library is version and maturity checked. See also: Plugin Verification

  • for each Plugin contained

    • plugin API version check is performed

    • the plugin system variables are added the variable global lists

    • the plugin is initialized

    • the status variables are added to the variable global lists

    • the plugin is added into the global plugin_array.

At unload time the effect of plugin library load above is reversed.

Plugin implementation

A MariaDB plugin is a logical, versioned implementation of exactly two interfaces:

Plugin System Variables Implementation

Broadly speaking the Plugin System Variables are a property of the st_maria_plugin interface that every plugin exposes. They are assigned to the st_maria_plugin::system_vars member. When the plugin is loaded the data in that NULL-terminated C++ array are used to register the system variables into the server. And also to unregister them at plugin unload time.

The plugin macros Plugin System Variable Macros, Plugin Session-Local System Variable Macros and MYSQL_SYSVAR facilitate defining an array of st_mysql_sys_var system variable definitions that is then passed as a value initializer to st_maria_plugin::system_vars member when defining the plugin.

But before we can examine more practical examples, there are some important decisions to make about each system variable: its type and settability cases.

Decide on Data Type and Settability.

Pick a Data Type

The data types available are listed in the Plugin System Variable Types group. Pick the one that matches your configuration option best.

Pick Settability

You need to decide if the system variable should just be a global variable or if it also should have a per-session value.

The global variable represents a single variable instance, containing a single value, shared across all worker threads inside the server. Think global scope C/C++ variables.

The per-session variable, in addition to what the global variable does, will also allow:

  • lock-free access to the session value.

  • Every session having a different value for this variable, that is defaulted to the current value of the global variable at session creation time.

See the corresponding server reference manual chapter on system variables for more details on this.

Check and Update Callbacks

The Check Callback

This callback (if non-NULL) is called by the server before the variable value is set. See mysql_var_check_func for the parameters of the callback. This is the plugin author's chance to check the new value and return an error if it cannot or should not be set.

This callback is expected to:

  • inspect the value passed to it

  • apply validity constraints (e.g. limits), if any to the value.

  • convert it to the correct native C type (see Plugin System Variable Types) for the destination value.

  • store it (in native C format) into the temp storage the save pointer points to

If no callback is supplied the server adds its own for each system variable type supported.

A very simple check function for the PLUGIN_VAR_DOUBLE type is shown below:

The Update Callback

This callback (if non-NULL) is called by the server to set the new value into the variable. See mysql_var_update_func for the parameters to the callback.

The plugin author should implement that if they want to have more control over how the variable is set. E.g. if they want to store the value into an atomic variable they will need the update callback.

Another reason to implement is to invoke any side effects on setting the variable. E.g. if a new value for a temp cache size is set, the code would need to actually resize the global cache list to the new size in addition to storing the new size value.

This callback is expected to:

  • fetch the value to be set stored by The Check Callback

  • store it into the variable pointed to by the var_ptr pointer argument

If no callback is supplied the server adds its own for each system variable type supported.

A very simple update function for the PLUGIN_VAR_DOUBLE type is shown below:

Define a Global Variable

To define a global only variable, do as follows:

  • Add the variable to a global per-plugin NULL terminated C++ array using MYSQL_SYSVAR, e.g.

Define a Session Variable

Unlike the global ones, session variables do not need a C++ placeholder variable. The global value is stored within a global server data structure. And the values for each session are stored into a per-session data structure, owned by the MYSQL_THD structure.

This also means that you will need to use the THDVAR macro to access both the global (pass NULL to the first MYSQL_THD argument) and the per-session values.

Example:

To define a global+session variable do as follows:

  • Add the variable to a global per-plugin NULL terminated C++ array using MYSQL_SYSVAR, e.g.

You can mix global and session variables into a single array.

Plugin Status Variables Implementation

Plugin Status Variables are defined similarly to Plugin System Variables by creating a per-plugin NULL terminated array of st_mysql_show_var structures and passing it down to maria_declare_plugin.

Unlike the system variables, the status variables are read-only, i.e. only the plugin code can manipulate the status variable's value.

Each status variable has a type: one of the enum_mysql_show_type types. In practice try to restrict your variables to use either SHOW_LONG or SHOW_LONGLONG. These are typically counters and these two types get reset by the server when you reset status. Plus, they are typically accessed atomically by modern CPUs so you can skip doing locking.

The scope for the status variables is handled by the server as follows: Plugin status variables are global: the server reads the variable the pointer refers to. If you need per-session values, use a SHOW_FUNC or SHOW_SIMPLE_FUNC variable and compute the value from the THD.

Status variables can be scalar or functional.

Scalar Status Variables

For "scalar" status variables the data is kept into a C/C++ variable and a pointer to that is passed to the infrastructure. When the server needs to read the value it just de-references the pointer and reads the value.

To implement a scalar status variable do as follows:

  • Define the global counter variable

  • Add it to a global per-plugin NULL-element terminated array:

  • Manipulate the global counter variable

Functional Status Variables

For functional status variables instead of dereferencing a pointer, the server takes a function pointer of the type mysql_show_var_func and is calling the function when it needs the value.

The function can also change the type for the variable.

A typical SHOW_SIMPLE_FUNC variable looks like this:

There's also SHOW_FUNC, which you can use through the SHOW_FUNC_ENTRY macro as follows:

Array Status Variables

Sometimes you want a group of status variables to have a custom prefix.

One approach is to copy that prefix to all the variables in the list, e.g.

This can be tedious, so MariaDB supports a trick to specify the prefix just once:

Here the SHOW_FUNC entry returns a SHOW_ARRAY, and the server prefixes the names of the array's variables with the entry's name.

This page is licensed: GPLv2

Last updated

Was this helpful?