SlideShare a Scribd company logo
1 of 21
Download to read offline
(Bottom Half of Screen)
      Intro to PL/Python

         Josh Williams
                
What is PL/Python?
       Procedural Language INSIDE PostgreSQL
       Not a replacement for SQL
       Run manually (functions / DO in 9.0)
           or automatically (triggers)
                                     
Why PostgreSQL?
       Powerful, yet very fast.
       Feature rich, but you can't beat the price.
       It's future is clear!

                                    
Why Python?
       Easy to read.  Easy to learn.  Easy to use.
       Powerful, yet very fast.
       Possibly already using it outside the database.
       It's Python!!
                                    
Okay, enough crowd pandering
       Step 1: Configure PostgreSQL for Python
       Step 2: Install PL/Python into database(s)
       Step 3: Profit

                                
1. Configure PG for Python
       If using source, `./configure ­­with­python`
       If using packages, install ”­plpython” or ”­py...”
       In either case you need Python installed (duh)

                                  
Is it there?
       SELECT * FROM pg_pltemplate;
       tmplname == plpythonu
           or plpython2u
           or plpython3u
                                   
Wait, u?
       PL/Python is an untrusted language...
       Only DB superusers can create functions :(
       Functions can communicate outside the DB :)

                               
2. Enable Databases
       Command line: createlang plpythonu dbname
       Inside DB: CREATE LANGUAGE plpythonu;
           Hint: Install in template1
       SELECT * FROM pg_language;
                                      
CREATE FUNCTION...
        CREATE FUNCTION function_name (....)
          AS $dollarquote$ ...python here...
          ... $dollarquote$ LANGUAGE plpythonu;
       See ”Intro to PL/pgSQL” for construct details
                                
The ...python here... bit
       Where the interesting Python­y stuff happens
       Simple processing of function parameters
           But also... Import modules (stats, XML, ASCII plot?)
           Interact with the outside world, the inside world
                                     
The web IN your database redux
       Need a type to represent composite RSS data
       Need RSS reader module … feedparser!
       CREATE FUNCTION … RETURNS SETOF …

                              
It works!
       But 11 lines?  This is Python, we can do better!
       There … Two lines (plus the module import)


                                
Something a little more fancy
       Create two really basic functions:
CREATE FUNCTION pydir(text) RETURNS SETOF text...
CREATE FUNCTION isdir(text) RETURNS boolean...
       Call SRF using just SELECT pydir('/')
                                
Enter Recursive Queries (8.4+)
       CTE construct: WITH RECURSIVE …
       Initial value(s) UNION recursive query
       Then SELECT … FROM cte

                                
But that's usual Python stuff
       PL/Python implicitly includes module ”plpy”
           Execute SQL statements
           Generates messages/raises errors back to PG


                                  
Last contrived example
       Basic personal finance application
       Add a trigger: Net balance must be positive


                               
Two Step Process
       CREATE FUNCTION … RETURNS trigger
       CREATE TRIGGER accounts_check AFTER
           INSERT OR UPDATE ON accounts FOR EACH
           STATEMENT EXECUTE PROCEDURE accheck();
                               
Boooring
       Wait, we're using Python.  We can do more!
       How about an early warning email?
           See package smtplib

                                   
That's PL/Python


       Any questions?


               
Intro to PL/Python

    http://joshwilliams.name/plpython/ (Now!)

     In the mean time: joshwilliams@ij.net
                         

More Related Content

What's hot

Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and PackagesDamian T. Gordon
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1Nicholas I
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in pythonSantosh Verma
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Paige Bailey
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in pythonJothi Thilaga P
 
Python's magic methods
Python's magic methodsPython's magic methods
Python's magic methodsReuven Lerner
 
What Are Python Modules? Edureka
What Are Python Modules? EdurekaWhat Are Python Modules? Edureka
What Are Python Modules? EdurekaEdureka!
 

What's hot (20)

Python libraries
Python librariesPython libraries
Python libraries
 
Python
PythonPython
Python
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1
 
Python Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math modulePython Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math module
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 
SFDC Batch Apex
SFDC Batch ApexSFDC Batch Apex
SFDC Batch Apex
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
Python
PythonPython
Python
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
Python made easy
Python made easy Python made easy
Python made easy
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in python
 
Python's magic methods
Python's magic methodsPython's magic methods
Python's magic methods
 
Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String FormattingPython Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String Formatting
 
C++ How to program
C++ How to programC++ How to program
C++ How to program
 
Multiprocessing in python
Multiprocessing in pythonMultiprocessing in python
Multiprocessing in python
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
What Are Python Modules? Edureka
What Are Python Modules? EdurekaWhat Are Python Modules? Edureka
What Are Python Modules? Edureka
 
