SlideShare a Scribd company logo
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Guided tour on the MySQL Source Code
Georgi Kodinov
MySQL team lead
Confidential – Oracle Internal/Restricted/Highly Restricted
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
Confidential – Oracle Internal/Restricted/Highly Restricted 2
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Agenda
Confidential – Oracle Internal/Restricted/Highly Restricted 3
The directory layout of the codebase
Session establishment and termination
Query execution
Add functionality to MySQL
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Important directories in the codebase
Directory Purpose
sql/ Most of the server code
storage/ Storage engines
plugin/ Non-storage-engine plugins
sql-common/ Code common between client and server
libmysql/ Client library code
libmysqld/ Embedded server client library glue code
mysql-test/ Integral tests
libservices/ The plugin services library, plugin part
mysys*/ Portability library, utilities
vio/ Network connection abstraction library
client/ Client tools
Confidential – Oracle Internal/Restricted/Highly Restricted 4
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Session establishment and termination
• Each transport has its own listening thread: *_conn_event_handler()
– Runs Connection_acceptor::connection_event_loop()
• Communicates via a Protocol class sub-class. Protocol_classic for the native
MySQL protocol. Work in progress !
• Each session has a THD created. The THD is the session state. The THD is in
a OS thread attribute (current_thd()). There can be nested THDs.
• Connection_handler_manager class drives the threading model
Confidential – Oracle Internal/Restricted/Highly Restricted 5
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Session establishment and termination contd.
• handle_connection() is the main worker thread execution function
• check_connection() is where authentication happens
• close_connection() is the function to close the connection. Called by both
the worker thread or KILL
Confidential – Oracle Internal/Restricted/Highly Restricted 6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Query execution at a glance
• do_command() fetches and executes a command (RPC)
• dispatch_command() has the command (RPC) id main switch()
• The THD has the current command being executed
• mysql_parse() parses and executes a single query.
– Called in a loop for the COM_QUERY command
• mysql_execute_command() is the execution part of mysql_parse()
– Used to be a switch() over the SQL command id and call different C functions
– Now in the process of being transformed to calls to Sql_cmd::execute().
– Lots of sub-classes for Sql_cmd.
Confidential – Oracle Internal/Restricted/Highly Restricted 7
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
The parser: work in progress
• In older releases the parser produced the execution plan directly
– Item class subclasses
• Started adding a separate parse stage data structure
– Parse_tree_node subclasses
• Extra pass
– Sql_cmd *PT_Statement::make_cmd()
– Still called by the parser
Confidential – Oracle Internal/Restricted/Highly Restricted 8
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Query execution passes: handle_query() et al
• “Open” all tables: open_tables_for_query ()
• Preparation: SELECT_LEX::prepare() and friends
– Name resolution: setup_fields() et al
– Some transformations, like flattening subqueries etc
• Optimization: SELECT_LEX::optimize() and friends
– Produces a JOIN object for each subquery level
– Calls JOIN::optimize() to produce query execution plan
• Execution: JOIN::exec() or st_select_lex_unit::execute()
– Pumps result into a Query_result sub-class.
Confidential – Oracle Internal/Restricted/Highly Restricted 9
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Adding functionality to MySQL: Plugins
Confidential – Oracle Internal/Restricted/Highly Restricted 10
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Plugins overview
• A plugin is:
– An API that the server calls
– init() and deinit()
– A bunch of system variables
– A bunch of status variables
• A plugin does:
– Initialize and de-initialize itself
– Implement the API that the server will call
– Call back into the server via the plugin services
Confidential – Oracle Internal/Restricted/Highly Restricted 11
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Plugins: Pros and Cons
Pros Cons
Clean versioned API to implement Can only implement a fixed set of APIs defined by the
server
Can be loaded and unloaded dynamically Can only interact with the server. Not other plugins
Can use services to call back into the server Acquiring and releasing plugin handles by the server is
expensive
Can be built with the binary distro, no need for the server
source
Very poor set of plugin services
Are versioned and can be reused across versions Linked with the server binary: can call into the server
code without services
The tree cmake files detect “foreign” plugin directories
and compile them
APIs cannot be related so APIs tend to be large (see e.g.
the SE API : 200+ methods)
Confidential – Oracle Internal/Restricted/Highly Restricted 12
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Most notable plugin types
• Storage engines
• INFORMATION_SCHEMA tables
• Daemon
• Authentication
• Audit
• Keyring
Confidential – Oracle Internal/Restricted/Highly Restricted 13
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
How to write a plugin ?
• Take an existing one and alter it
• Plenty of examples
– Live plugins
– Test plugins
• Put it in plugin/ or storage/ so it compiles
Confidential – Oracle Internal/Restricted/Highly Restricted 14
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Adding functionality to MySQL
Plugin Services
Confidential – Oracle Internal/Restricted/Highly Restricted 15
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
What is a plugin service ?
• The reverse of a plugin API
• Allows plugins to call into the server
• Version controlled
• In essence a set function pointers in each plugin that the server fills in at
INSTALL PLUGIN time
• Header in include/mysql/service_*.h
Confidential – Oracle Internal/Restricted/Highly Restricted 16
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Please use the plugin services !
• Calling server code directly => a “dirty” plugin
– No portability across versions
– No guarantee to work with future versions
• Plugin services are the reverse of Plugin APIs
– Inlcude/mysql/service_*.h
• We have some good ones in 5.7:
– command_service_st : execute a RPC command
– mysql_locking_service_st : interface to MDL locks
– security_context_service_st : interface to the security context
– srv_session_service_st : handle “sessions” inside the server
Confidential – Oracle Internal/Restricted/Highly Restricted 17
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Adding functionality to MySQL: UDFs
Confidential – Oracle Internal/Restricted/Highly Restricted 18
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
What are the UDFs
• Can reside in external binaries
• Can be loaded and unloaded
• Can share binaries with plugins or other UDFs
• Executed as single result or aggregate SQL functions
• Easy to write, lots of examples
Confidential – Oracle Internal/Restricted/Highly Restricted 19
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
UDFs: Pros and Cons
Pros Cons
Easy to write Terrible for the DBA to administer
Can share a binary with the plugins No global initialization and deinitialization
Can be version safe No access to plugin services
Somewhat limited data type range
Only SQL functions, not commands to execute
No THD context
Confidential – Oracle Internal/Restricted/Highly Restricted 20
OUGLS 2016: Guided Tour On The MySQL Source Code

