To debug this, you could try issuing the following query when things are deadlocked:
select datname, usename, xact_start, waiting, current_query from pg_stat_activity;
This should tell you what is going on from PostgreSQL's point of view. The deadlocked connections should show up with "waiting" set to true. If you have any "<IDLE> in transaction" connections, they are possible causes of the deadlock.
To debug this, you could try issuing the following query when things are deadlocked:
select datname, usename, xact_start, waiting, current_query from pg_stat_activity;
This should tell you what is going on from PostgreSQL's point of view. The deadlocked connections should show up with "waiting" set to true. If you have any "<IDLE> in transaction" connections, they are possible causes of the deadlock.