Document prepared transaction lock waits with two_phase subscriptions

Поиск
Список
Период
Сортировка
Искать

Document prepared transaction lock waits with two_phase subscriptions

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Вложения:
Hi,

While reviewing the prepared-transaction locking hazard involving the 
conflict log table [1], we noticed that the issue is not specific to the 
conflict log table and also affects regular user tables.

When two_phase is enabled, a prepared transaction on the publisher is 
also prepared on the subscriber, and like any prepared transaction, it 
continues to hold its locks until COMMIT PREPARED or ROLLBACK PREPARED. 
On the subscriber, if a replicated prepared transaction holds a lock on 
a user table and a local session then tries to acquire a conflicting 
lock on the same table (e.g., via TRUNCATE, VACUUM FULL, or ALTER 
TABLE), the local session will block.

In this state, if there are further replicated changes on the same 
table, the apply worker will also be blocked when acquiring the table 
lock to apply those changes. Because the apply worker cannot advance to 
subsequent transactions, even if COMMIT PREPARED or ROLLBACK PREPARED is 
issued on the publisher, the apply worker will not be able to reach and 
apply it. As a result, replication stalls because the sessions wait for 
each other, and the deadlock detector does not detect this wait cycle 
since the prepared transaction is not waiting on any lock. This is 
similar to the existing caution in the prepared transaction documentation:

"""
   It is unwise to leave transactions in the prepared state for a long 
time ... Keep in mind also that the transaction 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.
"""

To recover from this state, the user can either cancel the waiting local 
session on the subscriber so the apply worker can proceed, or manually 
roll back the prepared transaction on the subscriber, disable two_phase 
until that transaction is replicated again after being committed on the 
publisher, and then re-enable two_phase if required. Considering the 
existing caution about prepared transactions and the available 
workarounds, we suggest documenting this locking hazard and how to 
resolve it for logical replication as well. Please see the attached 
patch for the documentation changes.

Apart from documenting this, Amit, Nisha, and I also discussed off-list 
whether the apply worker could detect and resolve this wait cycle 
automatically. One option we considered is to have the apply worker walk 
its lock blocking chain once a lock wait exceeds deadlock_timeout. 
Starting from the lock it is waiting for, the worker would trace 
conflicting lock holders and conflicting waiters ahead of it in the wait 
queue (similar to how pg_blocking_pids() finds blockers) to see whether 
the chain leads back to a prepared transaction of the same subscription, 
and if so, cancel the session waiting on that prepared transaction (or 
raise an error if the worker is directly blocked by its own prepared 
transaction). However, this effectively introduces a separate wait-graph 
traversal alongside the existing deadlock detector, which seems overly 
complex for this scenario.

We also thought about a much simpler check where, on timeout, the apply 
worker only inspects the lock it is currently waiting on and treats it 
as a wait cycle if a prepared transaction of the same subscription also 
holds a lock on that same table. Unfortunately, that does not cover 
multi-table cases. For example, suppose a prepared transaction holds a 
lock on table t1, a local session acquires AccessExclusiveLock on table 
t2 and then blocks trying to TRUNCATE t1, and the apply worker next 
tries to apply a change to t2. Because the apply worker is waiting on t2 
while the prepared transaction holds the lock on t1, inspecting only 
t2's lock would miss the cycle.

Given these trade-offs, we prefer to document this locking hazard and 
the workarounds for now.

[1] 
https://www.postgresql.org/message-id/CAJpy0uAWb7hs0271M_NxhOpetAfFcQidrUce5xSOL8jU3Hx1iQ%40mail.gmail.com

-- 
Best Regards,
Zhijie Hou

Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Fri, Nov 13, 2026 at 11:58:10AM +0800, Zhijie Hou wrote:
> 
> To recover from this state, the user can either cancel the waiting local
> session on the subscriber so the apply worker can proceed, or manually roll
> back the prepared transaction on the subscriber, disable two_phase until
> that transaction is replicated again after being committed on the publisher,
> and then re-enable two_phase if required. Considering the existing caution
> about prepared transactions and the available workarounds, we suggest
> documenting this locking hazard and how to resolve it for logical
> replication as well.

Yeah, +1 for documenting it. 

> Please see the attached patch for the documentation changes.

