Well, “easy” if you know just a tiny bit of C++.

MySQL is well known for its ease of use, being easy to install, easy to configure, and easy to maintain. What if there is something more that you’d like MySQL to do? How would you integrate some new fancy processing library into MySQL without having to recreate the complexities in pure SQL?

MySQL Loadable Functions would be the way to go. In this blog post, you’ll learn how to set up a build environment for compiling your own MySQL plugin to be loaded into MySQL as a function. Our function will implement a ULID generator using a C++ library from ChrisBove/ulid.

Creating the build environment

The first step is downloading the source code to MySQL / Percona Server for MySQL 8.0.32, then extracting the tarball.

Next, we need to set up a location for our new plugin within the source tree. We can also grab a copy of the library that we will use within the plugin.

Now we need to create two files. The first file is for cmake which informs the overall build process that it should include, and compile our plugin.

MYSQL_ADD_PLUGIN is a macro for CMake that defines our plugin name, the plugin’s main source code file, and specifies that this is a loadable shared object (.so).

The second file we need to create is our actual source code for the plugin.

We will go into more detail about the code itself later in this post. For now, let’s continue to get everything compiled and working.

Now that we have the code tree extracted and our plugin code ready, we need to create a build location. You can build within the extracted source tree, but this goes against best practices. Once inside the build directory, execute cmake targeted to the source tree. There are some helper flags that you should use to download the Boost library and disable features that are not needed for compiling a plugin. If there are any fundamental libraries missing, the output will do its best to let you know what commands to run (apt / rpm / yum) to install the needed libraries.

Cmake is now finished, and all the initial checks look good. Time to compile just our plugin. We don’t need to compile the entire Percona MySQL server codebase.

Success! Plugin compiled. Let’s load it into a running MySQL server and check that it works.

More success! Our plugin works!

Understanding the plugin

Now that our plugin works, let’s rewind a bit and understand what is happening. The contents below is my tl;dr. If you want a more detailed explanation, please consult the documentation.

There are three functions within the code that you are required to define:

  • ulid – The main function, called for each row
  • ulid_init – Called at the beginning of the statement; used to verify number of arguments, and/or argument types.
  • ulid_deinit – Called at the end of the statement; Typically empty unless you allocated additional memory or other structures in _init.

Let’s look at each of these functions as implemented:

Despite being all C++, you still need to externalize the function names so “C” can view them. The above _init function first checks that there are either 0 or 1 parameters and, strangely, returns true if there’s an error.

If the user provided 1 argument, check that the argument is an integer. Lastly, tell MySQL the maximum length of the string value we will return.

On to the main function. There are inline comments to explain various lines and sections of the code.

Some example executions:

Conclusion

Extending MySQL’s functionality to utilize other libraries presents endless opportunities in expanding the capabilities of MySQL. How about writing a function that can send emails? Or blinking an LED? Hopefully, the above explanations and examples will spark something in you!

The above ULID plugin came as an idea from a post by Shopify, specifically #6, that mentioned moving to ULID over UUID for better sequential INSERTs in their MySQL databases. Since there was a C++ library for ULID, this presented a fantastic opportunity to showcase how MySQL can be extended to have native SQL-based ULID functionality.

Look for part two, where we benchmark ULID in MySQL! (*spolier* It’s better!)

Percona Distribution for MySQL is the most complete, stable, scalable, and secure open source MySQL solution available, delivering enterprise-grade database environments for your most critical business applications… and it’s free to use!

 

Try Percona Distribution for MySQL today!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments