pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Avoid resetting subscription two_phase state to pending when already enabled
18+ messages / 7 participants
[nested] [flat]

Commits from this thread:
  2026-11-05 fbaebf450d 1e1a4821a9 fc1900915b  Avoid resetting subscription two_phase state to pending when already enabled.
* Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 05:56  Zhijie Hou <houzhijie22@gmail.com>
  0 siblings, 2 replies; 18+ messages in thread

From: Zhijie Hou @ 2026-10-29 05:56 UTC (permalink / raw)
  To: pgsql-hackers@lists.postgresql.org; +Cc: amit.kapila16@gmail.com


Hi,

While reviewing another logical replication thread, I noticed an issue 
with altering a subscription's two_phase option.

In AlterSubscription(), executing ALTER SUBSCRIPTION ... SET (two_phase 
= true) unconditionally set subtwophasestate to 
LOGICALREP_TWOPHASE_STATE_PENDING, even when two_phase was already in 
LOGICALREP_TWOPHASE_STATE_ENABLED state.

Because two_phase remains enabled on the publisher's replication slot 
and prepared transactions may already exist on the subscriber, 
overwriting the state to PENDING broke checks that rely on 
subtwophasestate being ENABLED:

1. A subsequent ALTER SUBSCRIPTION ... SET (two_phase = false) skipped 
the LookupGXactBySubid() check and disabled two_phase while prepared 
transactions still existed on the subscriber. When those transactions 
were later committed on the publisher, the publisher re-sent all of 
their changes as regular transactions instead of COMMIT PREPARED, 
leading to self-deadlocks, duplicate data, or orphaned prepared 
transactions.

2. ALTER SUBSCRIPTION ... REFRESH PUBLICATION (and SET/ADD PUBLICATION) 
with copy_data = true was allowed even though the publisher's slot still 
had two_phase enabled, allowing prepared transactions on newly added 
tables to be streamed during initial table synchronization and applied 
as empty prepares.

I think we can fix this by not resetting subtwophasestate to PENDING 
when two_phase is already in the ENABLED state, as shown in the attached 
patch.

This issue has existed since PG 18, when altering the two_phase option 
was introduced, so it should be back-patched down to that branch.

-- 
Best Regards,
Zhijie Hou

Attachments:

v1-0001-Avoid-resetting-subscription-two_phase-state-to-p.patch (text/plain, 4.0K) download From 7afcf8a34aa479d2d81cc350352832b60ccaf844 Mon Sep 17 00:00:00 2001 From: Zhijie Hou <houzhijie22@gmail.com> Date: Thu, 29 Oct 2026 13:24:32 +0800 Subject: [PATCH v1] Avoid resetting subscription two_phase state to pending when already enabled. In AlterSubscription(), executing ALTER SUBSCRIPTION ... SET (two_phase = true) unconditionally set subtwophasestate to LOGICALREP_TWOPHASE_STATE_PENDING ('p'), even when two_phase was already in LOGICALREP_TWOPHASE_STATE_ENABLED ('e') state. Because two_phase remains enabled on the publisher's replication slot and prepared transactions may already exist on the subscriber, overwriting the state to 'p' broke checks that rely on subtwophasestate being 'e': 1. A subsequent ALTER SUBSCRIPTION ... SET (two_phase = false) skipped the LookupGXactBySubid() check and disabled two_phase while prepared transactions still existed on the subscriber. When those transactions were later committed on the publisher, the publisher re-sent all of their changes as regular transactions instead of COMMIT PREPARED, leading to self-deadlocks, duplicate data, or orphaned prepared transactions. 2. ALTER SUBSCRIPTION ... REFRESH PUBLICATION (and SET/ADD PUBLICATION) with copy_data = true was allowed even though the publisher's slot still had two_phase enabled, allowing prepared transactions on newly added tables to be streamed during initial table synchronization and applied as empty prepares. Fix this by not resetting subtwophasestate to PENDING when two_phase is already in ENABLED state. Author: Zhijie Hou <houzhijie22@gmail.com> Backpatch-through: 18 --- src/backend/commands/subscriptioncmds.c | 19 +++++++++++++------ src/test/subscription/t/021_twophase.pl | 10 ++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index 36bb20a1f6e..560b601a407 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -1963,12 +1963,19 @@ AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, errmsg("cannot disable \"two_phase\" when prepared transactions exist"), errhint("Resolve these transactions and try again."))); - /* Change system catalog accordingly */ - values[Anum_pg_subscription_subtwophasestate - 1] = - CharGetDatum(opts.twophase ? - LOGICALREP_TWOPHASE_STATE_PENDING : - LOGICALREP_TWOPHASE_STATE_DISABLED); - replaces[Anum_pg_subscription_subtwophasestate - 1] = true; + /* + * Change system catalog accordingly, but do not reset the + * state to PENDING if two_phase is already enabled. + */ + if (!opts.twophase || + sub->twophasestate != LOGICALREP_TWOPHASE_STATE_ENABLED) + { + values[Anum_pg_subscription_subtwophasestate - 1] = + CharGetDatum(opts.twophase ? + LOGICALREP_TWOPHASE_STATE_PENDING : + LOGICALREP_TWOPHASE_STATE_DISABLED); + replaces[Anum_pg_subscription_subtwophasestate - 1] = true; + } } if (IsSet(opts.specified_opts, SUBOPT_FAILOVER)) diff --git a/src/test/subscription/t/021_twophase.pl b/src/test/subscription/t/021_twophase.pl index f755e748e9c..eb85e8e43d3 100644 --- a/src/test/subscription/t/021_twophase.pl +++ b/src/test/subscription/t/021_twophase.pl @@ -447,6 +447,16 @@ $node_subscriber->safe_psql('postgres', $node_subscriber->poll_query_until('postgres', "SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'logical replication apply worker'" ); + +# Verify that setting two_phase to true when it is already enabled does not +# overwrite subtwophasestate to 'p' (pending). +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = true);"); +$result = $node_subscriber->safe_psql('postgres', + "SELECT subtwophasestate FROM pg_subscription WHERE subname = 'tap_sub_copy';" +); +is($result, qq(e), 'two-phase subscription option remains enabled'); + $node_subscriber->safe_psql( 'postgres', " ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = false); -- 2.54.0 (Apple Git-157)
^ permalink raw reply [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 06:33  shveta malik <shveta.malik@gmail.com>
  parent: Zhijie Hou <houzhijie22@gmail.com>
  1 sibling, 1 reply; 18+ messages in thread

From: shveta malik @ 2026-10-29 06:33 UTC (permalink / raw)
  To: Zhijie Hou <houzhijie22@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org, amit.kapila16@gmail.com, shveta malik <shvetamalik@gmail.com>

On Thu, Oct 29, 2026 at 11:27 AM Zhijie Hou <houzhijie22@gmail.com> wrote:
>
>
> Hi,
>
> While reviewing another logical replication thread, I noticed an issue
> with altering a subscription's two_phase option.
>
> In AlterSubscription(), executing ALTER SUBSCRIPTION ... SET (two_phase
> = true) unconditionally set subtwophasestate to
> LOGICALREP_TWOPHASE_STATE_PENDING, even when two_phase was already in
> LOGICALREP_TWOPHASE_STATE_ENABLED state.
>
> Because two_phase remains enabled on the publisher's replication slot
> and prepared transactions may already exist on the subscriber,
> overwriting the state to PENDING broke checks that rely on
> subtwophasestate being ENABLED:
>
> 1. A subsequent ALTER SUBSCRIPTION ... SET (two_phase = false) skipped
> the LookupGXactBySubid() check and disabled two_phase while prepared
> transactions still existed on the subscriber. When those transactions
> were later committed on the publisher, the publisher re-sent all of
> their changes as regular transactions instead of COMMIT PREPARED,
> leading to self-deadlocks, duplicate data, or orphaned prepared
> transactions.
>
> 2. ALTER SUBSCRIPTION ... REFRESH PUBLICATION (and SET/ADD PUBLICATION)
> with copy_data = true was allowed even though the publisher's slot still
> had two_phase enabled, allowing prepared transactions on newly added
> tables to be streamed during initial table synchronization and applied
> as empty prepares.
>
> I think we can fix this by not resetting subtwophasestate to PENDING
> when two_phase is already in the ENABLED state, as shown in the attached
> patch.
>
> This issue has existed since PG 18, when altering the two_phase option
> was introduced, so it should be back-patched down to that branch.

The fix looks good. A few trivial things:

1)
+ if (!opts.twophase ||
+ sub->twophasestate != LOGICALREP_TWOPHASE_STATE_ENABLED)

I find this condition slightly difficult to follow. Would below be
more readable?

if (!(opts.twophase &&
      sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED))

This simply says: skip the write if the user asked for true and it's already 'e'

2)
+# Verify that setting two_phase to true when it is already enabled does not
+# overwrite subtwophasestate to 'p' (pending).
+$node_subscriber->safe_psql('postgres',
+ "ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = true);");
+$result = $node_subscriber->safe_psql('postgres',
+ "SELECT subtwophasestate FROM pg_subscription WHERE subname = 'tap_sub_copy';"
+);
+is($result, qq(e), 'two-phase subscription option remains enabled');
+

