SlideShare a Scribd company logo
PL/Perl New Features
    in PostgreSQL 9.0


      Tim Bunce - Dec 2010
     Creative Commons BY-NC-SA 3.0
PL/Perl Changes
                 ‣ USER
                 ‣ INTERNAL
                 ‣ DBA
                 ‣ NYTPROF
                 ‣ FUTURE
Some features are in recent 8.x.y due to back-porting of security changes
PL/Perl Changes
   ‣ USER
   ‣ INTERNAL
   ‣ DBA
   ‣ NYTPROF
   ‣ FUTURE
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 "..."


  -   Thanks to Alexey Klyukin.
DO '...' LANGUAGE ...;

• Arbitrary chunks of code can be executed
  directly from psql, or client apps, via DO
   -   Thanks to Petr Jelinek, Joshua Tolley, Hannu Valtonen


• 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 "..." now work!
• 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
   ‣ FUTURE
INTERNAL
• The Safe module is no longer used for plperl
  - Now faster, simpler, and more secure


• Validates return values are in server encoding
  - ERROR: invalid byte sequence for encoding
  - Thanks to Andrew Dunstan


• Internal code refactoring and cleanup
PL/Perl Changes
   ‣ USER
   ‣ INTERNAL
   ‣ DBA
   ‣ NYTPROF
   ‣ FUTURE
New plperl.* Config
• Specify perl code to run during initialization:

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


• Can only be set by superuser or postgres.conf
• Code can't access the database
Interpreter Lifecycle
Birth
1. Perl interpreter created
2. Options from PERL5OPT env var are processed
3. PL/Perl support bootstrap code is executed
4.   plperl.on_init   code runs (unrestricted)


Above steps may happen in postmaster process at startup,
if plperl is loaded via shared_preload_libraries.
Otherwise they happen at first use.
No access to database.
Adolescence
6. Interpreter is specialised for plperl (if that’s used first)
    •   Modules loaded: strict, warnings, features, Carp
    •   Unsafe perl ops are restricted (require, open etc.)
    •   DynaLoader package is deleted
    •   plperl.on_plperl_init code runs (restricted)

7. Database access is enabled
8. Perl interpreter is made available for use
9. Executes whatever action called it into existence
Siblings
• If plplerlu code is run later
    - then a new interpreter is created
    - similarly if plperlu is run first and plperl run later
• If plplerl with a different security context is run
    - then a new interpreter is created for the ROLE
    - That’s a recent security fix:
       http://wiki.postgresql.org/wiki/20101005securityrelease
Note impact on shared_preload_libraries in these cases
Death


• Finally, when the session ends:
   - Access to the database is disabled
   - END blocks, if any, are run (if exiting cleanly)
plperl.on_init
• Handy to set global perl configuation
  plperl.on_init='use lib qw(/myapp); use ...;'
  plperl.on_init='require "plperloninit.pl";'
  Effectively   defines ‘approved’ modules for       plperl


