Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

"PostgreSQL and Python" Lightning Talk @EuroPython2014

897 views

Published on

"PostgreSQL and Python" Lightning Talk @EuroPython2014

Published in: Technology
  • Be the first to comment

  • Be the first to like this

"PostgreSQL and Python" Lightning Talk @EuroPython2014

  1. 1. PostgreSQL and Python henning.jacobs@zalando.de
  2. 2. Well known... import psycopg2 conn = psycopg2.connect('dbname=mydb user=myusr') cur = conn.cursor() cur.execute('SELECT * FROM mytable') row = cur.fetchone()
  3. 3. PostgreSQL speaks Python! CREATE FUNCTION greet(name TEXT) RETURNS TEXT AS $$ import random attr = random.choice([ 'great', 'fabulous', 'bright', 'brave']) return 'Hello {} {}!'.format(attr, name) $$ LANGUAGE plpythonu; SELECT greet('Python fan');
  4. 4. PostgreSQL speaks Python ● PL/Python – “just” another procedural language ● SQL script that contains a string of Python ● Allows using any Python module
  5. 5. Real World Examples... mydb=# SELECT * FROM get_server_connection_stats() LIMIT 5; server_addr | server_port | client_addr | client_port ---------------+-------------+--------------+------------- 10.160.26.128 | 5435 | 10.111.74.16 | 40524 10.160.26.128 | 5435 | 10.160.26.1 | 49681 10.160.26.128 | 5435 | 10.160.26.1 | 15456 10.160.26.128 | 5435 | 10.160.26.2 | 64136 10.160.26.128 | 5435 | 10.160.26.1 | 1938 (5 rows)
  6. 6. Expose Stats via SQL with Python CREATE FUNCTION get_server_connection_stats() RETURNS SETOF server_connection AS $$ # … try: with open('/proc/net/tcp') as fp: lines = fp.read().splitlines() except: plpy.error('Unable to read /proc/net/tcp') # … return result $$ LANGUAGE plpythonu;
  7. 7. Custom JSON Type Validation CREATE TRIGGER validation AFTER INSERT OR UPDATE ON config_value FOR EACH … CREATE FUNCTION validate(value JSON, type_id INT) RETURNS VOID AS $$ import json # … complex type checking here # … using all of Python's expressiveness :-) raise ValueError('{} not valid'.format(value)) $$ LANGUAGE plpythonu;
  8. 8. tech.zalando.com

×