1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Allow altering of two_phase option of a SUBSCRIPTION.

The two_phase option is controlled by both the publisher (as a slot
option) and the subscriber (as a subscription option), so the slot option
must also be modified.

Changing the 'two_phase' option for a subscription from 'true' to 'false'
is permitted only when there are no pending prepared transactions
corresponding to that subscription. Otherwise, the changes of already
prepared transactions can be replicated again along with their corresponding
commit leading to duplicate data or errors.

To avoid data loss, the 'two_phase' option for a subscription can only be
changed from 'false' to 'true' once the initial data synchronization is
completed. Therefore this is performed later by the logical replication worker.

Author: Hayato Kuroda, Ajin Cherian, Amit Kapila
Reviewed-by: Peter Smith, Hou Zhijie, Amit Kapila, Vitaly Davydov, Vignesh C
Discussion: https://postgr.es/m/8fab8-65d74c80-1-2f28e880@39088166
This commit is contained in:
Amit Kapila
2024-07-24 10:13:36 +05:30
parent 774d47b6c0
commit 1462aad2e4
17 changed files with 458 additions and 120 deletions

View File

@ -2192,7 +2192,23 @@ psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;"
</varlistentry>
</variablelist>
<para>The following option is supported:</para>
<para>The following options are supported:</para>
<variablelist>
<varlistentry>
<term><literal>TWO_PHASE [ <replaceable class="parameter">boolean</replaceable> ]</literal></term>
<listitem>
<para>
If true, this logical replication slot supports decoding of two-phase
commit. With this option, commands related to two-phase commit such as
<literal>PREPARE TRANSACTION</literal>, <literal>COMMIT PREPARED</literal>
and <literal>ROLLBACK PREPARED</literal> are decoded and transmitted.
The transaction will be decoded and transmitted at
<literal>PREPARE TRANSACTION</literal> time.
</para>
</listitem>
</varlistentry>
</variablelist>
<variablelist>
<varlistentry>

View File

@ -68,8 +68,9 @@ ALTER SUBSCRIPTION <replaceable class="parameter">name</replaceable> RENAME TO <
<para>
Commands <command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION</command>,
<command>ALTER SUBSCRIPTION ... {SET|ADD|DROP} PUBLICATION ...</command>
with <literal>refresh</literal> option as <literal>true</literal> and
<command>ALTER SUBSCRIPTION ... SET (failover = true|false)</command>
with <literal>refresh</literal> option as <literal>true</literal>,
<command>ALTER SUBSCRIPTION ... SET (failover = true|false)</command> and
<command>ALTER SUBSCRIPTION ... SET (two_phase = false)</command>
cannot be executed inside a transaction block.
These commands also cannot be executed when the subscription has
@ -228,8 +229,9 @@ ALTER SUBSCRIPTION <replaceable class="parameter">name</replaceable> RENAME TO <
<link linkend="sql-createsubscription-params-with-disable-on-error"><literal>disable_on_error</literal></link>,
<link linkend="sql-createsubscription-params-with-password-required"><literal>password_required</literal></link>,
<link linkend="sql-createsubscription-params-with-run-as-owner"><literal>run_as_owner</literal></link>,
<link linkend="sql-createsubscription-params-with-origin"><literal>origin</literal></link>, and
<link linkend="sql-createsubscription-params-with-failover"><literal>failover</literal></link>.
<link linkend="sql-createsubscription-params-with-origin"><literal>origin</literal></link>,
<link linkend="sql-createsubscription-params-with-failover"><literal>failover</literal></link>, and
<link linkend="sql-createsubscription-params-with-two-phase"><literal>two_phase</literal></link>.
Only a superuser can set <literal>password_required = false</literal>.
</para>
@ -252,6 +254,32 @@ ALTER SUBSCRIPTION <replaceable class="parameter">name</replaceable> RENAME TO <
<link linkend="sql-createsubscription-params-with-failover"><literal>failover</literal></link>
option is enabled.
</para>
<para>
The <link linkend="sql-createsubscription-params-with-failover"><literal>failover</literal></link>
and <link linkend="sql-createsubscription-params-with-two-phase"><literal>two_phase</literal></link>
parameters can only be altered when the subscription is disabled.
</para>
<para>
When altering <link linkend="sql-createsubscription-params-with-two-phase"><literal>two_phase</literal></link>
from <literal>true</literal> to <literal>false</literal>, the backend
process reports an error if any prepared transactions done by the
logical replication worker (from when <literal>two_phase</literal>
parameter was still <literal>true</literal>) are found. You can resolve
prepared transactions on the publisher node, or manually roll back them
on the subscriber, and then try again. The transactions prepared by
logical replication worker corresponding to a particular subscription have
the following pattern: <quote><literal>pg_gid_%u_%u</literal></quote>
(parameters: subscription <parameter>oid</parameter>, remote transaction id <parameter>xid</parameter>).
To resolve such transactions manually, you need to roll back all
the prepared transactions with corresponding subscription IDs in their
names. Applications can check
<link linkend="view-pg-prepared-xacts"><structname>pg_prepared_xacts</structname></link>
to find the required prepared transactions. After the <literal>two_phase</literal>
option is changed from <literal>true</literal> to <literal>false</literal>,
the publisher will replicate the transactions again when they are committed.
</para>
</listitem>
</varlistentry>