• 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

  For plperlu that'll actually load if needed
  For plperl it'll check that module is loaded
  - so you'll get an immediate clear failure if not
  - (e.g., 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
  - due to SECURITY DEFINER risk at the time
  - that’s now been patched (20101005, CVE-2010-3433)
  - so this restriction may be removed in future
PL/Perl Changes
   ‣ USER
   ‣ INTERNAL
   ‣ DBA
   ‣ NYTPROF
   ‣ FUTURE
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 ...



• Is immediately active for all connections.
• To enable on demand for one connection:
  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 ~
              Of plperl.on_init in postgresql.conf
            And use of PostgreSQL::PLPerl::NYTProf

          Screencast: http://timbunce.blip.tv/file/3691795/
Video: http://www.fosslc.org/drupal/content/plperl-new-features-90
PL/Perl Changes
   ‣ USER
   ‣ INTERNAL
   ‣ DBA
   ‣ NYTPROF
   ‣ FUTURE
Future Possibilities

•   Optimize trigger handling overheads
•   Remove overheads of threaded perls
•   $array_ref = decode_array_literal(‘{...}’)
•   $hash_ref = decode_hstore_literal(‘x=>42’)
•   Array params as array refs (with overloading)
•   Rewrite encode_array_literal in C.
Questions?

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

More Related Content

What's hot

Top 10 Perl Performance Tips
Top 10 Perl Performance TipsTop 10 Perl Performance Tips
Top 10 Perl Performance Tips
Perrin Harkins
 
DBD::Gofer 200809
DBD::Gofer 200809DBD::Gofer 200809
DBD::Gofer 200809
Tim Bunce
 
Profiling with Devel::NYTProf
Profiling with Devel::NYTProfProfiling with Devel::NYTProf
Profiling with Devel::NYTProf
bobcatfish
 
How to inspect a RUNNING perl process
How to inspect a RUNNING perl processHow to inspect a RUNNING perl process
How to inspect a RUNNING perl process
Masaaki HIROSE
 
On UnQLite
On UnQLiteOn UnQLite
On UnQLite
charsbar
 
typemap in Perl/XS
typemap in Perl/XS  typemap in Perl/XS
typemap in Perl/XS
charsbar
 
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
charsbar
 
RestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueRestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message Queue
Gleicon Moraes
 
Doing It Wrong with Puppet -
Doing It Wrong with Puppet - Doing It Wrong with Puppet -
Doing It Wrong with Puppet -
Puppet
 
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
 
Redis as a message queue
Redis as a message queueRedis as a message queue
Redis as a message queue
Brandon Lamb
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scriptingTony Fabeen
 
Unit Testing Lots of Perl
Unit Testing Lots of PerlUnit Testing Lots of Perl
Unit Testing Lots of Perl
Workhorse Computing
 
Using ngx_lua in UPYUN
Using ngx_lua in UPYUNUsing ngx_lua in UPYUN
Using ngx_lua in UPYUN
Cong Zhang
 
Tuning Solr for Logs
Tuning Solr for LogsTuning Solr for Logs
Tuning Solr for Logs
Sematext Group, Inc.
 
Better detection of what modules are used by some Perl 5 code
Better detection of what modules are used by some Perl 5 codeBetter detection of what modules are used by some Perl 5 code
Better detection of what modules are used by some Perl 5 code
charsbar
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
Workhorse Computing
 
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
rjsmelo
 
Roll Your Own API Management Platform with nginx and Lua
Roll Your Own API Management Platform with nginx and LuaRoll Your Own API Management Platform with nginx and Lua
Roll Your Own API Management Platform with nginx and Lua
Jon Moore
 
2016年のPerl (Long version)
2016年のPerl (Long version)2016年のPerl (Long version)
2016年のPerl (Long version)
charsbar
 

What's hot (20)

Top 10 Perl Performance Tips
Top 10 Perl Performance TipsTop 10 Perl Performance Tips
Top 10 Perl Performance Tips
 
DBD::Gofer 200809
DBD::Gofer 200809DBD::Gofer 200809
DBD::Gofer 200809
 
Profiling with Devel::NYTProf
Profiling with Devel::NYTProfProfiling with Devel::NYTProf
Profiling with Devel::NYTProf
 
How to inspect a RUNNING perl process
How to inspect a RUNNING perl processHow to inspect a RUNNING perl process
How to inspect a RUNNING perl process
 
On UnQLite
On UnQLiteOn UnQLite
On UnQLite
 
typemap in Perl/XS
typemap in Perl/XS  typemap in Perl/XS
typemap in Perl/XS
 
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
 
RestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueRestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message Queue
 
Doing It Wrong with Puppet -
Doing It Wrong with Puppet - Doing It Wrong with Puppet -
Doing It Wrong with Puppet -
 
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)
 
Redis as a message queue
Redis as a message queueRedis as a message queue
Redis as a message queue
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scripting
 
Unit Testing Lots of Perl
Unit Testing Lots of PerlUnit Testing Lots of Perl
Unit Testing Lots of Perl
 
Using ngx_lua in UPYUN
Using ngx_lua in UPYUNUsing ngx_lua in UPYUN
Using ngx_lua in UPYUN
 
Tuning Solr for Logs
Tuning Solr for LogsTuning Solr for Logs
Tuning Solr for Logs
 
Better detection of what modules are used by some Perl 5 code
Better detection of what modules are used by some Perl 5 codeBetter detection of what modules are used by some Perl 5 code
Better detection of what modules are used by some Perl 5 code
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
 
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
 
Roll Your Own API Management Platform with nginx and Lua
Roll Your Own API Management Platform with nginx and LuaRoll Your Own API Management Platform with nginx and Lua
Roll Your Own API Management Platform with nginx and Lua
 
2016年のPerl (Long version)
2016年のPerl (Long version)2016年のPerl (Long version)
2016年のPerl (Long version)
 

Viewers also liked

Perl6 DBDI YAPC::EU 201008
Perl6 DBDI YAPC::EU 201008Perl6 DBDI YAPC::EU 201008
Perl6 DBDI YAPC::EU 201008
Tim Bunce
 
Graphic visualization
Graphic visualizationGraphic visualization
Graphic visualizationuwevoelker
 
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 Dist::Surveyor 2011
Perl Dist::Surveyor 2011Perl Dist::Surveyor 2011
Perl Dist::Surveyor 2011
Tim Bunce
 
Perl-Critic
Perl-CriticPerl-Critic
Perl-Critic
Jonas Brømsø
 
Workflow Yapceu2010
Workflow Yapceu2010Workflow Yapceu2010
Workflow Yapceu2010
Jonas Brømsø
 
Stackato v6
Stackato v6Stackato v6
Stackato v6
Jonas Brømsø
 
Perl Tidy Perl Critic
Perl Tidy Perl CriticPerl Tidy Perl Critic
Perl Tidy Perl Critic
olegmmiller
 

Viewers also liked (8)

Perl6 DBDI YAPC::EU 201008
Perl6 DBDI YAPC::EU 201008Perl6 DBDI YAPC::EU 201008
Perl6 DBDI YAPC::EU 201008
 
Graphic visualization
Graphic visualizationGraphic visualization
Graphic visualization
 
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 Dist::Surveyor 2011
Perl Dist::Surveyor 2011Perl Dist::Surveyor 2011
Perl Dist::Surveyor 2011
 
Perl-Critic
Perl-CriticPerl-Critic
Perl-Critic
 
Workflow Yapceu2010
Workflow Yapceu2010Workflow Yapceu2010
Workflow Yapceu2010
 
Stackato v6
Stackato v6Stackato v6
Stackato v6
 
Perl Tidy Perl Critic
Perl Tidy Perl CriticPerl Tidy Perl Critic
Perl Tidy Perl Critic
 

Similar to PL/Perl - New Features in PostgreSQL 9.0 201012

Performance Profiling in Rust
Performance Profiling in RustPerformance Profiling in Rust
Performance Profiling in Rust
InfluxData
 
Alexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for DevelopersAlexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for Developers
DevDay Dresden
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-liners
daoswald
 
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
Kirk Kimmel
 
Puppi. Puppet strings to the shell
Puppi. Puppet strings to the shellPuppi. Puppet strings to the shell
Puppi. Puppet strings to the shell
Alessandro Franceschi
 
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
 
Php 5.6 From the Inside Out
Php 5.6 From the Inside OutPhp 5.6 From the Inside Out
Php 5.6 From the Inside Out
Ferenc Kovács
 
Learning Puppet basic thing
Learning Puppet basic thing Learning Puppet basic thing
Learning Puppet basic thing
DaeHyung Lee
 
PHP5.5 is Here
PHP5.5 is HerePHP5.5 is Here
PHP5.5 is Here
julien pauli
 
Intro to pl/PHP Oscon2007
Intro to pl/PHP Oscon2007Intro to pl/PHP Oscon2007
Intro to pl/PHP Oscon2007
Robert Treat
 
Node.js basics
Node.js basicsNode.js basics
Node.js basicsBen Lin
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
Sanjeev Kumar Jaiswal
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suite
Bram Vogelaar
 
Bare metal performance in Elixir
Bare metal performance in ElixirBare metal performance in Elixir
Bare metal performance in Elixir
Aaron Seigo
 
Null Bachaav - May 07 Attack Monitoring workshop.
Null Bachaav - May 07 Attack Monitoring workshop.Null Bachaav - May 07 Attack Monitoring workshop.
Null Bachaav - May 07 Attack Monitoring workshop.
Prajal Kulkarni
 
Anatomy of a PHP Request ( UTOSC 2010 )
Anatomy of a PHP Request ( UTOSC 2010 )Anatomy of a PHP Request ( UTOSC 2010 )
Anatomy of a PHP Request ( UTOSC 2010 )Joseph Scott
 
Linux configer
Linux configerLinux configer
Linux configer
MD. AL AMIN
 
Introduction to Perl
Introduction to PerlIntroduction to Perl

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

Performance Profiling in Rust
Performance Profiling in RustPerformance Profiling in Rust
Performance Profiling in Rust
 
Alexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for DevelopersAlexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for Developers
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-liners
 
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
 
Puppi. Puppet strings to the shell
Puppi. Puppet strings to the shellPuppi. Puppet strings to the shell
Puppi. Puppet strings to the shell
 
effective_r27
effective_r27effective_r27
effective_r27
 
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)
 
Php 5.6 From the Inside Out
Php 5.6 From the Inside OutPhp 5.6 From the Inside Out
Php 5.6 From the Inside Out
 
Learning Puppet basic thing
Learning Puppet basic thing Learning Puppet basic thing
Learning Puppet basic thing
 
Os Treat
Os TreatOs Treat
Os Treat
 
PHP5.5 is Here
PHP5.5 is HerePHP5.5 is Here
PHP5.5 is Here
 
Intro to pl/PHP Oscon2007
Intro to pl/PHP Oscon2007Intro to pl/PHP Oscon2007
Intro to pl/PHP Oscon2007
 
Node.js basics
Node.js basicsNode.js basics
Node.js basics
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suite
 
Bare metal performance in Elixir
Bare metal performance in ElixirBare metal performance in Elixir
Bare metal performance in Elixir
 
Null Bachaav - May 07 Attack Monitoring workshop.
Null Bachaav - May 07 Attack Monitoring workshop.Null Bachaav - May 07 Attack Monitoring workshop.
Null Bachaav - May 07 Attack Monitoring workshop.
 
Anatomy of a PHP Request ( UTOSC 2010 )
Anatomy of a PHP Request ( UTOSC 2010 )Anatomy of a PHP Request ( UTOSC 2010 )
Anatomy of a PHP Request ( UTOSC 2010 )
 
Linux configer
Linux configerLinux configer
Linux configer
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 

More from Tim 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 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 2007
Tim Bunce
 
Perl Myths 200909
Perl Myths 200909Perl Myths 200909
Perl Myths 200909
Tim Bunce
 
DashProfiler 200807
DashProfiler 200807DashProfiler 200807
DashProfiler 200807
Tim 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 2007
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 (7)

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 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
 
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
 
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

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 

Recently uploaded (20)

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 

