13
2PC and 3PC Protocols
Two-phase commit
prepare: the coordinator holds a vote
commit: if every node votes yes, the coordinator signals to commit
supported by PostgreSQL (PREPARE TRANSACTION + COMMIT)
Three-phase commit
prepare: the coordinator holds a vote
precommit: if every node votes yes, the coordinator notifies all participants
of the intent to commit
commit: if all nodes acknowledge, the coordinator signals to commit
Properties
there are situations (such as network partition) where a transaction cannot be
resolved until the failure is fixed
PostgreSQL comes with a straightforward two-phase commit (2PC) protocol
that handles global commits. The node executing the transaction acts as the
coordinator.
During the prepare phase, the coordinator informs the other nodes about the
transaction commit and waits for them to confirm that that they're ready to
commit (if at least one node isn't ready, the transaction is aborted). During
the commit phase, the coordinator orders all nodes to commit the
transaction. Once the coordinator receives commit confirmation from all
nodes, it commits the transaction as well.
This protocol requires minimal message exchange but isn't fault-tolerant. If
the coordinator fails after the prepare phase, the remaining nodes have no
way of knowing whether to commit or abort the transaction. They'll have to
wait until the failure is resolved.
A more sophisticated protocol called three-phase commit (3PC) addresses
this limitation. It introduces an additional precommit phase where the
coordinator informs all nodes of its *decision* to commit the transaction.
After that, if the coordinator fails, the remaining nodes can proceed to
commit the transaction on their own (or abort it independently if the failure
occurs before the precommit phase).
However, both algorithms break down in network partition scenarios.