This new test is added between the two steps of a test that verifies
disabling two-phase.  The comment in previous test '# Alter
subscription two_phase to false' now seems misplaced and thus should
be changed/shifted.

thanks
Shveta






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 07:04  Zhijie Hou <houzhijie22@gmail.com>
  parent: shveta malik <shveta.malik@gmail.com>
  0 siblings, 4 replies; 18+ messages in thread

From: Zhijie Hou @ 2026-10-29 07:04 UTC (permalink / raw)
  To: shveta malik <shveta.malik@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org, amit.kapila16@gmail.com, shveta malik <shvetamalik@gmail.com>

Hi,

On Thu, Oct 29, 2026 at 02:33 PM shveta malik wrote:
> On Thu, Oct 29, 2026 at 11:27 AM Zhijie Hou <houzhijie22@gmail.com> wrote:
>>
>> While reviewing another logical replication thread, I noticed an issue
>> with altering a subscription's two_phase option.
>>
>> In AlterSubscription(), executing ALTER SUBSCRIPTION ... SET (two_phase
>> = true) unconditionally set subtwophasestate to
>> LOGICALREP_TWOPHASE_STATE_PENDING, even when two_phase was already in
>> LOGICALREP_TWOPHASE_STATE_ENABLED state.
>>
>> Because two_phase remains enabled on the publisher's replication slot
>> and prepared transactions may already exist on the subscriber,
>> overwriting the state to PENDING broke checks that rely on
>> subtwophasestate being ENABLED:
>>
>> 1. A subsequent ALTER SUBSCRIPTION ... SET (two_phase = false) skipped
>> the LookupGXactBySubid() check and disabled two_phase while prepared
>> transactions still existed on the subscriber. When those transactions
>> were later committed on the publisher, the publisher re-sent all of
>> their changes as regular transactions instead of COMMIT PREPARED,
>> leading to self-deadlocks, duplicate data, or orphaned prepared
>> transactions.
>>
>> 2. ALTER SUBSCRIPTION ... REFRESH PUBLICATION (and SET/ADD PUBLICATION)
>> with copy_data = true was allowed even though the publisher's slot still
>> had two_phase enabled, allowing prepared transactions on newly added
>> tables to be streamed during initial table synchronization and applied
>> as empty prepares.
>>
>> I think we can fix this by not resetting subtwophasestate to PENDING
>> when two_phase is already in the ENABLED state, as shown in the attached
>> patch.
>>
>> This issue has existed since PG 18, when altering the two_phase option
>> was introduced, so it should be back-patched down to that branch.
> 
> The fix looks good. A few trivial things:

Thanks for reviewing.

> 
> 1)
> + if (!opts.twophase ||
> + sub->twophasestate != LOGICALREP_TWOPHASE_STATE_ENABLED)
> 
> I find this condition slightly difficult to follow. Would below be
> more readable?
> 
> if (!(opts.twophase &&
>        sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED))

Changed as suggested.

> 
> This simply says: skip the write if the user asked for true and it's already 'e'

I did not change the comment as I perfer the current version.

