I've already switched to 'UPSERT', it didn't resolved deadlock issue by
itself... Added LOCK TABLE ... IN EXCLUSIVE MODE; to one session, hope
it will help.
> You did not mention what version of Postgres you are using, if it is
> 9.5+ you have the 'UPSERT' option available instead of using the WITH
> construct:
>
>
> https://www.postgresql.org/docs/9.5/static/sql-insert.html
>
> ON CONFLICT Clause
>
> https://www.depesz.com/2015/05/10/waiting-for-9-5-add-support-for-insert-on-conflict-do-nothingupdate/
>
>
> If not you might to take a look at:
>
> http://stackoverflow.com/questions/17267417/how-to-upsert-merge-insert-on-duplicate-update-in-postgres
>
>
> Bulk upsert with lock
>
>