Обсуждение: Wrong comment for ReplicationSlotCreate

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

Wrong comment for ReplicationSlotCreate

От
Daniil Davydov
Дата:
Hi,

I noticed that the comment for ReplicationSlotCreate function contains this
description for the "two_phase" option :

 * two_phase: Allows decoding of prepared transactions. We allow this option
 *     to be enabled only at the slot creation time. If we allow this option
 *     to be changed during decoding then it is quite possible that we skip
 *     prepare first time because this option was not enabled. Now next time
 *     during getting changes, if the two_phase option is enabled it can skip
 *     prepare because by that time start decoding point has been moved. So the
 *     user will only get commit prepared.

But commit [1] introduced the ability to alter the "two_phase" option for the
replication slot. Thus, I guess that the comment mentioned above is
outdated and we should change it.

[1] 1462aad2e4474ab61174f8ab00992cd3d6d57c7b

--
Best regards,
Daniil Davydov

Вложения

Re: Wrong comment for ReplicationSlotCreate

От
Chao Li
Дата:

> On Dec 29, 2025, at 21:39, Daniil Davydov <3danissimo@gmail.com> wrote:
>
> Hi,
>
> I noticed that the comment for ReplicationSlotCreate function contains this
> description for the "two_phase" option :
>
> * two_phase: Allows decoding of prepared transactions. We allow this option
> *     to be enabled only at the slot creation time. If we allow this option
> *     to be changed during decoding then it is quite possible that we skip
> *     prepare first time because this option was not enabled. Now next time
> *     during getting changes, if the two_phase option is enabled it can skip
> *     prepare because by that time start decoding point has been moved. So the
> *     user will only get commit prepared.
>
> But commit [1] introduced the ability to alter the "two_phase" option for the
> replication slot. Thus, I guess that the comment mentioned above is
> outdated and we should change it.
>
> [1] 1462aad2e4474ab61174f8ab00992cd3d6d57c7b
>
> --
> Best regards,
> Daniil Davydov
> <0001-Fix-comment-for-ReplicationSlotCreate.patch>

The old comment claimed “We allow this option to be enabled only at the slot creation time” that is no longer true
aftercommit 1462aad2e4474ab61174f8ab00992cd3d6d57c7b, so yes, the old comment need updating. 

But I think the updated version is too simple. It loses the information that enabling two_phase later can result in
missingPREPARE. 

So, I would suggest something like:
```
* two_phase: If enabled, allows decoding of prepared transactions.
*     Note that enabling this option after decoding has already advanced
*     may result in missing PREPARE records for transactions that were
*     prepared before the option was enabled.

```

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/







Re: Wrong comment for ReplicationSlotCreate

От
Daniil Davydov
Дата:
Hi,

On Tue, Dec 30, 2025 at 4:18 PM Chao Li <li.evan.chao@gmail.com> wrote:
>
> But I think the updated version is too simple. It loses the information that enabling two_phase later can result in
missingPREPARE. 
>
> So, I would suggest something like:
> ```
> * two_phase: If enabled, allows decoding of prepared transactions.
> *     Note that enabling this option after decoding has already advanced
> *     may result in missing PREPARE records for transactions that were
> *     prepared before the option was enabled.
>
> ```

Thanks for the review!

As far as I understand, if the publisher prepares a transaction and
then subscriber
tries to create a subscription, walsender will wait until the prepared
transaction is
finished (during execution of  CREATE_REPLICATION_SLOT command).
We can find this logic inside the SnapBuildFindSnapshot function. Thus, we
cannot miss any PREPARE record for the created slot.

Am I missing something?

--
Best regards,
Daniil Davydov



Re: Wrong comment for ReplicationSlotCreate

От
Chao Li
Дата:

> On Dec 30, 2025, at 18:07, Daniil Davydov <3danissimo@gmail.com> wrote:
>
> Hi,
>
> On Tue, Dec 30, 2025 at 4:18 PM Chao Li <li.evan.chao@gmail.com> wrote:
>>
>> But I think the updated version is too simple. It loses the information that enabling two_phase later can result in
missingPREPARE. 
>>
>> So, I would suggest something like:
>> ```
>> * two_phase: If enabled, allows decoding of prepared transactions.
>> *     Note that enabling this option after decoding has already advanced
>> *     may result in missing PREPARE records for transactions that were
>> *     prepared before the option was enabled.
>>
>> ```
>
> Thanks for the review!
>
> As far as I understand, if the publisher prepares a transaction and
> then subscriber
> tries to create a subscription, walsender will wait until the prepared
> transaction is
> finished (during execution of  CREATE_REPLICATION_SLOT command).
> We can find this logic inside the SnapBuildFindSnapshot function. Thus, we
> cannot miss any PREPARE record for the created slot.
>
> Am I missing something?
>
> --
> Best regards,
> Daniil Davydov


You’re right that during CREATE_REPLICATION_SLOT, SnapBuildFindSnapshot() ensures we don’t miss PREPARE records for
preparedtransactions that exist at creation time. 