> 
> 2)
> +# Verify that setting two_phase to true when it is already enabled does not
> +# overwrite subtwophasestate to 'p' (pending).
> +$node_subscriber->safe_psql('postgres',
> + "ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = true);");
> +$result = $node_subscriber->safe_psql('postgres',
> + "SELECT subtwophasestate FROM pg_subscription WHERE subname = 'tap_sub_copy';"
> +);
> +is($result, qq(e), 'two-phase subscription option remains enabled');
> +
> 
> This new test is added between the two steps of a test that verifies
> disabling two-phase.  The comment in previous test '# Alter
> subscription two_phase to false' now seems misplaced and thus should
> be changed/shifted.

Agreed. I adjusted the test order to make it clear.

Here is the V2 patch.

-- 
Best Regards,
Zhijie Hou

Attachments:

v2-0001-Avoid-resetting-subscription-two_phase-state-to-p.patch (text/plain, 5.0K) download From 65b8560bb035a347823d5b45d63c66d825123141 Mon Sep 17 00:00:00 2001 From: Zhijie Hou <houzhijie22@gmail.com> Date: Thu, 29 Oct 2026 14:58:32 +0800 Subject: [PATCH v2] Avoid resetting subscription two_phase state to pending when already enabled. In AlterSubscription(), executing ALTER SUBSCRIPTION ... SET (two_phase = true) unconditionally set subtwophasestate to LOGICALREP_TWOPHASE_STATE_PENDING ('p'), even when two_phase was already in LOGICALREP_TWOPHASE_STATE_ENABLED ('e') state. Because two_phase remains enabled on the publisher's replication slot and prepared transactions may already exist on the subscriber, overwriting the state to 'p' broke checks that rely on subtwophasestate being 'e': 1. A subsequent ALTER SUBSCRIPTION ... SET (two_phase = false) skipped the LookupGXactBySubid() check and disabled two_phase while prepared transactions still existed on the subscriber. When those transactions were later committed on the publisher, the publisher re-sent all of their changes as regular transactions instead of COMMIT PREPARED, leading to self-deadlocks, duplicate data, or orphaned prepared transactions. 2. ALTER SUBSCRIPTION ... REFRESH PUBLICATION (and SET/ADD PUBLICATION) with copy_data = true was allowed even though the publisher's slot still had two_phase enabled, allowing prepared transactions on newly added tables to be streamed during initial table synchronization and applied as empty prepares. Fix this by not resetting subtwophasestate to PENDING when two_phase is already in ENABLED state. Author: Zhijie Hou <houzhijie22@gmail.com> Reviewed-by: shveta malik <shveta.malik@gmail.com> Discussion: https://postgr.es/m/8e85abdf-738b-4578-ac42-4dc8eaa061dd@gmail.com Backpatch-through: 18 --- src/backend/commands/subscriptioncmds.c | 19 +++++++++++++------ src/test/subscription/t/021_twophase.pl | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index 36bb20a1f6e..e4031efd7aa 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -1963,12 +1963,19 @@ AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, errmsg("cannot disable \"two_phase\" when prepared transactions exist"), errhint("Resolve these transactions and try again."))); - /* Change system catalog accordingly */ - values[Anum_pg_subscription_subtwophasestate - 1] = - CharGetDatum(opts.twophase ? - LOGICALREP_TWOPHASE_STATE_PENDING : - LOGICALREP_TWOPHASE_STATE_DISABLED); - replaces[Anum_pg_subscription_subtwophasestate - 1] = true; + /* + * Change system catalog accordingly, but do not reset the + * state to PENDING if two_phase is already enabled. + */ + if (!(opts.twophase && + sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED)) + { + values[Anum_pg_subscription_subtwophasestate - 1] = + CharGetDatum(opts.twophase ? + LOGICALREP_TWOPHASE_STATE_PENDING : + LOGICALREP_TWOPHASE_STATE_DISABLED); + replaces[Anum_pg_subscription_subtwophasestate - 1] = true; + } } if (IsSet(opts.specified_opts, SUBOPT_FAILOVER)) diff --git a/src/test/subscription/t/021_twophase.pl b/src/test/subscription/t/021_twophase.pl index f755e748e9c..ede7614be5c 100644 --- a/src/test/subscription/t/021_twophase.pl +++ b/src/test/subscription/t/021_twophase.pl @@ -431,8 +431,8 @@ is($result, qq(2), 'replicated data in subscriber table'); $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub"); ############################### -# Alter the subscription to set two_phase to false. -# Verify that the altered subscription reflects the new two_phase option. +# Verify that setting two_phase to true when it is already enabled does not +# overwrite subtwophasestate to 'p' (pending). ############################### # Confirm that the two-phase slot option is enabled before altering @@ -441,12 +441,22 @@ $result = $node_publisher->safe_psql('postgres', ); is($result, qq(t), 'two-phase is enabled'); -# Alter subscription two_phase to false $node_subscriber->safe_psql('postgres', "ALTER SUBSCRIPTION tap_sub_copy DISABLE;"); $node_subscriber->poll_query_until('postgres', "SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'logical replication apply worker'" ); +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = true);"); +$result = $node_subscriber->safe_psql('postgres', + "SELECT subtwophasestate FROM pg_subscription WHERE subname = 'tap_sub_copy';" +); +is($result, qq(e), 'two-phase subscription option remains enabled'); + +############################### +# Alter the subscription to set two_phase to false. +# Verify that the altered subscription reflects the new two_phase option. +############################### $node_subscriber->safe_psql( 'postgres', " ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = false); -- 2.54.0 (Apple Git-157)
^ permalink raw reply [nested|flat] 18+ messages in thread

* RE: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 07:29  Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
  parent: Zhijie Hou <houzhijie22@gmail.com>
  1 sibling, 1 reply; 18+ messages in thread

From: Hayato Kuroda (Fujitsu) @ 2026-10-29 07:29 UTC (permalink / raw)
  To: Zhijie Hou <houzhijie22@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>; +Cc: amit.kapila16@gmail.com <amit.kapila16@gmail.com>

Hi,

> While reviewing another logical replication thread, I noticed an issue
> with altering a subscription's two_phase option.

Good catch.

