O'Reilly logo

Understanding MySQL Internals by Sasha Pachev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Coding Guidelines

If you are making changes to MySQL, it is recommended that you follow the same coding guidelines as MySQL developers. This will make it easier for your code to work with what's already written, help you avoid bugs, and increase the chances of your patches being accepted by MySQL developers without serious modifications.

MySQL developers make their coding guidelines publicly available at http://dev.mysql.com/doc/internals/en/index.html.

In addition, I will provide a reorganized summary with some extra tips and comments from my own experience.

Stability

Here are some guidelines for preserving the code's stability while making changes:

  • Always remember that, more often than not, you are in a thread and must follow the rules of thread-safe programming.

  • Most global variables have an associated mutex that other threads will lock before accessing it. Make sure to learn which mutex is associated with each global variable, and lock it when accessing that variable.

  • Be aware that you have very little stack space available. Chunks of memory larger than 100 bytes or so should be allocated with sql_alloc( ) or my_malloc( ).

  • When possible, choose sql_alloc( ) over my_malloc( ) for small allocations. sql_ alloc( ) allocates memory from a pre-allocated connection memory pool, while my_malloc( ) is just a wrapper around the regular malloc( ) call. sql_alloc( ) can be called anywhere in the stack of execution below do_command( ). To verify the stack position in question, set a breakpoint ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required