Articles tagged recipe
PostgreSQL supports asynchronous notifications, a simple messaging system allowing clients to be notified about events occurred in the database. Notifications can be sent by any session using a "NOTIFY channel" command and will be received by any session which has subscribed with a LISTEN channel to receive updates. The system has been greatly improved in PostgreSQL 9.0 with the addition of a message payload, making the feature even more useful. Previously a typical use case would have been to notify interested sessions that a certain table was updated: now it is possible to signal for instance which record was changed. You can put the NOTIFY command in a database trigger for automatic notifications on insert or update... the possibilities are actually quite interesting.
Looks like building Psycopg on OS X is tricky: the code needs no tweak, but linking against the right library seems problematic.
In many script I write, there are functions requiring database operations. Every time I need them, I try to write such code in functions like:
@with_connection def do_some_job(cnn, arg1, arg2=None): cur = cnn.cursor() cur.execute(SQL, (arg1, arg2)) # or something else do_some_job(42, arg2='hi')