The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

PL/Python goodies Jan Urbanski [email protected] ducksboard.com May 19, 2011 04:03 AM Jan Urbanski [email protected] (ducksboard.com) PL/Python goodies May 19 ...

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by , 2016-11-15 05:15:03

PL/Python goodies - PostgreSQL wiki

PL/Python goodies Jan Urbanski [email protected] ducksboard.com May 19, 2011 04:03 AM Jan Urbanski [email protected] (ducksboard.com) PL/Python goodies May 19 ...

PL/Python goodies

Jan Urban´ski
[email protected]

ducksboard.com

May 19, 2011 04:03 AM

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 1 / 13

Recoverable database errors

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 2 / 13

Recoverable database errors

BEFORE

credit: gadgetdan May 19, 2011 04:03 AM 2 / 13
Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies

Recoverable database errors

BEFORE AFTER

credit: gadgetdan credit: VinothChandar
Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 2 / 13

Recoverable database errors

errors from executing SQL no longer terminate the
function

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 3 / 13

Recoverable database errors

errors from executing SQL no longer terminate the
function
use normal try/catch/finally blocks to manage
database errors

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 3 / 13

Recoverable database errors

errors from executing SQL no longer terminate the
function
use normal try/catch/finally blocks to manage
database errors
inspect caught errors to get SQLSTATE

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 3 / 13

Recoverable database errors

from plpy import spiexceptions

while True :
r e s = p l p y . e x e c u t e ( ” u p d a t e db s e t b = %s where a = %d” %
( plpy . quote nullable ( data ) , key ))
i f res . nrows ():
break

try :
p l p y . e x e c u t e ( ” i n s e r t i n t o db ( a , b ) v a l u e s (%d , %s ) ” %
( key , plpy . quote nullable ( data )))
break

except spiexceptions . UniqueViolation :
pass

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 4 / 13

Explicit subtransactions

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 5 / 13

Explicit subtransactions

credit: Idaho National Laboratory

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 5 / 13

Explicit subtransactions

execute several database calls atomically

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 6 / 13

Explicit subtransactions

execute several database calls atomically
comfortable with syntax

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 6 / 13

Explicit subtransactions

execute several database calls atomically
comfortable with syntax

with plpy . subtransaction (): this values ( ’ that ’)”)
plpy . execute (” insert into this values ( ’ other ’)”)
plpy . execute (” insert into this values ( ’ another ’)”)
plpy . execute (” insert into

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 6 / 13

Tracebacks

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 7 / 13

Tracebacks

BEFORE

credit: Peter Ward May 19, 2011 04:03 AM 7 / 13
Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies

Tracebacks

BEFORE

AFTER

credit: Peter Ward May 19, 2011 04:03 AM 7 / 13
Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies

Tracebacks

closely mimcking Python’s tracebacks

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 8 / 13

Tracebacks

closely mimcking Python’s tracebacks
very useful when tracking down typos in large
functions

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 8 / 13

Tracebacks

closely mimcking Python’s tracebacks
very useful when tracking down typos in large
functions
only lines from the PL/Python function, but line
numbers from all modules

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 8 / 13

Functions returning tables

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 9 / 13

Functions returning tables

credit: Silk Road Collections

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 9 / 13

Functions returning tables

in 8.4 PL/Python got support for one OUT
parameter in a function

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 10 / 13

Functions returning tables

in 8.4 PL/Python got support for one OUT
parameter in a function
in 9.1 you can have multiple

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 10 / 13

Hstore to dictionary converter

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 11 / 13

Hstore to dictionary converter

BEFORE

credit: HB Art May 19, 2011 04:03 AM 11 / 13
Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies

Hstore to dictionary converter

BEFORE AFTER

credit: HB Art credit: Muffet
Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 11 / 13

Hstore to dictionary converter

did not get into 9.1

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 12 / 13

Hstore to dictionary converter

did not get into 9.1
currently implemented as a Python extension
module

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 12 / 13

Hstore to dictionary converter

did not get into 9.1
currently implemented as a Python extension
module
uses hstore’s I/O functions to transform dicts to
and from hstores

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 12 / 13

Hstore to dictionary converter

did not get into 9.1
currently implemented as a Python extension
module
uses hstore’s I/O functions to transform dicts to
and from hstores
is very bad!

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 12 / 13

Hstore to dictionary converter

did not get into 9.1
currently implemented as a Python extension
module
uses hstore’s I/O functions to transform dicts to
and from hstores
is very bad!
https://github.com/wulczer/pyhstore

Jan Urban´ski [email protected] (ducksboard.com) PL/Python goodies May 19, 2011 04:03 AM 12 / 13


Click to View FlipBook Version