MySQL 8.4.0
Source Code Documentation
How to add a new service

A "plugin service" is in its core a C struct containing one or more function pointers.

If you want to export C++ class you need to provide an extern "C" function that will create a new instance of your class, and put it in a service. But be careful to also provide a destructor method since the heaps of the server and the plugin may be different.

Data structures are not part of the service structure, but they are part of the API you create and usually need to be declared in the same service_*.h file.

To turn a pre-existing set of functions (foo_func1, foo_func2) into a service "foo" you need to:

  1. Create a new file include/mysql/service_foo.h

    The template is:

    /* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2.0,
    as published by the Free Software Foundation.
    This program is designed to work with certain software (including
    but not limited to OpenSSL) that is licensed under separate terms,
    as designated in a particular file or component or in included license
    documentation. The authors of MySQL hereby grant you an additional
    permission to link the program and your derivative works with the
    separately licensed software that they have either included with
    the program or referenced in the documentation.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License, version 2.0, for more details.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
    #ifndef MYSQL_SERVICE_FOO_INCLUDED
    /**
    @file
    TODO: Fill in a description of your file here.
    */
    #ifdef __cplusplus
    extern "C" {
    #endif
    /**
    @ingroup group_ext_plugin_services
    TODO: Fill in the architecture of your service.
    This is the primary documentation of your new service
    and will be auto-added to the service description document
    because of it being a part of the doxygen group
    group_ext_plugin_services.
    */
    extern struct foo_service_st {
    /**
    TODO: Interface description of foo_func1_type.
    Fix the prototype as appropriate.
    You can add a see-also to the implementation too.
    */
    int (*foo_func1_type)(...);
    /**
    TODO: Interface description of foo_func2_type.
    Fix the prototype as appropriate.
    You can add a see-also to the implementation too.
    */
    void (*foo_func2_type)(...);
    } * foo_service;
    #ifdef MYSQL_DYNAMIC_PLUGIN
    #define foo_func1(...) foo_service->foo_func1_type(...)
    #define foo_func2(...) foo_service->foo_func2_type(...)
    #else
    int foo_func1_type(...); /** TODO: fix the prototype as appropriate */
    void foo_func2_type(...); /** TODO: fix the prototype as appropriate */
    #endif
    #ifdef __cplusplus
    }
    #endif
    #define MYSQL_SERVICE_FOO_INCLUDED
    #endif

    The service_foo.h file should be self-contained, if it needs system headers - include them in it, e.g. if you use size_t

    #include <stdlib.h>

    It should also declare all the accompanying data structures, as necessary (e.g. thd_alloc_service declares MYSQL_LEX_STRING).

  2. Add the new file to include/mysql/services.h
  3. Increase the minor plugin ABI version in include/mysql/plugin.h: MYSQL_PLUGIN_INTERFACE_VERSION = MYSQL_PLUGIN_INTERFACE_VERSION + 1
  4. Add the version of your service to include/service_versions.h:
    #define VERSION_foo 0x0100
  5. Create a new file libservices/foo_service.c using the following template:
    /* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2.0,
    as published by the Free Software Foundation.
    This program is designed to work with certain software (including
    but not limited to OpenSSL) that is licensed under separate terms,
    as designated in a particular file or component or in included license
    documentation. The authors of MySQL hereby grant you an additional
    permission to link the program and your derivative works with the
    separately licensed software that they have either included with
    the program or referenced in the documentation.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License, version 2.0, for more details.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
    SERVICE_VERSION *foo_service = (void *)VERSION_foo;
    #define SERVICE_VERSION
    Definition: service_versions.h:31
  6. Add the new file to libservices/CMakeLists.txt (MYSQLSERVICES_SOURCES)
  7. And finally, register your service for dynamic linking in sql/sql_plugin_services.h
    • Fill in the service structure:
      static struct foo_service_st foo_handler = {
      foo_func1,
      foo_func2
      }
    • Add it to the list_of_services

      { "foo_service", VERSION_foo, &foo_handler }