Proper use of serialization with psycopg2

I am accessing a postgresql table with serialization transaction isolation. I am doing something like this (with the existing psycopg2 conn connection and the cursor in this connection, cur :

 while True: try: cur.execute(query) break except TransactionRollbackError: [sleep a little] continue except Exception: [handle error here] 

The point of this is to retry if serialization is approved. Now it works fine. But often I get this error after it has done one iteration in the TransactionRollbackError trap:

current transaction is aborted, commands ignored until end of transaction block . Apparently rotating so as to avoid serialization competition is not appropriate? Should I do it differently?

Some notes: I refer to a table with different processes (they are all the same and do the same things: select, increase and update / insert into the table). Each of these processes has its own conn connection, they do not use the connection.

One more note: it seems that after passing the TransactionRollbackError exception block once, in the next scroll of the while loop, it falls into the Exception block.

One more note: the number of processes running at the same time directly affects the error rate, since more processes tend to create more errors. So there is some kind of disagreement. I get the impression that using serialized transaction isolation using attempts (like in my demo code) will fix this.

+4
source share
1 answer

You must do a rollback in the except TransactionError: branch to recover from the error state:

 while True: try: cur.execute(query) break except TransactionRollbackError: conn.rollback() [sleep a little] continue except Exception: [handle error here] 

This recommendation also introduced frequent questions .

Please note that for now, he will refuse all SQL commands if you do not perform your own transaction control or are not connected in autocommit mode.

+3
source

All Articles