postgres.git / summary / log / commit / refs

commit    3ec081c4ccabd6c918761148e5a1fa5fd2db60a2
Author:   Amit Kapila <akapila@postgresql.org>
Date:     Wed Nov 18 05:13:38 2026 +0000

    Doc: Explain prepared transaction lock waits with two_phase subscriptions.
    
    A transaction prepared by the apply worker retains its locks until the
    corresponding COMMIT PREPARED or ROLLBACK PREPARED is applied. If another
    session waits for a conflicting lock held by the prepared transaction, the
    apply worker's subsequent conflicting lock requests can queue behind that
    session. This can prevent the apply worker from reaching the
    COMMIT PREPARED or ROLLBACK PREPARED that would release the prepared
    transaction's locks.
    
    The deadlock detector cannot detect this situation because a prepared
    transaction does not wait for any locks and therefore cannot participate
    in a deadlock cycle. As a result, replication can remain stalled
    indefinitely.
    
    Document this behavior and the recovery procedure under
    CREATE SUBSCRIPTION and PREPARE TRANSACTION.
    
    Author: Zhijie Hou <houzhijie22@gmail.com>
    Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
    Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com>
    Reviewed-by: shveta malik <shveta.malik@gmail.com>
    Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
    Reviewed-by: Nisha Moond <nisha.moond412@gmail.com>
    Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
    Discussion: https://postgr.es/m/72c9b956-96c3-40a5-99bb-d9b7338c0bab@gmail.com
    Backpatch-through: 15


doc/src/sgml/ref/create_subscription.sgml | 22 ++++++++++++++++++++++ doc/src/sgml/ref/prepare_transaction.sgml | 6 +++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/ref/create_subscription.sgml b/doc/src/sgml/ref/create_subscription.sgml index 36ace635595..b75c648eb16 100644 --- a/doc/src/sgml/ref/create_subscription.sgml +++ b/doc/src/sgml/ref/create_subscription.sgml @@ -375,6 +375,28 @@ CREATE SUBSCRIPTION <replaceable class="parameter">subscription_name</replaceabl are then processed immediately by the subscriber. </para> + <para> + A prepared transaction applied on the subscriber keeps its locks + until the apply worker processes the corresponding + <command>COMMIT PREPARED</command> or <command>ROLLBACK + PREPARED</command>. If another session requests a conflicting lock + on a table modified by such a transaction (for example, with + <command>LOCK</command>, <command>TRUNCATE</command>, or + <command>ALTER TABLE</command>), it has to wait, and later + conflicting lock requests on that table, including the apply + worker's own next change to it, queue behind it. The apply worker + then cannot reach the <command>COMMIT PREPARED</command> or + <command>ROLLBACK PREPARED</command>. This situation is not + detected as a deadlock, so replication can remain stalled + indefinitely. To resolve this, cancel the waiting session. + Alternatively, disable the subscription, manually roll back the + prepared transaction on the subscriber, disable and then re-enable + <literal>two_phase</literal> (see + <link linkend="sql-altersubscription-params-set"><literal>ALTER SUBSCRIPTION ... SET</literal></link>), + and re-enable the subscription. The transaction is replicated again + when it is committed on the publisher. + </para> + <para> The implementation of two-phase commit requires that replication has successfully finished the initial table synchronization diff --git a/doc/src/sgml/ref/prepare_transaction.sgml b/doc/src/sgml/ref/prepare_transaction.sgml index f4f6118ac31..a9778866496 100644 --- a/doc/src/sgml/ref/prepare_transaction.sgml +++ b/doc/src/sgml/ref/prepare_transaction.sgml @@ -132,7 +132,11 @@ PREPARE TRANSACTION <replaceable class="parameter">transaction_id</replaceable> continues to hold whatever locks it held. The intended usage of the feature is that a prepared transaction will normally be committed or rolled back as soon as an external transaction manager has verified that - other databases are also prepared to commit. + other databases are also prepared to commit. A transaction prepared by a + logical replication apply worker is normally completed by the + subscription's apply worker; see + <xref linkend="sql-createsubscription-params-with-two-phase"/> for a + lock wait this can cause. </para> <para> [parent: 03a2086a497b]