More Related Content

What's hot (20)

PDF
Oracle ORAchk & EXAchk, What's New in 12.1.0.2.7
Gareth Chapman
 
PDF
Em13c features- HotSos 2016
Kellyn Pot'Vin-Gorman
 
PDF
Maximizing Oracle RAC Uptime
Markus Michalewicz
 
PDF
Mysql tech day_paris_ps_and_sys
Mark Leith
 
PPTX
Em13c New Features- Two of Two
Kellyn Pot'Vin-Gorman
 
PDF
Performance in the Oracle Cloud
Kellyn Pot'Vin-Gorman
 
PDF
TFA Collector - what can one do with it
Sandesh Rao
 
PPTX
Oracle EM12c Release 4 New Features!
Kellyn Pot'Vin-Gorman
 
PDF
Oracle Traffic Director - a vital part of your Oracle infrastructure
Simon Haslam
 
PDF
Oracle Enterprise Manager Cloud Control 13c for DBAs
Gokhan Atil
 
PDF
How to Use EXAchk Effectively to Manage Exadata Environments
Sandesh Rao
 
PDF
Oracle Database In-Memory Meets Oracle RAC
Markus Michalewicz
 
PPTX
Em13c New Features- One of Two
Kellyn Pot'Vin-Gorman
 
PPTX
Oracle REST Data Services: Options for your Web Services
Jeff Smith
 
PPT
Anatomy of Autoconfig in Oracle E-Business Suite
vasuballa
 
PPTX
Making MySQL highly available using Oracle Grid Infrastructure
Ilmar Kerm
 
PDF
AWR and ASH in an EM12c World
Kellyn Pot'Vin-Gorman
 
PPTX
Boost Your Content Strategy for REST APIs with Gururaj BS
Information Development World
 
PDF
Colvin exadata and_oem12c
Enkitec
 
PPTX
Oracle REST Data Services Best Practices/ Overview
Kris Rice
 
Oracle ORAchk & EXAchk, What's New in 12.1.0.2.7
Gareth Chapman
 
Em13c features- HotSos 2016
Kellyn Pot'Vin-Gorman
 
Maximizing Oracle RAC Uptime
Markus Michalewicz
 
Mysql tech day_paris_ps_and_sys
Mark Leith
 
Em13c New Features- Two of Two
Kellyn Pot'Vin-Gorman
 
Performance in the Oracle Cloud
Kellyn Pot'Vin-Gorman
 
TFA Collector - what can one do with it
Sandesh Rao
 
Oracle EM12c Release 4 New Features!
Kellyn Pot'Vin-Gorman
 
