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 has been released. You can get it from:
This version contains several bug fixes over the previous release 2.5.2:
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 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.
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:
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.
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.
Many thanks to everybody that contributed with bug reports and comments to this 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.