Re: Fix SPLIT PARTITION bound-overlap bug and other improvements

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

Re: Fix SPLIT PARTITION bound-overlap bug and other improvements

От:
Chao Li <li.evan.chao@gmail.com>
Дата:


> On May 18, 2026, at 17:16, Alexander Korotkov  wrote:
> 
> On Mon, May 18, 2026 at 2:15 AM Chao Li  wrote:
>>> On May 18, 2026, at 05:45, Alexander Korotkov  wrote:
>>> 
>>> Hi, Chao!
>>> 
>>> On Thu, May 14, 2026 at 9:59 AM Chao Li  wrote:
>>>> To make this patch easier to process, I split it into 4 commits:
>>>> 
>>>> 0001 - Fixes the bound-overlap bug
>>>> 0002 - Fix the incorrect HINT message for the DEFAULT case
>>>> 0003 - Fix the incorrect description about combined bound in the SGML doc
>>>> 0004 - Reject only-create-default-partition usage
>>> 
>>> Thank you for your work.  I've revised the patchset.
>>> 0002 - I've also fixed gramma of hints in other branches
>>> 0004 - In the check_split_partition_not_same_bound(), calling
>>> partition_bounds_create() and partition_bounds_equal() looks a bit
>>> heavyweight.  It doesn't matter much performance-wise, but it feels
>>> like start processing from scratch while we're on quite late stage
>>> already.  I've replaced that with more lightweight check.  Also I
>>> removed dealing with memory context.  This code implies small
>>> non-repetitive memory allocations which only lives during DDL
>>> operation, no need to wrap them with memory context as we don't do so
>>> in other places.
>>> 
>>> Any objections if I commit this?
>>> 
>>> ------
>>> Regards,
>>> Alexander Korotkov
>>> Supabase
>> 
>> Hi Alexander,
>> 
>> Thanks for the revisions. I think you may have missed the attachments, so I cannot review the changes.
> 
> Sorry. Here it is.
> 
> ------
> Regards,
> Alexander Korotkov
> Supabase
> 

v3-0001 through v3-0003 look good to me.

For v3-0004, I have a suspicion, but it's late here and my brain is getting slow, so I would like to study it more tomorrow.

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






Re: Fix SPLIT PARTITION bound-overlap bug and other improvements

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
On Mon, May 18, 2026 at 2:57 PM Chao Li  wrote:
> > 
>
> v3-0001 through v3-0003 look good to me.
>
> For v3-0004, I have a suspicion, but it's late here and my brain is getting slow, so I would like to study it more tomorrow.

Sure, take your time.

------
Regards,
Alexander Korotkov
Supabase


Re: Fix SPLIT PARTITION bound-overlap bug and other improvements

От:
Chao Li <li.evan.chao@gmail.com>
Дата:
Вложения:


> On May 18, 2026, at 20:04, Alexander Korotkov  wrote:
> 
> On Mon, May 18, 2026 at 2:57 PM Chao Li  wrote:
>>> 
>> 
>> v3-0001 through v3-0003 look good to me.
>> 
>> For v3-0004, I have a suspicion, but it's late here and my brain is getting slow, so I would like to study it more tomorrow.
> 
> Sure, take your time.
> 
> ------
> Regards,
> Alexander Korotkov
> Supabase

My suspicion was that check_split_partition_not_same_bound() now has two paths. The RANGE path honors collation, while the LIST path does not. So I spent some time creating a test that uses a case-insensitive collation:
```
evantest=# create collation case_insensitive (provider=icu, locale='und-u-ks-level2', deterministic = false);
CREATE COLLATION
evantest=# create table t (b text collate case_insensitive) partition by list (b);
CREATE TABLE
evantest=# create table tp_ab partition of t for values in ('a', 'b');
CREATE TABLE
evantest=# alter table t split partition tp_ab into
evantest-#   (partition tp_a for values in ('a', 'A'),
evantest(#   partition tp_default default);
ERROR:  cannot split partition "tp_ab" only to add a DEFAULT partition
LINE 2:   (partition tp_a for values in ('a', 'A'),
                     ^
DETAIL:  The non-DEFAULT partition would keep the same partition bound.
HINT:  Use CREATE TABLE ... PARTITION OF ... DEFAULT to add a DEFAULT partition.
```

In this test, the split partition’s bound is ('a', 'b'), and the new partition’s bound is ('a', 'A'). Their list lengths are both 2, but the two bounds are actually different, because 'a' and 'A' are considered equal by the collation.

So, in the LIST path, since check_partition_bounds_for_split_list() has already ensured that the new partition’s bound is contained within the split partition’s bound, we need to check the reverse direction as well. Whether the split partition’s bound is also contained in the new partition’s bound. If yes, the two bounds are identical.

See the attached v4 for my changes for 0004. 0001-0003 are unchanged. Since 0001 and 0003 are independent of 0004, maybe they can be pushed first.

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




Re: Fix SPLIT PARTITION bound-overlap bug and other improvements

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
Вложения:
Hi, Chao!

On Tue, May 19, 2026 at 5:50 AM Chao Li  wrote:
> > On May 18, 2026, at 20:04, Alexander Korotkov  wrote:
> >
> > On Mon, May 18, 2026 at 2:57 PM Chao Li  wrote:
> >>> 
> >>
> >> v3-0001 through v3-0003 look good to me.
> >>
> >> For v3-0004, I have a suspicion, but it's late here and my brain is getting slow, so I would like to study it more tomorrow.
> >
> > Sure, take your time.
> >
> > ------
> > Regards,
> > Alexander Korotkov
> > Supabase
>
> My suspicion was that check_split_partition_not_same_bound() now has two paths. The RANGE path honors collation, while the LIST path does not. So I spent some time creating a test that uses a case-insensitive collation:
> ```
> evantest=# create collation case_insensitive (provider=icu, locale='und-u-ks-level2', deterministic = false);
> CREATE COLLATION
> evantest=# create table t (b text collate case_insensitive) partition by list (b);
> CREATE TABLE
> evantest=# create table tp_ab partition of t for values in ('a', 'b');
> CREATE TABLE
> evantest=# alter table t split partition tp_ab into
> evantest-#   (partition tp_a for values in ('a', 'A'),
> evantest(#   partition tp_default default);
> ERROR:  cannot split partition "tp_ab" only to add a DEFAULT partition
> LINE 2:   (partition tp_a for values in ('a', 'A'),
>                      ^
> DETAIL:  The non-DEFAULT partition would keep the same partition bound.
> HINT:  Use CREATE TABLE ... PARTITION OF ... DEFAULT to add a DEFAULT partition.
> ```
>
> In this test, the split partition’s bound is ('a', 'b'), and the new partition’s bound is ('a', 'A'). Their list lengths are both 2, but the two bounds are actually different, because 'a' and 'A' are considered equal by the collation.
>
> So, in the LIST path, since check_partition_bounds_for_split_list() has already ensured that the new partition’s bound is contained within the split partition’s bound, we need to check the reverse direction as well. Whether the split partition’s bound is also contained in the new partition’s bound. If yes, the two bounds are identical.
>
> See the attached v4 for my changes for 0004. 0001-0003 are unchanged. Since 0001 and 0003 are independent of 0004, maybe they can be pushed first.

I've pushed 0001-0003.  Thank you for discovering the collation issue
in 0004.  Note that original approach of using
partition_bounds_equal() can't handle different collations too (as it
internally uses datumIsEqual()).  I've revised the remaining patch:
made function header comment a bit more detailed and added additional
regression tests.  Please, check.

------
Regards,
Alexander Korotkov
Supabase

Re: Fix SPLIT PARTITION bound-overlap bug and other improvements

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
On Wed, May 20, 2026 at 9:29 AM Chao Li  wrote:
> > On May 20, 2026, at 14:19, Alexander Korotkov  wrote:
> >
> > Hi, Chao!
> >
> > On Wed, May 20, 2026 at 2:37 AM Chao Li  wrote:
> >>> On May 19, 2026, at 19:00, Alexander Korotkov  wrote:
> >>> On Tue, May 19, 2026 at 5:50 AM Chao Li  wrote:
> >>>>> On May 18, 2026, at 20:04, Alexander Korotkov  wrote:
> >>>>>
> >>>>> On Mon, May 18, 2026 at 2:57 PM Chao Li  wrote:
> >>>>>>> 
> >>>>>>
> >>>>>> v3-0001 through v3-0003 look good to me.
> >>>>>>
> >>>>>> For v3-0004, I have a suspicion, but it's late here and my brain is getting slow, so I would like to study it more tomorrow.
> >>>>>
> >>>>> Sure, take your time.
> >>>>>
> >>>>> ------
> >>>>> Regards,
> >>>>> Alexander Korotkov
> >>>>> Supabase
> >>>>
> >>>> My suspicion was that check_split_partition_not_same_bound() now has two paths. The RANGE path honors collation, while the LIST path does not. So I spent some time creating a test that uses a case-insensitive collation:
> >>>> ```
> >>>> evantest=# create collation case_insensitive (provider=icu, locale='und-u-ks-level2', deterministic = false);
> >>>> CREATE COLLATION
> >>>> evantest=# create table t (b text collate case_insensitive) partition by list (b);
> >>>> CREATE TABLE
> >>>> evantest=# create table tp_ab partition of t for values in ('a', 'b');
> >>>> CREATE TABLE
> >>>> evantest=# alter table t split partition tp_ab into
> >>>> evantest-#   (partition tp_a for values in ('a', 'A'),
> >>>> evantest(#   partition tp_default default);
> >>>> ERROR:  cannot split partition "tp_ab" only to add a DEFAULT partition
> >>>> LINE 2:   (partition tp_a for values in ('a', 'A'),
> >>>>                    ^
> >>>> DETAIL:  The non-DEFAULT partition would keep the same partition bound.
> >>>> HINT:  Use CREATE TABLE ... PARTITION OF ... DEFAULT to add a DEFAULT partition.
> >>>> ```
> >>>>
> >>>> In this test, the split partition’s bound is ('a', 'b'), and the new partition’s bound is ('a', 'A'). Their list lengths are both 2, but the two bounds are actually different, because 'a' and 'A' are considered equal by the collation.
> >>>>
> >>>> So, in the LIST path, since check_partition_bounds_for_split_list() has already ensured that the new partition’s bound is contained within the split partition’s bound, we need to check the reverse direction as well. Whether the split partition’s bound is also contained in the new partition’s bound. If yes, the two bounds are identical.
> >>>>
> >>>> See the attached v4 for my changes for 0004. 0001-0003 are unchanged. Since 0001 and 0003 are independent of 0004, maybe they can be pushed first.
> >>>
> >>> I've pushed 0001-0003.
> >>
> >> Thanks for pushing them.
> >>
> >>> Thank you for discovering the collation issue
> >>> in 0004.  Note that original approach of using
> >>> partition_bounds_equal() can't handle different collations too (as it
> >>> internally uses datumIsEqual()).
> >>
> >> Yes, I realized that while reviewing v3. That’s reason I didn’t get back v2 and only worked again based on v3.
> >>
> >>> I've revised the remaining patch:
> >>> made function header comment a bit more detailed
> >>
> >> This part looks good to me.
> >>
> >>> and added additional
> >>> regression tests.  Please, check.
> >>>
> >>
> >> But I don’t see any change for regression test between v4 and v5. Maybe you forgot to save your changes?
> >
> > Sorry, I just mess up, no changes in tests.
> > I'm going to push this if no objection.
> >
>
> No worries. Then v5 looks good to me.