> I think we can fix this by not resetting subtwophasestate to PENDING
> when two_phase is already in the ENABLED state, as shown in the attached
> patch.

+1 the fix, but I have a quick confirmation.

Assuming that twophasestate is 'e' and the user runs ALTER SUBSCRIPTION SET
(two_phase = on). In this case all attributes are retained as-is but update_tuple
is set to true anyway - CatalogTupleUpdate() would update the tuple with the
completely the same tuple.

But I feel it's acceptable because 1) it rarely happens,
2) conflict_log_destination option has the same issue, and 3) fixing it may
need wider changes, like setting update_tuple in the if-statement, thought?

Best regards,
Hayato Kuroda
FUJITSU LIMITED



^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 07:37  Zhijie Hou <houzhijie22@gmail.com>
  parent: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Zhijie Hou @ 2026-10-29 07:37 UTC (permalink / raw)
  To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>; +Cc: amit.kapila16@gmail.com <amit.kapila16@gmail.com>

Hi,

On Thu, Oct 29, 2026 at 03:29 PM "Hayato Kuroda (Fujitsu)" wrote:
>> While reviewing another logical replication thread, I noticed an issue
>> with altering a subscription's two_phase option.
> 
> Good catch.
> 
>> I think we can fix this by not resetting subtwophasestate to PENDING
>> when two_phase is already in the ENABLED state, as shown in the attached
>> patch.
> 
> +1 the fix, but I have a quick confirmation.
> 
> Assuming that twophasestate is 'e' and the user runs ALTER SUBSCRIPTION SET
> (two_phase = on). In this case all attributes are retained as-is but update_tuple
> is set to true anyway - CatalogTupleUpdate() would update the tuple with the
> completely the same tuple.
> 
> But I feel it's acceptable because 1) it rarely happens,
> 2) conflict_log_destination option has the same issue, and 3) fixing it may
> need wider changes, like setting update_tuple in the if-statement, thought?

Right. This is the same as all (or most) subscription options and even 
other object options like publication options have the same behavior, 
since we don't compare the new value with the old one in most cases. So 
I think that's an independent topic.

-- 
Best Regards,
Zhijie Hou






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* RE: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 07:56  Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
  parent: Zhijie Hou <houzhijie22@gmail.com>
  0 siblings, 0 replies; 18+ messages in thread

From: Hayato Kuroda (Fujitsu) @ 2026-10-29 07:56 UTC (permalink / raw)
  To: Zhijie Hou <houzhijie22@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>; +Cc: amit.kapila16@gmail.com <amit.kapila16@gmail.com>

Hi,

> Right. This is the same as all (or most) subscription options and even
> other object options like publication options have the same behavior,
> since we don't compare the new value with the old one in most cases. So
> I think that's an independent topic.

Make sense. I checked the v2 patch and LGTM.

Best regards,
Hayato Kuroda
FUJITSU LIMITED



^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 08:26  shveta malik <shveta.malik@gmail.com>
  parent: Zhijie Hou <houzhijie22@gmail.com>
  3 siblings, 0 replies; 18+ messages in thread

From: shveta malik @ 2026-10-29 08:26 UTC (permalink / raw)
  To: Zhijie Hou <houzhijie22@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org, amit.kapila16@gmail.com, shveta malik <shvetamalik@gmail.com>

On Thu, Oct 29, 2026 at 12:34 PM Zhijie Hou <houzhijie22@gmail.com> wrote:
>
> Hi,
>
> On Thu, Oct 29, 2026 at 02:33 PM shveta malik wrote:
> > On Thu, Oct 29, 2026 at 11:27 AM Zhijie Hou <houzhijie22@gmail.com> wrote:
> >>
> >> While reviewing another logical replication thread, I noticed an issue
> >> with altering a subscription's two_phase option.
> >>
> >> In AlterSubscription(), executing ALTER SUBSCRIPTION ... SET (two_phase
> >> = true) unconditionally set subtwophasestate to
> >> LOGICALREP_TWOPHASE_STATE_PENDING, even when two_phase was already in
> >> LOGICALREP_TWOPHASE_STATE_ENABLED state.
> >>
> >> Because two_phase remains enabled on the publisher's replication slot
> >> and prepared transactions may already exist on the subscriber,
> >> overwriting the state to PENDING broke checks that rely on
> >> subtwophasestate being ENABLED:
> >>
> >> 1. A subsequent ALTER SUBSCRIPTION ... SET (two_phase = false) skipped
> >> the LookupGXactBySubid() check and disabled two_phase while prepared
> >> transactions still existed on the subscriber. When those transactions
> >> were later committed on the publisher, the publisher re-sent all of
> >> their changes as regular transactions instead of COMMIT PREPARED,
> >> leading to self-deadlocks, duplicate data, or orphaned prepared
> >> transactions.
> >>
> >> 2. ALTER SUBSCRIPTION ... REFRESH PUBLICATION (and SET/ADD PUBLICATION)
> >> with copy_data = true was allowed even though the publisher's slot still
> >> had two_phase enabled, allowing prepared transactions on newly added
> >> tables to be streamed during initial table synchronization and applied
> >> as empty prepares.
> >>
> >> I think we can fix this by not resetting subtwophasestate to PENDING
> >> when two_phase is already in the ENABLED state, as shown in the attached
> >> patch.
> >>
> >> This issue has existed since PG 18, when altering the two_phase option
> >> was introduced, so it should be back-patched down to that branch.
> >
> > The fix looks good. A few trivial things:
>
> Thanks for reviewing.
>
> >
> > 1)
> > + if (!opts.twophase ||
> > + sub->twophasestate != LOGICALREP_TWOPHASE_STATE_ENABLED)
> >
> > I find this condition slightly difficult to follow. Would below be
> > more readable?
> >
> > if (!(opts.twophase &&
> >        sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED))
>
> Changed as suggested.
>
> >
> > This simply says: skip the write if the user asked for true and it's already 'e'
>
> I did not change the comment as I perfer the current version.

Sure. I did not suggest changing the code-comment, I was just
describing the simplicity of the change.

