SlideShare a Scribd company logo
Perl Stored Procedures for MySQL
Using the Perl plugin for the External
Language Stored Procedure framework.
Antony T Curtis <atcurtis@google.com>
Why Perl?
CPAN
"Multiplicity" / Thread-friendly
Lots of code already written
Clean APIs - example: DBI
MySQL UDFs have no access control
MySQL UDFs cannot execute dynamic SQL
Faster than MySQL's SQL Stored Procedures
Complex code uses less memory than MySQL's SQL SP
Server Overview
Use the source,
Download the source tarball from
https://launchpad.net/mysql-wl820/trunk/5.1.33-wl820
# configure and build
BUILD/compile-pentium-debug-max-no-ndb --no-tune-cpu 
--prefix=<install path>
# install
make install
# initialize db
cd <install path> ; bin/mysql_install_db
Preparing for the first time
Three ways to prepare the mysqld for Perl:
1. INSTALL PLUGIN Perl SONAME "psm_perl.so";
2. command line mysqld parameters
$
libexec/mysqld 
--pluginload=perl=psm_perl.so
3. within the my.cnf config file
[mysqld]
plugin-load=perl=psm_perl.so
"Hello World"
mysql> CREATE PROCEDURE PerlHello()
->
DYNAMIC RESULT SETS 1
->
NO SQL
->
LANGUAGE Perl
->
EXTERNAL NAME "HelloWorld::test";
Query OK, 0 rows affected (0.00 sec)
mysql> CALL PerlHello;
+-----------------------+
| message
|
+-----------------------+
| Hello World from Perl |
+-----------------------+
1 row in set (0.02 sec)
Query OK, 0 rows affected (0.03 sec)

package HelloWorld;
# put this file in <prefix>/lib/mysql/perl
use 5.008008;
use strict;
use warnings;
use Symbol qw(delete_package);
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw( );
our @EXPORT = qw( test );
our $VERSION = '0.01';
 
 
sub test()
{
  return {
    'message' => 'Hello World from Perl',
  };
}
 
1;
Features
Uses Perl prototypes for simplicity
threads don't block each other
pass by value (IN) parameters
pass-by-reference (INOUT/OUT) parameters
simple auto-casting of values
functions have simple single value result
procedure may return single result set
array of hash
array of array
hash result (single row)
array result (single row)
Features, part 2
detects if Perl module has changed and reloads
dynamic SQL support using DBI and DBD::mysql
READS SQL DATA attribute forces read-only mode
NO SQL attribute blocks inline dynamic SQL.
executes dynamic SQL in same thread/transaction
die returns error message up to the client
routines may be used in triggers and events
procedures may be used as table functions
NOTE: Unmodified MySQL does not have table functions, yet!
No dynamic SQL support from triggers, table funcs or events.
Table Functions (example)
mysql> CREATE FUNCTION HelloFunc()
->
RETURNS TABLE (
->
message VARCHAR(64)
->
)
->
NO SQL
->
LANGUAGE Perl
->
EXTERNAL NAME "HelloWorld::test";
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM TABLE(HelloFunc) AS HelloFunc;
+-----------------------+
| message
|
+-----------------------+
| Hello World from Perl |
+-----------------------+
1 row in set (0.01 sec)

Note: Correlation name is mandatory for table functions.
Demonstration
Limitations and gotchas
routines must not fork or create new threads
bad things happen - usually crashes.
functions cannot access tables with dynamic SQL
limitation of MySQL's open_and_lock internals
cannot detect changes in dependent modules
force reload by touching module time-stamp
currently,no way to flush MySQL procedure/function cache
client connection must disconnect to 'forget'
Runs "in-process" with mysqld
can cause mysqld to die from OOM error
can potentially corrupt mysqld internal data
Future Directions
Inline declaration of Perl code
Store code in database
"Fenced" mode
Perl modules would load in separate process
likely to use "process per user or security context"
"Atomic" routines
create SAVEPOINT before entering routine
ROLLBACK SAVEPOINT on error.
Fix/modify MySQL's table opening code (unlikely)
allow routines to open additional tables in read mode
would allow routines to query database without nonstandard SQL syntax hacks
Resources...
LaunchPad project
https://launchpad.net/sakila-server/wl820
Source tarballs
https://launchpad.net/mysql-wl820/trunk/5.1.33-wl820
Perl sources used in demo will be uploaded.
Modified PIE LaunchPad project
https://code.launchpad.net/~atcurtis/pie/devel
Questions?

