Hi,

On Tue, Nov17, 2026 at 05:44 PM Nisha Moond wrote:
On Tue, Nov 17, 2026 at 12:29 PM Zhijie Hou <[email protected]> wrote:
>

Thanks for the patch. +1 for documenting this. The doc change looks
good to me. I have one suggestion:

+          and re-enable the subscription.  Once the transaction is finished
+          on the publisher and replicated again, <literal>two_phase</literal>
+          can be re-enabled if required.

I don't think we need to wait for the prepared transaction to finish
on the publisher. two_phase can be re-enabled right after it is
disabled, while the subscription is still disabled, and the
transaction is still replicated correctly when the publisher commits
it. (Confirmed by test)

This works because re-enabling two_phase sets the slot's two_phase_at
to where replication resumes. The subscriber had already applied the
PREPARE, so that point is after it. The publisher then sends the whole
transaction at commit time.
So the steps could be:

            Alternatively, disable the subscription, manually roll back the
-          prepared transaction on the subscriber, disable
+          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.  Once the transaction is finished
-          on the publisher and replicated again, <literal>two_phase</literal>
-          can be re-enabled if required.
+          and re-enable the subscription.  The transaction is replicated again
+          when it is committed on the publisher.
           </para>

Thoughts?

Agreed. Here is the updated patches.


--
Best Regards,
Zhijie Hou
From 9c4bfb874b6e49f3664bcdfdf67e825b74451793 Mon Sep 17 00:00:00 2001
From: Zhijie Hou <[email protected]>
Date: Tue, 17 Nov 2026 17:51:01 +0800
Subject: [PATCH v4] Document prepared transaction lock waits with two_phase
 subscriptions.

When two_phase is enabled for a subscription, a transaction prepared by
the apply worker continues to hold its locks until COMMIT PREPARED or
ROLLBACK PREPARED is applied. If another session requests a conflicting
lock on a table modified by that prepared transaction, it waits behind
the prepared transaction, and subsequent conflicting lock requests by
the apply worker on the same table queue behind that session. Because
the prepared transaction is not waiting on any lock, this situation is
not detected as a deadlock and replication can remain stalled
indefinitely.

Document this caveat and how to recover from it under CREATE SUBSCRIPTION
and PREPARE TRANSACTION.

Author: Zhijie Hou <[email protected]>
Reviewed-by: Bertrand Drouvot <[email protected]>
Reviewed-by: Hayato Kuroda <[email protected]>
Reviewed-by: shveta malik <[email protected]>
Reviewed-by: Masahiko Sawada <[email protected]>
Reviewed-by: Nisha Moond <[email protected]>
Reviewed-by: Amit Kapila <[email protected]>
Discussion: https://postgr.es/m/[email protected]
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 6248c228a64..92552e97009 100644
--- a/doc/src/sgml/ref/create_subscription.sgml
+++ b/doc/src/sgml/ref/create_subscription.sgml
@@ -424,6 +424,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>
-- 
2.54.0 (Apple Git-157)

From e189f3d05020b898bb91fbba8bb60503e80f2e90 Mon Sep 17 00:00:00 2001
From: Zhijie Hou <[email protected]>
Date: Tue, 17 Nov 2026 17:03:44 +0800
Subject: [PATCH v4] Document prepared transaction lock waits with two_phase
 subscriptions.

When two_phase is enabled for a subscription, a transaction prepared by
the apply worker continues to hold its locks until COMMIT PREPARED or
ROLLBACK PREPARED is applied. If another session requests a conflicting
lock on a table modified by that prepared transaction, it waits behind
the prepared transaction, and subsequent conflicting lock requests by
the apply worker on the same table queue behind that session. Because
the prepared transaction is not waiting on any lock, this situation is
not detected as a deadlock and replication can remain stalled
indefinitely.

Document this caveat and how to recover from it under CREATE SUBSCRIPTION
and PREPARE TRANSACTION.

Author: Zhijie Hou <[email protected]>
Reviewed-by: Bertrand Drouvot <[email protected]>
Reviewed-by: Hayato Kuroda <[email protected]>
Reviewed-by: shveta malik <[email protected]>
Reviewed-by: Masahiko Sawada <[email protected]>
Reviewed-by: Nisha Moond <[email protected]>
Reviewed-by: Amit Kapila <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Backpatch-through: 15
---
 doc/src/sgml/ref/create_subscription.sgml | 16 ++++++++++++++++
 doc/src/sgml/ref/prepare_transaction.sgml |  6 +++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/ref/create_subscription.sgml 
b/doc/src/sgml/ref/create_subscription.sgml
index c9c8dd440dc..3d18ad60458 100644
--- a/doc/src/sgml/ref/create_subscription.sgml
+++ b/doc/src/sgml/ref/create_subscription.sgml
@@ -343,6 +343,22 @@ 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 until the waiting session is canceled.
+         </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..c3848fbc72c 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 the <literal>two_phase</literal>
+    parameter of <xref linkend="sql-createsubscription"/> for a lock wait
+    this can cause.
    </para>
 
    <para>
-- 
2.54.0 (Apple Git-157)

Reply via email to