Pl/Python

21
 (Bottom Half of Screen) Intro to PL/Python Josh Williams

description

Josh WilliamsYou may be familiar with connecting to a database using Python, but did you know it's possible for your database itself to use Python? PostgreSQL has the ability to write functions and procedures in PL/Python, which can use Python to both work with data inside your database, and bring in information from the outside world. This tutorial-format demonstration is designed to show you how to get up and running on PL/Python, and some of the cool things you can accomplish using it.

Transcript of Pl/Python

Page 1: Pl/Python

   

(Bottom Half of Screen)

Intro to PL/Python

Josh Williams

Page 2: Pl/Python

   

What is PL/Python? Procedural Language INSIDE PostgreSQL Not a replacement for SQL Run manually (functions / DO in 9.0)

or automatically (triggers)

Page 3: Pl/Python

   

Why PostgreSQL? Powerful, yet very fast. Feature rich, but you can't beat the price. It's future is clear!

Page 4: Pl/Python

   

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

Page 5: Pl/Python

   

Okay, enough crowd pandering Step 1: Configure PostgreSQL for Python Step 2: Install PL/Python into database(s) Step 3: Profit

Page 6: Pl/Python

   

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)

Page 7: Pl/Python

   

Is it there? SELECT * FROM pg_pltemplate; tmplname == plpythonu

or plpython2u or plpython3u

Page 8: Pl/Python

   

Wait, u? PL/Python is an untrusted language... Only DB superusers can create functions :( Functions can communicate outside the DB :)

Page 9: Pl/Python

   

2. Enable Databases Command line: createlang plpythonu dbname Inside DB: CREATE LANGUAGE plpythonu;

Hint: Install in template1

SELECT * FROM pg_language;

Page 10: Pl/Python

   

CREATE FUNCTION...CREATE FUNCTION function_name (....)

AS $dollarquote$ ...python here...

... $dollarquote$ LANGUAGE plpythonu;

See ”Intro to PL/pgSQL” for construct details

Page 11: Pl/Python

   

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

Page 12: Pl/Python

   

The web IN your database redux Need a type to represent composite RSS data Need RSS reader module … feedparser! CREATE FUNCTION … RETURNS SETOF …

Page 13: Pl/Python

   

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

Page 14: Pl/Python

   

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('/')

Page 15: Pl/Python

   

Enter Recursive Queries (8.4+) CTE construct: WITH RECURSIVE … Initial value(s) UNION recursive query Then SELECT … FROM cte

Page 16: Pl/Python

   

But that's usual Python stuff PL/Python implicitly includes module ”plpy”

Execute SQL statements Generates messages/raises errors back to PG

Page 17: Pl/Python

   

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

Page 18: Pl/Python

   

Two Step Process CREATE FUNCTION … RETURNS trigger CREATE TRIGGER accounts_check AFTER

INSERT OR UPDATE ON accounts FOR EACH STATEMENT EXECUTE PROCEDURE accheck();

Page 19: Pl/Python

   

Boooring Wait, we're using Python.  We can do more! How about an early warning email?

See package smtplib

Page 20: Pl/Python

   

That's PL/Python

Any questions?

Page 21: Pl/Python

   

Intro to PL/Python

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

In the mean time: [email protected]