=== 1

I wonder if it would also be worth mentioning a nonzero lock_timeout as a possible
mitigation too?

=== 2

+    logical replication apply worker can be completed only by that worker, see

s/can be completed only by that worker/is normally completed by the subscription's apply worker/?

=== 3

+          ALTER TABLE), it has to wait, and later lock
+          requests on that table

s/later lock requests/later conflicting lock requests/?

=== 4

+          The apply worker then cannot reach
+          the COMMIT PREPARED or ROLLBACK
+          PREPARED that would release the lock, and the sessions
+          wait for each other.  This is not detected as a deadlock, so
+          replication stalls.

what about?

or ROLLBACK PREPARED. This situation is not detected as a
deadlock, so replication can remain stalled indefinitely.

> Apart from documenting this, Amit, Nisha, and I also discussed off-list
> whether the apply worker could detect and resolve this wait cycle
> automatically. One option we considered is to have the apply worker walk its
> lock blocking chain once a lock wait exceeds deadlock_timeout. Starting from
> the lock it is waiting for, the worker would trace conflicting lock holders
> and conflicting waiters ahead of it in the wait queue (similar to how
> pg_blocking_pids() finds blockers) to see whether the chain leads back to a
> prepared transaction of the same subscription, and if so, cancel the session
> waiting on that prepared transaction (or raise an error if the worker is
> directly blocked by its own prepared transaction). However, this effectively
> introduces a separate wait-graph traversal alongside the existing deadlock
> detector, which seems overly complex for this scenario.

Yeah, I agree. Teaching the lock manager about this logical dependency seems too
complex.

> We also thought about a much simpler check where, on timeout, the apply
> worker only inspects the lock it is currently waiting on and treats it as a
> wait cycle if a prepared transaction of the same subscription also holds a
> lock on that same table. Unfortunately, that does not cover multi-table
> cases.

Agreed. FWIW, lock_timeout on the local session handles this multi-table case too.

> Given these trade-offs, we prefer to document this locking hazard and the
> workarounds for now.

Another options I could see (because the doc changes explain the workarounds)
would be to help diagnose:

1/ improve log_lock_waits to identify a prepared transaction holding a lock by
its XID, instead of reporting it as process 0.

2/ improve the existing pg_locks example query so that it identifies locks
held by both newly prepared and recovered prepared transactions.

Thoughts?

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Вложения:
Hi,

On Mon, Nov16, 2026 at 05:36 PM Bertrand Drouvot wrote:
>
> On Fri, Nov 13, 2026 at 11:58:10AM +0800, Zhijie Hou wrote:
> >
> > To recover from this state, the user can either cancel the waiting
> > local session on the subscriber so the apply worker can proceed, or
> > manually roll back the prepared transaction on the subscriber,
> > disable two_phase until that transaction is replicated again after
> > being committed on the publisher, and then re-enable two_phase if
> > required. Considering the existing caution about prepared
> > transactions and the available workarounds, we suggest documenting
> > this locking hazard and how to resolve it for logical replication as
> > well.
>
> Yeah, +1 for documenting it.
>
> > Please see the attached patch for the documentation changes.

Thanks for reviewing.

>
> === 1
>
> I wonder if it would also be worth mentioning a nonzero lock_timeout
> as a possible mitigation too?

Before adding this, I'd like to confirm what would be a good way to 
suggest users configure lock_timeout here. Setting lock_timeout globally 
(or at the database level) would also apply to the logical replication 
apply worker itself (potentially causing it to error out on normal lock 
waits). On the other hand, setting it per session/transaction before 
running commands that take conflicting locks (such as TRUNCATE, ALTER 
TABLE, LOCK, etc.) avoids that, though there are many commands that can 
take conflicting locks so users would need to remember to set it 
whenever running them (or set it per role for administrative sessions). 
Do you have per-session usage in mind, or another way of using lock_timeout?

