SlideShare a Scribd company logo
1 of 29
Download to read offline
PL/Perl New Features
    in PostgreSQL 9.0


     Tim Bunce - June 2010
     Creative Commons BY-NC-SA 3.0
PL/Perl Changes

   ‣ USER
   ‣ INTERNAL
   ‣ DBA
   ‣ NYTPROF
PL/Perl Changes

   ‣ USER
   ‣ INTERNAL
   ‣ DBA
   ‣ NYTPROF
New Builtins
quote_...

 quote_literal( "foo"            ) ==> "'foo'"
 quote_literal( "don't "carp"" ) ==> "'don''t "carp"'"
 quote_literal( ""               ) ==> "''"


quote_nullable( "foo"            ) ==> "'foo'"
quote_nullable( "don't "carp"" ) ==> "'don''t "carp"'"
quote_nullable( ""               ) ==> "''"


quote_ident( "foo"            ) ==> "foo"
quote_ident( "don't "carp"" ) ==> ""don't ""carp""""
quote_ident( ""               ) ==> """"
quote_...(undef)


 quote_literal( undef   ) ==> undef


quote_nullable( undef   ) ==> "NULL"


   quote_ident( undef   ) ==> """"   (warn)
{en,de}code_bytea
encode_bytea( "foo"            ) ==> "x666f6f"
decode_bytea( "x666f6f"      ) ==> "foo"
decode_bytea( "146157157") ==> "foo"


encode_bytea( "x{263a}"       ) ==> "xe298ba"         UTF8
decode_bytea( "xe298ba"      ) ==> "342230272"      Not UTF8


encode_bytea( ""               ) ==> "x"
decode_bytea( "x"            ) ==> ""
decode_bytea( ""               ) ==> ""


encode_bytea( undef            ) ==> "x"      (warn)
decode_bytea( undef            ) ==> ""      (warn)
looks_like_number

looks_like_number( 1           ) ==> 1
looks_like_number( 0           ) ==> 1
looks_like_number( "+7.2e-9"   ) ==> 1


looks_like_number( "foo"       ) ==> 0
looks_like_number( ""          ) ==> 0
looks_like_number( undef       ) ==> undef


looks_like_number( " 4 "       ) ==> 1


looks_like_number( "5plus"     ) ==> 0   (but '5plus'+0=5)
encode_array_*

encode_array_literal( ["foo","bar"] )
   ==> "{"foo", "bar"}"


encode_array_constructor( ["foo","bar"] )
   ==> "ARRAY['foo', 'bar']"



encode_array_literal( [1,[2,[undef]]]   )
   ==> "{"1", {"2", {NULL}}}"


encode_array_constructor( [1,[2,[undef]]]   )
   ==> "ARRAY['1', ARRAY['2', ARRAY[NULL]]]"
encode_array_*


encode_array_literal(       "foo"   ) ==> "foo"
encode_array_constructor(   "foo"   ) ==> "'foo'"


encode_array_literal(       undef   ) ==> undef
encode_array_constructor(   undef   ) ==> "NULL"
Trusted require/use
• require/use work for already loaded modules
use strict;            # old way: BEGIN { strict->import(); }



• extra pre-loaded modules
use warnings;
use Carp;
use feature qw(say);   # for perl 5.10 or later
use utf8;              # if server_encoding is utf8
CONTEXT: ...

• PL/Perl tracks the context of log messages
  - before:
    WARNING: ...some warning from perl code...


  - now:
    WARNING: ...some warning from perl code...
    CONTEXT: PL/Perl function "..."
DO '...' LANGUAGE plperl;

• Arbitrary chunks of perl code can be executed
    directly from psql, or client apps, via DO
•   No need to create and run a stored procedure
    each time.

    DO $$
        spi_exec("... $_ ...") for 'a'..'z';
    $$ language plperl;
Other Changes
• Using $a and $b in sort blocks now works!
• eval { ... } and eval "..."
  - can now be used in plperl
• END blocks are now run at end of session
  - they can't (currently) access the database
• Warnings from perl are now WARNINGs
  - they used to be NOTICE
PL/Perl Changes

   ‣ USER
   ‣ INTERNAL
   ‣ DBA
   ‣ NYTPROF
INTERNAL

• The Safe module is no longer used for plperl
  - Improved security and reduced call overheads
  - Upgrade to latest security patch!
• Validates return values are in server encoding
  - ERROR: invalid byte sequence for encoding
• Internal code refactoring and cleanup
PL/Perl Changes

   ‣ USER
   ‣ INTERNAL
   ‣ DBA
   ‣ NYTPROF
New plperl.* Config
• Specify perl code to run during initialization:

  plperl.on_init            = '...perl...'
  plperl.on_plperlu_init = '...perl...'
  plperl.on_plperl_init     = '...perl...'


• Can only be set by superuser or postgres.conf
• Code can't access the database
~ Timeline ~
▾   Perl interpreter created on demand
    ▿ Options from PERL5OPT env var are processed
    ▿ PL/Perl support code bootstraps
    ▿ plperl.on_init code runs
      Above may happen in postmaster at startup if
      plperl is loaded via shared_preload_libraries
▾ Interpreter is specialised for plperl (or plperlu)
    ▿ Modules loaded: strict, warnings, features, Carp
    ▿ Perl operators are restricted (require, open etc.)
    ▿ DynaLoader package is deleted
    ▿ plperl.on_plperl_init code runs
    ▿ Database access is enabled
plperl.on_init

• Handy to set global perl configuation
  plperl.on_init='use lib qw(/myapp); use ...;'
  plperl.on_init='require "plperloninit.pl";'


• SECURITY RISK!
  Only load modules you're happy for plperl code to use!
  Also check any other modules loaded as dependencies!
  Use Devel::TraceLoad to see what's actually loaded:
  PERL5OPT='-MDevel::Trace=summary' pg_ctl ...
PL/Perl Best Practice

•   Include explicit use statements in functions
•   Don't assume modules have been pre-loaded
•   For plperlu that'll actually load if needed
•   For plperl it'll check that module was loaded
    - so you'll get an immediate clear failure if not
    - for example on a replica with old postgres.conf file
plperl.on_plperl_init

• Originally intended for things like
  -   PGOPTIONS="-c plperl.on_plperl_init='...'"

  - to enable debug or profiling for a session
• But...
• Can only be set by superuser or postgres.conf
  - sadly, due to SECURITY DEFINER risks.
  - You shouldn't write SECURITY DEFINER
    functions in plperl if untrusted users can use plperl!
PL/Perl Changes

   ‣ USER
   ‣ INTERNAL
   ‣ DBA
   ‣ NYTPROF
Devel::NYTProf
Perl Source Code Profiler


PostgreSQL::PLPerl::NYTProf
Enabling NYTProf
• Via postgres.conf:
  plperl.on_init='use PostgreSQL::PLPerl::NYTProf'



• Via environment variable:
  PERL5OPT='-MPostgreSQL::PLPerl::NYTProf' pg_ctl ...



• Immediately active. To enable on demand:
  NYTPROF=start=no PERL5OPT=... pg_ctl ...
  DO 'DB::enable_profile' LANGUAGE plperl;
Reporting from NYTProf

• Writes per-backend data files:
  $PGDATA/nytprof.out.$pid



• To generate a report:
  nytprofhtml --file=$PGDATA/nytprof.out.4321 --open
~ Demo ~
                Screencast: http://timbunce.blip.tv
Video: http://www.fosslc.org/drupal/content/plperl-new-features-90
Questions?

Tim.Bunce@pobox.com
http://blog.timbunce.org
 @timbunce on twitter
http://xkcd.com/519/

More Related Content

What's hot

On UnQLite
On UnQLiteOn UnQLite
On UnQLitecharsbar
 
RestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueRestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueGleicon Moraes
 
Bottom to Top Stack Optimization - CICON2011
Bottom to Top Stack Optimization - CICON2011Bottom to Top Stack Optimization - CICON2011
Bottom to Top Stack Optimization - CICON2011CodeIgniter Conference
 
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)julien pauli
 
typemap in Perl/XS
typemap in Perl/XS  typemap in Perl/XS
typemap in Perl/XS charsbar
 
Redis as a message queue
Redis as a message queueRedis as a message queue
Redis as a message queueBrandon Lamb
 
AnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webAnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webclkao
 
What you need to remember when you upload to CPAN
What you need to remember when you upload to CPANWhat you need to remember when you upload to CPAN
What you need to remember when you upload to CPANcharsbar
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7julien pauli
 
How to Develop Puppet Modules: From Source to the Forge With Zero Clicks
How to Develop Puppet Modules: From Source to the Forge With Zero ClicksHow to Develop Puppet Modules: From Source to the Forge With Zero Clicks
How to Develop Puppet Modules: From Source to the Forge With Zero ClicksCarlos Sanchez
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionjulien pauli
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scriptingTony Fabeen
 
Using ngx_lua in UPYUN
Using ngx_lua in UPYUNUsing ngx_lua in UPYUN
Using ngx_lua in UPYUNCong Zhang
 
Doing It Wrong with Puppet -
Doing It Wrong with Puppet - Doing It Wrong with Puppet -
Doing It Wrong with Puppet - Puppet
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objectsjulien pauli
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationrjsmelo
 
Lightweight wrapper for Hive on Amazon EMR
Lightweight wrapper for Hive on Amazon EMRLightweight wrapper for Hive on Amazon EMR
Lightweight wrapper for Hive on Amazon EMRShinji Tanaka
 

What's hot (20)

On UnQLite
On UnQLiteOn UnQLite
On UnQLite
 
RestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueRestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message Queue
 
Bottom to Top Stack Optimization - CICON2011
Bottom to Top Stack Optimization - CICON2011Bottom to Top Stack Optimization - CICON2011
Bottom to Top Stack Optimization - CICON2011
 
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)
 
typemap in Perl/XS
typemap in Perl/XS  typemap in Perl/XS
typemap in Perl/XS
 
Redis as a message queue
Redis as a message queueRedis as a message queue
Redis as a message queue
 
AnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webAnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time web
 
What you need to remember when you upload to CPAN
What you need to remember when you upload to CPANWhat you need to remember when you upload to CPAN
What you need to remember when you upload to CPAN
 
High Performance tDiary
High Performance tDiaryHigh Performance tDiary
High Performance tDiary
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7
 
How to Develop Puppet Modules: From Source to the Forge With Zero Clicks
How to Develop Puppet Modules: From Source to the Forge With Zero ClicksHow to Develop Puppet Modules: From Source to the Forge With Zero Clicks
How to Develop Puppet Modules: From Source to the Forge With Zero Clicks
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extension
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scripting
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
 
Using ngx_lua in UPYUN
Using ngx_lua in UPYUNUsing ngx_lua in UPYUN
Using ngx_lua in UPYUN
 
Doing It Wrong with Puppet -
Doing It Wrong with Puppet - Doing It Wrong with Puppet -
Doing It Wrong with Puppet -
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
 
PHP5.5 is Here
PHP5.5 is HerePHP5.5 is Here
PHP5.5 is Here
 
Lightweight wrapper for Hive on Amazon EMR
Lightweight wrapper for Hive on Amazon EMRLightweight wrapper for Hive on Amazon EMR
Lightweight wrapper for Hive on Amazon EMR
 

Similar to PL/Perl - New Features in PostgreSQL 9.0

Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlHideaki Ohno
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersKirk Kimmel
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-linersdaoswald
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)CODE BLUE
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In PerlKang-min Liu
 
Performance Profiling in Rust
Performance Profiling in RustPerformance Profiling in Rust
Performance Profiling in RustInfluxData
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationAttila Balazs
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsPierre MARTIN
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteBram Vogelaar
 
Learning Puppet basic thing
Learning Puppet basic thing Learning Puppet basic thing
Learning Puppet basic thing DaeHyung Lee
 
Apache Hadoop Shell Rewrite
Apache Hadoop Shell RewriteApache Hadoop Shell Rewrite
Apache Hadoop Shell RewriteAllen Wittenauer
 
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+ConFoo
 
What's new in Perl 5.10?
What's new in Perl 5.10?What's new in Perl 5.10?
What's new in Perl 5.10?acme
 

Similar to PL/Perl - New Features in PostgreSQL 9.0 (20)

Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
SPL, not a bridge too far
SPL, not a bridge too farSPL, not a bridge too far
SPL, not a bridge too far
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-liners
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Performance Profiling in Rust
Performance Profiling in RustPerformance Profiling in Rust
Performance Profiling in Rust
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suite
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Learning Puppet basic thing
Learning Puppet basic thing Learning Puppet basic thing
Learning Puppet basic thing
 
Apache Hadoop Shell Rewrite
Apache Hadoop Shell RewriteApache Hadoop Shell Rewrite
Apache Hadoop Shell Rewrite
 
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
 
Puppet @ Seat
Puppet @ SeatPuppet @ Seat
Puppet @ Seat
 
Os Treat
Os TreatOs Treat
Os Treat
 
Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
 
What's new in Perl 5.10?
What's new in Perl 5.10?What's new in Perl 5.10?
What's new in Perl 5.10?
 

More from Tim Bunce

Devel::NYTProf v5 at YAPC::NA 201406
Devel::NYTProf v5 at YAPC::NA 201406Devel::NYTProf v5 at YAPC::NA 201406
Devel::NYTProf v5 at YAPC::NA 201406Tim Bunce
 
Perl Memory Use 201207 (OUTDATED, see 201209 )
Perl Memory Use 201207 (OUTDATED, see 201209 )Perl Memory Use 201207 (OUTDATED, see 201209 )
Perl Memory Use 201207 (OUTDATED, see 201209 )Tim Bunce
 
Perl Dist::Surveyor 2011
Perl Dist::Surveyor 2011Perl Dist::Surveyor 2011
Perl Dist::Surveyor 2011Tim Bunce
 
Perl6 DBDI YAPC::EU 201008
Perl6 DBDI YAPC::EU 201008Perl6 DBDI YAPC::EU 201008
Perl6 DBDI YAPC::EU 201008Tim Bunce
 
Perl 6 DBDI 201007 (OUTDATED, see 201008)
Perl 6 DBDI 201007 (OUTDATED, see 201008)Perl 6 DBDI 201007 (OUTDATED, see 201008)
Perl 6 DBDI 201007 (OUTDATED, see 201008)Tim Bunce
 
DBI Advanced Tutorial 2007
DBI Advanced Tutorial 2007DBI Advanced Tutorial 2007
DBI Advanced Tutorial 2007Tim Bunce
 
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Tim Bunce
 
Perl Myths 200909
Perl Myths 200909Perl Myths 200909
Perl Myths 200909Tim Bunce
 
DashProfiler 200807
DashProfiler 200807DashProfiler 200807
DashProfiler 200807Tim Bunce
 
DBI for Parrot and Perl 6 Lightning Talk 2007
DBI for Parrot and Perl 6 Lightning Talk 2007DBI for Parrot and Perl 6 Lightning Talk 2007
DBI for Parrot and Perl 6 Lightning Talk 2007Tim Bunce
 
Devel::NYTProf 2009-07 (OUTDATED, see 201008)
Devel::NYTProf 2009-07 (OUTDATED, see 201008)Devel::NYTProf 2009-07 (OUTDATED, see 201008)
Devel::NYTProf 2009-07 (OUTDATED, see 201008)Tim Bunce
 
Perl Myths 200802 with notes (OUTDATED, see 200909)
Perl Myths 200802 with notes (OUTDATED, see 200909)Perl Myths 200802 with notes (OUTDATED, see 200909)
Perl Myths 200802 with notes (OUTDATED, see 200909)Tim Bunce
 

More from Tim Bunce (12)

Devel::NYTProf v5 at YAPC::NA 201406
Devel::NYTProf v5 at YAPC::NA 201406Devel::NYTProf v5 at YAPC::NA 201406
Devel::NYTProf v5 at YAPC::NA 201406
 
Perl Memory Use 201207 (OUTDATED, see 201209 )
Perl Memory Use 201207 (OUTDATED, see 201209 )Perl Memory Use 201207 (OUTDATED, see 201209 )
Perl Memory Use 201207 (OUTDATED, see 201209 )
 
Perl Dist::Surveyor 2011
Perl Dist::Surveyor 2011Perl Dist::Surveyor 2011
Perl Dist::Surveyor 2011
 
Perl6 DBDI YAPC::EU 201008
Perl6 DBDI YAPC::EU 201008Perl6 DBDI YAPC::EU 201008
Perl6 DBDI YAPC::EU 201008
 
Perl 6 DBDI 201007 (OUTDATED, see 201008)
Perl 6 DBDI 201007 (OUTDATED, see 201008)Perl 6 DBDI 201007 (OUTDATED, see 201008)
Perl 6 DBDI 201007 (OUTDATED, see 201008)
 
DBI Advanced Tutorial 2007
DBI Advanced Tutorial 2007DBI Advanced Tutorial 2007
DBI Advanced Tutorial 2007
 
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
 
Perl Myths 200909
Perl Myths 200909Perl Myths 200909
Perl Myths 200909
 
DashProfiler 200807
DashProfiler 200807DashProfiler 200807
DashProfiler 200807
 
DBI for Parrot and Perl 6 Lightning Talk 2007
DBI for Parrot and Perl 6 Lightning Talk 2007DBI for Parrot and Perl 6 Lightning Talk 2007
DBI for Parrot and Perl 6 Lightning Talk 2007
 
Devel::NYTProf 2009-07 (OUTDATED, see 201008)
Devel::NYTProf 2009-07 (OUTDATED, see 201008)Devel::NYTProf 2009-07 (OUTDATED, see 201008)
Devel::NYTProf 2009-07 (OUTDATED, see 201008)
 
Perl Myths 200802 with notes (OUTDATED, see 200909)
Perl Myths 200802 with notes (OUTDATED, see 200909)Perl Myths 200802 with notes (OUTDATED, see 200909)
Perl Myths 200802 with notes (OUTDATED, see 200909)
 

Recently uploaded

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

PL/Perl - New Features in PostgreSQL 9.0

  • 1. PL/Perl New Features in PostgreSQL 9.0 Tim Bunce - June 2010 Creative Commons BY-NC-SA 3.0
  • 2. PL/Perl Changes ‣ USER ‣ INTERNAL ‣ DBA ‣ NYTPROF
  • 3. PL/Perl Changes ‣ USER ‣ INTERNAL ‣ DBA ‣ NYTPROF
  • 5. quote_... quote_literal( "foo" ) ==> "'foo'" quote_literal( "don't "carp"" ) ==> "'don''t "carp"'" quote_literal( "" ) ==> "''" quote_nullable( "foo" ) ==> "'foo'" quote_nullable( "don't "carp"" ) ==> "'don''t "carp"'" quote_nullable( "" ) ==> "''" quote_ident( "foo" ) ==> "foo" quote_ident( "don't "carp"" ) ==> ""don't ""carp"""" quote_ident( "" ) ==> """"
  • 6. quote_...(undef) quote_literal( undef ) ==> undef quote_nullable( undef ) ==> "NULL" quote_ident( undef ) ==> """" (warn)
  • 7. {en,de}code_bytea encode_bytea( "foo" ) ==> "x666f6f" decode_bytea( "x666f6f" ) ==> "foo" decode_bytea( "146157157") ==> "foo" encode_bytea( "x{263a}" ) ==> "xe298ba" UTF8 decode_bytea( "xe298ba" ) ==> "342230272" Not UTF8 encode_bytea( "" ) ==> "x" decode_bytea( "x" ) ==> "" decode_bytea( "" ) ==> "" encode_bytea( undef ) ==> "x" (warn) decode_bytea( undef ) ==> "" (warn)
  • 8. looks_like_number looks_like_number( 1 ) ==> 1 looks_like_number( 0 ) ==> 1 looks_like_number( "+7.2e-9" ) ==> 1 looks_like_number( "foo" ) ==> 0 looks_like_number( "" ) ==> 0 looks_like_number( undef ) ==> undef looks_like_number( " 4 " ) ==> 1 looks_like_number( "5plus" ) ==> 0 (but '5plus'+0=5)
  • 9. encode_array_* encode_array_literal( ["foo","bar"] ) ==> "{"foo", "bar"}" encode_array_constructor( ["foo","bar"] ) ==> "ARRAY['foo', 'bar']" encode_array_literal( [1,[2,[undef]]] ) ==> "{"1", {"2", {NULL}}}" encode_array_constructor( [1,[2,[undef]]] ) ==> "ARRAY['1', ARRAY['2', ARRAY[NULL]]]"
  • 10. encode_array_* encode_array_literal( "foo" ) ==> "foo" encode_array_constructor( "foo" ) ==> "'foo'" encode_array_literal( undef ) ==> undef encode_array_constructor( undef ) ==> "NULL"
  • 11. Trusted require/use • require/use work for already loaded modules use strict; # old way: BEGIN { strict->import(); } • extra pre-loaded modules use warnings; use Carp; use feature qw(say); # for perl 5.10 or later use utf8; # if server_encoding is utf8
  • 12. CONTEXT: ... • PL/Perl tracks the context of log messages - before: WARNING: ...some warning from perl code... - now: WARNING: ...some warning from perl code... CONTEXT: PL/Perl function "..."
  • 13. DO '...' LANGUAGE plperl; • Arbitrary chunks of perl code can be executed directly from psql, or client apps, via DO • No need to create and run a stored procedure each time. DO $$ spi_exec("... $_ ...") for 'a'..'z'; $$ language plperl;
  • 14. Other Changes • Using $a and $b in sort blocks now works! • eval { ... } and eval "..." - can now be used in plperl • END blocks are now run at end of session - they can't (currently) access the database • Warnings from perl are now WARNINGs - they used to be NOTICE
  • 15. PL/Perl Changes ‣ USER ‣ INTERNAL ‣ DBA ‣ NYTPROF
  • 16. INTERNAL • The Safe module is no longer used for plperl - Improved security and reduced call overheads - Upgrade to latest security patch! • Validates return values are in server encoding - ERROR: invalid byte sequence for encoding • Internal code refactoring and cleanup
  • 17. PL/Perl Changes ‣ USER ‣ INTERNAL ‣ DBA ‣ NYTPROF
  • 18. New plperl.* Config • Specify perl code to run during initialization: plperl.on_init = '...perl...' plperl.on_plperlu_init = '...perl...' plperl.on_plperl_init = '...perl...' • Can only be set by superuser or postgres.conf • Code can't access the database
  • 19. ~ Timeline ~ ▾ Perl interpreter created on demand ▿ Options from PERL5OPT env var are processed ▿ PL/Perl support code bootstraps ▿ plperl.on_init code runs Above may happen in postmaster at startup if plperl is loaded via shared_preload_libraries ▾ Interpreter is specialised for plperl (or plperlu) ▿ Modules loaded: strict, warnings, features, Carp ▿ Perl operators are restricted (require, open etc.) ▿ DynaLoader package is deleted ▿ plperl.on_plperl_init code runs ▿ Database access is enabled
  • 20. plperl.on_init • Handy to set global perl configuation plperl.on_init='use lib qw(/myapp); use ...;' plperl.on_init='require "plperloninit.pl";' • SECURITY RISK! Only load modules you're happy for plperl code to use! Also check any other modules loaded as dependencies! Use Devel::TraceLoad to see what's actually loaded: PERL5OPT='-MDevel::Trace=summary' pg_ctl ...
  • 21. PL/Perl Best Practice • Include explicit use statements in functions • Don't assume modules have been pre-loaded • For plperlu that'll actually load if needed • For plperl it'll check that module was loaded - so you'll get an immediate clear failure if not - for example on a replica with old postgres.conf file
  • 22. plperl.on_plperl_init • Originally intended for things like - PGOPTIONS="-c plperl.on_plperl_init='...'" - to enable debug or profiling for a session • But... • Can only be set by superuser or postgres.conf - sadly, due to SECURITY DEFINER risks. - You shouldn't write SECURITY DEFINER functions in plperl if untrusted users can use plperl!
  • 23. PL/Perl Changes ‣ USER ‣ INTERNAL ‣ DBA ‣ NYTPROF
  • 24. Devel::NYTProf Perl Source Code Profiler PostgreSQL::PLPerl::NYTProf
  • 25. Enabling NYTProf • Via postgres.conf: plperl.on_init='use PostgreSQL::PLPerl::NYTProf' • Via environment variable: PERL5OPT='-MPostgreSQL::PLPerl::NYTProf' pg_ctl ... • Immediately active. To enable on demand: NYTPROF=start=no PERL5OPT=... pg_ctl ... DO 'DB::enable_profile' LANGUAGE plperl;
  • 26. Reporting from NYTProf • Writes per-backend data files: $PGDATA/nytprof.out.$pid • To generate a report: nytprofhtml --file=$PGDATA/nytprof.out.4321 --open
  • 27. ~ Demo ~ Screencast: http://timbunce.blip.tv Video: http://www.fosslc.org/drupal/content/plperl-new-features-90