> >
> > 2)
> > +# Verify that setting two_phase to true when it is already enabled does not
> > +# overwrite subtwophasestate to 'p' (pending).
> > +$node_subscriber->safe_psql('postgres',
> > + "ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = true);");
> > +$result = $node_subscriber->safe_psql('postgres',
> > + "SELECT subtwophasestate FROM pg_subscription WHERE subname = 'tap_sub_copy';"
> > +);
> > +is($result, qq(e), 'two-phase subscription option remains enabled');
> > +
> >
> > This new test is added between the two steps of a test that verifies
> > disabling two-phase.  The comment in previous test '# Alter
> > subscription two_phase to false' now seems misplaced and thus should
> > be changed/shifted.
>
> Agreed. I adjusted the test order to make it clear.
>
> Here is the V2 patch.

thanks, v2 LGTM.






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 09:11  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Zhijie Hou <houzhijie22@gmail.com>
  3 siblings, 1 reply; 18+ messages in thread

From: Bertrand Drouvot @ 2026-10-29 09:11 UTC (permalink / raw)
  To: Zhijie Hou <houzhijie22@gmail.com>; +Cc: shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, amit.kapila16@gmail.com, shveta malik <shvetamalik@gmail.com>

Hi,

On Thu, Oct 29, 2026 at 03:04:12PM +0800, Zhijie Hou wrote:
> Agreed. I adjusted the test order to make it clear.
> 
> Here is the V2 patch.

Thanks for the patch!

+         /*
+          * Change system catalog accordingly, but do not reset the
+          * state to PENDING if two_phase is already enabled.
+          */
+          if (!(opts.twophase &&
+                sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED))

The new condition also preserves 'e' when slot_name changes. In that case, a
replacement slot with two_phase=false remains false because the worker does not
go through the PENDING state.

I reproduced this with:

ALTER SUBSCRIPTION sub SET (slot_name = 'replacement', two_phase = true);

Should we preserve 'e' only when the slot name is unchanged?

Regards,

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






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 10:22  vignesh C <vignesh21@gmail.com>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 18+ messages in thread

From: vignesh C @ 2026-10-29 10:22 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Zhijie Hou <houzhijie22@gmail.com>; shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, amit.kapila16@gmail.com, shveta malik <shvetamalik@gmail.com>

On Thu, 29 Oct 2026 at 14:41, Bertrand Drouvot
<bertranddrouvot.pg@gmail.com> wrote:
>
> Hi,
>
> On Thu, Oct 29, 2026 at 03:04:12PM +0800, Zhijie Hou wrote:
> > Agreed. I adjusted the test order to make it clear.
> >
> > Here is the V2 patch.
>
> Thanks for the patch!
>
> +         /*
> +          * Change system catalog accordingly, but do not reset the
> +          * state to PENDING if two_phase is already enabled.
> +          */
> +          if (!(opts.twophase &&
> +                sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED))
>
> The new condition also preserves 'e' when slot_name changes. In that case, a
> replacement slot with two_phase=false remains false because the worker does not
> go through the PENDING state.
>
> I reproduced this with:
>
> ALTER SUBSCRIPTION sub SET (slot_name = 'replacement', two_phase = true);
>
> Should we preserve 'e' only when the slot name is unchanged?

There is  the following documentation mentioning that slots must be
created with the same parameters of the subscription at [1].
When altering the slot_name, the failover and two_phase property
values of the named slot may differ from the counterpart failover and
two_phase parameters specified in the subscription. When creating the
slot, ensure the slot properties failover and two_phase match their
counterpart parameters of the subscription. Otherwise, the slot on the
publisher may behave differently from what these subscription options
say: for example, the slot on the publisher could either be synced to
the standbys even when the subscription's failover option is disabled
or could be disabled for sync even when the subscription's failover
option is enabled.

A similar thing can happen with SET (slot_name = 'other_slot') i.e,
without specifying two_phase option.

I'm not sure if this should be handled here as it is documented already.

[1] - https://www.postgresql.org/docs/devel/sql-altersubscription.html

Regards,
Vignesh






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 10:41  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: vignesh C <vignesh21@gmail.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Bertrand Drouvot @ 2026-10-29 10:41 UTC (permalink / raw)
  To: vignesh C <vignesh21@gmail.com>; +Cc: Zhijie Hou <houzhijie22@gmail.com>; shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, amit.kapila16@gmail.com, shveta malik <shvetamalik@gmail.com>

Hi,

On Thu, Oct 29, 2026 at 03:52:46PM +0530, vignesh C wrote:
> There is  the following documentation mentioning that slots must be
> created with the same parameters of the subscription at [1].
> When altering the slot_name, the failover and two_phase property
> values of the named slot may differ from the counterpart failover and
> two_phase parameters specified in the subscription. When creating the
> slot, ensure the slot properties failover and two_phase match their
> counterpart parameters of the subscription. Otherwise, the slot on the
> publisher may behave differently from what these subscription options
> say: for example, the slot on the publisher could either be synced to
> the standbys even when the subscription's failover option is disabled
> or could be disabled for sync even when the subscription's failover
> option is enabled.
> 
> I'm not sure if this should be handled here as it is documented already.

I agree about the documentation, but the patch introduces a regression: without
the patch, two_phase is enabled on the replacement slot. With the patch, it remains
disabled.

It might be worth fixing, as preserving 'e' only when slot_name is unchanged
should be simple enough.

Regards,

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






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-29 15:14  Zhijie Hou <houzhijie22@gmail.com>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Zhijie Hou @ 2026-10-29 15:14 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; vignesh C <vignesh21@gmail.com>; +Cc: shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, amit.kapila16@gmail.com, shveta malik <shvetamalik@gmail.com>

Hi,

On Thu, Oct 29, 2026 at 06:41 PM Bertrand Drouvot wrote:
> 
> On Thu, Oct 29, 2026 at 03:52:46PM +0530, vignesh C wrote:
>> There is  the following documentation mentioning that slots must be
>> created with the same parameters of the subscription at [1].
>> When altering the slot_name, the failover and two_phase property
>> values of the named slot may differ from the counterpart failover and
>> two_phase parameters specified in the subscription. When creating the
>> slot, ensure the slot properties failover and two_phase match their
>> counterpart parameters of the subscription. Otherwise, the slot on the
>> publisher may behave differently from what these subscription options
>> say: for example, the slot on the publisher could either be synced to
>> the standbys even when the subscription's failover option is disabled
>> or could be disabled for sync even when the subscription's failover
>> option is enabled.
>>
>> I'm not sure if this should be handled here as it is documented already.
> 
> I agree about the documentation, but the patch introduces a regression: without
> the patch, two_phase is enabled on the replacement slot. With the patch, it remains
> disabled.