Python 2 vs. Python 3
Python 2 vs. Python 3Python 2 vs. Python 3
Python 2 vs. Python 3
 

Similar to Intro to Using PL/Python in PostgreSQL Databases

Business logic with PostgreSQL and Python
Business logic with PostgreSQL and PythonBusiness logic with PostgreSQL and Python
Business logic with PostgreSQL and PythonHubert Piotrowski
 
Massively Parallel Processing with Procedural Python (PyData London 2014)
Massively Parallel Processing with Procedural Python (PyData London 2014)Massively Parallel Processing with Procedural Python (PyData London 2014)
Massively Parallel Processing with Procedural Python (PyData London 2014)Ian Huston
 
Euro python2011 High Performance Python
Euro python2011 High Performance PythonEuro python2011 High Performance Python
Euro python2011 High Performance PythonIan Ozsvald
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python ProgrammingDozie Agbo
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experiencedzynofustechnology
 
Moving to Python 3
Moving to Python 3Moving to Python 3
Moving to Python 3Nick Efford
 
Learning Python. Level 0
Learning Python. Level 0Learning Python. Level 0
Learning Python. Level 0Datio Big Data
 
Monitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntopMonitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntopPyCon Italia
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MoreMatt Harrison
 

Similar to Intro to Using PL/Python in PostgreSQL Databases (20)

Business logic with PostgreSQL and Python
Business logic with PostgreSQL and PythonBusiness logic with PostgreSQL and Python
Business logic with PostgreSQL and Python
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
 
Massively Parallel Processing with Procedural Python (PyData London 2014)
Massively Parallel Processing with Procedural Python (PyData London 2014)Massively Parallel Processing with Procedural Python (PyData London 2014)
Massively Parallel Processing with Procedural Python (PyData London 2014)
 
Euro python2011 High Performance Python
Euro python2011 High Performance PythonEuro python2011 High Performance Python
Euro python2011 High Performance Python
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
 
Pyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdfPyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdf
 
C pythontalk
C pythontalkC pythontalk
C pythontalk
 
Python Programming Course in janak puri, New Delhi
Python Programming Course in janak puri, New DelhiPython Programming Course in janak puri, New Delhi
Python Programming Course in janak puri, New Delhi
 
Python PPT1.pdf
Python PPT1.pdfPython PPT1.pdf
Python PPT1.pdf
 
Data analysis with pandas
Data analysis with pandasData analysis with pandas
Data analysis with pandas
 
Data Analysis With Pandas
Data Analysis With PandasData Analysis With Pandas
Data Analysis With Pandas
 
Python for dummies
Python for dummiesPython for dummies
Python for dummies
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experienced
 
Learn python
Learn pythonLearn python
Learn python
 
Moving to Python 3
Moving to Python 3Moving to Python 3
Moving to Python 3
 
Core python programming tutorial
Core python programming tutorialCore python programming tutorial
Core python programming tutorial
 
Learning Python. Level 0
Learning Python. Level 0Learning Python. Level 0
Learning Python. Level 0
 
Monitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntopMonitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntop
 
Elasticwulf Pycon Talk
Elasticwulf Pycon TalkElasticwulf Pycon Talk
Elasticwulf Pycon Talk
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
 

More from Command Prompt., Inc

Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and ExceptableHowdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and ExceptableCommand Prompt., Inc
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL AdministrationCommand Prompt., Inc
 
Replication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorReplication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorCommand Prompt., Inc
 
Python utilities for data presentation
Python utilities for data presentationPython utilities for data presentation
Python utilities for data presentationCommand Prompt., Inc
 
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...Command Prompt., Inc
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLCommand Prompt., Inc
 
Not Just UNIQUE: Generalized Index Constraints
Not Just UNIQUE: Generalized Index ConstraintsNot Just UNIQUE: Generalized Index Constraints
Not Just UNIQUE: Generalized Index ConstraintsCommand Prompt., Inc
 
Implementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with TungstenImplementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with TungstenCommand Prompt., Inc
 
Elephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forksElephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forksCommand Prompt., Inc
 
configuring a warm standby, the easy way
configuring a warm standby, the easy wayconfiguring a warm standby, the easy way
configuring a warm standby, the easy wayCommand Prompt., Inc
 
Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2Command Prompt., Inc
 
Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1Command Prompt., Inc
 

More from Command Prompt., Inc (20)

Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and ExceptableHowdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
 
Backup and-recovery2
Backup and-recovery2Backup and-recovery2
Backup and-recovery2
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
 
