I am trying to understand two-phase-commit and it is not clear to me when each local site executes its portion of the distributed transaction.
Does that happen before the prepare messages are sent. That is does it happen before the two-phase-commit xa protocol is even run?
Or does each site executes its portion of the distributed transaction after receiving the prepare message, meaning the prepare message itself also includes the transaction query to be executed?
Yes, execution happens before prepare messages are sent. You should assume that the entire 2PC protocol runs within the commit() operation after everything has already been executed. Consider the following imagined trace of a distributed transaction that eventually commits. Indentation means procedure nesting:
Actually, as you can see, the prepare message will be sent by the coordinator only to those sites that have previously executed something within the context of such transaction, and thus have previously registered as participants in that transaction.