Thank you, pushed.

------
Regards,
Alexander Korotkov
Supabase


Re: Fix SPLIT PARTITION bound-overlap bug and other improvements

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
On Wed, May 20, 2026 at 2:46 PM Alexander Korotkov  wrote:
> On Wed, May 20, 2026 at 9:29 AM Chao Li  wrote:
> > > On May 20, 2026, at 14:19, Alexander Korotkov  wrote:
> > > On Wed, May 20, 2026 at 2:37 AM Chao Li  wrote:
> > >>> On May 19, 2026, at 19:00, Alexander Korotkov  wrote:
> > >>> On Tue, May 19, 2026 at 5:50 AM Chao Li  wrote:
> > >>>>> On May 18, 2026, at 20:04, Alexander Korotkov  wrote:
> > >>>>>
> > >>>>> On Mon, May 18, 2026 at 2:57 PM Chao Li  wrote:
> > >>>>>>> 
> > >>>>>>
> > >>>>>> v3-0001 through v3-0003 look good to me.
> > >>>>>>
> > >>>>>> For v3-0004, I have a suspicion, but it's late here and my brain is getting slow, so I would like to study it more tomorrow.
> > >>>>>
> > >>>>> Sure, take your time.
> > >>>>>
> > >>>>> ------
> > >>>>> Regards,
> > >>>>> Alexander Korotkov
> > >>>>> Supabase
> > >>>>
> > >>>> My suspicion was that check_split_partition_not_same_bound() now has two paths. The RANGE path honors collation, while the LIST path does not. So I spent some time creating a test that uses a case-insensitive collation:
> > >>>> ```
> > >>>> evantest=# create collation case_insensitive (provider=icu, locale='und-u-ks-level2', deterministic = false);
> > >>>> CREATE COLLATION
> > >>>> evantest=# create table t (b text collate case_insensitive) partition by list (b);
> > >>>> CREATE TABLE
> > >>>> evantest=# create table tp_ab partition of t for values in ('a', 'b');
> > >>>> CREATE TABLE
> > >>>> evantest=# alter table t split partition tp_ab into
> > >>>> evantest-#   (partition tp_a for values in ('a', 'A'),
> > >>>> evantest(#   partition tp_default default);
> > >>>> ERROR:  cannot split partition "tp_ab" only to add a DEFAULT partition
> > >>>> LINE 2:   (partition tp_a for values in ('a', 'A'),
> > >>>>                    ^
> > >>>> DETAIL:  The non-DEFAULT partition would keep the same partition bound.
> > >>>> HINT:  Use CREATE TABLE ... PARTITION OF ... DEFAULT to add a DEFAULT partition.
> > >>>> ```
> > >>>>
> > >>>> In this test, the split partition’s bound is ('a', 'b'), and the new partition’s bound is ('a', 'A'). Their list lengths are both 2, but the two bounds are actually different, because 'a' and 'A' are considered equal by the collation.
> > >>>>
> > >>>> So, in the LIST path, since check_partition_bounds_for_split_list() has already ensured that the new partition’s bound is contained within the split partition’s bound, we need to check the reverse direction as well. Whether the split partition’s bound is also contained in the new partition’s bound. If yes, the two bounds are identical.
> > >>>>
> > >>>> See the attached v4 for my changes for 0004. 0001-0003 are unchanged. Since 0001 and 0003 are independent of 0004, maybe they can be pushed first.
> > >>>
> > >>> I've pushed 0001-0003.
> > >>
> > >> Thanks for pushing them.
> > >>
> > >>> Thank you for discovering the collation issue
> > >>> in 0004.  Note that original approach of using
> > >>> partition_bounds_equal() can't handle different collations too (as it
> > >>> internally uses datumIsEqual()).
> > >>
> > >> Yes, I realized that while reviewing v3. That’s reason I didn’t get back v2 and only worked again based on v3.
> > >>
> > >>> I've revised the remaining patch:
> > >>> made function header comment a bit more detailed
> > >>
> > >> This part looks good to me.
> > >>
> > >>> and added additional
> > >>> regression tests.  Please, check.
> > >>>
> > >>
> > >> But I don’t see any change for regression test between v4 and v5. Maybe you forgot to save your changes?
> > >
> > > Sorry, I just mess up, no changes in tests.
> > > I'm going to push this if no objection.
> > >
> >
> > No worries. Then v5 looks good to me.
>
> Thank you, pushed.

Uhhh, most of buildfarm animals don't support locales used in our
tests.  I've to revert that,

------
Regards,
Alexander Korotkov
Supabase


Re: Fix SPLIT PARTITION bound-overlap bug and other improvements

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
Вложения:
On Wed, May 20, 2026 at 11:29 PM Alexander Korotkov
 wrote:
> On Wed, May 20, 2026 at 2:46 PM Alexander Korotkov  wrote:
> > On Wed, May 20, 2026 at 9:29 AM Chao Li  wrote:
> > > > On May 20, 2026, at 14:19, Alexander Korotkov  wrote:
> > > > On Wed, May 20, 2026 at 2:37 AM Chao Li  wrote:
> > > >>> On May 19, 2026, at 19:00, Alexander Korotkov  wrote:
> > > >>> On Tue, May 19, 2026 at 5:50 AM Chao Li  wrote:
> > > >>>>> On May 18, 2026, at 20:04, Alexander Korotkov  wrote:
> > > >>>>>
> > > >>>>> On Mon, May 18, 2026 at 2:57 PM Chao Li  wrote:
> > > >>>>>>> 
> > > >>>>>>
> > > >>>>>> v3-0001 through v3-0003 look good to me.
> > > >>>>>>
> > > >>>>>> For v3-0004, I have a suspicion, but it's late here and my brain is getting slow, so I would like to study it more tomorrow.
> > > >>>>>
> > > >>>>> Sure, take your time.
> > > >>>>>
> > > >>>>> ------
> > > >>>>> Regards,
> > > >>>>> Alexander Korotkov
> > > >>>>> Supabase
> > > >>>>
> > > >>>> My suspicion was that check_split_partition_not_same_bound() now has two paths. The RANGE path honors collation, while the LIST path does not. So I spent some time creating a test that uses a case-insensitive collation:
> > > >>>> ```
> > > >>>> evantest=# create collation case_insensitive (provider=icu, locale='und-u-ks-level2', deterministic = false);
> > > >>>> CREATE COLLATION
> > > >>>> evantest=# create table t (b text collate case_insensitive) partition by list (b);
> > > >>>> CREATE TABLE
> > > >>>> evantest=# create table tp_ab partition of t for values in ('a', 'b');
> > > >>>> CREATE TABLE
> > > >>>> evantest=# alter table t split partition tp_ab into
> > > >>>> evantest-#   (partition tp_a for values in ('a', 'A'),
> > > >>>> evantest(#   partition tp_default default);
> > > >>>> ERROR:  cannot split partition "tp_ab" only to add a DEFAULT partition
> > > >>>> LINE 2:   (partition tp_a for values in ('a', 'A'),
> > > >>>>                    ^
> > > >>>> DETAIL:  The non-DEFAULT partition would keep the same partition bound.
> > > >>>> HINT:  Use CREATE TABLE ... PARTITION OF ... DEFAULT to add a DEFAULT partition.
> > > >>>> ```
> > > >>>>
> > > >>>> In this test, the split partition’s bound is ('a', 'b'), and the new partition’s bound is ('a', 'A'). Their list lengths are both 2, but the two bounds are actually different, because 'a' and 'A' are considered equal by the collation.
> > > >>>>
> > > >>>> So, in the LIST path, since check_partition_bounds_for_split_list() has already ensured that the new partition’s bound is contained within the split partition’s bound, we need to check the reverse direction as well. Whether the split partition’s bound is also contained in the new partition’s bound. If yes, the two bounds are identical.
> > > >>>>
> > > >>>> See the attached v4 for my changes for 0004. 0001-0003 are unchanged. Since 0001 and 0003 are independent of 0004, maybe they can be pushed first.
> > > >>>
> > > >>> I've pushed 0001-0003.
> > > >>
> > > >> Thanks for pushing them.
> > > >>
> > > >>> Thank you for discovering the collation issue
> > > >>> in 0004.  Note that original approach of using
> > > >>> partition_bounds_equal() can't handle different collations too (as it
> > > >>> internally uses datumIsEqual()).
> > > >>
> > > >> Yes, I realized that while reviewing v3. That’s reason I didn’t get back v2 and only worked again based on v3.
> > > >>
> > > >>> I've revised the remaining patch:
> > > >>> made function header comment a bit more detailed
> > > >>
> > > >> This part looks good to me.
> > > >>
> > > >>> and added additional
> > > >>> regression tests.  Please, check.
> > > >>>
> > > >>
> > > >> But I don’t see any change for regression test between v4 and v5. Maybe you forgot to save your changes?
> > > >
> > > > Sorry, I just mess up, no changes in tests.
> > > > I'm going to push this if no objection.
> > > >
> > >
> > > No worries. Then v5 looks good to me.
> >
> > Thank you, pushed.
>
> Uhhh, most of buildfarm animals don't support locales used in our
> tests.  I've to revert that,

The another attempt is attached.  Now use -0.0 and 0.0 as binary
different but logically equivalent values, no locale dependence.

------
Regards,
Alexander Korotkov
Supabase

Re: Fix SPLIT PARTITION bound-overlap bug and other improvements

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
On Thu, May 21, 2026 at 2:23 AM Chao Li  wrote:
>
> > On May 21, 2026, at 05:17, Alexander Korotkov  wrote:
> > On Wed, May 20, 2026 at 11:29 PM Alexander Korotkov
> >  wrote:
> >> On Wed, May 20, 2026 at 2:46 PM Alexander Korotkov  wrote:
> >>> On Wed, May 20, 2026 at 9:29 AM Chao Li  wrote:
> >>>>> On May 20, 2026, at 14:19, Alexander Korotkov  wrote:
> >>>>> On Wed, May 20, 2026 at 2:37 AM Chao Li  wrote:
> >>>>>>> On May 19, 2026, at 19:00, Alexander Korotkov  wrote:
> >>>>>>> On Tue, May 19, 2026 at 5:50 AM Chao Li  wrote:
> >>>>>>>>> On May 18, 2026, at 20:04, Alexander Korotkov  wrote:
> >>>>>>>>>
> >>>>>>>>> On Mon, May 18, 2026 at 2:57 PM Chao Li  wrote:
> >>>>>>>>>>> 
> >>>>>>>>>>
> >>>>>>>>>> v3-0001 through v3-0003 look good to me.
> >>>>>>>>>>
> >>>>>>>>>> For v3-0004, I have a suspicion, but it's late here and my brain is getting slow, so I would like to study it more tomorrow.
> >>>>>>>>>
> >>>>>>>>> Sure, take your time.
> >>>>>>>>>
> >>>>>>>>> ------
> >>>>>>>>> Regards,
> >>>>>>>>> Alexander Korotkov
> >>>>>>>>> Supabase
> >>>>>>>>
> >>>>>>>> My suspicion was that check_split_partition_not_same_bound() now has two paths. The RANGE path honors collation, while the LIST path does not. So I spent some time creating a test that uses a case-insensitive collation:
> >>>>>>>> ```
> >>>>>>>> evantest=# create collation case_insensitive (provider=icu, locale='und-u-ks-level2', deterministic = false);
> >>>>>>>> CREATE COLLATION
> >>>>>>>> evantest=# create table t (b text collate case_insensitive) partition by list (b);
> >>>>>>>> CREATE TABLE
> >>>>>>>> evantest=# create table tp_ab partition of t for values in ('a', 'b');
> >>>>>>>> CREATE TABLE
> >>>>>>>> evantest=# alter table t split partition tp_ab into
> >>>>>>>> evantest-#   (partition tp_a for values in ('a', 'A'),
> >>>>>>>> evantest(#   partition tp_default default);
> >>>>>>>> ERROR:  cannot split partition "tp_ab" only to add a DEFAULT partition
> >>>>>>>> LINE 2:   (partition tp_a for values in ('a', 'A'),
> >>>>>>>>                   ^
> >>>>>>>> DETAIL:  The non-DEFAULT partition would keep the same partition bound.
> >>>>>>>> HINT:  Use CREATE TABLE ... PARTITION OF ... DEFAULT to add a DEFAULT partition.
> >>>>>>>> ```
> >>>>>>>>
> >>>>>>>> In this test, the split partition’s bound is ('a', 'b'), and the new partition’s bound is ('a', 'A'). Their list lengths are both 2, but the two bounds are actually different, because 'a' and 'A' are considered equal by the collation.
> >>>>>>>>
> >>>>>>>> So, in the LIST path, since check_partition_bounds_for_split_list() has already ensured that the new partition’s bound is contained within the split partition’s bound, we need to check the reverse direction as well. Whether the split partition’s bound is also contained in the new partition’s bound. If yes, the two bounds are identical.
> >>>>>>>>
> >>>>>>>> See the attached v4 for my changes for 0004. 0001-0003 are unchanged. Since 0001 and 0003 are independent of 0004, maybe they can be pushed first.
> >>>>>>>
> >>>>>>> I've pushed 0001-0003.
> >>>>>>
> >>>>>> Thanks for pushing them.
> >>>>>>
> >>>>>>> Thank you for discovering the collation issue
> >>>>>>> in 0004.  Note that original approach of using
> >>>>>>> partition_bounds_equal() can't handle different collations too (as it
> >>>>>>> internally uses datumIsEqual()).
> >>>>>>
> >>>>>> Yes, I realized that while reviewing v3. That’s reason I didn’t get back v2 and only worked again based on v3.
> >>>>>>
> >>>>>>> I've revised the remaining patch:
> >>>>>>> made function header comment a bit more detailed
> >>>>>>
> >>>>>> This part looks good to me.
> >>>>>>
> >>>>>>> and added additional
> >>>>>>> regression tests.  Please, check.
> >>>>>>>
> >>>>>>
> >>>>>> But I don’t see any change for regression test between v4 and v5. Maybe you forgot to save your changes?
> >>>>>
> >>>>> Sorry, I just mess up, no changes in tests.
> >>>>> I'm going to push this if no objection.
> >>>>>
> >>>>
> >>>> No worries. Then v5 looks good to me.
> >>>
> >>> Thank you, pushed.
> >>
> >> Uhhh, most of buildfarm animals don't support locales used in our
> >> tests.  I've to revert that,
> >
> > The another attempt is attached.  Now use -0.0 and 0.0 as binary
> > different but logically equivalent values, no locale dependence.
> >
> > ------
> > Regards,
> > Alexander Korotkov
> > Supabase
> > 
>
> Thank you very much for taking care of that. This was a lesson learned for me.
>
> Actually, when I added the ICU test, I did think about whether regression tests support ICU. So I searched the existing tests for “icu” and found some examples. For instance, src/test/regress/sql/collate.icu.utf8.sql has tests like:
> ```
> RESET icu_validation_level;
> CREATE COLLATION testx (provider = icu, locale = '@colStrength=primary;nonsense=yes'); DROP COLLATION testx;
> CREATE COLLATION testx (provider = icu, locale = 'nonsense-nowhere'); DROP COLLATION testx;
> ```
>
> I added v6 to CF to get a CI run and check whether the buildfarm is happy with v6. The CI test has passed now. See [1]. The new test using “-0.0" also looks good to me.
>
> [1] https://commitfest.postgresql.org/patch/6796/

Pushed, thank you.  AFAICS, it runs ok on buildfarm now.

------
Regards,
Alexander Korotkov
Supabase


Re: Why is the LSN reported for pg_logical_emit_message() different from other decoded operations?

От:
solai v <solai.cdac@gmail.com>
Дата:
Вложения:
Hi all,

On Tue, Jul 21, 2026 at 10:30 AM torikoshia  wrote:
>
> On 2026-06-11 11:27, torikoshia wrote:
> > On this point, I have started discussing with the Debezium
> > community, where I originally encountered this issue. The main
> > question is whether a change in PostgreSQL's behavior would be
> > welcomed, or whether Debezium instead treats logical messages
> > differently from other decoded records.
>
> After asking the Debezium community, I learned that they have no
> particular preference as to how PostgreSQL addresses this issue:
>
> -- https://github.com/debezium/dbz/issues/2058#issuecomment-4774845738
> > Based on the discussions so far, my understanding is that, regardless
> > of whether PostgreSQL implements this fix in a minor or major release,
> > Debezium will accommodate either approach and therefore does not have a
> > preference for one over the other, or for any alternative solution.
>
>
> I think there would be roughly three ways to address the issue:
>
>    1) Preserve the current behavior, that is, continue returning endptr
> only when decoding pg_logical_emit_message(), and update the
> documentation accordingly.
>    2) Change the behavior so that decoding pg_logical_emit_message()
> returns startptr, as is the case for DML records.
>      2.1) Make the change in minor releases.
>      2.2) Make the change in a major release, while updating the
> documentation for earlier major versions to describe their existing
> behavior.
>
> If there are requirements from other consumers of the logical
> decoding, I think those should of course be taken into account.
> Personally, however, I do not think we should choose option 1.
> As I wrote previously, the current behavior can be a pitfall
> that may lead to data loss when implementing a logical
> decoding consumer:
>
> ```
> Regarding the third point, I believe this distinction is
> important for consumers of logical decoding. Typically, a
> consumer records the LSN up to which the decoded result has been
> successfully processed and uses that LSN as the restart point
> after a crash or other failure. From that perspective, whether
> startptr or endptr is returned matters.
> ```
>
> As for options 2.1 and 2.2, given the potential impact on consumers,
> changing the behavior only in a major release, as in option 2.2,
> initially seems like a good approach.
>
> However, the discussion so far has not identified any particular
> reason why endptr should be returned, and the current behavior
> also appears to differ from the documentation. This therefore
> seems to me more like a bug. If we regard it as such, fixing it
> in minor releases, as in option 2.1, would seem to be the most
> straightforward approach.
>
> If there are consumers that are already aware of this issue and
> have implemented special handling for pg_logical_emit_message(),
> they would need to remove or adjust that handling, which could
> be inconvenient.
> That said, this behavior cannot readily be inferred from the
> documentation. It is also a relatively niche issue that is likely
> to be noticed only after it causes a problem such as data loss.
> Moreover, once someone identifies the cause, it seems like the kind
> of issue they would be inclined to report to the PostgreSQL
> community, but it seems that we haven't received such reports.
> For these reasons, I suspect there are few, if any, products that
> currently implement such a workaround.
>
> For consumers that are affected by this issue but are not yet aware
> of it, fixing it in minor releases would have the advantage that
> simply upgrading PostgreSQL to a newer minor release would resolve
> the problem without adding special handling for
> pg_logical_emit_message() to their products.
> I imagine most consumers would gain this advantage.
>
> For these reasons, I am beginning to think that fixing this in minor
> releases may be the preferable approach.
>
> What do you think?
>
>


Thank you for the patch. I reviewed and tested this patch on my
PostgreSQL 19beta2 tree. The patch did not apply cleanly on my branch
because of a context mismatch in
src/test/subscription/t/020_messages.pl. However, the backend change
in decode.c was applied manually for functional verification. I
reproduced the reported issue before applying the fix for
confirmation. After emitting a transactional logical message using
pg_logical_emit_message(), pg_logical_slot_get_changes() reported the
message LSN as the end of the WAL record, which matched the LSN of the
following INSERT record instead of the LogicalMessage WAL record
itself. After applying the backend change, and rerunning the test, the
decoded message LSN matched the LogicalMessage WAL record's start LSN
(origptr) as expected. The following INSERT retained its own higher
LSN, confirming that the decoded logical message now reports the
correct WAL position.

pg_logical_slot_get_changes():

 0/0188DE08 | 711 | message: transactional: 1 prefix: test1, sz: 3 content:aaa
 0/0188DE48 | 711 | table public.data: INSERT: id[integer]:2 data[text]:'2'

pg_waldump:

rmgr: LogicalMessage len (rec/tot):     59/    59, tx:        711,
lsn: 0/0188DE08, prev 0/0188DDC8, desc: MESSAGE transactional, prefix
"test1"; payload (3 bytes): 61 61 61
rmgr: Heap        len (rec/tot):     61/    61, tx:        711, lsn:
0/0188DE48, prev 0/0188DE08, desc: INSERT off: 3, flags: 0x08, blkref
#0: rel 1663/5/16417 blk 0

The behavior is now consistent with other decoded records such as
INSERT, UPDATE, DELETE, and TRUNCATE, which already report the start
LSN of their corresponding WAL records.  Overall, the functional
change behaves as intended, and I did not observe any issues during
testing. The patch looks good to me.


Regards,
Solai


Re: Why is the LSN reported for pg_logical_emit_message() different from other decoded operations?

От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
Hi,

Sorry for the late reply.

On Wed, Aug 12, 2026 at 12:16 AM torikoshia  wrote:
>
> On 2026-08-11 09:04, Masahiko Sawada wrote:
>
> Thanks for the comment!
> > I'm still studying this patch but I want to clarify: was the problem
> > you faced caused by logical decoding showing different types of
> > changes(insert/update/delete/truncate/message) with the same LSN?
>
> Yes. The Debezium issue I encountered was caused by different types of
> changes being reported with the same LSN.
>
> More specifically, consider a transaction like this:
>
> begin;
> select pg_logical_emit_message(true, 'test1', 'xxx');
> insert into t1 values (1, 'aaa');
> commit;
>
> The problem occurred when Debezium received the logical message but
> crashed before receiving the INSERT.
>
> After a restart, Debezium uses the LSN of the last record it received
> to determine where to resume and which records have already been
> processed. Since the INSERT is reported with the same LSN as the
> logical message, it can be treated as already processed and skipped,
> resulting in data loss.
>
> > IIUC
> > logical decoding of MULTI_INSERT emits all INSERT changes with the
> > same LSN, so I think showing the same LSN multiple times is fine.
>
> So I agree that having the same LSN for multiple changes is not in
> itself a problem. The problem in this case is that changes originating
> from different WAL positions can be reported with the same LSN.
>
> > One possible reason why the message uses endptr instead of origptr is
> > for non-transactional messages; on receiver sides a non-transactional
> > message would be handled as a separate transaction, so it would be
> > useful to use endptr as the confirmed flush position. That said, it
> > doesn't apply for transactional messages.
>
> > While I agree that logical decoding uses origptr of logical decoding
> > messages as their LSN, I think having its endptr is also useful for
> > the above reason. For instance, I proposed to extend logical decoding
> > message handling[1] so that extension can define a function to handle
> > logical decoding messages on the subscriber. If we use origptr as the
> > flushed position, the same message is replicated again after the
> > server restart even if the subscriber has committed the message as a
> > separate transaction and sent an ack to the publisher.
>
> That makes sense.
>
> > So an alternative idea would be to use origptr for transactional
> > messages and endptr for non-transactional ones. With this idea, a
> > transactional message's LSN could coincide with the LSN reported for
> > the preceding COMMIT, or for a preceding non-transactional message. I
> > think that is acceptable for the same reason as MULTI_INSERT above:
> > what matters is that the LSN identifies the record the change came
> > from, not that it is unique.
>
> That approach seems reasonable to me.


>
> > I'm hesitant to backpatch it. What this issue tells us is that
> > consumers do look at the LSN of individual changes and possibly
> > persist it, and use it to decide where to resume and what has already
> > been processed. If we change the LSN we report in a minor release, a
> > position that a consumer recorded under the old behavior will be
> > interpreted under the new one after the upgrade. Whether that ends up
> > re-processing changes, skipping them, or failing to locate the resume
> > point depends on the consumer, and none of those seems like something
> > an operator should have to expect from a minor upgrade.
> >
> > There is no correctness problem within PostgreSQL here. So I think
> > this is pushed to master only, with the current behavior documented in
> > the back branches. Consumers hitting this can handle it on their side
> > in the meantime.
>
> This may be a safer approach.
>
> One thing that still concerns me is that consumer developers may
> assume that the LSN reported for a logical message identifies
> the WAL record containing that message, just as it does for other
> decoded changes. Even after consulting the documentation, one
> could reach that conclusion from this description:
>
> https://www.postgresql.org/docs/devel/protocol-logicalrep-message-formats.html
>
> > Message
> > ....
> > Int64 (XLogRecPtr)
> > The LSN of the logical decoding message.
>
> Also, as far as I imagine, the LSN of an individual decoded change is
> typically relevant mainly when a consumer has to determine its resume
> position after an unexpected interruption. Therefore, there may be
> consumers for which this behavior has not caused a visible problem
> during normal replication, and whose developers are not aware of this
> difference for logical messages.
>
> If that assumption is common, treating the documentation as defining
> the intended behavior and backpatching the change would make the fix
> easier for consumers: they could get the corrected behavior simply by
> upgrading PostgreSQL, without adding special handling for logical
> message LSNs only for PostgreSQL versions before 19.
>
> That said, I don't know whether there are actually many such consumers.
> It seems difficult to predict how changing the behavior in already
> released major versions might affect existing consumers.
>  From that perspective, leaving the behavior unchanged in released
> branches seems safer.
>
> So I'm fine with changing this only in master and documenting the
> existing behavior in the back branches.

Yeah, backpatching has another risk too: a consumer that has persisted
the LSNs of decoded messages would find them meaning something
different after a minor version update, and would have to behave
differently depending on the publisher's minor version. I don't think
we can expect consumers to handle that.

> For the released branches, though, I think it would be useful not only
> to update the documentation, but also to make the issue clearly visible
> in the release notes, since some consumers may need to review or adjust
> their logics.

Agreed. I'll describe the behavior difference in the commit message so
that it's picked up when the release notes are written.

Regarding the patch:

- ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
+ /*
+ * Non-transactional messages are processed as separate transactions on
+ * the receiver, so use endptr as the confirmed flush position for them.
+ */
+ ReorderBufferQueueMessage(ctx->reorder, xid, snapshot,
+  message->transactional ? buf->origptr : buf->endptr,
   message->transactional,

I think this needs more comments: what we assume on the consumer side
(a non-transactional message is applied and confirmed as a unit of its
own, like a commit), and what happens if we used origptr for a
non-transactional message instead; a consumer that reports the
message's LSN as its flush position can get the message decoded again
after a restart, since decoding resumes from the confirmed position
inclusive. It's the same reason why the commit message carries both
commit_lsn and end_lsn.

Also, logicaldecoding.sgml says "The lsn has WAL location of the
message" for message_cb, which needs the same update, in the
back-branch patches too.

Regards,

-- 
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com


Re: Why is the LSN reported for pg_logical_emit_message() different from other decoded operations?

От:
João Rito <joaovictor.rito@gmail.com>
Дата:
Hi Torikoshi-san, Sawada-san,

I tested v3 on current master (8d87367b39e). It applies cleanly and
builds with assertions, and contrib/test_decoding (16/16) and
src/test/subscription/t/020_messages.pl pass.

I also checked the behavior with test_decoding. In a transaction with
INSERT, pg_logical_emit_message(true, ...), INSERT and COMMIT, the
message and the following INSERT were both reported at 0/017A1D08
before the patch. With v3 the message is reported at 0/017A1CC8, which
is the start of the LogicalMessage record in pg_waldump, and the
INSERT stays at 0/017A1D08.

+1 to Sawada-san's two points (the comment about non-transactional
messages, and the message_cb text in logicaldecoding.sgml).

One more small idea: the existing tests pass both before and after the
patch, so none of them covers this change. Maybe a small test in
contrib/test_decoding could check that a transactional message and the
next change get different LSNs.

I did not test the non-transactional case or the back-branch patches.

Thanks,
João

Em qua., 11 de nov. de 2026 às 19:14, Masahiko Sawada
 escreveu:
>
> Hi,
>
> Sorry for the late reply.
>
> On Wed, Aug 12, 2026 at 12:16 AM torikoshia  wrote:
> >
> > On 2026-08-11 09:04, Masahiko Sawada wrote:
> >
> > Thanks for the comment!
> > > I'm still studying this patch but I want to clarify: was the problem
> > > you faced caused by logical decoding showing different types of
> > > changes(insert/update/delete/truncate/message) with the same LSN?
> >
> > Yes. The Debezium issue I encountered was caused by different types of
> > changes being reported with the same LSN.
> >
> > More specifically, consider a transaction like this:
> >
> > begin;
> > select pg_logical_emit_message(true, 'test1', 'xxx');
> > insert into t1 values (1, 'aaa');
> > commit;
> >
> > The problem occurred when Debezium received the logical message but
> > crashed before receiving the INSERT.
> >
> > After a restart, Debezium uses the LSN of the last record it received
> > to determine where to resume and which records have already been
> > processed. Since the INSERT is reported with the same LSN as the
> > logical message, it can be treated as already processed and skipped,
> > resulting in data loss.
> >
> > > IIUC
> > > logical decoding of MULTI_INSERT emits all INSERT changes with the
> > > same LSN, so I think showing the same LSN multiple times is fine.
> >
> > So I agree that having the same LSN for multiple changes is not in
> > itself a problem. The problem in this case is that changes originating
> > from different WAL positions can be reported with the same LSN.
> >
> > > One possible reason why the message uses endptr instead of origptr is
> > > for non-transactional messages; on receiver sides a non-transactional
> > > message would be handled as a separate transaction, so it would be
> > > useful to use endptr as the confirmed flush position. That said, it
> > > doesn't apply for transactional messages.
> >
> > > While I agree that logical decoding uses origptr of logical decoding
> > > messages as their LSN, I think having its endptr is also useful for
> > > the above reason. For instance, I proposed to extend logical decoding
> > > message handling[1] so that extension can define a function to handle
> > > logical decoding messages on the subscriber. If we use origptr as the
> > > flushed position, the same message is replicated again after the
> > > server restart even if the subscriber has committed the message as a
> > > separate transaction and sent an ack to the publisher.
> >
> > That makes sense.
> >
> > > So an alternative idea would be to use origptr for transactional
> > > messages and endptr for non-transactional ones. With this idea, a
> > > transactional message's LSN could coincide with the LSN reported for
> > > the preceding COMMIT, or for a preceding non-transactional message. I
> > > think that is acceptable for the same reason as MULTI_INSERT above:
> > > what matters is that the LSN identifies the record the change came
> > > from, not that it is unique.
> >
> > That approach seems reasonable to me.
>
>
> >
> > > I'm hesitant to backpatch it. What this issue tells us is that
> > > consumers do look at the LSN of individual changes and possibly
> > > persist it, and use it to decide where to resume and what has already
> > > been processed. If we change the LSN we report in a minor release, a
> > > position that a consumer recorded under the old behavior will be
> > > interpreted under the new one after the upgrade. Whether that ends up
> > > re-processing changes, skipping them, or failing to locate the resume
> > > point depends on the consumer, and none of those seems like something
> > > an operator should have to expect from a minor upgrade.
> > >
> > > There is no correctness problem within PostgreSQL here. So I think
> > > this is pushed to master only, with the current behavior documented in
> > > the back branches. Consumers hitting this can handle it on their side
> > > in the meantime.
> >
> > This may be a safer approach.
> >
> > One thing that still concerns me is that consumer developers may
> > assume that the LSN reported for a logical message identifies
> > the WAL record containing that message, just as it does for other
> > decoded changes. Even after consulting the documentation, one
> > could reach that conclusion from this description:
> >
> > https://www.postgresql.org/docs/devel/protocol-logicalrep-message-formats.html
> >
> > > Message
> > > ....
> > > Int64 (XLogRecPtr)
> > > The LSN of the logical decoding message.
> >
> > Also, as far as I imagine, the LSN of an individual decoded change is
> > typically relevant mainly when a consumer has to determine its resume
> > position after an unexpected interruption. Therefore, there may be
> > consumers for which this behavior has not caused a visible problem
> > during normal replication, and whose developers are not aware of this
> > difference for logical messages.
> >
> > If that assumption is common, treating the documentation as defining
> > the intended behavior and backpatching the change would make the fix
> > easier for consumers: they could get the corrected behavior simply by
> > upgrading PostgreSQL, without adding special handling for logical
> > message LSNs only for PostgreSQL versions before 19.
> >
> > That said, I don't know whether there are actually many such consumers.
> > It seems difficult to predict how changing the behavior in already
> > released major versions might affect existing consumers.
> >  From that perspective, leaving the behavior unchanged in released
> > branches seems safer.
> >
> > So I'm fine with changing this only in master and documenting the
> > existing behavior in the back branches.
>
> Yeah, backpatching has another risk too: a consumer that has persisted
> the LSNs of decoded messages would find them meaning something
> different after a minor version update, and would have to behave
> differently depending on the publisher's minor version. I don't think
> we can expect consumers to handle that.
>
> > For the released branches, though, I think it would be useful not only
> > to update the documentation, but also to make the issue clearly visible
> > in the release notes, since some consumers may need to review or adjust
> > their logics.
>
> Agreed. I'll describe the behavior difference in the commit message so
> that it's picked up when the release notes are written.
>
> Regarding the patch:
>
> - ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
> + /*
> + * Non-transactional messages are processed as separate transactions on
> + * the receiver, so use endptr as the confirmed flush position for them.
> + */
> + ReorderBufferQueueMessage(ctx->reorder, xid, snapshot,
> +  message->transactional ? buf->origptr : buf->endptr,
>    message->transactional,
>
> I think this needs more comments: what we assume on the consumer side
> (a non-transactional message is applied and confirmed as a unit of its
> own, like a commit), and what happens if we used origptr for a
> non-transactional message instead; a consumer that reports the
> message's LSN as its flush position can get the message decoded again
> after a restart, since decoding resumes from the confirmed position
> inclusive. It's the same reason why the commit message carries both
> commit_lsn and end_lsn.
>
> Also, logicaldecoding.sgml says "The lsn has WAL location of the
> message" for message_cb, which needs the same update, in the
> back-branch patches too.
>
> Regards,
>
> --
> Masahiko Sawada
> Amazon Web Services: https://aws.amazon.com
>
>
>
>


FAQ