two-phase-commit · shipping · also 2PC

Two-phase commit

Two-phase commit coordinates an all-or-nothing transaction outcome through a prepare/vote phase and a commit-or-abort decision phase.

Related Observatory article: Read on hesela.com

In basic 2PC, a participant records its prepared state before voting yes; the coordinator records its commit decision before announcing it. Commit requires every participant to prepare, not merely a majority. Prepared is a promise to follow the outcome, not the outcome itself. [1]

If the coordinator becomes unreachable after preparation, participants can lack enough information to finish. A timeout alone cannot authorize a prepared participant to abort: another participant might already have learned commit. This blocking behavior preserves agreement rather than proving data loss. [1]

Two-phase commit concerns the shared outcome; two-phase locking concerns concurrent access. [4] PostgreSQL PREPARE TRANSACTION detaches the transaction from its session but retains its locks. COMMIT PREPARED or ROLLBACK PREPARED later resolves it under an external transaction manager's decision. [2]

In PostgreSQL 16, short-lived prepared transactions reside in shared memory and WAL; those spanning a checkpoint also have files in pg_twophase. Absence of such a file does not prove absence of a prepared transaction. Inspect pg_prepared_xacts. [3]

Paxos Commit runs a separate consensus instance for each participant's vote; it is more than taking a majority of participants willing to commit. With a unique nonfaulty leader and timely communication with an acceptor majority for long enough, it can reach an outcome, possibly abort; commit still needs Prepared in every participant instance. [1]

With a read-only optimization, a participant that made no persistent changes can vote ReadOnly and leave without waiting for phase two. This is not a yes vote for pending updates. [5]

Relations

related
Two-phase locking, Stable storage, Sinfonia, Presumed abort

Limitations

  • Long-lived prepared transactions can obstruct VACUUM and retain transaction IDs; preparation needs a recovery owner, not just a client-side deadline. [2]

Citations

  1. Gray and Lamport. Consensus on Transaction Commit. ACM TODS 31(1), 2006, pp. 133-160; author version, sections 2-5. (accessed 2026-11-16)
  2. PostgreSQL 16: PREPARE TRANSACTION; external manager, session detachment and retained locks. (accessed 2026-11-16)
  3. PostgreSQL 16: Two-Phase Transactions; shared memory, WAL, checkpoint files and pg_prepared_xacts. (accessed 2026-11-16)
  4. Bernstein, Hadzilacos and Goodman. Concurrency Control and Recovery in Database Systems (1987), chapter 3, section 3.2. (accessed 2026-11-16)
  5. Narayana Project Documentation: Resource.prepare, VoteReadOnly and two-phase commit recovery. (accessed 2026-11-16)
Sources & caveats
  • Primary-source synthesis of the transaction-commit protocol and PostgreSQL 16 participant behavior. No distributed database, failure injection or performance experiment was executed.
  • The paper is the author-hosted version revised 5 July 2017 of the 2006 publication. Progress assumptions are distinct from safety under delayed messages.

JSON: /data/terms/two-phase-commit.json

Published by Hesela team · Updated

Report a correction · Editorial standards · Reuse guidance

Cite this page
Hesela team. Two-phase commit. Hesela. 2026-11-16. https://hesela.dev/terms/two-phase-commit/