Oracle Traffic Director - a vital part of your Oracle infrastructure
Simon Haslam
 
Oracle Enterprise Manager Cloud Control 13c for DBAs
Gokhan Atil
 
How to Use EXAchk Effectively to Manage Exadata Environments
Sandesh Rao
 
Oracle Database In-Memory Meets Oracle RAC
Markus Michalewicz
 
Em13c New Features- One of Two
Kellyn Pot'Vin-Gorman
 
Oracle REST Data Services: Options for your Web Services
Jeff Smith
 
Anatomy of Autoconfig in Oracle E-Business Suite
vasuballa
 
Making MySQL highly available using Oracle Grid Infrastructure
Ilmar Kerm
 
AWR and ASH in an EM12c World
Kellyn Pot'Vin-Gorman
 
Boost Your Content Strategy for REST APIs with Gururaj BS
Information Development World
 
Colvin exadata and_oem12c
Enkitec
 
Oracle REST Data Services Best Practices/ Overview
Kris Rice
 

Similar to OUGLS 2016: Guided Tour On The MySQL Source Code (20)

PPTX
How to add stuff to MySQL
Georgi Kodinov
 
ODP
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
Ulf Wendel
 
PDF
Ipc mysql php
Anis Berejeb
 
PDF
Instrumenting plugins for Performance Schema
Mark Leith
 
PDF
MySQL Tech Tour Nov, 2013
Mysql Latinoamérica
 
ODP
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
Ulf Wendel
 
PDF
Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.
Geir Høydalsvik
 
PDF
Mysqlnd, an unknown powerful PHP extension
julien pauli
 
PDF
MySQL
PT.JUG
 
PDF
MySQL Document Store
Mario Beck
 
ODP
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
Ulf Wendel
 
PDF
MySQL Connector/J Feature Review and How to Upgrade from Connector/J 5.1
Filipe Silva
 
ODP
MySQL for Oracle DBAs
Ben Krug
 
PPTX
FOSDEM19 MySQL Component Infrastructure
Georgi Kodinov
 
PDF
HTTP Plugin for MySQL!
Ulf Wendel
 
PDF
From Nice to Have to Mission Critical: MySQL Enterprise Edition
郁萍 王
 
PDF
MySQL 8.0.1 DMR
MySQL Brasil
 
ODP
The power of mysqlnd plugins
Ulf Wendel
 
PDF
Php & my sql - how do pdo, mysq-li, and x devapi do what they do
Dave Stokes
 
ODP
PHP mysqlnd connection multiplexing plugin
Ulf Wendel
 
How to add stuff to MySQL
Georgi Kodinov
 
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
Ulf Wendel
 
Ipc mysql php
Anis Berejeb
 
Instrumenting plugins for Performance Schema
Mark Leith
 
MySQL Tech Tour Nov, 2013
Mysql Latinoamérica
 
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
Ulf Wendel
 
Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.
Geir Høydalsvik
 
Mysqlnd, an unknown powerful PHP extension
julien pauli
 
MySQL
PT.JUG
 
MySQL Document Store
Mario Beck
 
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
Ulf Wendel
 
MySQL Connector/J Feature Review and How to Upgrade from Connector/J 5.1
Filipe Silva
 
MySQL for Oracle DBAs
Ben Krug
 
FOSDEM19 MySQL Component Infrastructure
Georgi Kodinov
 
HTTP Plugin for MySQL!
Ulf Wendel
 
From Nice to Have to Mission Critical: MySQL Enterprise Edition
郁萍 王
 
MySQL 8.0.1 DMR
MySQL Brasil
 
The power of mysqlnd plugins
Ulf Wendel
 
Php & my sql - how do pdo, mysq-li, and x devapi do what they do
Dave Stokes
 
PHP mysqlnd connection multiplexing plugin
Ulf Wendel
 
Ad

More from Georgi Kodinov (20)

PPTX
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
PPTX
2023 TurnovoConf MySQL Authentication.pptx
Georgi Kodinov
 
PPTX
2022 TurnovoConf MySQL за начинаещи.pptx
Georgi Kodinov
 
PPTX
OpenSUSE Conf 2020 MySQL Clone
Georgi Kodinov
 
PPTX
2020 pre fosdem mysql clone
Georgi Kodinov
 
PPTX
2019 BGOUG Autumn MySQL Clone
Georgi Kodinov
 
PPTX
2019 indit blackhat_honeypot your database server
Georgi Kodinov
 
PPTX
PLe19 How To Instrument Your Code in performance_schema
Georgi Kodinov
 
