business logic with postgresql and python
TRANSCRIPT
![Page 1: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/1.jpg)
Business logic with PostgreSQL and Python
Using functions and triggers and PostgreSQL magic with plpy.
![Page 2: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/2.jpg)
Knock knock, who's there?
Hubert Piotrowski Senior systems architect
![Page 3: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/3.jpg)
How to build API
![Page 4: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/4.jpg)
How to build API
Perfect API
![Page 5: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/5.jpg)
My awesome software
Where API functionalities are
part of it
![Page 6: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/6.jpg)
Welcome to the
Reality
![Page 7: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/7.jpg)
My awesome software
API
Another app
Oh this this small tool
![Page 8: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/8.jpg)
My awesome software
API
Oh this this small tool
Another magic
Fancy twisted
stackless
![Page 9: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/9.jpg)
HELP?
![Page 10: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/10.jpg)
My awesome software
API
Oh this this small tool
Another magic
Business LOGIC
![Page 11: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/11.jpg)
What is so great about PostgreSQL
• Dynamic data typing
• Procedural functions (many languages)
• Horizontal scaling
• Plugins
• Triggers
• Granularity for access privileges
![Page 12: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/12.jpg)
Data types
• Standard: Strings, float, inter, etc.
• JSON
• Dynamic (composite)
• Arrays
![Page 13: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/13.jpg)
Arrays
Or, if no array size is to be specified:
pay_by_quarter integer ARRAY
pay_by_quarter integer ARRAY[4]
![Page 14: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/14.jpg)
Arrays
And get it
INSERT INTO sal_emp VALUES ('Bill', '{10000, 10000, 10000, 10000}', '{{"meeting", "lunch"}, {"training", "presentation"}}');
SELECT name FROM sal_emp WHERE pay_by_quarter[1] <> pay_by_quarter[2];
name ------- Carol (1 row)
![Page 15: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/15.jpg)
Still arraysSELECT schedule[1:2][2] FROM sal_emp WHERE name = 'Bill';
schedule ------------------------------------------- {{meeting,lunch},{training,presentation}} (1 row)
SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);
![Page 16: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/16.jpg)
Composite
CREATE TYPE inventory_item AS ( name text, supplier_id integer, price numeric );
CREATE TABLE on_hand ( item inventory_item, count integer );
INSERT INTO on_hand VALUES (ROW('fuzzy dice', 42, 1.99), 1000);
![Page 17: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/17.jpg)
ProceduresplPythonu(?) one second
CREATE FUNCTION merge_fields(t_row table1) RETURNS text AS $$ DECLARE t2_row table2%ROWTYPE; BEGIN SELECT * INTO t2_row FROM table2 WHERE ... ; RETURN t_row.f1 || t2_row.f3 || t_row.f5 || t2_row.f7; END; $$ LANGUAGE plpgsql;
SELECT merge_fields(t.*) FROM table1 t WHERE ... ;
![Page 18: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/18.jpg)
Better formatting
CREATE FUNCTION merge_fields(t_row table1) RETURNS text AS $$ DECLARE t2_row table2%ROWTYPE; BEGIN SELECT * INTO t2_row FROM table2 WHERE ... ; RETURN t_row.f1 || t2_row.f3 || t_row.f5 || t2_row.f7; END; $$ LANGUAGE plpgsql;
SELECT merge_fields(t.*) FROM table1 t WHERE ... ;
![Page 19: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/19.jpg)
Let there be light...and there was light
Compile PostgreSQL 9.4 with Python 2.7.10
![Page 20: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/20.jpg)
• PYTHONHOME
• PYTHONPATH
• PYTHONY2K
• PYTHONOPTIMIZE
• PYTHONDEBUG
• PYTHONVERBOSE
• PYTHONCASEOK
• PYTHONDONTWRITEBYTECODE
• PYTHONIOENCODING
• PYTHONUSERBASE
Python compiled with special flags
![Page 21: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/21.jpg)
Compile python
Compilation takes some time…
Once it’s done… we need easy_install and other python modules
[hubert@ThePit]~/stuff/Python-2.7.10% ./configure —PREFIX=/opt/py --enable-shared
![Page 22: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/22.jpg)
issues?
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/py/lib
py ➤ bin/python2.7 bin/python2.7: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
simple fix
![Page 23: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/23.jpg)
Compile essential contrib data
contrib ➤ cd ~/stuff/postgresql-9.4.3/contrib contrib ➤ make contrib ➤ make install
Compile PostgreSQL
postgresql-9.4.3 ➤ ./configure --prefix=/opt/pgsql \ —with-python PYTHON=/opt/py/bin/python postgresql-9.4.3 ➤ make postgresql-9.4.3 ➤ make install
![Page 24: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/24.jpg)
~ ➤ mkdir /opt/pg_data ~ ➤ /opt/pgsql/bin/initdb /opt/pg_data
~ ➤ /opt/pgsql/bin/postmaster -p 5432 -D /opt/pg_data/
~ ➤ /opt/pgsql/bin/createdb -h localhost -E utf8 pie
initialize database
Initialize postgresql data space
![Page 25: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/25.jpg)
Create language plpython in database
~ ➤ /opt/pgsql/bin/createlang -h localhost -d pie plpythonu
~ ➤ /opt/pgsql/bin/postmaster -p 5432 -D /opt/pg_data/ LOG: database system was shut down at 2015-06-07 17:29:05 SGT LOG: database system is ready to accept connections LOG: autovacuum launcher started
pie=# select * from pg_extension ; extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition -----------+----------+--------------+----------------+------------+-----------+-------------- plpgsql | 10 | 11 | f | 1.0 | | plpythonu | 10 | 11 | f | 1.0 | | (2 rows)
![Page 26: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/26.jpg)
Let uncle Google help you
didn’t find the answer?
Probably the issue that you have means thatwhat you try to achieve is not possible or…
doesn’t make sense
![Page 27: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/27.jpg)
First plPy function
![Page 28: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/28.jpg)
Hello world
create or replace function logic.hello_world() returns void as $$ """ Code code code code """ print "hello world" $$ LANGUAGE plpythonu VOLATILE;
![Page 29: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/29.jpg)
Hello world
create or replace function logic.hello_world() returns void as $$ """ Code code code code """ plpy.info("hello world")
$$ LANGUAGE plpythonu VOLATILE;
![Page 30: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/30.jpg)
Triggers
• can be fired before or after (instead of)
• upon action: insert, update or delete
• trigger fires function (also by using when condition)
• trigger function can but don’t have to manipulate data
• multiple triggers on the same table
![Page 31: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/31.jpg)
Let’s see some action
![Page 32: Business logic with PostgreSQL and Python](https://reader034.vdocument.in/reader034/viewer/2022042522/55c785c1bb61ebc5348b472c/html5/thumbnails/32.jpg)
Hungry for more?@dj_techhub
darkman66
http://my-twisted-code.tumblr.com
https://github.com/darkman66/pyconsg2015