Home > Mobile >  Is it possible to let Postgres automatically retry transactions?
Is it possible to let Postgres automatically retry transactions?

Time:09-22

I'm developing a Django application in which I want to have strong guarantees about the correctness of the data. For this reason, I have SERIALIZABLE as the transaction isolation level.

However, during load testing I see some related error messages:

could not serialize access due to read/write dependencies among transactions
DETAIL: Reason code: Canceled on identification as a pivot, during write.
HINT: The transaction might succeed if retried.

and

could not serialize access due to read/write dependencies among transactions
DETAIL: Reason code: Canceled on conflict out to pivot 23933812, during read.
HINT: The transaction might succeed if retried.

and

current transaction is aborted, commands ignored until end of transaction block

I do automatically retry the transaction via Django, but I'm uncertain if this solution is actually good. I wondered: Is it possible to let Postgres automatically retry transactions that failed for those reasons? Is there maybe something directly in Django allowing me to configure that? (It would not have to be for all transactions; I know the candidates that are failing more often / that are more important)

CodePudding user response:

No, PostgreSQL doesn't offer such a functionality. If Django does, that is great, and you should use it.

The SQLSTATEs you should catch are 40P01 (deadlock) and 40001 (serialization failure).

  • Related