PPTX
DevTalks.ro 2019 What's New in MySQL 8.0 Security
Georgi Kodinov
 
PPTX
DevTalks.ro 2019 MySQL Data Masking Talk
Georgi Kodinov
 
PPTX
MySQL Enterprise Data Masking
Georgi Kodinov
 
PPTX
Percona Live Europe 2018: What's New in MySQL 8.0 Security
Georgi Kodinov
 
PPTX
Pl18 saving bandwidth
Georgi Kodinov
 
PPTX
BGOUG17: Cloudy with a chance of MySQL
Georgi Kodinov
 
PPTX
Pl17: MySQL 8.0: security
Georgi Kodinov
 
PPTX
Fosdem17 honeypot your database server
Georgi Kodinov
 
PPTX
2016 oSC MySQL Firewall
Georgi Kodinov
 
PPTX
OpenSuse 2015: Secure Deployment Changes Coming in MySQL 5.7
Georgi Kodinov
 
PPTX
BGOUG 2014 Decrease Your MySQL Attack Surface
Georgi Kodinov
 
PPTX
BGOUG 2014: Developing Using MySQL
Georgi Kodinov
 
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
2023 TurnovoConf MySQL Authentication.pptx
Georgi Kodinov
 
2022 TurnovoConf MySQL за начинаещи.pptx
Georgi Kodinov
 
OpenSUSE Conf 2020 MySQL Clone
Georgi Kodinov
 
2020 pre fosdem mysql clone
Georgi Kodinov
 
2019 BGOUG Autumn MySQL Clone
Georgi Kodinov
 
2019 indit blackhat_honeypot your database server
Georgi Kodinov
 
PLe19 How To Instrument Your Code in performance_schema
Georgi Kodinov
 
DevTalks.ro 2019 What's New in MySQL 8.0 Security
Georgi Kodinov
 
DevTalks.ro 2019 MySQL Data Masking Talk
Georgi Kodinov
 
MySQL Enterprise Data Masking
Georgi Kodinov
 
Percona Live Europe 2018: What's New in MySQL 8.0 Security
Georgi Kodinov
 
Pl18 saving bandwidth
Georgi Kodinov
 
BGOUG17: Cloudy with a chance of MySQL
Georgi Kodinov
 
Pl17: MySQL 8.0: security
Georgi Kodinov
 
Fosdem17 honeypot your database server
Georgi Kodinov
 
2016 oSC MySQL Firewall
Georgi Kodinov
 
OpenSuse 2015: Secure Deployment Changes Coming in MySQL 5.7
Georgi Kodinov
 
BGOUG 2014 Decrease Your MySQL Attack Surface
Georgi Kodinov
 
BGOUG 2014: Developing Using MySQL
Georgi Kodinov
 
Ad

Recently uploaded (20)

PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 

OUGLS 2016: Guided Tour On The MySQL Source Code

  • 1. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Guided tour on the MySQL Source Code Georgi Kodinov MySQL team lead Confidential – Oracle Internal/Restricted/Highly Restricted
  • 2. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. Confidential – Oracle Internal/Restricted/Highly Restricted 2
  • 3. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Agenda Confidential – Oracle Internal/Restricted/Highly Restricted 3 The directory layout of the codebase Session establishment and termination Query execution Add functionality to MySQL
  • 4. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Important directories in the codebase Directory Purpose sql/ Most of the server code storage/ Storage engines plugin/ Non-storage-engine plugins sql-common/ Code common between client and server libmysql/ Client library code libmysqld/ Embedded server client library glue code mysql-test/ Integral tests libservices/ The plugin services library, plugin part mysys*/ Portability library, utilities vio/ Network connection abstraction library client/ Client tools Confidential – Oracle Internal/Restricted/Highly Restricted 4
  • 5. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Session establishment and termination • Each transport has its own listening thread: *_conn_event_handler() – Runs Connection_acceptor::connection_event_loop() • Communicates via a Protocol class sub-class. Protocol_classic for the native MySQL protocol. Work in progress ! • Each session has a THD created. The THD is the session state. The THD is in a OS thread attribute (current_thd()). There can be nested THDs. • Connection_handler_manager class drives the threading model Confidential – Oracle Internal/Restricted/Highly Restricted 5
  • 6. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Session establishment and termination contd. • handle_connection() is the main worker thread execution function • check_connection() is where authentication happens • close_connection() is the function to close the connection. Called by both the worker thread or KILL Confidential – Oracle Internal/Restricted/Highly Restricted 6
  • 7. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Query execution at a glance • do_command() fetches and executes a command (RPC) • dispatch_command() has the command (RPC) id main switch() • The THD has the current command being executed • mysql_parse() parses and executes a single query. – Called in a loop for the COM_QUERY command • mysql_execute_command() is the execution part of mysql_parse() – Used to be a switch() over the SQL command id and call different C functions – Now in the process of being transformed to calls to Sql_cmd::execute(). – Lots of sub-classes for Sql_cmd. Confidential – Oracle Internal/Restricted/Highly Restricted 7
  • 8. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | The parser: work in progress • In older releases the parser produced the execution plan directly – Item class subclasses • Started adding a separate parse stage data structure – Parse_tree_node subclasses • Extra pass – Sql_cmd *PT_Statement::make_cmd() – Still called by the parser Confidential – Oracle Internal/Restricted/Highly Restricted 8
  • 9. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Query execution passes: handle_query() et al • “Open” all tables: open_tables_for_query () • Preparation: SELECT_LEX::prepare() and friends – Name resolution: setup_fields() et al – Some transformations, like flattening subqueries etc • Optimization: SELECT_LEX::optimize() and friends – Produces a JOIN object for each subquery level – Calls JOIN::optimize() to produce query execution plan • Execution: JOIN::exec() or st_select_lex_unit::execute() – Pumps result into a Query_result sub-class. Confidential – Oracle Internal/Restricted/Highly Restricted 9
  • 10. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Adding functionality to MySQL: Plugins Confidential – Oracle Internal/Restricted/Highly Restricted 10
  • 11. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Plugins overview • A plugin is: – An API that the server calls – init() and deinit() – A bunch of system variables – A bunch of status variables • A plugin does: – Initialize and de-initialize itself – Implement the API that the server will call – Call back into the server via the plugin services Confidential – Oracle Internal/Restricted/Highly Restricted 11
  • 12. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Plugins: Pros and Cons Pros Cons Clean versioned API to implement Can only implement a fixed set of APIs defined by the server Can be loaded and unloaded dynamically Can only interact with the server. Not other plugins Can use services to call back into the server Acquiring and releasing plugin handles by the server is expensive Can be built with the binary distro, no need for the server source Very poor set of plugin services Are versioned and can be reused across versions Linked with the server binary: can call into the server code without services The tree cmake files detect “foreign” plugin directories and compile them APIs cannot be related so APIs tend to be large (see e.g. the SE API : 200+ methods) Confidential – Oracle Internal/Restricted/Highly Restricted 12
  • 13. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Most notable plugin types • Storage engines • INFORMATION_SCHEMA tables • Daemon • Authentication • Audit • Keyring Confidential – Oracle Internal/Restricted/Highly Restricted 13
  • 14. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | How to write a plugin ? • Take an existing one and alter it • Plenty of examples – Live plugins – Test plugins • Put it in plugin/ or storage/ so it compiles Confidential – Oracle Internal/Restricted/Highly Restricted 14
  • 15. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Adding functionality to MySQL Plugin Services Confidential – Oracle Internal/Restricted/Highly Restricted 15
  • 16. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | What is a plugin service ? • The reverse of a plugin API • Allows plugins to call into the server • Version controlled • In essence a set function pointers in each plugin that the server fills in at INSTALL PLUGIN time • Header in include/mysql/service_*.h Confidential – Oracle Internal/Restricted/Highly Restricted 16
  • 17. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Please use the plugin services ! • Calling server code directly => a “dirty” plugin – No portability across versions – No guarantee to work with future versions • Plugin services are the reverse of Plugin APIs – Inlcude/mysql/service_*.h • We have some good ones in 5.7: – command_service_st : execute a RPC command – mysql_locking_service_st : interface to MDL locks – security_context_service_st : interface to the security context – srv_session_service_st : handle “sessions” inside the server Confidential – Oracle Internal/Restricted/Highly Restricted 17
  • 18. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Adding functionality to MySQL: UDFs Confidential – Oracle Internal/Restricted/Highly Restricted 18
  • 19. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | What are the UDFs • Can reside in external binaries • Can be loaded and unloaded • Can share binaries with plugins or other UDFs • Executed as single result or aggregate SQL functions • Easy to write, lots of examples Confidential – Oracle Internal/Restricted/Highly Restricted 19
  • 20. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | UDFs: Pros and Cons Pros Cons Easy to write Terrible for the DBA to administer Can share a binary with the plugins No global initialization and deinitialization Can be version safe No access to plugin services Somewhat limited data type range Only SQL functions, not commands to execute No THD context Confidential – Oracle Internal/Restricted/Highly Restricted 20