How does PostgreSQL decide which query to cancel during deadlock cleanup
According to the documentation located at http://www.postgresql.org/docs/9.4/static/explicit-locking.html#LOCKING-DEADLOCKS (emphasis mine):


  PostgreSQL automatically detects deadlock situations and resolves them
  by aborting one of the transactions involved, allowing the other(s) to
  complete. (Exactly which transaction will be aborted is difficult to
  predict and should not be relied upon.)