Articles tagged recipe

Cancelling PostgreSQL statements from Python

Posted by Daniele Varrazzo on 2014-07-20
Tagged as recipe

Cancelling a long running query from Python is not something that happens automatically: the libpq doesn't react to Python signals so the only way to stop a query is to run a pg_cancel_backend from another process. Killing the Python process won't cancel the query: it will run until completion and then rolled back. This makes working wth long-running query from the Python interpreter somewhat frustrating.

Read more...

Prepared statements in Psycopg

Posted by Daniele Varrazzo on 2012-10-01
Tagged as recipe

Although the libpq library supports prepared statements, psycopg2 doesn't offer yet a direct way to access the relevant functions. This will probably change in the future, but in the meantime it is possible to use prepared statements in PostgreSQL using the PREPARE SQL command.

Read more...

Building Psycopg on Windows using MinGW

Posted by Daniele Varrazzo on 2011-06-05
Tagged as windows, build, recipe

My goal was to install Psycopg on Windows using MinGW and the PostgreSQL binary package.

I have used the MinGW GCC binaries packaged by Giovanni Bajo. The package takes care of a lot of details, for instance registering MinGW as default compiler for Python, plus some magic I don't even want to know, and makes the entire process simple enough.

Read more...

PostgreSQL notifications with Psycopg2 and Eventlet

Posted by Daniele Varrazzo on 2010-12-01
Tagged as recipe, async, eventlet, notify

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.

Read more...

Links about building Psycopg on Mac OS X

Posted by Daniele Varrazzo on 2010-11-11
Tagged as recipe, os-x

Looks like building Psycopg on OS X is tricky: the code needs no tweak, but linking against the right library seems problematic.

Read more...

Passing connections to functions using a decorator

Posted by Daniele Varrazzo on 2010-10-22
Tagged as recipe

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

Read more...