Psycopg; How to detect row locking?

Jorge Godoy godoy at
Mon Sep 27 14:23:05 CEST 2004

Alban Hertroys <alban at> writes:

> Good day,
> I have a number of threads doing inserts in a table, after which I want 
> to do a select. This means that it will occur that the row that I want 
> to select is locked (by the DB). In these cases, I believe I receive an 
> OperationalError (or is it an InterfaceError?).
> Is it possible (and if so - how?) to verify that the exception occured 
> because of row locking, so that I can wait and try again?
> Or are there better ways to achieve this? I'm not too charmed about 
> polling loops that may never end.
> I'm using python 2.3 with psycopg 1.1.13 and PostgreSQL 7.4.2.

Are you manually locking those rows?  If so, you can maintain some
structure to keep track of locked rows.

If you are not locking, PostgreSQL uses MVCC where it locks as little as
possible and you are able to select the new data inside the same
transaction and old data outside of it (until it is commited).

Godoy.     <godoy at>

More information about the Python-list mailing list