{
  "slug": "two-phase-locking",
  "term": "Two-phase locking",
  "aliases": [
    "2PL"
  ],
  "definition": "Two-phase locking is a transaction concurrency-control protocol in which a transaction acquires no new locks after releasing its first lock.",
  "note": "In the basic shared/exclusive locking model, shared locks permit concurrent readers; exclusive locks conflict with other readers and writers. With suitable locks covering every conflicting access, the two-phase discipline produces conflict-serializable histories. [bhg-locking]\n\nBasic 2PL can release locks before commit and permit cascading aborts. A common strict variant retains exclusive locks through commit or abort; rigorous 2PL retains shared locks too. Upgrades belong to the growing phase, downgrades to the shrinking phase. [dbsc-locking]\n\nNames vary: Bernstein, Hadzilacos and Goodman call the all-locks-until-completion variant Strict 2PL. Specify the retention rule instead of relying on the adjective alone. [bhg-locking]\n\nFor predicate-based queries, lock coverage must include changes to predicate membership, including possible inserts, not merely rows returned by a query. An empty result can still carry a dependency. [berenson-isolation]\n\nPostgreSQL's Serializable SIReadLock tracks dependencies without blocking writers; it is not a blocking 2PL read lock. [postgres-isolation]\n\nTwo-phase commit (2PC) coordinates a transaction's commit decision across participants, not its growing and shrinking lock phases. PostgreSQL PREPARE TRANSACTION serves an external transaction manager; the prepared transaction retains its locks until resolved. [postgres-prepare]\n\nLomet's key-range locking separates a key from the range it bounds. In the paper's unique-key model, combining them with ordinary multigranularity modes can block a logically nonconflicting insertion before a singleton-read key. Composite range/key modes preserve that distinction while using one named resource. Fewer lock resources need not mean the same permitted concurrency. [lomet-krl]",
  "status": "shipping",
  "advantages": [],
  "limitations": [
    {
      "text": "2PL does not prevent deadlock; waiting transactions may need to be aborted and retried.",
      "citationIds": [
        "bhg-locking"
      ]
    },
    {
      "text": "A locking protocol alone does not specify crash durability or the distributed commit procedure.",
      "citationIds": [
        "bhg-locking",
        "postgres-prepare"
      ]
    }
  ],
  "related": [
    "serializability",
    "serializable-snapshot-isolation",
    "write-skew"
  ],
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "provenance": [
    "Primary-source synthesis; no database engine or failure experiment was run."
  ],
  "citations": [
    {
      "id": "bhg-locking",
      "title": "Bernstein, Hadzilacos and Goodman. Concurrency Control and Recovery in Database Systems (1987), chapter 3, sections 3.2-3.5 and 3.7.",
      "url": "https://www.microsoft.com/en-us/research/wp-content/uploads/2016/05/chapter3.pdf",
      "accessed": "2026-11-15"
    },
    {
      "id": "dbsc-locking",
      "title": "Silberschatz, Korth and Sudarshan. Database System Concepts, fifth edition: chapter 16 author slides, 16.8-16.11.",
      "url": "https://www.db-book.com/Previous-editions/db5/slide-dir/ch16.pdf",
      "accessed": "2026-11-15"
    },
    {
      "id": "berenson-isolation",
      "title": "Berenson et al. A Critique of ANSI SQL Isolation Levels. SIGMOD 1995; MSR-TR-95-51, section 2.3.",
      "url": "https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tr-95-51.pdf",
      "accessed": "2026-11-15"
    },
    {
      "id": "postgres-isolation",
      "title": "PostgreSQL 18: Transaction Isolation, section 13.2.3.",
      "url": "https://www.postgresql.org/docs/18/transaction-iso.html",
      "accessed": "2026-11-15"
    },
    {
      "id": "postgres-prepare",
      "title": "PostgreSQL 18: PREPARE TRANSACTION, purpose and prepared-state lock retention.",
      "url": "https://www.postgresql.org/docs/18/sql-prepare-transaction.html",
      "accessed": "2026-11-15"
    },
    {
      "id": "lomet-krl",
      "title": "Lomet. Key Range Locking Strategies for Improved Concurrency. VLDB 1993, pp. 655-664, sections 3.2 and 4.1.",
      "url": "https://www.vldb.org/conf/1993/P655.PDF",
      "accessed": "2026-11-15"
    },
    {
      "id": "hesela-consistency",
      "title": "Hesela: Storage consistency, two-phase locking and dependency coverage.",
      "url": "https://hesela.com/analyses/storage-consistency-ordering/#two-phase-locking",
      "accessed": "2026-11-15"
    }
  ],
  "updated": "2026-11-15"
}
