Dear hackers,

> Looking at this a bit more, maybe rolling back all prepared transactions on 
> the
> subscriber when toggling two_phase from true to false might not be desirable
> for the customer. Maybe we should have an option for customers to control
> whether transactions should be rolled back or not. Maybe transactions should
> only be rolled back if a "force" option is also set. What do people think?

And here is a patch for adds new option "force_alter" (better name is very 
welcome).
It could be used only when altering two_phase option. Let me share examples.

Assuming that there are logical replication system with two_phase = on, and
there are prepared transactions:

```
subscriber=# SELECT * FROM pg_prepared_xacts ;
 transaction |       gid        |           prepared            |  owner   | 
database 
-------------+------------------+-------------------------------+----------+----------
         741 | pg_gid_16390_741 | 2024-04-22 08:02:34.727913+00 | postgres | 
postgres
         742 | pg_gid_16390_742 | 2024-04-22 08:02:34.729486+00 | postgres | 
postgres
(2 rows)
```

At that time, altering two_phase to false alone will be failed:

```
subscriber=# ALTER SUBSCRIPTION sub DISABLE ;
ALTER SUBSCRIPTION
subscriber=# ALTER SUBSCRIPTION sub SET (two_phase = off);
ERROR:  cannot alter two_phase = false when there are prepared transactions
```

It succeeds if force_alter is also expressly set. Prepared transactions will be
aborted at that time.

```
subscriber=# ALTER SUBSCRIPTION sub SET (two_phase = off, force_alter = on);
ALTER SUBSCRIPTION
subscriber=# SELECT * FROM pg_prepared_xacts ;
 transaction | gid | prepared | owner | database 
-------------+-----+----------+-------+----------
(0 rows)
```

Best Regards,
Hayato Kuroda
FUJITSU LIMITED
https://www.fujitsu.com/global/ 

Attachment: v5-0001-Allow-altering-of-two_phase-option-of-a-SUBSCRIPT.patch
Description: v5-0001-Allow-altering-of-two_phase-option-of-a-SUBSCRIPT.patch

Attachment: v5-0002-Alter-slot-option-two_phase-only-when-altering-tr.patch
Description: v5-0002-Alter-slot-option-two_phase-only-when-altering-tr.patch

Attachment: v5-0003-Abort-prepared-transactions-while-altering-two_ph.patch
Description: v5-0003-Abort-prepared-transactions-while-altering-two_ph.patch

Attachment: v5-0004-Add-force_alter-option.patch
Description: v5-0004-Add-force_alter-option.patch

Reply via email to