>
> === 2
>
> +    logical replication apply worker can be completed only by that worker, see
>
> s/can be completed only by that worker/is normally completed by the subscription's apply worker/?
>
> === 3
>
> +          ALTER TABLE), it has to wait, and later lock
> +          requests on that table
>
> s/later lock requests/later conflicting lock requests/?
>
> === 4
>
> +          The apply worker then cannot reach
> +          the COMMIT PREPARED or ROLLBACK
> +          PREPARED that would release the lock, and the sessions
> +          wait for each other.  This is not detected as a deadlock, so
> +          replication stalls.
>
> what about?
>
> or ROLLBACK PREPARED. This situation is not
> detected as a deadlock, so replication can remain stalled
> indefinitely.

Thanks 2, 3 and 4 look good to me and addressed in V2.


> > Given these trade-offs, we prefer to document this locking hazard
> > and the workarounds for now.
>
> Another options I could see (because the doc changes explain the
> workarounds) would be to help diagnose:
>
> 1/ improve log_lock_waits to identify a prepared transaction holding a
> lock by its XID, instead of reporting it as process 0.
>
> 2/ improve the existing pg_locks example query so that it identifies
> locks held by both newly prepared and recovered prepared transactions.
>
> Thoughts?

Both sound like useful diagnostic improvements for prepared transactions 
in general (even outside logical replication), so +1 for discussing them 
in a separate thread.

-- 
Best Regards,
Zhijie Hou

Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Mon, Nov 16, 2026 at 08:20:45PM +0800, Zhijie Hou wrote:
> On Mon, Nov16, 2026 at 05:36 PM Bertrand Drouvot wrote:
> > 
> > === 1
> > 
> > I wonder if it would also be worth mentioning a nonzero lock_timeout
> > as a possible mitigation too?
> 
> Before adding this, I'd like to confirm what would be a good way to suggest
> users configure lock_timeout here. Setting lock_timeout globally (or at the
> database level) would also apply to the logical replication apply worker
> itself (potentially causing it to error out on normal lock waits). On the
> other hand, setting it per session/transaction before running commands that
> take conflicting locks (such as TRUNCATE, ALTER TABLE, LOCK, etc.) avoids
> that, though there are many commands that can take conflicting locks so
> users would need to remember to set it whenever running them (or set it per
> role for administrative sessions). Do you have per-session usage in mind, or
> another way of using lock_timeout?

I had per-session/transaction usage in mind, before executing commands that might
request conflicting table locks. A per role setting also makes sense when a
dedicated role is used for administrative commands. I agree that we should not
recommend setting it globally or at the database level, as that would also affect
the apply workers.

What about juse one sentence, something like?

"
Using a nonzero lock_timeout in sessions or transactions that might request such
conflicting locks can prevent them from waiting indefinitely.
"

> Thanks 2, 3 and 4 look good to me and addressed in V2.

Thanks!

> > Another options I could see (because the doc changes explain the
> > workarounds) would be to help diagnose:
> > 
> > 1/ improve log_lock_waits to identify a prepared transaction holding a
> > lock by its XID, instead of reporting it as process 0.
> > 
> > 2/ improve the existing pg_locks example query so that it identifies
> > locks held by both newly prepared and recovered prepared transactions.
> > 
> > Thoughts?
> 
> Both sound like useful diagnostic improvements for prepared transactions in
> general (even outside logical replication), so +1 for discussing them in a
> separate thread.

Thanks for confirming, I'll open a dedicated thread.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Mon, Nov 16, 2026 at 7:14 PM Bertrand Drouvot
 wrote:
>
> On Mon, Nov 16, 2026 at 08:20:45PM +0800, Zhijie Hou wrote:
> > On Mon, Nov16, 2026 at 05:36 PM Bertrand Drouvot wrote:
> > >
> > > === 1
> > >
> > > I wonder if it would also be worth mentioning a nonzero lock_timeout
> > > as a possible mitigation too?
> >
> > Before adding this, I'd like to confirm what would be a good way to suggest
> > users configure lock_timeout here. Setting lock_timeout globally (or at the
> > database level) would also apply to the logical replication apply worker
> > itself (potentially causing it to error out on normal lock waits). On the
> > other hand, setting it per session/transaction before running commands that
> > take conflicting locks (such as TRUNCATE, ALTER TABLE, LOCK, etc.) avoids
> > that, though there are many commands that can take conflicting locks so
> > users would need to remember to set it whenever running them (or set it per
> > role for administrative sessions). Do you have per-session usage in mind, or
> > another way of using lock_timeout?
>
> I had per-session/transaction usage in mind, before executing commands that might
> request conflicting table locks. A per role setting also makes sense when a
> dedicated role is used for administrative commands. I agree that we should not
> recommend setting it globally or at the database level, as that would also affect
> the apply workers.
>
> What about juse one sentence, something like?
>
> "
> Using a nonzero lock_timeout in sessions or transactions that might request such
> conflicting locks can prevent them from waiting indefinitely.
> "