Temporal Data
Temporal DataTemporal Data
Temporal Data
 
Replication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorReplication using PostgreSQL Replicator
Replication using PostgreSQL Replicator
 
Go replicator
Go replicatorGo replicator
Go replicator
 
Pg migrator
Pg migratorPg migrator
Pg migrator
 
Python utilities for data presentation
Python utilities for data presentationPython utilities for data presentation
Python utilities for data presentation
 
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
Not Just UNIQUE: Generalized Index Constraints
Not Just UNIQUE: Generalized Index ConstraintsNot Just UNIQUE: Generalized Index Constraints
Not Just UNIQUE: Generalized Index Constraints
 
Implementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with TungstenImplementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with Tungsten
 
Elephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forksElephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forks
 
configuring a warm standby, the easy way
configuring a warm standby, the easy wayconfiguring a warm standby, the easy way
configuring a warm standby, the easy way
 
Bucardo
BucardoBucardo
Bucardo
 
Basic Query Tuning Primer
Basic Query Tuning PrimerBasic Query Tuning Primer
Basic Query Tuning Primer
 
A Practical Multi-Tenant Cluster
A Practical Multi-Tenant ClusterA Practical Multi-Tenant Cluster
A Practical Multi-Tenant Cluster
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 
Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2
 
Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1
 

Intro to Using PL/Python in PostgreSQL Databases

  • 1. (Bottom Half of Screen) Intro to PL/Python Josh Williams    
  • 2. What is PL/Python?  Procedural Language INSIDE PostgreSQL  Not a replacement for SQL  Run manually (functions / DO in 9.0)  or automatically (triggers)    
  • 3. Why PostgreSQL?  Powerful, yet very fast.  Feature rich, but you can't beat the price.  It's future is clear!    
  • 4. Why Python?  Easy to read.  Easy to learn.  Easy to use.  Powerful, yet very fast.  Possibly already using it outside the database.  It's Python!!    
  • 5. Okay, enough crowd pandering  Step 1: Configure PostgreSQL for Python  Step 2: Install PL/Python into database(s)  Step 3: Profit    
  • 6. 1. Configure PG for Python  If using source, `./configure ­­with­python`  If using packages, install ”­plpython” or ”­py...”  In either case you need Python installed (duh)    
  • 7. Is it there?  SELECT * FROM pg_pltemplate;  tmplname == plpythonu  or plpython2u  or plpython3u    
  • 8. Wait, u?  PL/Python is an untrusted language...  Only DB superusers can create functions :(  Functions can communicate outside the DB :)    
  • 9. 2. Enable Databases  Command line: createlang plpythonu dbname  Inside DB: CREATE LANGUAGE plpythonu;  Hint: Install in template1  SELECT * FROM pg_language;    
  • 10. CREATE FUNCTION... CREATE FUNCTION function_name (....) AS $dollarquote$ ...python here... ... $dollarquote$ LANGUAGE plpythonu;  See ”Intro to PL/pgSQL” for construct details    
  • 11. The ...python here... bit  Where the interesting Python­y stuff happens  Simple processing of function parameters  But also... Import modules (stats, XML, ASCII plot?)  Interact with the outside world, the inside world    
  • 12. The web IN your database redux  Need a type to represent composite RSS data  Need RSS reader module … feedparser!  CREATE FUNCTION … RETURNS SETOF …    
  • 13. It works!  But 11 lines?  This is Python, we can do better!  There … Two lines (plus the module import)    
  • 14. Something a little more fancy  Create two really basic functions: CREATE FUNCTION pydir(text) RETURNS SETOF text... CREATE FUNCTION isdir(text) RETURNS boolean...  Call SRF using just SELECT pydir('/')    
  • 15. Enter Recursive Queries (8.4+)  CTE construct: WITH RECURSIVE …  Initial value(s) UNION recursive query  Then SELECT … FROM cte    
  • 16. But that's usual Python stuff  PL/Python implicitly includes module ”plpy”  Execute SQL statements  Generates messages/raises errors back to PG    
  • 17. Last contrived example  Basic personal finance application  Add a trigger: Net balance must be positive    
  • 18. Two Step Process  CREATE FUNCTION … RETURNS trigger  CREATE TRIGGER accounts_check AFTER  INSERT OR UPDATE ON accounts FOR EACH  STATEMENT EXECUTE PROCEDURE accheck();    
  • 19. Boooring  Wait, we're using Python.  We can do more!  How about an early warning email?  See package smtplib    
  • 20. That's PL/Python Any questions?    
  • 21. Intro to PL/Python http://joshwilliams.name/plpython/ (Now!) In the mean time: joshwilliams@ij.net