MySQL source code reading 1 - get ready

Introduction

Began to read MySQL server source code. I'll write down what I read and what I understand to keep them in my mind. (Too complex for me to remember without any memorandum...)

First of all, I wrote how I prepared to read the code.

Reference

I use this book.

Understanding MySQL Internals: Discovering and Improving a Great Database (English Edition)

Understanding MySQL Internals: Discovering and Improving a Great Database (English Edition)

Damn, not sure but this blog won't display the original version here. So, I'll put translated version as well, just in case.

詳解 MySQL

詳解 MySQL

If your local amazon is the US amazon, see http://www.amazon.com/dp/0596009577/

How to get the source code

Written in the internal manual.

How to generate the document of the source code

See Doxyfile-perfschema in the source code directory.

How to see the source code on Web

This is one of the web sites you can see the code. It has search function which may help you.

How to build and install MySQL server

Written in the internal manual.

How to configure mysqld

Copy my.cnf to ~/.my.cnf and then modify it. I added two lines to easily boot. /home/takahiro/mysql-bin is the built server directory.

# These are commonly set, remove the # and set as required.
basedir=/home/takahiro/mysql-bin
datadir=/home/takahiro/mysql-bin/var

How to use debugger

Written in the internal manual.


Run mysqld with gdb on a terminal window.

$ gdb mysqld

Then run mysql client on another terminal window.

$ mysql


I use CUI debugger, gdb.

Storage

I use MyISAM for now. Because it looks more straightforward than Innodb. The problem of MyISAM is that transaction seems not work on it. When I read transaction related code, I might switch storage from MyISAM to Innodb.

Add the following line in ~/.my.cnf to use MyISAM as default storage.

[mysqld]
default-storage-engine=MyISAM

Sample tables

I made two tables for my code reading.

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a              |
| b              |
+----------------+
2 rows in set (0.00 sec)

mysql> show columns from a;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| b_id  | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> show columns from b;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| val   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from a;
+------+------+
| id   | b_id |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    3 |    3 |
|    4 |    4 |
|    5 |    5 |
|    6 |    1 |
|    7 |    2 |
|    8 |    3 |
|    9 |    4 |
|   10 |    5 |
+------+------+
10 rows in set (0.80 sec)

mysql> select * from b;
+------+------+
| id   | val  |
+------+------+
|    1 |    0 |
|    2 |    1 |
|    3 |    2 |
|    4 |    3 |
|    5 |    4 |
+------+------+
5 rows in set (0.67 sec)

Raw files

takahiro@ubuntu:~/mysql-bin/var/test$ hexdump -C a.MYD
00000000  f9 01 00 00 00 01 00 00  00 f9 02 00 00 00 02 00  |................|
00000010  00 00 f9 03 00 00 00 03  00 00 00 f9 04 00 00 00  |................|
00000020  04 00 00 00 f9 05 00 00  00 05 00 00 00 f9 06 00  |................|
00000030  00 00 01 00 00 00 f9 07  00 00 00 02 00 00 00 f9  |................|
00000040  08 00 00 00 03 00 00 00  f9 09 00 00 00 04 00 00  |................|
00000050  00 f9 0a 00 00 00 05 00  00 00                    |..........|
0000005a
takahiro@ubuntu:~/mysql-bin/var/test$ hexdump -C b.MYD
00000000  f9 01 00 00 00 00 00 00  00 f9 02 00 00 00 01 00  |................|
00000010  00 00 f9 03 00 00 00 02  00 00 00 f9 04 00 00 00  |................|
00000020  03 00 00 00 f9 05 00 00  00 04 00 00 00           |.............|
0000002d

test is the boring database name for the code reading.

You see that 9 bytes are used for a row. The first byte in the 9 bytes is flag. The following two 4 bytes are the integer values. Recall columns in the table.

Conclusion

I'm ready to read the MySQL server code. I'll read simple query processing next.