I think configuring such a timeout could be tricky for users
especially when the commands are from scripts. We don't
recommend/document such a setting for blocking due to prepared
transactions, for example, I find the following in the docs: "In cases
involving a conflicting prepared transaction, it may not be possible
to make progress until the prepared transaction commits or rolls
back." for serialization failure handling. So, on similar lines we
should try to avoid lock_timeout recommendation here but if you or
others still feel it is helpful then we can consider.

Overall, I agree with you and Hou-san that documenting the lock waits
due to prepared transactions with two_phase enabled subscriptions is a
better approach as we do at other places especially for back-branches.
Even if we decide to do some special lock-detection mechanism for
prepared transactions (by apply worker) as Hou-San mentioned in his
first email, it should be for HEAD but considering the documented
behavior of prepared transactions and no user report, I feel we don't
need to go there.

-- 
With Regards,
Amit Kapila.


RE: Document prepared transaction lock waits with two_phase subscriptions

От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Hi,

Thanks for posting the patch. I feel the logging should be improved, but
it might be out-of-scope. Here I focused on the doc patch.

```
+          or manually roll back the prepared transaction on the subscriber,
+          disable two_phase (see
+          ALTER SUBSCRIPTION ... SET)
+          until that transaction is committed on the publisher and replicated
+          again, and then re-enable two_phase if required.
```

Isn't it better to disable the subscription before rolling back? If the publisher
runs COMMIT/ROLLBACK PREPARED soon, the resumed apply worker raises
"prepared transaction ... does not exist" error.

Best regards,
Hayato Kuroda
FUJITSU LIMITED



Re: Document prepared transaction lock waits with two_phase subscriptions

От:
shveta malik <shveta.malik@gmail.com>
Дата:
I looked at v2 and have a few suggestions, which you can take if you agree.

1)
+ This situation is not
+          detected as a deadlock, so replication can remain stalled
+          indefinitely.

How about a little elaborate:

Because the prepared transaction is not waiting on any lock itself,
this is not detected as a deadlock, and replication can remain stalled
indefinitely.

2)

+          indefinitely.  To resolve this, either cancel the waiting session,
+          or manually roll back the prepared transaction on the subscriber,
+          disable two_phase (see
+          ALTER SUBSCRIPTION
... SET)
+          until that transaction is committed on the publisher and replicated
+          again, and then re-enable two_phase if required.
+         


--We can separate 2 suggestions, else it becomes difficult to
understand (at-least I find it difficult).
--Also we can improve second suggestion a bit.

How about:

To recover, preferably cancel the waiting session, allowing the apply
worker to proceed and complete the prepared transaction normally.
Alternatively, execute ROLLBACK PREPARED on the subscriber to release
the locks. However, this discards the changes made by the prepared
transaction
on the subscriber, so the transaction must be re-applied once it is
committed on the publisher, to keep the subscriber consistent with the
publisher. To achieve this, temporarily disable two_phase using ALTER
SUBSCRIPTION ... SET until the transaction is committed on the
publisher and replicated to the subscriber, then re-enable it.