PL/Perl - New Features in PostgreSQL 9.0 201012

  • 1. PL/Perl New Features in PostgreSQL 9.0 Tim Bunce - Dec 2010 Creative Commons BY-NC-SA 3.0
  • 2. PL/Perl Changes ‣ USER ‣ INTERNAL ‣ DBA ‣ NYTPROF ‣ FUTURE Some features are in recent 8.x.y due to back-porting of security changes
  • 3. PL/Perl Changes ‣ USER ‣ INTERNAL ‣ DBA ‣ NYTPROF ‣ FUTURE
  • 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 "..." - Thanks to Alexey Klyukin.
  • 13. DO '...' LANGUAGE ...; • Arbitrary chunks of code can be executed directly from psql, or client apps, via DO - Thanks to Petr Jelinek, Joshua Tolley, Hannu Valtonen • 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 "..." now work! • 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 ‣ FUTURE
  • 16. INTERNAL • The Safe module is no longer used for plperl - Now faster, simpler, and more secure • Validates return values are in server encoding - ERROR: invalid byte sequence for encoding - Thanks to Andrew Dunstan • Internal code refactoring and cleanup
  • 17. PL/Perl Changes ‣ USER ‣ INTERNAL ‣ DBA ‣ NYTPROF ‣ FUTURE
  • 18. New plperl.* Config • Specify perl code to run during initialization: plperl.on_init = '...perl code...' plperl.on_plperlu_init = '...perl code...' plperl.on_plperl_init = '...perl code...' • Can only be set by superuser or postgres.conf • Code can't access the database
  • 20. Birth 1. Perl interpreter created 2. Options from PERL5OPT env var are processed 3. PL/Perl support bootstrap code is executed 4. plperl.on_init code runs (unrestricted) Above steps may happen in postmaster process at startup, if plperl is loaded via shared_preload_libraries. Otherwise they happen at first use. No access to database.
  • 21. Adolescence 6. Interpreter is specialised for plperl (if that’s used first) • Modules loaded: strict, warnings, features, Carp • Unsafe perl ops are restricted (require, open etc.) • DynaLoader package is deleted • plperl.on_plperl_init code runs (restricted) 7. Database access is enabled 8. Perl interpreter is made available for use 9. Executes whatever action called it into existence
  • 22. Siblings • If plplerlu code is run later - then a new interpreter is created - similarly if plperlu is run first and plperl run later • If plplerl with a different security context is run - then a new interpreter is created for the ROLE - That’s a recent security fix: http://wiki.postgresql.org/wiki/20101005securityrelease Note impact on shared_preload_libraries in these cases
  • 23. Death • Finally, when the session ends: - Access to the database is disabled - END blocks, if any, are run (if exiting cleanly)
  • 24. plperl.on_init • Handy to set global perl configuation plperl.on_init='use lib qw(/myapp); use ...;' plperl.on_init='require "plperloninit.pl";' Effectively defines ‘approved’ modules for plperl • 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 ...
  • 25. PL/Perl Best Practice • Include explicit use statements in functions For plperlu that'll actually load if needed For plperl it'll check that module is loaded - so you'll get an immediate clear failure if not - (e.g., on a replica with old postgres.conf file)
  • 26. 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 - due to SECURITY DEFINER risk at the time - that’s now been patched (20101005, CVE-2010-3433) - so this restriction may be removed in future
  • 27. PL/Perl Changes ‣ USER ‣ INTERNAL ‣ DBA ‣ NYTPROF ‣ FUTURE
  • 28. Devel::NYTProf Perl Source Code Profiler PostgreSQL::PLPerl::NYTProf
  • 29. Enabling NYTProf • Via postgres.conf: plperl.on_init='use PostgreSQL::PLPerl::NYTProf' • Via environment variable: PERL5OPT='-MPostgreSQL::PLPerl::NYTProf' pg_ctl ... • Is immediately active for all connections. • To enable on demand for one connection: NYTPROF=start=no PERL5OPT=... pg_ctl ... DO 'DB::enable_profile' LANGUAGE plperl;
  • 30. Reporting from NYTProf • Writes per-backend data files: $PGDATA/nytprof.out.$pid • To generate a report: nytprofhtml --file=$PGDATA/nytprof.out.4321 --open
  • 31. ~ Demo ~ Of plperl.on_init in postgresql.conf And use of PostgreSQL::PLPerl::NYTProf Screencast: http://timbunce.blip.tv/file/3691795/ Video: http://www.fosslc.org/drupal/content/plperl-new-features-90
  • 32. PL/Perl Changes ‣ USER ‣ INTERNAL ‣ DBA ‣ NYTPROF ‣ FUTURE
  • 33. Future Possibilities • Optimize trigger handling overheads • Remove overheads of threaded perls • $array_ref = decode_array_literal(‘{...}’) • $hash_ref = decode_hstore_literal(‘x=>42’) • Array params as array refs (with overloading) • Rewrite encode_array_literal in C.