Articles from Daniele Varrazzo

Psycopg 2.5.4 released

Posted by Daniele Varrazzo on August 30, 2014
Tagged as news, release

Psycopg 2.5.4 has been released. You can get it from:

This version supports the new jsonb PostgreSQL type out-of-the-box. And of course there are a few bug fixed:

Read more...

Cancelling PostgreSQL statements from Python

Posted by Daniele Varrazzo on July 20, 2014
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.

Using psycopg in green mode moves the waiting from the libpq C code to Python: this gives Python some chance of interaction: it is possible for instance to catch a ctrl-c and send a cancel request:

from select import select
from psycopg2.extensions import POLL_OK, POLL_READ, POLL_WRITE

def wait_select_inter(conn):
    while 1:
        try:
            state = conn.poll()
            if state == POLL_OK:
                break
            elif state == POLL_READ:
                select([conn.fileno()], [], [])
            elif state == POLL_WRITE:
                select([], [conn.fileno()], [])
            else:
                raise conn.OperationalError(
                    "bad state from poll: %s" % state)
        except KeyboardInterrupt:
            conn.cancel()
            # the loop will be broken by a server error
            continue

psycopg2.extensions.set_wait_callback(wait_select_inter)

An interactive session would look like:

>>> cnn = psycopg2.connect('')
>>> cur = cnn.cursor()
>>> cur.execute("select pg_sleep(10)")
^C
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
QueryCanceledError: canceling statement due to user request

The connection is now in error state, but a cnn.rollback() would make it working again.

Psycopg 2.5.3 Released

Posted by Daniele Varrazzo on May 13, 2014
Tagged as news, release

Psycopg 2.5.3 has been released. You can get it from:

This version contains several bug fixes over the previous release 2.5.2:

Read more...

Psycopg 2.5.2 released

Posted by Daniele Varrazzo on January 7, 2014
Tagged as news, release

Psycopg 2.5.2 has been released. You can get it from:

This version contains a few bug fixes over the previous release 2.5.1:

  • Fixed segfault pickling the exception raised on connection error (ticket #170).
  • Meaningful connection errors report a meaningful message, thanks to Alexey Borzenkov (ticket #173).
  • Manually creating lobject with the wrong parameter doesn't segfault (ticket #187).

Thank you very much to all the people who helped during the development!

Psycopg 2.5.1 released

Posted by Daniele Varrazzo on June 23, 2013
Tagged as news, release

Psycopg 2.5.1 has been released. You can get it from:

The version contains a few bug fixes over the previous 2.5:

  • Fixed build on Solaris 10 and 11 where the round() function is already declared (ticket #146).
  • Fixed comparison of Range with non-range objects (ticket #164). Thanks to Chris Withers for the patch.
  • Fixed double-free on connection dealloc (ticket #166). Thanks to Gangadharan S.A. for the report and fix suggestion.

Happy hacking!

Psycopg 2.5 released

Posted by Daniele Varrazzo on April 7, 2013
Tagged as news, release

We are happy to introduce the release 2.5 of Psycopg, packed with several juicy new features!

Here are a few highlights of the release:

Read more...

Psycopg 2.4.6 released

Posted by Daniele Varrazzo on December 12, 2012
Tagged as news, release

I'm happy to announce the release of Psycopg 2.4.6: a huge thank you to the many contributors.

This is a bugfix release, introducing no new feature. There are several small corrections in different areas (copy, adaptation, use of extra cursors, stability). The biggest improvements are with the Zope adapter: Zope users using previous 2.4.x versions are encouraged to update to version 2.4.6 soon.

Read more...

Prepared statements in Psycopg

Posted by Daniele Varrazzo on October 1, 2012

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...

Psycopg 2.4.5 released

Posted by Daniele Varrazzo on March 29, 2012
Tagged as news, release

Many thanks to everybody that contributed with bug reports and comments to this release!

Read more...

Psycopg 2.4.4 released

Posted by Daniele Varrazzo on December 19, 2011
Tagged as news, release

After a short discussion on this list we decided to change the definitions of isolation levels to make sure old code using numeric constants (both psycopg1 and psycopg2) continue to works. Other small fixes are included in the release: see below for details.

Read more...