Antony T Curtis <atcurtis@google.com>
More Perl Examples
#
#
#
#
#
#
#

CREATE FUNCTION error_check(
error_code INT,
error_message VARCHAR(512)
) RETURNS INT
NO SQL
LANGUAGE Perl
EXTERNAL NAME "...::error_check"

sub error_check($$)
{
my ($errcode, $errmsg) = @_;
die "[$errcode] $errmsg"
if $errcode;
return $errcode;
}

#
#
#
#

CREATE PROCEDURE check_data()
READS SQL DATA
LANGUAGE Perl
EXTERNAL NAME "...::check_data"

sub check_data()
{
my $dbh = DBI->connect(
"dbi:mysql:test", undef, undef)
or die "Failed to connect";
# attempts to update/write data
# using this connection will fail.
...
}
More Perl Examples, part 2
#
#
#
#
#
#
#

CREATE FUNCTION my_reverse(
string TEXT
) RETURNS TEXT
NO SQL
LANGUAGE Perl
EXTERNAL NAME
"MyUDFExample::my_reverse";

sub my_reverse($)
{
my($string)= @_;
# print "foon";
return reverse $string;
}

#
#
#
#
#
#
#

CREATE PROCEDURE my_reverseproc(
INOUT string TEXT
)
NO SQL
LANGUAGE Perl
EXTERNAL NAME
"MyUDFExample::my_reverseproc";

sub my_reverseproc($)
{
my($stringref)= @_;
$$stringref= reverse $$stringref;
# no resultsets
return undef;
}

More Related Content

What's hot (20)

PDF
More on gdb for my sql db as (fosdem 2016)
Valeriy Kravchuk
 
PDF
MySQL Performance schema missing_manual_flossuk
Valeriy Kravchuk
 
PDF
Introduction to MySQL InnoDB Cluster
I Goo Lee
 
PDF
Flame Graphs for MySQL DBAs - FOSDEM 2022 MySQL Devroom
Valeriy Kravchuk
 
PDF
New features in Performance Schema 5.7 in action
Sveta Smirnova
 
PDF
MariaDB for developers
Colin Charles
 
PPT
HandlerSocket - A NoSQL plugin for MySQL
Jui-Nan Lin
 
PDF
Summary tables with flexviews
Justin Swanhart
 
PDF
Mysql 56-experiences-bugs-solutions-50mins
Valeriy Kravchuk
 
PPT
HandlerSocket plugin for MySQL (English)
akirahiguchi
 
PDF
Gdb basics for my sql db as (openfest 2017) final
Valeriy Kravchuk
 
PDF
MySQL Performance Schema in Action
Sveta Smirnova
 
PDF
MySQL JSON Functions
Sveta Smirnova
 
PPTX
PostgreSQL Database Slides
metsarin
 
PDF
Instant add column for inno db in mariadb 10.3+ (fosdem 2018, second draft)
Valerii Kravchuk
 
PDF
Introduction to MySQL Query Tuning for Dev[Op]s
Sveta Smirnova
 
PDF
FOSDEM 2015: gdb tips and tricks for MySQL DBAs
Valerii Kravchuk
 
PPTX
PostgreSQL- An Introduction
Smita Prasad
 
More on gdb for my sql db as (fosdem 2016)
Valeriy Kravchuk
 
MySQL Performance schema missing_manual_flossuk
Valeriy Kravchuk
 
Introduction to MySQL InnoDB Cluster
I Goo Lee
 
Flame Graphs for MySQL DBAs - FOSDEM 2022 MySQL Devroom
Valeriy Kravchuk
 
New features in Performance Schema 5.7 in action
Sveta Smirnova
 
MariaDB for developers
Colin Charles
 
HandlerSocket - A NoSQL plugin for MySQL
Jui-Nan Lin
 
Summary tables with flexviews
Justin Swanhart
 
Mysql 56-experiences-bugs-solutions-50mins
Valeriy Kravchuk
 
HandlerSocket plugin for MySQL (English)
akirahiguchi
 
Gdb basics for my sql db as (openfest 2017) final
Valeriy Kravchuk
 
MySQL Performance Schema in Action
Sveta Smirnova
 
MySQL JSON Functions
Sveta Smirnova
 
PostgreSQL Database Slides
metsarin
 
Instant add column for inno db in mariadb 10.3+ (fosdem 2018, second draft)
Valerii Kravchuk
 
