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

Pl python python w postgre-sql
Pl python   python w postgre-sqlPl python   python w postgre-sql
Pl python python w postgre-sqlPiotr Pałkiewicz
 
Redis in Practice
Redis in PracticeRedis in Practice
Redis in PracticeNoah Davis
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in PythonSujith Kumar
 
Gitlab Training with GIT and SourceTree
Gitlab Training with GIT and SourceTreeGitlab Training with GIT and SourceTree
Gitlab Training with GIT and SourceTreeTeerapat Khunpech
 
Apache Knox setup and hive and hdfs Access using KNOX
Apache Knox setup and hive and hdfs Access using KNOXApache Knox setup and hive and hdfs Access using KNOX
Apache Knox setup and hive and hdfs Access using KNOXAbhishek Mallick
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaEdureka!
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data AnalysisAndrew Henshaw
 
파이썬 생존 안내서 (자막)
파이썬 생존 안내서 (자막)파이썬 생존 안내서 (자막)
파이썬 생존 안내서 (자막)Heungsub Lee
 
Python SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite DatabasePython SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite DatabaseElangovanTechNotesET
 
[DevGround] 린하게 구축하는 스타트업 데이터파이프라인
[DevGround] 린하게 구축하는 스타트업 데이터파이프라인[DevGround] 린하게 구축하는 스타트업 데이터파이프라인
[DevGround] 린하게 구축하는 스타트업 데이터파이프라인Jae Young Park
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & styleKevlin Henney
 
Iceberg: A modern table format for big data (Strata NY 2018)
Iceberg: A modern table format for big data (Strata NY 2018)Iceberg: A modern table format for big data (Strata NY 2018)
Iceberg: A modern table format for big data (Strata NY 2018)Ryan Blue
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlibPiyush rai
 
What is in a Lucene index?
What is in a Lucene index?What is in a Lucene index?
What is in a Lucene index?lucenerevolution
 

What's hot (20)

Pl python python w postgre-sql
Pl python   python w postgre-sqlPl python   python w postgre-sql
Pl python python w postgre-sql
 
Redis in Practice
Redis in PracticeRedis in Practice
Redis in Practice
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
Gitlab Training with GIT and SourceTree
Gitlab Training with GIT and SourceTreeGitlab Training with GIT and SourceTree
Gitlab Training with GIT and SourceTree
 
Apache Knox setup and hive and hdfs Access using KNOX
Apache Knox setup and hive and hdfs Access using KNOXApache Knox setup and hive and hdfs Access using KNOX
Apache Knox setup and hive and hdfs Access using KNOX
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
 
PYthon
PYthonPYthon
PYthon
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
The basics of fluentd
The basics of fluentdThe basics of fluentd
The basics of fluentd
 
파이썬 생존 안내서 (자막)
파이썬 생존 안내서 (자막)파이썬 생존 안내서 (자막)
파이썬 생존 안내서 (자막)
 
Python: Polymorphism
Python: PolymorphismPython: Polymorphism
Python: Polymorphism
 
Python SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite DatabasePython SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite Database
 
[DevGround] 린하게 구축하는 스타트업 데이터파이프라인
[DevGround] 린하게 구축하는 스타트업 데이터파이프라인[DevGround] 린하게 구축하는 스타트업 데이터파이프라인
[DevGround] 린하게 구축하는 스타트업 데이터파이프라인
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
 
Iceberg: A modern table format for big data (Strata NY 2018)
Iceberg: A modern table format for big data (Strata NY 2018)Iceberg: A modern table format for big data (Strata NY 2018)
Iceberg: A modern table format for big data (Strata NY 2018)
 
Introduction git
Introduction gitIntroduction git
Introduction git
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlib
 
Git 勉強会
Git 勉強会Git 勉強会
Git 勉強会
 
What is in a Lucene index?
What is in a Lucene index?What is in a Lucene index?
What is in a Lucene index?
 

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
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughgabriellekuruvilla
 
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
 

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)
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
 
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
 

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