Is my theory on INSERT deadlocks correct?
Process 7508 waits for AccessExclusiveLock on relation 939490

Nothing in your shown code would take an AccessExclusiveLock on a relation.  The 7508 member of your deadlocked pair is doing something different.  Look in the log file to see if there is a clue about what that is. (The log file will show what statement it was running when the deadlock occured, but that might not be the same statement as what took out the lock, but at least it will be more information than you currently have)

maybe, the transaction block causes the third INSERT to not &quot;see&quot; the previous INSERTs because they have not yet been committed? Is that it?

No, a statement in a transaction can see INSERTs done by earlier statements in the same transaction.