Introduction to MySQL Query Tuning for Dev[Op]s
Sveta Smirnova
 
FOSDEM 2015: gdb tips and tricks for MySQL DBAs
Valerii Kravchuk
 
PostgreSQL- An Introduction
Smita Prasad
 

Similar to Perl Stored Procedures for MySQL (2009) (20)

PDF
Basic MySQL Troubleshooting for Oracle Database Administrators
Sveta Smirnova
 
PDF
Preparse Query Rewrite Plugins
Sveta Smirnova
 
PPT
Mysql
guest817344
 
PPT
Mysql DBI
Joe Christensen
 
PPT
Php and MySQL Web Development
w3ondemand
 
PPT
Lect_04b_PhpMysqlKEY PERFORMANCE INDICATOR FOR ICT-UNIT (new).ppt
SenzotaSemakuwa
 
PPTX
20141011 mastering mysqlnd
do_aki
 
PDF
DPC2007 MySQL Stored Routines for PHP Developers (Roland Bouman)
dpc
 
PDF
wee
gapczar
 
PDF
Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.
Geir Høydalsvik
 
PDF
Memcached Functions For My Sql Seemless Caching In My Sql
MySQLConference
 
PDF
Sqlite perl
Ashoka Vanjare
 
PPTX
Php and database functionality
Sayed Ahmed
 
PPTX
Php and database functionality
Sayed Ahmed
 
PPTX
PHP and database functionality
Sayed Ahmed
 
PDF
The Ring programming language version 1.3 book - Part 20 of 88
Mahmoud Samir Fayed
 
PPT
PHP - Intriduction to MySQL And PHP
Vibrant Technologies & Computers
 
ODP
Database Programming with Perl and DBIx::Class
Dave Cross
 
PPT
Libmysqld Introduction
papablues
 
PDF
Introducing new SQL syntax and improving performance with preparse Query Rewr...
Sveta Smirnova
 
Basic MySQL Troubleshooting for Oracle Database Administrators
Sveta Smirnova
 
Preparse Query Rewrite Plugins
Sveta Smirnova
 
Mysql DBI
Joe Christensen
 
Php and MySQL Web Development
w3ondemand
 
Lect_04b_PhpMysqlKEY PERFORMANCE INDICATOR FOR ICT-UNIT (new).ppt
SenzotaSemakuwa
 
20141011 mastering mysqlnd
do_aki
 
DPC2007 MySQL Stored Routines for PHP Developers (Roland Bouman)
dpc
 
wee
gapczar
 
Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.
Geir Høydalsvik
 
Memcached Functions For My Sql Seemless Caching In My Sql
MySQLConference
 
Sqlite perl
Ashoka Vanjare
 
Php and database functionality
Sayed Ahmed
 
Php and database functionality
Sayed Ahmed
 
PHP and database functionality
Sayed Ahmed
 
The Ring programming language version 1.3 book - Part 20 of 88
Mahmoud Samir Fayed
 
PHP - Intriduction to MySQL And PHP
Vibrant Technologies & Computers
 
Database Programming with Perl and DBIx::Class
Dave Cross
 
Libmysqld Introduction
papablues
 
Introducing new SQL syntax and improving performance with preparse Query Rewr...
Sveta Smirnova
 
Ad

Recently uploaded (20)

PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Ad

