Yes, you should look for deadlock_timeout in the docs .
But your script does not mean there will be a dead end, "cos PostgreSQL uses row-level locks, and it is unclear if your transactions match for the same rows.
Another option is to use a serialization level higher than default READ COMMITTED . But in this case, your application should be ready to receive exceptions with SQLCODE=40001 :
ERROR: could not serialize access due to concurrent update
It is expected that you should simply repeat the transaction as is.
A very good overview of the isolation level of Serializable, which you can find on the wiki .
source share