Like Vignesh, I think the documentation already requires users to create 
the replacement slot with matching properties when altering slot_name, 
so we don't promise that setting two_phase = true will update the 
replacement slot.

> It might be worth fixing, as preserving 'e' only when slot_name is unchanged
> should be simple enough.

I don't think handling that here would be simple. Resetting ENABLED to 
PENDING when slot_name is changed would re-introduce the same bugs that 
this patch aims to fix: prepared transactions from the previous slot may 
still exist on the subscriber, and if the replacement slot already has 
two_phase enabled on the publisher, prepared transactions could again be 
streamed concurrently with table synchronization. Avoiding that would 
require checking prepared transactions and the remote slot's status 
during the change, which would complicate the fix.


-- 
Best Regards,
Zhijie Hou






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-10-30 09:55  vignesh C <vignesh21@gmail.com>
  parent: Zhijie Hou <houzhijie22@gmail.com>
  3 siblings, 0 replies; 18+ messages in thread

From: vignesh C @ 2026-10-30 09:55 UTC (permalink / raw)
  To: Zhijie Hou <houzhijie22@gmail.com>; +Cc: shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, amit.kapila16@gmail.com, shveta malik <shvetamalik@gmail.com>

On Thu, 29 Oct 2026 at 12:34, Zhijie Hou <houzhijie22@gmail.com> wrote:
>
> Here is the V2 patch.

I had a look at the patch, the patch looks good to me.

Regards,
Vignesh






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-11-02 02:04  Peter Smith <smithpb2250@gmail.com>
  parent: Zhijie Hou <houzhijie22@gmail.com>
  3 siblings, 1 reply; 18+ messages in thread

From: Peter Smith @ 2026-11-02 02:04 UTC (permalink / raw)
  To: Zhijie Hou <houzhijie22@gmail.com>; +Cc: shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, amit.kapila16@gmail.com, shveta malik <shvetamalik@gmail.com>

IMO, the nested checking of opts.twophase in the ternary makes the
patch code unnecessarily hard to follow across combinations.

My suggestion is to implement it using more verbose, but easier-to-read code:

     /* Change system catalog accordingly */
if (sub->twophasestate == LOGICALREP_TWOPHASE_STATE_DISABLED)
{
    if (opts.twophase)
    {
        /* enable two_phase */
        values[Anum_pg_subscription_subtwophasestate - 1] =
            CharGetDatum(LOGICALREP_TWOPHASE_STATE_PENDING);
        replaces[Anum_pg_subscription_subtwophasestate - 1] = true;
    }
}
else if (!opts.twophase)
{
    /* disable two_phase */
    values[Anum_pg_subscription_subtwophasestate - 1] =
        CharGetDatum(LOGICALREP_TWOPHASE_STATE_DISABLED);
    replaces[Anum_pg_subscription_subtwophasestate - 1] = true;
}

~

This way also avoids redundant replacements like 'd' to 'd', 'p' to 'p', etc.

======
Kind Regards,
Peter Smith.
Fujitsu Australia






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-11-02 02:51  Zhijie Hou <houzhijie22@gmail.com>
  parent: Peter Smith <smithpb2250@gmail.com>
  0 siblings, 0 replies; 18+ messages in thread

From: Zhijie Hou @ 2026-11-02 02:51 UTC (permalink / raw)
  To: Peter Smith <smithpb2250@gmail.com>; +Cc: shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, amit.kapila16@gmail.com, shveta malik <shvetamalik@gmail.com>

Hi,

On Mon, Nov  2, 2026 at 10:04 AM Peter Smith wrote:
> IMO, the nested checking of opts.twophase in the ternary makes the
> patch code unnecessarily hard to follow across combinations.
> 
> My suggestion is to implement it using more verbose, but easier-to-read code:
> 
>       /* Change system catalog accordingly */
> if (sub->twophasestate == LOGICALREP_TWOPHASE_STATE_DISABLED)
> {
>      if (opts.twophase)
>      {
>          /* enable two_phase */
>          values[Anum_pg_subscription_subtwophasestate - 1] =
>              CharGetDatum(LOGICALREP_TWOPHASE_STATE_PENDING);
>          replaces[Anum_pg_subscription_subtwophasestate - 1] = true;
>      }
> }
> else if (!opts.twophase)
> {
>      /* disable two_phase */
>      values[Anum_pg_subscription_subtwophasestate - 1] =
>          CharGetDatum(LOGICALREP_TWOPHASE_STATE_DISABLED);
>      replaces[Anum_pg_subscription_subtwophasestate - 1] = true;
> }
>
> ~

Thank for the suggestion, but I personally don't prefer the longer, 
nested style, as I don't find it clearly easier to read. So I would keep 
the current version for now and see what the committer thinks.

-- 
Best Regards,
Zhijie Hou






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-11-04 10:01  Amit Kapila <amit.kapila16@gmail.com>
  parent: Zhijie Hou <houzhijie22@gmail.com>
  0 siblings, 2 replies; 18+ messages in thread

From: Amit Kapila @ 2026-11-04 10:01 UTC (permalink / raw)
  To: Zhijie Hou <houzhijie22@gmail.com>; +Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; vignesh C <vignesh21@gmail.com>; shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, shveta malik <shvetamalik@gmail.com>