Perl Stored Procedures for MySQL (2009)

  • 1. Perl Stored Procedures for MySQL Using the Perl plugin for the External Language Stored Procedure framework. Antony T Curtis <[email protected]>
  • 2. Why Perl? CPAN "Multiplicity" / Thread-friendly Lots of code already written Clean APIs - example: DBI MySQL UDFs have no access control MySQL UDFs cannot execute dynamic SQL Faster than MySQL's SQL Stored Procedures Complex code uses less memory than MySQL's SQL SP
  • 4. Use the source, Download the source tarball from https://launchpad.net/mysql-wl820/trunk/5.1.33-wl820 # configure and build BUILD/compile-pentium-debug-max-no-ndb --no-tune-cpu --prefix=<install path> # install make install # initialize db cd <install path> ; bin/mysql_install_db
  • 5. Preparing for the first time Three ways to prepare the mysqld for Perl: 1. INSTALL PLUGIN Perl SONAME "psm_perl.so"; 2. command line mysqld parameters $ libexec/mysqld --pluginload=perl=psm_perl.so 3. within the my.cnf config file [mysqld] plugin-load=perl=psm_perl.so
  • 6. "Hello World" mysql> CREATE PROCEDURE PerlHello() -> DYNAMIC RESULT SETS 1 -> NO SQL -> LANGUAGE Perl -> EXTERNAL NAME "HelloWorld::test"; Query OK, 0 rows affected (0.00 sec) mysql> CALL PerlHello; +-----------------------+ | message | +-----------------------+ | Hello World from Perl | +-----------------------+ 1 row in set (0.02 sec) Query OK, 0 rows affected (0.03 sec) package HelloWorld; # put this file in <prefix>/lib/mysql/perl use 5.008008; use strict; use warnings; use Symbol qw(delete_package); require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw( ); our @EXPORT = qw( test ); our $VERSION = '0.01';     sub test() {   return {     'message' => 'Hello World from Perl',   }; }   1;
  • 7. Features Uses Perl prototypes for simplicity threads don't block each other pass by value (IN) parameters pass-by-reference (INOUT/OUT) parameters simple auto-casting of values functions have simple single value result procedure may return single result set array of hash array of array hash result (single row) array result (single row)
  • 8. Features, part 2 detects if Perl module has changed and reloads dynamic SQL support using DBI and DBD::mysql READS SQL DATA attribute forces read-only mode NO SQL attribute blocks inline dynamic SQL. executes dynamic SQL in same thread/transaction die returns error message up to the client routines may be used in triggers and events procedures may be used as table functions NOTE: Unmodified MySQL does not have table functions, yet! No dynamic SQL support from triggers, table funcs or events.
  • 9. Table Functions (example) mysql> CREATE FUNCTION HelloFunc() -> RETURNS TABLE ( -> message VARCHAR(64) -> ) -> NO SQL -> LANGUAGE Perl -> EXTERNAL NAME "HelloWorld::test"; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM TABLE(HelloFunc) AS HelloFunc; +-----------------------+ | message | +-----------------------+ | Hello World from Perl | +-----------------------+ 1 row in set (0.01 sec) Note: Correlation name is mandatory for table functions.
  • 11. Limitations and gotchas routines must not fork or create new threads bad things happen - usually crashes. functions cannot access tables with dynamic SQL limitation of MySQL's open_and_lock internals cannot detect changes in dependent modules force reload by touching module time-stamp currently,no way to flush MySQL procedure/function cache client connection must disconnect to 'forget' Runs "in-process" with mysqld can cause mysqld to die from OOM error can potentially corrupt mysqld internal data
  • 12. Future Directions Inline declaration of Perl code Store code in database "Fenced" mode Perl modules would load in separate process likely to use "process per user or security context" "Atomic" routines create SAVEPOINT before entering routine ROLLBACK SAVEPOINT on error. Fix/modify MySQL's table opening code (unlikely) allow routines to open additional tables in read mode would allow routines to query database without nonstandard SQL syntax hacks
  • 13. Resources... LaunchPad project https://launchpad.net/sakila-server/wl820 Source tarballs https://launchpad.net/mysql-wl820/trunk/5.1.33-wl820 Perl sources used in demo will be uploaded. Modified PIE LaunchPad project https://code.launchpad.net/~atcurtis/pie/devel
  • 15. More Perl Examples # # # # # # # CREATE FUNCTION error_check( error_code INT, error_message VARCHAR(512) ) RETURNS INT NO SQL LANGUAGE Perl EXTERNAL NAME "...::error_check" sub error_check($$) { my ($errcode, $errmsg) = @_; die "[$errcode] $errmsg" if $errcode; return $errcode; } # # # # CREATE PROCEDURE check_data() READS SQL DATA LANGUAGE Perl EXTERNAL NAME "...::check_data" sub check_data() { my $dbh = DBI->connect( "dbi:mysql:test", undef, undef) or die "Failed to connect"; # attempts to update/write data # using this connection will fail. ... }
  • 16. More Perl Examples, part 2 # # # # # # # CREATE FUNCTION my_reverse( string TEXT ) RETURNS TEXT NO SQL LANGUAGE Perl EXTERNAL NAME "MyUDFExample::my_reverse"; sub my_reverse($) { my($string)= @_; # print "foon"; return reverse $string; } # # # # # # # CREATE PROCEDURE my_reverseproc( INOUT string TEXT ) NO SQL LANGUAGE Perl EXTERNAL NAME "MyUDFExample::my_reverseproc"; sub my_reverseproc($) { my($stringref)= @_; $$stringref= reverse $$stringref; # no resultsets return undef; }