Posgresql - deadlock on non related queries
These queries are generated internally by the triggers that implement foreign key constraints.

For example, adding a row to a table with a foreign key constraint will add such a key share lock on the corresponding row in the referenced table, so that nobody can delete it or modify the primary key until the inserting transaction is done.

This can cause deadlocks between transactions that modify both tables.

When searching for the statements at fault, don't forget that it was not necessarily the last statement in the transaction (that is shown in the log message) that took the problematic lock.

One way to proceed is to set log_min_duration_statement to 0, and when the deadlock strikes again, examine all statements run by the affected transactions. The other way is to review the application code to find out which statements were executed in the transactions.