{
  "slug": "two-phase-commit",
  "term": "Two-phase commit",
  "aliases": [
    "2PC"
  ],
  "definition": "Two-phase commit coordinates an all-or-nothing transaction outcome through a prepare/vote phase and a commit-or-abort decision phase.",
  "note": "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. [gray-lamport]\n\nIf 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. [gray-lamport]\n\nTwo-phase commit concerns the shared outcome; two-phase locking concerns concurrent access. [bhg-locking] 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. [postgres-prepare]\n\nIn 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. [postgres-two-phase]\n\nPaxos 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. [gray-lamport]\n\nWith 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. [narayana]",
  "status": "shipping",
  "advantages": [],
  "limitations": [
    {
      "text": "Long-lived prepared transactions can obstruct VACUUM and retain transaction IDs; preparation needs a recovery owner, not just a client-side deadline.",
      "citationIds": [
        "postgres-prepare"
      ]
    }
  ],
  "related": [
    "two-phase-locking",
    "stable-storage",
    "sinfonia",
    "presumed-abort"
  ],
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "updated": "2026-11-16",
  "citations": [
    {
      "id": "gray-lamport",
      "title": "Gray and Lamport. Consensus on Transaction Commit. ACM TODS 31(1), 2006, pp. 133-160; author version, sections 2-5.",
      "url": "https://lamport.azurewebsites.net/video/consensus-on-transaction-commit.pdf",
      "accessed": "2026-11-16"
    },
    {
      "id": "postgres-prepare",
      "title": "PostgreSQL 16: PREPARE TRANSACTION; external manager, session detachment and retained locks.",
      "url": "https://www.postgresql.org/docs/16/sql-prepare-transaction.html",
      "accessed": "2026-11-16"
    },
    {
      "id": "postgres-two-phase",
      "title": "PostgreSQL 16: Two-Phase Transactions; shared memory, WAL, checkpoint files and pg_prepared_xacts.",
      "url": "https://www.postgresql.org/docs/16/two-phase.html",
      "accessed": "2026-11-16"
    },
    {
      "id": "bhg-locking",
      "title": "Bernstein, Hadzilacos and Goodman. Concurrency Control and Recovery in Database Systems (1987), chapter 3, section 3.2.",
      "url": "https://www.microsoft.com/en-us/research/wp-content/uploads/2016/05/chapter3.pdf",
      "accessed": "2026-11-16"
    },
    {
      "id": "narayana",
      "title": "Narayana Project Documentation: Resource.prepare, VoteReadOnly and two-phase commit recovery.",
      "url": "https://www.narayana.io/docs/project/index.html",
      "accessed": "2026-11-16"
    }
  ],
  "provenance": [
    "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."
  ]
}