thanks
Shveta


Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
On Mon, Nov 16, 2026 at 6:54 PM Amit Kapila  wrote:
>
> On Mon, Nov 16, 2026 at 7:14 PM Bertrand Drouvot
>  wrote:
> >
> > On Mon, Nov 16, 2026 at 08:20:45PM +0800, Zhijie Hou wrote:
> > > On Mon, Nov16, 2026 at 05:36 PM Bertrand Drouvot wrote:
> > > >
> > > > === 1
> > > >
> > > > I wonder if it would also be worth mentioning a nonzero lock_timeout
> > > > as a possible mitigation too?
> > >
> > > Before adding this, I'd like to confirm what would be a good way to suggest
> > > users configure lock_timeout here. Setting lock_timeout globally (or at the
> > > database level) would also apply to the logical replication apply worker
> > > itself (potentially causing it to error out on normal lock waits). On the
> > > other hand, setting it per session/transaction before running commands that
> > > take conflicting locks (such as TRUNCATE, ALTER TABLE, LOCK, etc.) avoids
> > > that, though there are many commands that can take conflicting locks so
> > > users would need to remember to set it whenever running them (or set it per
> > > role for administrative sessions). Do you have per-session usage in mind, or
> > > another way of using lock_timeout?
> >
> > I had per-session/transaction usage in mind, before executing commands that might
> > request conflicting table locks. A per role setting also makes sense when a
> > dedicated role is used for administrative commands. I agree that we should not
> > recommend setting it globally or at the database level, as that would also affect
> > the apply workers.
> >
> > What about juse one sentence, something like?
> >
> > "
> > Using a nonzero lock_timeout in sessions or transactions that might request such
> > conflicting locks can prevent them from waiting indefinitely.
> > "
>
> I think configuring such a timeout could be tricky for users
> especially when the commands are from scripts. We don't
> recommend/document such a setting for blocking due to prepared
> transactions, for example, I find the following in the docs: "In cases
> involving a conflicting prepared transaction, it may not be possible
> to make progress until the prepared transaction commits or rolls
> back." for serialization failure handling. So, on similar lines we
> should try to avoid lock_timeout recommendation here but if you or
> others still feel it is helpful then we can consider.
>
> Overall, I agree with you and Hou-san that documenting the lock waits
> due to prepared transactions with two_phase enabled subscriptions is a
> better approach as we do at other places especially for back-branches.
> Even if we decide to do some special lock-detection mechanism for
> prepared transactions (by apply worker) as Hou-San mentioned in his
> first email, it should be for HEAD but considering the documented
> behavior of prepared transactions and no user report, I feel we don't
> need to go there.

+1 for documenting this behavior. I think we can backpatch it.

Regarding the proposed description, I think it would be better to
suggest cancelling the waiting session as the primary way to resolve
the situation, and to mention manually rolling back the prepared
transaction as an alternative. Cancelling the waiting session always
unblocks the apply worker, whereas the latter requires several steps.
On the other hand, manually rolling back the prepared transaction
requires several steps: disabling the subscription, waiting for the
worker to exit, setting two_phase to false, and enabling the
subscription again etc.. Also, I think the description should say to
disable the subscription first, then roll back the prepared
transaction, set two_phase to false, and enable the subscription again
as Kuroda-san suggested. It might also be worth noting that the
transaction is applied again after the waiting command completes.

Regards,

-- 
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com


Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Вложения:
Hi,

On Tue, Nov17, 2026 at 01:25 PM "Hayato Kuroda (Fujitsu)" wrote:
>
> Thanks for posting the patch. I feel the logging should be improved,
> but it might be out-of-scope. Here I focused on the doc patch.

Thanks for the comments.

>
> ```
> +          or manually roll back the prepared transaction on the subscriber,
> +          disable two_phase (see
> +          ALTER SUBSCRIPTION ... SET)
> +          until that transaction is committed on the publisher and replicated
> +          again, and then re-enable two_phase if required.
> ```
>
> Isn't it better to disable the subscription before rolling back? If
> the publisher runs COMMIT/ROLLBACK PREPARED soon, the resumed apply
> worker raises "prepared transaction ... does not exist" error.

Although I think the ERROR won't affect the recoevry steps as the 
transaction can be replicated anyway after disabling the two_phase, I 
agree that the suggested order looks cleaner, so changed.

Here is the V3 patches for all branches.


-- 
Best Regards,
Zhijie Hou

Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Hi,

On Tue, Nov17, 2026 at 02:26 PM shveta malik wrote:
> I looked at v2 and have a few suggestions, which you can take if you agree.

Thanks for the comments.

> 1)
> + This situation is not
> +          detected as a deadlock, so replication can remain stalled
> +          indefinitely.
>
> How about a little elaborate:
>
> Because the prepared transaction is not waiting on any lock itself,
> this is not detected as a deadlock, and replication can remain stalled
> indefinitely.

I think if a transaction can be prepared, it naturally means it doesn't 
conflict with the others, so I'd keep the current version in the patch.


>
> 2)
>
> +          indefinitely.  To resolve this, either cancel the waiting session,
> +          or manually roll back the prepared transaction on the subscriber,
> +          disable two_phase (see
> +           linkend="sql-altersubscription-params-set">ALTER SUBSCRIPTION
> ... SET)
> +          until that transaction is committed on the publisher and replicated
> +          again, and then re-enable two_phase if required.
> +         
>
>
> --We can separate 2 suggestions, else it becomes difficult to
> understand (at-least I find it difficult).

Agreed.

> --Also we can improve second suggestion a bit.
>
> How about:
>
> To recover, preferably cancel the waiting session, allowing the apply
> worker to proceed and complete the prepared transaction normally.
> Alternatively, execute ROLLBACK PREPARED on the subscriber to release
> the locks. However, this discards the changes made by the prepared
> transaction on the subscriber, so the transaction must be re-applied
> once it is committed on the publisher, to keep the subscriber
> consistent with the publisher. To achieve this, temporarily disable
> two_phase using ALTER SUBSCRIPTION ... SET until the transaction is
> committed on the publisher and replicated to the subscriber, then
> re-enable it.

The proposed text for the second point feels a bit verbose to me. I'd 
prefer to keep it short and clean.


-- 
Best Regards,
Zhijie Hou


Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Hi,

On Tue, Nov17, 2026 at 02:49 PM Masahiko Sawada wrote:
>
> +1 for documenting this behavior. I think we can backpatch it.
>
> Regarding the proposed description, I think it would be better to
> suggest cancelling the waiting session as the primary way to resolve
> the situation, and to mention manually rolling back the prepared
> transaction as an alternative. Cancelling the waiting session always
> unblocks the apply worker, whereas the latter requires several steps.
> On the other hand, manually rolling back the prepared transaction
> requires several steps: disabling the subscription, waiting for the
> worker to exit, setting two_phase to false, and enabling the
> subscription again etc.. Also, I think the description should say to
> disable the subscription first, then roll back the prepared
> transaction, set two_phase to false, and enable the subscription again
> as Kuroda-san suggested. It might also be worth noting that the
> transaction is applied again after the waiting command completes.

Thanks for the comments. I think V3[1] covers the points you mentioned.

what do you think?

[1] 
https://www.postgresql.org/message-id/51a90195-3a5c-429b-a978-e639df8bc225%40gmail.com

-- 
Best Regards,
Zhijie Hou


Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Nisha Moond <nisha.moond412@gmail.com>
Дата:
On Tue, Nov 17, 2026 at 12:29 PM Zhijie Hou  wrote:
>
> Here is the V3 patches for all branches.
>
>

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, two_phase
+          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
           two_phase (see
           ALTER SUBSCRIPTION
... SET),
-          and re-enable the subscription.  Once the transaction is finished
-          on the publisher and replicated again, two_phase
-          can be re-enabled if required.
+          and re-enable the subscription.  The transaction is replicated again
+          when it is committed on the publisher.
          

Thoughts?

--
Thanks,
Nisha


Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Вложения:
Hi,

On Tue, Nov17, 2026 at 05:44 PM Nisha Moond wrote:
> On Tue, Nov 17, 2026 at 12:29 PM Zhijie Hou  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, two_phase
> +          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
>             two_phase (see
>              linkend="sql-altersubscription-params-set">ALTER SUBSCRIPTION
> ... SET),
> -          and re-enable the subscription.  Once the transaction is finished
> -          on the publisher and replicated again, two_phase
> -          can be re-enabled if required.
> +          and re-enable the subscription.  The transaction is replicated again
> +          when it is committed on the publisher.
>            
>
> Thoughts?

Agreed. Here is the updated patches.


-- 
Best Regards,
Zhijie Hou

RE: Document prepared transaction lock waits with two_phase subscriptions

От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Hi,

Thanks for updating the patch. LGTM.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Re: Document prepared transaction lock waits with two_phase subscriptions

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Wed, Nov 18, 2026 at 10:49 AM Hayato Kuroda (Fujitsu)
 wrote:
>
> Thanks for updating the patch. LGTM.
>

Pushed.

-- 
With Regards,
Amit Kapila.


FAQ