1462aad2e introduced support for altering logical replication slot options, including two_phase, after the slot has
beencreated. The commit comment says: 
```
    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.
```

true->false is not allowed, however false->true is permitted. So, I think the old comment is still possible today:
```
*     Note that enabling this option after decoding has already advanced
*     may result in missing PREPARE records for transactions that were
*     prepared before the option was enabled.
```

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/







Re: Wrong comment for ReplicationSlotCreate

От
Daniil Davydov
Дата:
Hi,

On Wed, Dec 31, 2025 at 9:32 AM Chao Li <li.evan.chao@gmail.com> wrote:
>
> You’re right that during CREATE_REPLICATION_SLOT, SnapBuildFindSnapshot() ensures we don’t miss PREPARE records for
preparedtransactions that exist at creation time. 
>
> 1462aad2e introduced support for altering logical replication slot options, including two_phase, after the slot has
beencreated. The commit comment says: 
> ```
>     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.
> ```
>
> true->false is not allowed, however false->true is permitted. So, I think the old comment is still possible today:
> ```
> *     Note that enabling this option after decoding has already advanced
> *     may result in missing PREPARE records for transactions that were
> *     prepared before the option was enabled.
> ```
>

Hm, I still can't understand why the comment that you provided is correct.

How can we "miss PREPARE records" if slot creation requires all  prepared
transactions to finish? The commit message says about risks during the
change of the parameter "on the fly". But we are dealing with slot creation.

--
Best regards,
Daniil Davydov



Re: Wrong comment for ReplicationSlotCreate

От
Chao Li
Дата:

> On Jan 2, 2026, at 00:31, Daniil Davydov <3danissimo@gmail.com> wrote:
>
> Hi,
>
> On Wed, Dec 31, 2025 at 9:32 AM Chao Li <li.evan.chao@gmail.com> wrote:
>>
>> You’re right that during CREATE_REPLICATION_SLOT, SnapBuildFindSnapshot() ensures we don’t miss PREPARE records for
preparedtransactions that exist at creation time. 
>>
>> 1462aad2e introduced support for altering logical replication slot options, including two_phase, after the slot has
beencreated. The commit comment says: 
>> ```
>>    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.
>> ```
>>
>> true->false is not allowed, however false->true is permitted. So, I think the old comment is still possible today:
>> ```
>> *     Note that enabling this option after decoding has already advanced
>> *     may result in missing PREPARE records for transactions that were
>> *     prepared before the option was enabled.
>> ```
>>
>
> Hm, I still can't understand why the comment that you provided is correct.
>
> How can we "miss PREPARE records" if slot creation requires all  prepared
> transactions to finish? The commit message says about risks during the
> change of the parameter "on the fly". But we are dealing with slot creation.
>
> --
> Best regards,
> Daniil Davydov

No problem, maybe I am wrong. Then please ignore my comment and wait for other review comments.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/







RE: Wrong comment for ReplicationSlotCreate

От
"Hayato Kuroda (Fujitsu)"
Дата:
Dear Daniil, Chao,

I was the main author of 1462aad2. It is enough to remove outdated comments atop
the definition. In other words, your patch looks good to me.

If needed, we can also notify developers that the two-phase option should not be
altered while decoding WAL records. In logical replication, we ensure that the
subscription is disabled and there are no apply workers. However, I don't think
such comments can be atop the ReplicationSlotCreate(). Maybe around
ReplicationSlotAlter(), but it may be out of scope of the initial motivation.

By the way, the comment may have been broken since a8fd13. Even when the
subscription was defined with two_phase=on, the backend creates the slot with
two_phase = off. The configuration is changed after the tablesync is done.

Best regards,
Hayato Kuroda
FUJITSU LIMITED


Re: Wrong comment for ReplicationSlotCreate

От
Amit Kapila
Дата:
On Thu, Jan 1, 2026 at 10:01 PM Daniil Davydov <3danissimo@gmail.com> wrote:
>
> On Wed, Dec 31, 2025 at 9:32 AM Chao Li <li.evan.chao@gmail.com> wrote:
> >
> > You’re right that during CREATE_REPLICATION_SLOT, SnapBuildFindSnapshot() ensures we don’t miss PREPARE records for
preparedtransactions that exist at creation time. 
> >
> > 1462aad2e introduced support for altering logical replication slot options, including two_phase, after the slot has
beencreated. The commit comment says: 
> > ```
> >     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.
> > ```
> >
> > true->false is not allowed, however false->true is permitted. So, I think the old comment is still possible today:
> > ```
> > *     Note that enabling this option after decoding has already advanced
> > *     may result in missing PREPARE records for transactions that were
> > *     prepared before the option was enabled.
> > ```
> >
>
> Hm, I still can't understand why the comment that you provided is correct.
>
> How can we "miss PREPARE records" if slot creation requires all  prepared
> transactions to finish? The commit message says about risks during the
> change of the parameter "on the fly". But we are dealing with slot creation.
>

The point is about changing the two_phase property of slot after
slot_creation which we carefully change for slots related to
subscription. So, if we don't take necessary precautions then enabling
it on the fly could lead to the problem Chao is worried about.

--
With Regards,
Amit Kapila.



Re: Wrong comment for ReplicationSlotCreate

От
Amit Kapila
Дата:
On Mon, Jan 5, 2026 at 9:46 AM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
>
> Dear Daniil, Chao,
>
> I was the main author of 1462aad2. It is enough to remove outdated comments atop
> the definition. In other words, your patch looks good to me.
>
> If needed, we can also notify developers that the two-phase option should not be
> altered while decoding WAL records. In logical replication, we ensure that the
> subscription is disabled and there are no apply workers. However, I don't think
> such comments can be atop the ReplicationSlotCreate(). Maybe around
> ReplicationSlotAlter(), but it may be out of scope of the initial motivation.
>

I think it is better if we add some comments atop
ReplicationSlotAlter() as you are suggesting. What do you think of the
attached?

--
With Regards,
Amit Kapila.

Вложения

RE: Wrong comment for ReplicationSlotCreate

От
"Hayato Kuroda (Fujitsu)"
Дата:
Dear Amit,

> 
> I think it is better if we add some comments atop
> ReplicationSlotAlter() as you are suggesting. What do you think of the
> attached?

Thanks for attaching the patch. There is a small typo:

> + * clinet-side. Enabling it at any random point during decoding has the

"clinet" should be client. Others are OK for me.

Best regards,
Hayato Kuroda
FUJITSU LIMITED


Re: Wrong comment for ReplicationSlotCreate

От
Chao Li
Дата:

> On Jan 5, 2026, at 13:51, Amit Kapila <amit.kapila16@gmail.com> wrote:
>
> On Mon, Jan 5, 2026 at 9:46 AM Hayato Kuroda (Fujitsu)
> <kuroda.hayato@fujitsu.com> wrote:
>>
>> Dear Daniil, Chao,
>>
>> I was the main author of 1462aad2. It is enough to remove outdated comments atop
>> the definition. In other words, your patch looks good to me.
>>
>> If needed, we can also notify developers that the two-phase option should not be
>> altered while decoding WAL records. In logical replication, we ensure that the
>> subscription is disabled and there are no apply workers. However, I don't think
>> such comments can be atop the ReplicationSlotCreate(). Maybe around
>> ReplicationSlotAlter(), but it may be out of scope of the initial motivation.
>>
>
> I think it is better if we add some comments atop
> ReplicationSlotAlter() as you are suggesting. What do you think of the
> attached?
>
> --
> With Regards,
> Amit Kapila.
> <v1_improve_alter_slot_comments.patch>

Hi Amit,

While reviewing your change, I find the other typo in slot.c:
```
-       /* Check if the slot exits with the given name. */
+       /* Check if the slot exists with the given name. */
        s = SearchNamedReplicationSlot(name, false);
        if (s == NULL || !s->in_use)
```

“Exits” and “exists” have totally different meanings, thus might lead to misunderstanding. Attached is a trivial diff
tofix that. 

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/





Вложения

Re: Wrong comment for ReplicationSlotCreate

От
Daniil Davydov
Дата:
Hi,

On Mon, Jan 5, 2026 at 1:25 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
>
> Dear Amit,
>
> >
> > I think it is better if we add some comments atop
> > ReplicationSlotAlter() as you are suggesting. What do you think of the
> > attached?
>
> Thanks for attaching the patch. There is a small typo:
>
> > + * clinet-side. Enabling it at any random point during decoding has the
>
> "clinet" should be client. Others are OK for me.
>

Thank you all for your comments!
I agree with suggested fixes. Please, see them in the attached patch.

On Mon, Jan 5, 2026 at 2:56 PM Chao Li <li.evan.chao@gmail.com> wrote:
>
> Hi Amit,
>
> While reviewing your change, I find the other typo in slot.c:
> ```
> -       /* Check if the slot exits with the given name. */
> +       /* Check if the slot exists with the given name. */
>         s = SearchNamedReplicationSlot(name, false);
>         if (s == NULL || !s->in_use)
> ```
>
> “Exits” and “exists” have totally different meanings, thus might lead to misunderstanding. Attached is a trivial diff
tofix that. 
>

Good catch!
I am not sure that we should put both fixes together, so I'll put your fix in a
separate patch.

--
Best regards,
Daniil Davydov

Вложения

Re: Wrong comment for ReplicationSlotCreate

От
Amit Kapila
Дата:
On Mon, Jan 5, 2026 at 4:01 PM Daniil Davydov <3danissimo@gmail.com> wrote:
>
> Good catch!
> I am not sure that we should put both fixes together, so I'll put your fix in a
> separate patch.
>

It is okay to fix the typo patch in HEAD but the other patch related
to fixing the outdated comment needs to be backpatched, so pushed
separately. Thanks!

--
With Regards,
Amit Kapila.