postgres.git / summary / log / commit / refs

commit    fbaebf450d444782d25fa5a25b7bf0fb41403abc
Author:   Amit Kapila <akapila@postgresql.org>
Date:     Thu Nov 05 04:08:18 2026 +0000
First released in: 18.7, 19.0

    Avoid resetting subscription two_phase state to pending when already enabled.
    
    ALTER SUBSCRIPTION ... SET (two_phase = true) always moved the
    subscription's two_phase state to pending, even if two_phase was already
    enabled. The publisher's slot still has two_phase enabled in that case,
    and prepared transactions may already exist on the subscriber, so the
    reset broke checks that rely on the enabled state:
    
    1. A later SET (two_phase = false) no longer checked for existing prepared
    transactions and disabled two_phase anyway. When those transactions were
    committed on the publisher, their changes were re-sent as regular
    transactions instead of COMMIT PREPARED, leading to self-deadlocks,
    duplicate data, or orphaned prepared transactions.
    
    2. REFRESH PUBLICATION (and SET/ADD PUBLICATION) with copy_data = true was
    allowed even though the publisher's slot still had two_phase enabled. This
    could stream prepared transactions during the initial sync of newly added
    tables and apply them as empty prepares.
    
    Fix this by leaving the state unchanged when two_phase is already enabled.
    
    This problem was introduced by commit 1462aad2e44, which allowed altering
    the two_phase option of a subscription.
    
    This changes behavior when slot_name is altered together with SET
    (two_phase = true) on an ENABLED subscription. Previously the state was
    reset to PENDING and the apply worker enabled two_phase on the replacement
    slot. Now the state stays ENABLED and the apply worker does not touch the
    replacement slot's two_phase property. This is intentional: resetting the
    state would reintroduce the problems described above, since prepared
    transactions from the old slot may still exist on the subscriber. 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 88b71187b7d..8f9d43a1903 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -1326,12 +1326,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 ab2e0a7fa8d..04f66622482 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); [parent: 52033b8871b8]