On Thu, Oct 29, 2026 at 8:44 PM Zhijie Hou <houzhijie22@gmail.com> wrote:
>
> On Thu, Oct 29, 2026 at 06:41 PM Bertrand Drouvot wrote:
> >
> > On Thu, Oct 29, 2026 at 03:52:46PM +0530, vignesh C wrote:
> >> There is  the following documentation mentioning that slots must be
> >> created with the same parameters of the subscription at [1].
> >> When altering the slot_name, the failover and two_phase property
> >> values of the named slot may differ from the counterpart failover and
> >> two_phase parameters specified in the subscription. When creating the
> >> slot, ensure the slot properties failover and two_phase match their
> >> counterpart parameters of the subscription. Otherwise, the slot on the
> >> publisher may behave differently from what these subscription options
> >> say: for example, the slot on the publisher could either be synced to
> >> the standbys even when the subscription's failover option is disabled
> >> or could be disabled for sync even when the subscription's failover
> >> option is enabled.
> >>
> >> I'm not sure if this should be handled here as it is documented already.
> >
> > I agree about the documentation, but the patch introduces a regression: without
> > the patch, two_phase is enabled on the replacement slot. With the patch, it remains
> > disabled.
>
> Like Vignesh, I think the documentation already requires users to create
> the replacement slot with matching properties when altering slot_name,
> so we don't promise that setting two_phase = true will update the
> replacement slot.
>
> > It might be worth fixing, as preserving 'e' only when slot_name is unchanged
> > should be simple enough.
>
> I don't think handling that here would be simple. Resetting ENABLED to
> PENDING when slot_name is changed would re-introduce the same bugs that
> this patch aims to fix: prepared transactions from the previous slot may
> still exist on the subscriber, and if the replacement slot already has
> two_phase enabled on the publisher, prepared transactions could again be
> streamed concurrently with table synchronization. Avoiding that would
> require checking prepared transactions and the remote slot's status
> during the change, which would complicate the fix.
>

I agree that we don't need to preserve the old behavior as it will
open up other bugs and we already document that such slot's properties
should match subscription's options. But I feel we should add that
change in behavior for new slot's in the commit message. Other than
that the latest patch in this thread looks good to me.

-- 
With Regards,
Amit Kapila.






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-11-04 12:50  Zhijie Hou <houzhijie22@gmail.com>
  parent: Amit Kapila <amit.kapila16@gmail.com>
  1 sibling, 0 replies; 18+ messages in thread

From: Zhijie Hou @ 2026-11-04 12:50 UTC (permalink / raw)
  To: Amit Kapila <amit.kapila16@gmail.com>; +Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; vignesh C <vignesh21@gmail.com>; shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, shveta malik <shvetamalik@gmail.com>

Hi,

On Wed, Nov 4, 2026 at 6:01 PM Amit Kapila <amit.kapila16@gmail.com> wrote:
>
> On Thu, Oct 29, 2026 at 8:44 PM Zhijie Hou <houzhijie22@gmail.com> wrote:
> >
> > On Thu, Oct 29, 2026 at 06:41 PM Bertrand Drouvot wrote:
> > > It might be worth fixing, as preserving 'e' only when slot_name is unchanged
> > > should be simple enough.
> >
> > I don't think handling that here would be simple. Resetting ENABLED to
> > PENDING when slot_name is changed would re-introduce the same bugs that
> > this patch aims to fix: prepared transactions from the previous slot may
> > still exist on the subscriber, and if the replacement slot already has
> > two_phase enabled on the publisher, prepared transactions could again be
> > streamed concurrently with table synchronization. Avoiding that would
> > require checking prepared transactions and the remote slot's status
> > during the change, which would complicate the fix.
> >
>
> I agree that we don't need to preserve the old behavior as it will
> open up other bugs and we already document that such slot's properties
> should match subscription's options. But I feel we should add that
> change in behavior for new slot's in the commit message. Other than
> that the latest patch in this thread looks good to me.

I added a section describing this behavior change, please see the
attached V3 patch.

I confirmed the patch applies cleanly to all affected branches (PG 18
through HEAD),
and the tests pass on all of them.

--
Best Regards,
Zhijie Hou

Attachments:

