SlideShare a Scribd company logo
MariaDB stored procedures
and why (I think that)
they should be improved
● Why stored procedures are important
● Examples of libraries
● Improvements that we need
Agenda
A user perspective:
● Stored procedures form an API that can be maintained by
most DBAs / DB engineers, whereas developers can keep
using ORMs
● Avoid network I/O overhead, resource consumption
● Shorter locks
● Write it once, call it from everywhere
Why Stored Procedures are useful
A community and vendor perspective:
● Easy way to add callable code to MariaDB
● Easy to distribute and "install"
● Some MariaDB features could have been implemented as
stored procedures in an acceptable way
(by someone who's not a C developer)
KILL QUERY, IF [NOT] EXISTS, SET STATEMENT…
Why Stored Procedures are useful
Currently MariaDB stored procedures:
● Are too slow
● Miss many features that make
development easier or
help generalise the code
● Don't have a native debugger
When why doesn't it happen?
Though MariaDB stored procedures have great improvements
compared to MySQL.
The native language has improvements, such as
EXECUTE IMMEDIATE or BEGIN NOT ATOMIC.
It also has a PL/SQL parser.
But some work is still needed.
But it happened (a bit)
It's half the time used by MySQL…
But come on, it should be instantaneous.
But it happened (a bit)
But we had good examples, including:
● common_schema
● MySQL General Purpose
Stored Routines Library
● SecuRich
● Flexviews
● MyTap, utMySQL, STK/Unit
● sql_games
But it happened (a bit)
External languages
● SQL is great for queries because it describes what you want
in English
● And then procedural constructs were added
● But they resemble prehistoric languages like COBOL
Missing features: External languages
● PostgreSQL supports natively SQL and C
● Support for other languages is implemented by the
community
Missing features: External languages
● The list is here:
https://wiki.postgresql.org/wiki/PL_Matrix
● Both interpreted and compiled
● List includes Python, JavaScript, PHP, Perl, Rust, …
Missing features: External languages
● Python is a sort of lingua franca for both
systems people and data people
● So I recommend to implement it
● But it would probably be impossible to implement venv's
● So I recommend that it's not the only option
Missing features: External languages
Missing features:
Input / Output
To write code that's reusable by many people in many contexts,
parameters need be more flexible. We miss:
● Optional arguments
● Variadic arguments
● Overloading
● RETURNS ON NULL INPUT
Missing features: Input / Output
Output should also be more flexible:
● Table functions
Missing features: Input / Output
MDEV-10862
CREATE FUNCTION get_hash(
string TEXT,
algorithm VARCHAR(20) DEFAULT 'sha512'
)
BEGIN
…
END
Optional arguments
MDEV-524
CREATE FUNCTION csv_list(VARCHAR(50), …)
Add 2 array variables or functions:
● argc()
● argv(offset)
Variadic arguments
(MDEV-23290)
PostgreSQL has this. Procedures can have the same name but
different parameter types.
CREATE FUNCTION get_month_days(month INT, DEFAULT year YEAR)
CREATE FUNCTION get_month_days(month VARCHAR(10), DEFAULT year YEAR)
SELECT get_month_days(12);
SELECT get_month_days('February', 2020);
Overloading
Since version 10.6 we have JSON_TABLE()
which accepts a JSON document and returns a table.
Stored Functions would be much more flexible if they
could return tables.
We also need functions to navigate the returned table
columns and rows.
It would be even better if TABLE becomes a first class type.
Table functions
PostgreSQL has this clause:
● RETURNS ON NULL INPUT / STRICT
Is not executed and returns NULL if one of the arguments is
NULL. It could save a lot of lines of code and it's faster.
● CALLED ON NULL INPUT
Executed normally even if there are NULL arguments.
Table functions
Missing features:
Optimisations
We need some optimisations:
● Inline functions
● DETERMINISTIC / STABLE / IMMUTABLE
● Use DETERMINISTIC functions in generated columns
● COMMUTATOR, NEGATOR, RESTRICTION
Missing Features: Optimisations
Sometimes a function can be inlined.
Inlining a function sometimes allows to use indexes.
CREATE FUNCTION pos_less_than(num INT, maxnum INT)
BEGIN
RETURN num < 0 AND num < maxnum;
END;
SELECT * FROM orders
WHERE pos_less_then(cost, 1000)
=> WHERE cost > 0 AND cost < 1000
Inline Functions
● MariaDB has DETERMINISTIC but specifying it has no
effect. Deterministic routines arguments and results should
be cached.
● STABLE is used in PostgreSQL. It means that a function is
deterministic for the current query only.
● Exception: a routine shouldn't be cached if it
MODIFIES SQL DATA
Missing Features: Optimisations
● PostgreSQL supports CREATE OPERATOR
● An operator is:
○ A function
○ + some hints for the optimiser
Missing Features: Optimisations
● Allow function authors to specify hints, such as:
○ Hints on function execution cost
○ A commutator function (eg: > and <)
○ A negator function (eg: > and =<)
○ Restriction (eqsel, neqsel, …, UDF)
Missing Features: Optimisations
Missing features:
Types
● Arrays
● Polymorphic types
Missing Features: Optimisations
MDEV-6121
● At least for stored routines and variables
● If external languages are supported, they'll have good array
support
● But MariaDB should be able, at least, to pass arrays as
arguments and return them from functions
Arrays
MDEV-18951
Any type can be converted to a string, but sometimes doing so
would bring too many problems (different order, inaccurate
conversions).
An ANY type would make things easy.
CREATE FUNCTION is_ordered(v1, v2)
BEGIN
RETURN v1 < v2;
END
Polymorphic types
MariaDB stored procedures and why they should be improved

More Related Content

Similar to MariaDB stored procedures and why they should be improved (20)

PPTX
Eko10 workshop - OPEN SOURCE DATABASE MONITORING
Pablo Garbossa
 
PDF
Challenges of Building a First Class SQL-on-Hadoop Engine
Nicolas Morales
 
PDF
Simplify Pulsar Functions Development with SQL - Pulsar Summit SF 2022
StreamNative
 
PDF
LAS16-TR06: Remoteproc & rpmsg development
Linaro
 
PDF
MySQL Query Optimisation 101
Federico Razzoli
 
PPTX
Eko10 Workshop Opensource Database Auditing
Juan Berner
 
PDF
MariaDB Enterprise Tools introduction
MariaDB plc
 
PDF
What's New in OpenLDAP
LDAPCon
 
PDF
Devops with Python by Yaniv Cohen DevopShift
Yaniv cohen
 
PDF
PostgreSQL and Redis - talk at pgcon 2013
Andrew Dunstan
 
PDF
Log forwarding at Scale
Eduardo Silva Pereira
 
PDF
Meet MariaDB 10.3 Debconf 2017
Vicentiu Ciorbaru
 
PDF
An evening with Postgresql
Joshua Drake
 
PDF
MySQL HA Orchestrator Proxysql Consul.pdf
YunusShaikh49
 
PDF
Building your first aplication using Apache Apex
Yogi Devendra Vyavahare
 
PDF
Building Your First Apache Apex Application
Apache Apex
 
PDF
RESTful Machine Learning with Flask and TensorFlow Serving - Carlo Mazzaferro
PyData
 
PDF
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
Federico Razzoli
 
ODP
BlaBlaCar Elastic Search Feedback
sinfomicien
 
PDF
OUG Ireland 2019 - building free, open-source, PL/SQL products in cloud
Jacek Gebal
 
Eko10 workshop - OPEN SOURCE DATABASE MONITORING
Pablo Garbossa
 
Challenges of Building a First Class SQL-on-Hadoop Engine
Nicolas Morales
 
Simplify Pulsar Functions Development with SQL - Pulsar Summit SF 2022
StreamNative
 
LAS16-TR06: Remoteproc & rpmsg development
Linaro
 
MySQL Query Optimisation 101
Federico Razzoli
 
Eko10 Workshop Opensource Database Auditing
Juan Berner
 
MariaDB Enterprise Tools introduction
MariaDB plc
 
What's New in OpenLDAP
LDAPCon
 
Devops with Python by Yaniv Cohen DevopShift
Yaniv cohen
 
PostgreSQL and Redis - talk at pgcon 2013
Andrew Dunstan
 
Log forwarding at Scale
Eduardo Silva Pereira
 
Meet MariaDB 10.3 Debconf 2017
Vicentiu Ciorbaru
 
An evening with Postgresql
Joshua Drake
 
MySQL HA Orchestrator Proxysql Consul.pdf
YunusShaikh49
 
Building your first aplication using Apache Apex
Yogi Devendra Vyavahare
 
Building Your First Apache Apex Application
Apache Apex
 
RESTful Machine Learning with Flask and TensorFlow Serving - Carlo Mazzaferro
PyData
 
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
Federico Razzoli
 
BlaBlaCar Elastic Search Feedback
sinfomicien
 
OUG Ireland 2019 - building free, open-source, PL/SQL products in cloud
Jacek Gebal
 

More from Federico Razzoli (20)

PDF
MariaDB Data Protection: Backup Strategies for the Real World
Federico Razzoli
 
PDF
MariaDB/MySQL_: Developing Scalable Applications
Federico Razzoli
 
PDF
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
PDF
High-level architecture of a complete MariaDB deployment
Federico Razzoli
 
PDF
Webinar - Unleash AI power with MySQL and MindsDB
Federico Razzoli
 
PDF
MariaDB Security Best Practices
Federico Razzoli
 
PDF
A first look at MariaDB 11.x features and ideas on how to use them
Federico Razzoli
 
PDF
Webinar - MariaDB Temporal Tables: a demonstration
Federico Razzoli
 
PDF
MariaDB 10.11 key features overview for DBAs
Federico Razzoli
 
PDF
Recent MariaDB features to learn for a happy life
Federico Razzoli
 
PDF
Advanced MariaDB features that developers love.pdf
Federico Razzoli
 
PDF
Automate MariaDB Galera clusters deployments with Ansible
Federico Razzoli
 
PDF
Creating Vagrant development machines with MariaDB
Federico Razzoli
 
PDF
MariaDB, MySQL and Ansible: automating database infrastructures
Federico Razzoli
 
PDF
Playing with the CONNECT storage engine
Federico Razzoli
 
PDF
MariaDB Temporal Tables
Federico Razzoli
 
PDF
Database Design most common pitfalls
Federico Razzoli
 
PDF
MySQL and MariaDB Backups
Federico Razzoli
 
PDF
JSON in MySQL and MariaDB Databases
Federico Razzoli
 
PDF
How MySQL can boost (or kill) your application v2
Federico Razzoli
 
MariaDB Data Protection: Backup Strategies for the Real World
Federico Razzoli
 
MariaDB/MySQL_: Developing Scalable Applications
Federico Razzoli
 
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
High-level architecture of a complete MariaDB deployment
Federico Razzoli
 
Webinar - Unleash AI power with MySQL and MindsDB
Federico Razzoli
 
MariaDB Security Best Practices
Federico Razzoli
 
A first look at MariaDB 11.x features and ideas on how to use them
Federico Razzoli
 
Webinar - MariaDB Temporal Tables: a demonstration
Federico Razzoli
 
MariaDB 10.11 key features overview for DBAs
Federico Razzoli
 
Recent MariaDB features to learn for a happy life
Federico Razzoli
 
Advanced MariaDB features that developers love.pdf
Federico Razzoli
 
Automate MariaDB Galera clusters deployments with Ansible
Federico Razzoli
 
Creating Vagrant development machines with MariaDB
Federico Razzoli
 
MariaDB, MySQL and Ansible: automating database infrastructures
Federico Razzoli
 
Playing with the CONNECT storage engine
Federico Razzoli
 
MariaDB Temporal Tables
Federico Razzoli
 
Database Design most common pitfalls
Federico Razzoli
 
MySQL and MariaDB Backups
Federico Razzoli
 
JSON in MySQL and MariaDB Databases
Federico Razzoli
 
How MySQL can boost (or kill) your application v2
Federico Razzoli
 
Ad

Recently uploaded (20)

PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Ad

MariaDB stored procedures and why they should be improved

  • 1. MariaDB stored procedures and why (I think that) they should be improved
  • 2. ● Why stored procedures are important ● Examples of libraries ● Improvements that we need Agenda
  • 3. A user perspective: ● Stored procedures form an API that can be maintained by most DBAs / DB engineers, whereas developers can keep using ORMs ● Avoid network I/O overhead, resource consumption ● Shorter locks ● Write it once, call it from everywhere Why Stored Procedures are useful
  • 4. A community and vendor perspective: ● Easy way to add callable code to MariaDB ● Easy to distribute and "install" ● Some MariaDB features could have been implemented as stored procedures in an acceptable way (by someone who's not a C developer) KILL QUERY, IF [NOT] EXISTS, SET STATEMENT… Why Stored Procedures are useful
  • 5. Currently MariaDB stored procedures: ● Are too slow ● Miss many features that make development easier or help generalise the code ● Don't have a native debugger When why doesn't it happen?
  • 6. Though MariaDB stored procedures have great improvements compared to MySQL. The native language has improvements, such as EXECUTE IMMEDIATE or BEGIN NOT ATOMIC. It also has a PL/SQL parser. But some work is still needed. But it happened (a bit)
  • 7. It's half the time used by MySQL… But come on, it should be instantaneous. But it happened (a bit)
  • 8. But we had good examples, including: ● common_schema ● MySQL General Purpose Stored Routines Library ● SecuRich ● Flexviews ● MyTap, utMySQL, STK/Unit ● sql_games But it happened (a bit)
  • 10. ● SQL is great for queries because it describes what you want in English ● And then procedural constructs were added ● But they resemble prehistoric languages like COBOL Missing features: External languages
  • 11. ● PostgreSQL supports natively SQL and C ● Support for other languages is implemented by the community Missing features: External languages
  • 12. ● The list is here: https://wiki.postgresql.org/wiki/PL_Matrix ● Both interpreted and compiled ● List includes Python, JavaScript, PHP, Perl, Rust, … Missing features: External languages
  • 13. ● Python is a sort of lingua franca for both systems people and data people ● So I recommend to implement it ● But it would probably be impossible to implement venv's ● So I recommend that it's not the only option Missing features: External languages
  • 15. To write code that's reusable by many people in many contexts, parameters need be more flexible. We miss: ● Optional arguments ● Variadic arguments ● Overloading ● RETURNS ON NULL INPUT Missing features: Input / Output
  • 16. Output should also be more flexible: ● Table functions Missing features: Input / Output
  • 17. MDEV-10862 CREATE FUNCTION get_hash( string TEXT, algorithm VARCHAR(20) DEFAULT 'sha512' ) BEGIN … END Optional arguments
  • 18. MDEV-524 CREATE FUNCTION csv_list(VARCHAR(50), …) Add 2 array variables or functions: ● argc() ● argv(offset) Variadic arguments
  • 19. (MDEV-23290) PostgreSQL has this. Procedures can have the same name but different parameter types. CREATE FUNCTION get_month_days(month INT, DEFAULT year YEAR) CREATE FUNCTION get_month_days(month VARCHAR(10), DEFAULT year YEAR) SELECT get_month_days(12); SELECT get_month_days('February', 2020); Overloading
  • 20. Since version 10.6 we have JSON_TABLE() which accepts a JSON document and returns a table. Stored Functions would be much more flexible if they could return tables. We also need functions to navigate the returned table columns and rows. It would be even better if TABLE becomes a first class type. Table functions
  • 21. PostgreSQL has this clause: ● RETURNS ON NULL INPUT / STRICT Is not executed and returns NULL if one of the arguments is NULL. It could save a lot of lines of code and it's faster. ● CALLED ON NULL INPUT Executed normally even if there are NULL arguments. Table functions
  • 23. We need some optimisations: ● Inline functions ● DETERMINISTIC / STABLE / IMMUTABLE ● Use DETERMINISTIC functions in generated columns ● COMMUTATOR, NEGATOR, RESTRICTION Missing Features: Optimisations
  • 24. Sometimes a function can be inlined. Inlining a function sometimes allows to use indexes. CREATE FUNCTION pos_less_than(num INT, maxnum INT) BEGIN RETURN num < 0 AND num < maxnum; END; SELECT * FROM orders WHERE pos_less_then(cost, 1000) => WHERE cost > 0 AND cost < 1000 Inline Functions
  • 25. ● MariaDB has DETERMINISTIC but specifying it has no effect. Deterministic routines arguments and results should be cached. ● STABLE is used in PostgreSQL. It means that a function is deterministic for the current query only. ● Exception: a routine shouldn't be cached if it MODIFIES SQL DATA Missing Features: Optimisations
  • 26. ● PostgreSQL supports CREATE OPERATOR ● An operator is: ○ A function ○ + some hints for the optimiser Missing Features: Optimisations
  • 27. ● Allow function authors to specify hints, such as: ○ Hints on function execution cost ○ A commutator function (eg: > and <) ○ A negator function (eg: > and =<) ○ Restriction (eqsel, neqsel, …, UDF) Missing Features: Optimisations
  • 29. ● Arrays ● Polymorphic types Missing Features: Optimisations
  • 30. MDEV-6121 ● At least for stored routines and variables ● If external languages are supported, they'll have good array support ● But MariaDB should be able, at least, to pass arrays as arguments and return them from functions Arrays
  • 31. MDEV-18951 Any type can be converted to a string, but sometimes doing so would bring too many problems (different order, inaccurate conversions). An ANY type would make things easy. CREATE FUNCTION is_ordered(v1, v2) BEGIN RETURN v1 < v2; END Polymorphic types