v3-0001-Avoid-resetting-subscription-two_phase-state-to-p.patch (application/octet-stream, 5.6K) download From 65b8560bb035a347823d5b45d63c66d825123141 Mon Sep 17 00:00:00 2001 From: Zhijie Hou <houzhijie22@gmail.com> Date: Wed, 4 Nov 2026 20:17:33 +0800 Subject: [PATCH v3] Avoid resetting subscription two_phase state to pending when already enabled. In AlterSubscription(), executing ALTER SUBSCRIPTION ... SET (two_phase = true) unconditionally set subtwophasestate to LOGICALREP_TWOPHASE_STATE_PENDING ('p'), even when two_phase was already in LOGICALREP_TWOPHASE_STATE_ENABLED ('e') state. Because two_phase remains enabled on the publisher's replication slot and prepared transactions may already exist on the subscriber, overwriting the state to 'p' broke checks that rely on subtwophasestate being 'e': 1. A subsequent ALTER SUBSCRIPTION ... SET (two_phase = false) skipped the LookupGXactBySubid() check and disabled two_phase while prepared transactions still existed on the subscriber. When those transactions were later committed on the publisher, the publisher re-sent all of their changes as regular transactions instead of COMMIT PREPARED, leading to self-deadlocks, duplicate data, or orphaned prepared transactions. 2. ALTER SUBSCRIPTION ... REFRESH PUBLICATION (and SET/ADD PUBLICATION) with copy_data = true was allowed even though the publisher's slot still had two_phase enabled, allowing prepared transactions on newly added tables to be streamed during initial table synchronization and applied as empty prepares. Fix this by not resetting subtwophasestate to PENDING when two_phase is already in ENABLED state. Changing slot_name together with SET (two_phase = true) on an already ENABLED subscription likewise leaves the state ENABLED, so the apply worker no longer enables two_phase on the replacement slot. This is intentional: resetting the state would reintroduce the problems described above, and the documentation already requires the replacement slot's two_phase property to match the subscription option. Author: Zhijie Hou <houzhijie22@gmail.com> Reviewed-by: shveta malik <shveta.malik@gmail.com> Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com> Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Reviewed-by: vignesh C <vignesh21@gmail.com> Reviewed-by: Peter Smith <smithpb2250@gmail.com> Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Discussion: https://postgr.es/m/8e85abdf-738b-4578-ac42-4dc8eaa061dd@gmail.com Backpatch-through: 18 --- src/backend/commands/subscriptioncmds.c | 19 +++++++++++++------ src/test/subscription/t/021_twophase.pl | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index 36bb20a1f6e..e4031efd7aa 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -1963,12 +1963,19 @@ AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, errmsg("cannot disable \"two_phase\" when prepared transactions exist"), errhint("Resolve these transactions and try again."))); - /* Change system catalog accordingly */ - values[Anum_pg_subscription_subtwophasestate - 1] = - CharGetDatum(opts.twophase ? - LOGICALREP_TWOPHASE_STATE_PENDING : - LOGICALREP_TWOPHASE_STATE_DISABLED); - replaces[Anum_pg_subscription_subtwophasestate - 1] = true; + /* + * Change system catalog accordingly, but do not reset the + * state to PENDING if two_phase is already enabled. + */ + if (!(opts.twophase && + sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED)) + { + values[Anum_pg_subscription_subtwophasestate - 1] = + CharGetDatum(opts.twophase ? + LOGICALREP_TWOPHASE_STATE_PENDING : + LOGICALREP_TWOPHASE_STATE_DISABLED); + replaces[Anum_pg_subscription_subtwophasestate - 1] = true; + } } if (IsSet(opts.specified_opts, SUBOPT_FAILOVER)) diff --git a/src/test/subscription/t/021_twophase.pl b/src/test/subscription/t/021_twophase.pl index f755e748e9c..ede7614be5c 100644 --- a/src/test/subscription/t/021_twophase.pl +++ b/src/test/subscription/t/021_twophase.pl @@ -431,8 +431,8 @@ is($result, qq(2), 'replicated data in subscriber table'); $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub"); ############################### -# Alter the subscription to set two_phase to false. -# Verify that the altered subscription reflects the new two_phase option. +# Verify that setting two_phase to true when it is already enabled does not +# overwrite subtwophasestate to 'p' (pending). ############################### # Confirm that the two-phase slot option is enabled before altering @@ -441,12 +441,22 @@ $result = $node_publisher->safe_psql('postgres', ); is($result, qq(t), 'two-phase is enabled'); -# Alter subscription two_phase to false $node_subscriber->safe_psql('postgres', "ALTER SUBSCRIPTION tap_sub_copy DISABLE;"); $node_subscriber->poll_query_until('postgres', "SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'logical replication apply worker'" ); +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = true);"); +$result = $node_subscriber->safe_psql('postgres', + "SELECT subtwophasestate FROM pg_subscription WHERE subname = 'tap_sub_copy';" +); +is($result, qq(e), 'two-phase subscription option remains enabled'); + +############################### +# Alter the subscription to set two_phase to false. +# Verify that the altered subscription reflects the new two_phase option. +############################### $node_subscriber->safe_psql( 'postgres', " ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = false); -- 2.54.0 (Apple Git-157)
^ permalink raw reply [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-11-04 13:24  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Amit Kapila <amit.kapila16@gmail.com>
  1 sibling, 1 reply; 18+ messages in thread

From: Bertrand Drouvot @ 2026-11-04 13:24 UTC (permalink / raw)
  To: Amit Kapila <amit.kapila16@gmail.com>; +Cc: Zhijie Hou <houzhijie22@gmail.com>; vignesh C <vignesh21@gmail.com>; shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, shveta malik <shvetamalik@gmail.com>

Hi,

On Wed, Nov 04, 2026 at 03:31:19PM +0530, Amit Kapila wrote:
> But I feel we should add that
> change in behavior for new slot's in the commit message. 

+1. I think this behavior change should also be mentioned in the release notes
for the affected branches. Is that also the intention behind adding it to the
commit message?

Regards,

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






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Avoid resetting subscription two_phase state to pending when already enabled
@ 2026-11-05 06:13  Amit Kapila <amit.kapila16@gmail.com>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 0 replies; 18+ messages in thread

From: Amit Kapila @ 2026-11-05 06:13 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Zhijie Hou <houzhijie22@gmail.com>; vignesh C <vignesh21@gmail.com>; shveta malik <shveta.malik@gmail.com>; pgsql-hackers@lists.postgresql.org, shveta malik <shvetamalik@gmail.com>

On Wed, Nov 4, 2026 at 6:54 PM Bertrand Drouvot
<bertranddrouvot.pg@gmail.com> wrote:
>
> On Wed, Nov 04, 2026 at 03:31:19PM +0530, Amit Kapila wrote:
> > But I feel we should add that
> > change in behavior for new slot's in the commit message.
>
> +1. I think this behavior change should also be mentioned in the release notes
> for the affected branches. Is that also the intention behind adding it to the
> commit message?
>

Yes, I have pushed the patch. Thanks.

-- 
With Regards,
Amit Kapila.






^ permalink  raw  reply  [nested|flat] 18+ messages in thread


end of thread, other threads:[~2026-11-05 06:13 UTC | newest]

Thread overview: 18+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-29 05:56 Avoid resetting subscription two_phase state to pending when already enabled Zhijie Hou <houzhijie22@gmail.com>
2026-10-29 06:33 ` shveta malik <shveta.malik@gmail.com>
2026-10-29 07:04   ` Zhijie Hou <houzhijie22@gmail.com>
2026-10-29 08:26     ` shveta malik <shveta.malik@gmail.com>
2026-10-29 09:11     ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2026-10-29 10:22       ` vignesh C <vignesh21@gmail.com>
2026-10-29 10:41         ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2026-10-29 15:14           ` Zhijie Hou <houzhijie22@gmail.com>
2026-11-04 10:01             ` Amit Kapila <amit.kapila16@gmail.com>
2026-11-04 12:50               ` Zhijie Hou <houzhijie22@gmail.com>
2026-11-04 13:24               ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2026-11-05 06:13                 ` Amit Kapila <amit.kapila16@gmail.com>
2026-10-30 09:55     ` vignesh C <vignesh21@gmail.com>
2026-11-02 02:04     ` Peter Smith <smithpb2250@gmail.com>
2026-11-02 02:51       ` Zhijie Hou <houzhijie22@gmail.com>
2026-10-29 07:29 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-29 07:37   ` Zhijie Hou <houzhijie22@gmail.com>
2026-10-29 07:56     ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox