tablecmds: fix bug where index rebuild loses replica identity on partitions
tablecmds: fix bug where index rebuild loses replica identity on partitions
Hi Hackers,
I found this bug while working on a related patch [1].
When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and that index is used as REPLICA IDENTITY on a partitioned table, the replica
identity marking on partitions can be silently lost after the rebuild.
Below is a simple reproduction:
```
-- create a partitioned table and a parition
create table parent (id int not null, val int not null) partition by range (id);
create table child partition of parent for values from (1) to (100);
-- create an index on parent
create unique index indx_1 on parent(id, val);
-- the index is auto created on child, and both indexes’ indisreplident are false
select c.relname as index_name, c.oid as index_oid, i.indisreplident from pg_class c join pg_index i on c.oid = i.indexrelid where (c.relname = 'indx_1' or c.relname = 'child_id_val_idx’);
index_name | index_oid | indisreplident
------------------+-----------+----------------
indx_1 | 24594 | f
child_id_val_idx | 24595 | f
(2 rows)
-- as replica identity doesn’t recurse, set it on parent and child individually
alter table parent replica identity using index indx_1;
alter table child replica identity using index child_id_val_idx;
-- now both indexes are marked as replica identity
select c.relname as index_name, c.oid as index_oid, i.indisreplident from pg_class c join pg_index i on c.oid = i.indexrelid where (c.relname = 'indx_1' or c.relname = ‘child_id_val_idx');
index_name | index_oid | indisreplident
------------------+-----------+----------------
indx_1 | 24594 | t
child_id_val_idx | 24595 | t
(2 rows)
-- alter a column type, the column is part of the index, it will cause the index to rebuid
alter table parent alter val type bigint;
-- from the OIDs, we can see both indexes are rebuilt, but the child partition loses its replica identity marking
select c.relname as index_name, c.oid as index_oid, i.indisreplident from pg_class c join pg_index i on c.oid = i.indexrelid where (c.relname = 'indx_1' or c.relname = ‘child_id_val_idx');
index_name | index_oid | indisreplident
------------------+-----------+----------------
child_id_val_idx | 24597 | f
indx_1 | 24596 | t
(2 rows)
```
This patch fixes the bug by tracking replica identity indexes across partition hierarchies and restoring replica identity markings on all affected partitions after index rebuilds. Regression tests are added.
[1] https://postgr.es/m/CAEoWx2nJ71hy8R614HQr7vQhkBReO9AANPODPg0aSQs74eOdLQ@mail.gmail.com
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote:
> I found this bug while working on a related patch [1].
>
> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and
> that index is used as REPLICA IDENTITY on a partitioned table, the
> replica identity marking on partitions can be silently lost after the
> rebuild.
I am slightly confused by the tests included in the proposed patch.
On HEAD, if I undo the proposed changes of tablecmds.c, the tests
pass. If I run the tests of the patch with the changes of
tablecmds.c, the tests also pass. These tests don't check what you
want them to.
- if (tab->replicaIdentityIndex)
+ if (tab->replicaIdentityIndexOids != NIL)
elog(ERROR, "relation %u has multiple indexes marked as replica identity", tab->relid);
This looks wrong to me. This new list tracks the OIDs of indexes
where you'd want to make sure that relreplident is updated, and it
could be possible, based on your proposal, that multiple indexes are
stored in this list. The error message is at least not in line
anymore. Actually, do we really need this extra list at all? The
list of indexes to rebuild are tracked already in changedIndexOids,
and the partitioned indexes seem to be in it, no?
--
Michael
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Jan 27, 2026, at 15:39, Michael Paquier wrote: > > On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote: >> I found this bug while working on a related patch [1]. >> >> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and >> that index is used as REPLICA IDENTITY on a partitioned table, the >> replica identity marking on partitions can be silently lost after the >> rebuild. > > I am slightly confused by the tests included in the proposed patch. > On HEAD, if I undo the proposed changes of tablecmds.c, the tests > pass. If I run the tests of the patch with the changes of > tablecmds.c, the tests also pass. Oops, that isn’t supposed to be so. I’ll check the test. > These tests don't check what you > want them to. > > - if (tab->replicaIdentityIndex) > + if (tab->replicaIdentityIndexOids != NIL) > elog(ERROR, "relation %u has multiple indexes marked as replica identity", tab->relid); > > This looks wrong to me. This new list tracks the OIDs of indexes > where you'd want to make sure that relreplident is updated, and it > could be possible, based on your proposal, that multiple indexes are > stored in this list. The error message is at least not in line > anymore. Actually, do we really need this extra list at all? The > list of indexes to rebuild are tracked already in changedIndexOids, > and the partitioned indexes seem to be in it, no? No, changedIndexOids only tracks the root index OID. DefineIndex() will automatically rebuild indexes on all partitions, and won’t return the rebuilt index OIDs to “alter table”. So, before index rebuild, this patch records potential affected indexes in replicaIdentityIndexOids, and after index rebuild, restore replica identity on them. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
> On Jan 27, 2026, at 15:59, Chao Li wrote: > > > >> On Jan 27, 2026, at 15:39, Michael Paquier wrote: >> >> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote: >>> I found this bug while working on a related patch [1]. >>> >>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and >>> that index is used as REPLICA IDENTITY on a partitioned table, the >>> replica identity marking on partitions can be silently lost after the >>> rebuild. >> >> I am slightly confused by the tests included in the proposed patch. >> On HEAD, if I undo the proposed changes of tablecmds.c, the tests >> pass. If I run the tests of the patch with the changes of >> tablecmds.c, the tests also pass. > > Oops, that isn’t supposed to be so. I’ll check the test. > Okay, I see the problem is here: ``` +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id); ``` I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild. Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea? Please see v2, the test should fail on master now. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
> On Jan 27, 2026, at 16:30, Chao Li wrote:
>
>
>
>> On Jan 27, 2026, at 15:59, Chao Li wrote:
>>
>>
>>
>>> On Jan 27, 2026, at 15:39, Michael Paquier wrote:
>>>
>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote:
>>>> I found this bug while working on a related patch [1].
>>>>
>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and
>>>> that index is used as REPLICA IDENTITY on a partitioned table, the
>>>> replica identity marking on partitions can be silently lost after the
>>>> rebuild.
>>>
>>> I am slightly confused by the tests included in the proposed patch.
>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests
>>> pass. If I run the tests of the patch with the changes of
>>> tablecmds.c, the tests also pass.
>>
>> Oops, that isn’t supposed to be so. I’ll check the test.
>>
>
> Okay, I see the problem is here:
> ```
> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id);
> ```
>
> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild.
>
> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea?
I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved.
I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions:
```
@@ -360,9 +360,9 @@
ORDER BY b.index_name;
index_name | rebuilt | ri_lost
---------------------------------------------------+---------+---------
- test_replica_identity_partitioned_p1_id_val_idx | t | f
- test_replica_identity_partitioned_p2_1_id_val_idx | t | f
- test_replica_identity_partitioned_p2_2_id_val_idx | t | f
+ test_replica_identity_partitioned_p1_id_val_idx | t | t
+ test_replica_identity_partitioned_p2_1_id_val_idx | t | t
+ test_replica_identity_partitioned_p2_2_id_val_idx | t | t
test_replica_identity_partitioned_p2_id_val_idx | t | f
test_replica_identity_partitioned_pkey | t | f
(5 rows)
```
With this patch, the test passes and all replica identity are preserved.
PFA v3:
* Enhanced the test.
* A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
> On Jan 28, 2026, at 10:49, Chao Li wrote: > > > >> On Jan 27, 2026, at 16:30, Chao Li wrote: >> >> >> >>> On Jan 27, 2026, at 15:59, Chao Li wrote: >>> >>> >>> >>>> On Jan 27, 2026, at 15:39, Michael Paquier wrote: >>>> >>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote: >>>>> I found this bug while working on a related patch [1]. >>>>> >>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and >>>>> that index is used as REPLICA IDENTITY on a partitioned table, the >>>>> replica identity marking on partitions can be silently lost after the >>>>> rebuild. >>>> >>>> I am slightly confused by the tests included in the proposed patch. >>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests >>>> pass. If I run the tests of the patch with the changes of >>>> tablecmds.c, the tests also pass. >>> >>> Oops, that isn’t supposed to be so. I’ll check the test. >>> >> >> Okay, I see the problem is here: >> ``` >> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id); >> ``` >> >> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild. >> >> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea? > > I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved. > > I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions: > ``` > @@ -360,9 +360,9 @@ > ORDER BY b.index_name; > index_name | rebuilt | ri_lost > ---------------------------------------------------+---------+--------- > - test_replica_identity_partitioned_p1_id_val_idx | t | f > - test_replica_identity_partitioned_p2_1_id_val_idx | t | f > - test_replica_identity_partitioned_p2_2_id_val_idx | t | f > + test_replica_identity_partitioned_p1_id_val_idx | t | t > + test_replica_identity_partitioned_p2_1_id_val_idx | t | t > + test_replica_identity_partitioned_p2_2_id_val_idx | t | t > test_replica_identity_partitioned_p2_id_val_idx | t | f > test_replica_identity_partitioned_pkey | t | f > (5 rows) > ``` > > With this patch, the test passes and all replica identity are preserved. > > PFA v3: > * Enhanced the test. > * A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it. > > Best regards, > -- > Chao Li (Evan) > HighGo Software Co., Ltd. > https://www.highgo.com/ > > > > > The CF asked for a rebase, thus rebased as v4. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
> On Feb 26, 2026, at 14:59, Chao Li wrote: > > > >> On Jan 28, 2026, at 10:49, Chao Li wrote: >> >> >> >>> On Jan 27, 2026, at 16:30, Chao Li wrote: >>> >>> >>> >>>> On Jan 27, 2026, at 15:59, Chao Li wrote: >>>> >>>> >>>> >>>>> On Jan 27, 2026, at 15:39, Michael Paquier wrote: >>>>> >>>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote: >>>>>> I found this bug while working on a related patch [1]. >>>>>> >>>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and >>>>>> that index is used as REPLICA IDENTITY on a partitioned table, the >>>>>> replica identity marking on partitions can be silently lost after the >>>>>> rebuild. >>>>> >>>>> I am slightly confused by the tests included in the proposed patch. >>>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests >>>>> pass. If I run the tests of the patch with the changes of >>>>> tablecmds.c, the tests also pass. >>>> >>>> Oops, that isn’t supposed to be so. I’ll check the test. >>>> >>> >>> Okay, I see the problem is here: >>> ``` >>> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id); >>> ``` >>> >>> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild. >>> >>> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea? >> >> I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved. >> >> I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions: >> ``` >> @@ -360,9 +360,9 @@ >> ORDER BY b.index_name; >> index_name | rebuilt | ri_lost >> ---------------------------------------------------+---------+--------- >> - test_replica_identity_partitioned_p1_id_val_idx | t | f >> - test_replica_identity_partitioned_p2_1_id_val_idx | t | f >> - test_replica_identity_partitioned_p2_2_id_val_idx | t | f >> + test_replica_identity_partitioned_p1_id_val_idx | t | t >> + test_replica_identity_partitioned_p2_1_id_val_idx | t | t >> + test_replica_identity_partitioned_p2_2_id_val_idx | t | t >> test_replica_identity_partitioned_p2_id_val_idx | t | f >> test_replica_identity_partitioned_pkey | t | f >> (5 rows) >> ``` >> >> With this patch, the test passes and all replica identity are preserved. >> >> PFA v3: >> * Enhanced the test. >> * A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it. >> >> Best regards, >> -- >> Chao Li (Evan) >> HighGo Software Co., Ltd. >> https://www.highgo.com/ >> >> >> >> >> > > The CF asked for a rebase, thus rebased as v4. > > Best regards, > -- > Chao Li (Evan) > HighGo Software Co., Ltd. > https://www.highgo.com/ > > > > > Rebased, and a gentle ping. Since this is a bug fix and the issue is easy to reproduce, I’m hoping it can still make it into v19. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
On Thu, Mar 19, 2026 at 1:07 PM Chao Li wrote: > > > > > On Feb 26, 2026, at 14:59, Chao Li wrote: > > > > > > > >> On Jan 28, 2026, at 10:49, Chao Li wrote: > >> > >> > >> > >>> On Jan 27, 2026, at 16:30, Chao Li wrote: > >>> > >>> > >>> > >>>> On Jan 27, 2026, at 15:59, Chao Li wrote: > >>>> > >>>> > >>>> > >>>>> On Jan 27, 2026, at 15:39, Michael Paquier wrote: > >>>>> > >>>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote: > >>>>>> I found this bug while working on a related patch [1]. > >>>>>> > >>>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and > >>>>>> that index is used as REPLICA IDENTITY on a partitioned table, the > >>>>>> replica identity marking on partitions can be silently lost after the > >>>>>> rebuild. > >>>>> > >>>>> I am slightly confused by the tests included in the proposed patch. > >>>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests > >>>>> pass. If I run the tests of the patch with the changes of > >>>>> tablecmds.c, the tests also pass. > >>>> > >>>> Oops, that isn’t supposed to be so. I’ll check the test. > >>>> > >>> > >>> Okay, I see the problem is here: > >>> ``` > >>> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id); > >>> ``` > >>> > >>> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild. > >>> > >>> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea? > >> > >> I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved. > >> > >> I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions: > >> ``` > >> @@ -360,9 +360,9 @@ > >> ORDER BY b.index_name; > >> index_name | rebuilt | ri_lost > >> ---------------------------------------------------+---------+--------- > >> - test_replica_identity_partitioned_p1_id_val_idx | t | f > >> - test_replica_identity_partitioned_p2_1_id_val_idx | t | f > >> - test_replica_identity_partitioned_p2_2_id_val_idx | t | f > >> + test_replica_identity_partitioned_p1_id_val_idx | t | t > >> + test_replica_identity_partitioned_p2_1_id_val_idx | t | t > >> + test_replica_identity_partitioned_p2_2_id_val_idx | t | t > >> test_replica_identity_partitioned_p2_id_val_idx | t | f > >> test_replica_identity_partitioned_pkey | t | f > >> (5 rows) > >> ``` > >> > >> With this patch, the test passes and all replica identity are preserved. > >> > >> PFA v3: > >> * Enhanced the test. > >> * A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it. > >> > >> Best regards, > >> -- > >> Chao Li (Evan) > >> HighGo Software Co., Ltd. > >> https://www.highgo.com/ > >> > >> > >> > >> > >> > > > > The CF asked for a rebase, thus rebased as v4. > > Hi, I reproduced this with the test case, and the patch appears to resolve it. Some comments on v5: -- Whether it makes sense to use a single list of pair structs instead of two parallel OID lists (replicaIdentityIndexOids + replicaIdentityTableOids) to avoid accidental desync. -- It would be better to make lock handling in find_partition_replica_identity_indexes() consistent (relation_open(..., NoLock) if child is already locked, and avoid mixed relation_close(..., lockmode)/NoLock behavior). -- Some typos in comments/tests (partion/parition). -- Best, Xuneng
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
> On Mar 21, 2026, at 18:29, Xuneng Zhou wrote: > > On Thu, Mar 19, 2026 at 1:07 PM Chao Li wrote: >> >> >> >>> On Feb 26, 2026, at 14:59, Chao Li wrote: >>> >>> >>> >>>> On Jan 28, 2026, at 10:49, Chao Li wrote: >>>> >>>> >>>> >>>>> On Jan 27, 2026, at 16:30, Chao Li wrote: >>>>> >>>>> >>>>> >>>>>> On Jan 27, 2026, at 15:59, Chao Li wrote: >>>>>> >>>>>> >>>>>> >>>>>>> On Jan 27, 2026, at 15:39, Michael Paquier wrote: >>>>>>> >>>>>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote: >>>>>>>> I found this bug while working on a related patch [1]. >>>>>>>> >>>>>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and >>>>>>>> that index is used as REPLICA IDENTITY on a partitioned table, the >>>>>>>> replica identity marking on partitions can be silently lost after the >>>>>>>> rebuild. >>>>>>> >>>>>>> I am slightly confused by the tests included in the proposed patch. >>>>>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests >>>>>>> pass. If I run the tests of the patch with the changes of >>>>>>> tablecmds.c, the tests also pass. >>>>>> >>>>>> Oops, that isn’t supposed to be so. I’ll check the test. >>>>>> >>>>> >>>>> Okay, I see the problem is here: >>>>> ``` >>>>> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id); >>>>> ``` >>>>> >>>>> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild. >>>>> >>>>> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea? >>>> >>>> I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved. >>>> >>>> I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions: >>>> ``` >>>> @@ -360,9 +360,9 @@ >>>> ORDER BY b.index_name; >>>> index_name | rebuilt | ri_lost >>>> ---------------------------------------------------+---------+--------- >>>> - test_replica_identity_partitioned_p1_id_val_idx | t | f >>>> - test_replica_identity_partitioned_p2_1_id_val_idx | t | f >>>> - test_replica_identity_partitioned_p2_2_id_val_idx | t | f >>>> + test_replica_identity_partitioned_p1_id_val_idx | t | t >>>> + test_replica_identity_partitioned_p2_1_id_val_idx | t | t >>>> + test_replica_identity_partitioned_p2_2_id_val_idx | t | t >>>> test_replica_identity_partitioned_p2_id_val_idx | t | f >>>> test_replica_identity_partitioned_pkey | t | f >>>> (5 rows) >>>> ``` >>>> >>>> With this patch, the test passes and all replica identity are preserved. >>>> >>>> PFA v3: >>>> * Enhanced the test. >>>> * A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it. >>>> >>>> Best regards, >>>> -- >>>> Chao Li (Evan) >>>> HighGo Software Co., Ltd. >>>> https://www.highgo.com/ >>>> >>>> >>>> >>>> >>>> >>> >>> The CF asked for a rebase, thus rebased as v4. >>> > > > Hi, I reproduced this with the test case, and the patch appears > to resolve it. > > Some comments on v5: Thanks a lot for your review. > > -- Whether it makes sense to use a single list of pair structs instead > of two parallel OID lists (replicaIdentityIndexOids + > replicaIdentityTableOids) to avoid accidental desync. I don’t think that helps much. The current code of rebuilding index uses two lists changedIndexOids and changedIndexDefs. So, this patch matches the pattern of the existing code. > > -- It would be better to make lock handling in > find_partition_replica_identity_indexes() consistent > (relation_open(..., NoLock) if child is already locked, and avoid > mixed relation_close(..., lockmode)/NoLock behavior). That’s because if we are going to update a partition, then we need to hold the lock on the partition. > > -- Some typos in comments/tests (partion/parition). > Fixed. PFA v6: fixed a typo in comment. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
Hi,
On Mon, Mar 23, 2026 at 3:57 PM Chao Li <li.evan.chao@gmail.com> wrote:
>
>
>
> > On Mar 21, 2026, at 18:29, Xuneng Zhou <xunengzhou@gmail.com> wrote:
> >
> > On Thu, Mar 19, 2026 at 1:07 PM Chao Li <li.evan.chao@gmail.com> wrote:
> >>
> >>
> >>
> >>> On Feb 26, 2026, at 14:59, Chao Li <li.evan.chao@gmail.com> wrote:
> >>>
> >>>
> >>>
> >>>> On Jan 28, 2026, at 10:49, Chao Li <li.evan.chao@gmail.com> wrote:
> >>>>
> >>>>
> >>>>
> >>>>> On Jan 27, 2026, at 16:30, Chao Li <li.evan.chao@gmail.com> wrote:
> >>>>>
> >>>>>
> >>>>>
> >>>>>> On Jan 27, 2026, at 15:59, Chao Li <li.evan.chao@gmail.com> wrote:
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>>> On Jan 27, 2026, at 15:39, Michael Paquier <michael@paquier.xyz> wrote:
> >>>>>>>
> >>>>>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote:
> >>>>>>>> I found this bug while working on a related patch [1].
> >>>>>>>>
> >>>>>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and
> >>>>>>>> that index is used as REPLICA IDENTITY on a partitioned table, the
> >>>>>>>> replica identity marking on partitions can be silently lost after the
> >>>>>>>> rebuild.
> >>>>>>>
> >>>>>>> I am slightly confused by the tests included in the proposed patch.
> >>>>>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests
> >>>>>>> pass. If I run the tests of the patch with the changes of
> >>>>>>> tablecmds.c, the tests also pass.
> >>>>>>
> >>>>>> Oops, that isn’t supposed to be so. I’ll check the test.
> >>>>>>
> >>>>>
> >>>>> Okay, I see the problem is here:
> >>>>> ```
> >>>>> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id);
> >>>>> ```
> >>>>>
> >>>>> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild.
> >>>>>
> >>>>> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea?
> >>>>
> >>>> I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved.
> >>>>
> >>>> I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions:
> >>>> ```
> >>>> @@ -360,9 +360,9 @@
> >>>> ORDER BY b.index_name;
> >>>> index_name | rebuilt | ri_lost
> >>>> ---------------------------------------------------+---------+---------
> >>>> - test_replica_identity_partitioned_p1_id_val_idx | t | f
> >>>> - test_replica_identity_partitioned_p2_1_id_val_idx | t | f
> >>>> - test_replica_identity_partitioned_p2_2_id_val_idx | t | f
> >>>> + test_replica_identity_partitioned_p1_id_val_idx | t | t
> >>>> + test_replica_identity_partitioned_p2_1_id_val_idx | t | t
> >>>> + test_replica_identity_partitioned_p2_2_id_val_idx | t | t
> >>>> test_replica_identity_partitioned_p2_id_val_idx | t | f
> >>>> test_replica_identity_partitioned_pkey | t | f
> >>>> (5 rows)
> >>>> ```
> >>>>
> >>>> With this patch, the test passes and all replica identity are preserved.
> >>>>
> >>>> PFA v3:
> >>>> * Enhanced the test.
> >>>> * A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it.
> >>>>
> >>>> Best regards,
> >>>> --
> >>>> Chao Li (Evan)
> >>>> HighGo Software Co., Ltd.
> >>>> https://www.highgo.com/
> >>>>
> >>>>
> >>>>
> >>>>
> >>>> <v3-0001-tablecmds-fix-bug-where-index-rebuild-loses-repli.patch>
> >>>
> >>> The CF asked for a rebase, thus rebased as v4.
> >>>
> >
> >
> > Hi, I reproduced this with the test case, and the patch appears
> > to resolve it.
> >
> > Some comments on v5:
>
> Thanks a lot for your review.
>
> >
> > -- Whether it makes sense to use a single list of pair structs instead
> > of two parallel OID lists (replicaIdentityIndexOids +
> > replicaIdentityTableOids) to avoid accidental desync.
>
> I don’t think that helps much. The current code of rebuilding index uses two lists changedIndexOids and changedIndexDefs. So, this patch matches the pattern of the existing code.
>
> >
> > -- It would be better to make lock handling in
> > find_partition_replica_identity_indexes() consistent
> > (relation_open(..., NoLock) if child is already locked, and avoid
> > mixed relation_close(..., lockmode)/NoLock behavior).
>
> That’s because if we are going to update a partition, then we need to hold the lock on the partition.
There is one locking cleanup in find_partition_replica_identity_indexes().
find_inheritance_children(relId, lockmode) already acquires lockmode on
every partition it returns, so I think the later relation_open() should use
NoLock, not lockmode. For the same reason, all relation_close() calls in
this function should use NoLock as well.
Today the code does:
partRel = relation_open(partRelOid, lockmode);
...
relation_close(partRel, lockmode);
That does not cause a correctness issue, because the lock manager
reference-counts same-transaction acquisitions, so the lock remains held
either way. But it is misleading: it suggests that relation_open() is where
the partition lock is taken, and that the early relation_close(..., lockmode)
is intentionally releasing it. Neither is actually true here, because the lock
was already acquired by find_inheritance_children().
So I think this should be adjusted to:
partRel = relation_open(partRelOid, NoLock);
and all close sites in this function should be:
relation_close(partRel, NoLock);
The comment on the early-close path should also be updated, since it is not
really unlocking the partition. Something like "No matching partition index;
just close the relcache entry" would match the actual behavior better.
> >
> > -- Some typos in comments/tests (partion/parition).
> >
>
> Fixed.
>
> PFA v6: fixed a typo in comment.
>
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
>
>
>
>
--
Best,
Xuneng
On Mon, Mar 23, 2026 at 3:57 PM Chao Li <li.evan.chao@gmail.com> wrote:
>
>
>
> > On Mar 21, 2026, at 18:29, Xuneng Zhou <xunengzhou@gmail.com> wrote:
> >
> > On Thu, Mar 19, 2026 at 1:07 PM Chao Li <li.evan.chao@gmail.com> wrote:
> >>
> >>
> >>
> >>> On Feb 26, 2026, at 14:59, Chao Li <li.evan.chao@gmail.com> wrote:
> >>>
> >>>
> >>>
> >>>> On Jan 28, 2026, at 10:49, Chao Li <li.evan.chao@gmail.com> wrote:
> >>>>
> >>>>
> >>>>
> >>>>> On Jan 27, 2026, at 16:30, Chao Li <li.evan.chao@gmail.com> wrote:
> >>>>>
> >>>>>
> >>>>>
> >>>>>> On Jan 27, 2026, at 15:59, Chao Li <li.evan.chao@gmail.com> wrote:
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>>> On Jan 27, 2026, at 15:39, Michael Paquier <michael@paquier.xyz> wrote:
> >>>>>>>
> >>>>>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote:
> >>>>>>>> I found this bug while working on a related patch [1].
> >>>>>>>>
> >>>>>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and
> >>>>>>>> that index is used as REPLICA IDENTITY on a partitioned table, the
> >>>>>>>> replica identity marking on partitions can be silently lost after the
> >>>>>>>> rebuild.
> >>>>>>>
> >>>>>>> I am slightly confused by the tests included in the proposed patch.
> >>>>>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests
> >>>>>>> pass. If I run the tests of the patch with the changes of
> >>>>>>> tablecmds.c, the tests also pass.
> >>>>>>
> >>>>>> Oops, that isn’t supposed to be so. I’ll check the test.
> >>>>>>
> >>>>>
> >>>>> Okay, I see the problem is here:
> >>>>> ```
> >>>>> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id);
> >>>>> ```
> >>>>>
> >>>>> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild.
> >>>>>
> >>>>> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea?
> >>>>
> >>>> I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved.
> >>>>
> >>>> I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions:
> >>>> ```
> >>>> @@ -360,9 +360,9 @@
> >>>> ORDER BY b.index_name;
> >>>> index_name | rebuilt | ri_lost
> >>>> ---------------------------------------------------+---------+---------
> >>>> - test_replica_identity_partitioned_p1_id_val_idx | t | f
> >>>> - test_replica_identity_partitioned_p2_1_id_val_idx | t | f
> >>>> - test_replica_identity_partitioned_p2_2_id_val_idx | t | f
> >>>> + test_replica_identity_partitioned_p1_id_val_idx | t | t
> >>>> + test_replica_identity_partitioned_p2_1_id_val_idx | t | t
> >>>> + test_replica_identity_partitioned_p2_2_id_val_idx | t | t
> >>>> test_replica_identity_partitioned_p2_id_val_idx | t | f
> >>>> test_replica_identity_partitioned_pkey | t | f
> >>>> (5 rows)
> >>>> ```
> >>>>
> >>>> With this patch, the test passes and all replica identity are preserved.
> >>>>
> >>>> PFA v3:
> >>>> * Enhanced the test.
> >>>> * A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it.
> >>>>
> >>>> Best regards,
> >>>> --
> >>>> Chao Li (Evan)
> >>>> HighGo Software Co., Ltd.
> >>>> https://www.highgo.com/
> >>>>
> >>>>
> >>>>
> >>>>
> >>>> <v3-0001-tablecmds-fix-bug-where-index-rebuild-loses-repli.patch>
> >>>
> >>> The CF asked for a rebase, thus rebased as v4.
> >>>
> >
> >
> > Hi, I reproduced this with the test case, and the patch appears
> > to resolve it.
> >
> > Some comments on v5:
>
> Thanks a lot for your review.
>
> >
> > -- Whether it makes sense to use a single list of pair structs instead
> > of two parallel OID lists (replicaIdentityIndexOids +
> > replicaIdentityTableOids) to avoid accidental desync.
>
> I don’t think that helps much. The current code of rebuilding index uses two lists changedIndexOids and changedIndexDefs. So, this patch matches the pattern of the existing code.
>
> >
> > -- It would be better to make lock handling in
> > find_partition_replica_identity_indexes() consistent
> > (relation_open(..., NoLock) if child is already locked, and avoid
> > mixed relation_close(..., lockmode)/NoLock behavior).
>
> That’s because if we are going to update a partition, then we need to hold the lock on the partition.
There is one locking cleanup in find_partition_replica_identity_indexes().
find_inheritance_children(relId, lockmode) already acquires lockmode on
every partition it returns, so I think the later relation_open() should use
NoLock, not lockmode. For the same reason, all relation_close() calls in
this function should use NoLock as well.
Today the code does:
partRel = relation_open(partRelOid, lockmode);
...
relation_close(partRel, lockmode);
That does not cause a correctness issue, because the lock manager
reference-counts same-transaction acquisitions, so the lock remains held
either way. But it is misleading: it suggests that relation_open() is where
the partition lock is taken, and that the early relation_close(..., lockmode)
is intentionally releasing it. Neither is actually true here, because the lock
was already acquired by find_inheritance_children().
So I think this should be adjusted to:
partRel = relation_open(partRelOid, NoLock);
and all close sites in this function should be:
relation_close(partRel, NoLock);
The comment on the early-close path should also be updated, since it is not
really unlocking the partition. Something like "No matching partition index;
just close the relcache entry" would match the actual behavior better.
> >
> > -- Some typos in comments/tests (partion/parition).
> >
>
> Fixed.
>
> PFA v6: fixed a typo in comment.
>
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
>
>
>
>
--
Best,
Xuneng
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
> On Mar 23, 2026, at 16:41, Xuneng Zhou wrote: > > Hi, > > On Mon, Mar 23, 2026 at 3:57 PM Chao Li wrote: > > > > > > > > > On Mar 21, 2026, at 18:29, Xuneng Zhou wrote: > > > > > > On Thu, Mar 19, 2026 at 1:07 PM Chao Li wrote: > > >> > > >> > > >> > > >>> On Feb 26, 2026, at 14:59, Chao Li wrote: > > >>> > > >>> > > >>> > > >>>> On Jan 28, 2026, at 10:49, Chao Li wrote: > > >>>> > > >>>> > > >>>> > > >>>>> On Jan 27, 2026, at 16:30, Chao Li wrote: > > >>>>> > > >>>>> > > >>>>> > > >>>>>> On Jan 27, 2026, at 15:59, Chao Li wrote: > > >>>>>> > > >>>>>> > > >>>>>> > > >>>>>>> On Jan 27, 2026, at 15:39, Michael Paquier wrote: > > >>>>>>> > > >>>>>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote: > > >>>>>>>> I found this bug while working on a related patch [1]. > > >>>>>>>> > > >>>>>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and > > >>>>>>>> that index is used as REPLICA IDENTITY on a partitioned table, the > > >>>>>>>> replica identity marking on partitions can be silently lost after the > > >>>>>>>> rebuild. > > >>>>>>> > > >>>>>>> I am slightly confused by the tests included in the proposed patch. > > >>>>>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests > > >>>>>>> pass. If I run the tests of the patch with the changes of > > >>>>>>> tablecmds.c, the tests also pass. > > >>>>>> > > >>>>>> Oops, that isn’t supposed to be so. I’ll check the test. > > >>>>>> > > >>>>> > > >>>>> Okay, I see the problem is here: > > >>>>> ``` > > >>>>> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id); > > >>>>> ``` > > >>>>> > > >>>>> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild. > > >>>>> > > >>>>> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea? > > >>>> > > >>>> I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved. > > >>>> > > >>>> I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions: > > >>>> ``` > > >>>> @@ -360,9 +360,9 @@ > > >>>> ORDER BY b.index_name; > > >>>> index_name | rebuilt | ri_lost > > >>>> ---------------------------------------------------+---------+--------- > > >>>> - test_replica_identity_partitioned_p1_id_val_idx | t | f > > >>>> - test_replica_identity_partitioned_p2_1_id_val_idx | t | f > > >>>> - test_replica_identity_partitioned_p2_2_id_val_idx | t | f > > >>>> + test_replica_identity_partitioned_p1_id_val_idx | t | t > > >>>> + test_replica_identity_partitioned_p2_1_id_val_idx | t | t > > >>>> + test_replica_identity_partitioned_p2_2_id_val_idx | t | t > > >>>> test_replica_identity_partitioned_p2_id_val_idx | t | f > > >>>> test_replica_identity_partitioned_pkey | t | f > > >>>> (5 rows) > > >>>> ``` > > >>>> > > >>>> With this patch, the test passes and all replica identity are preserved. > > >>>> > > >>>> PFA v3: > > >>>> * Enhanced the test. > > >>>> * A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it. > > >>>> > > >>>> Best regards, > > >>>> -- > > >>>> Chao Li (Evan) > > >>>> HighGo Software Co., Ltd. > > >>>> https://www.highgo.com/ > > >>>> > > >>>> > > >>>> > > >>>> > > >>>> > > >>> > > >>> The CF asked for a rebase, thus rebased as v4. > > >>> > > > > > > > > > Hi, I reproduced this with the test case, and the patch appears > > > to resolve it. > > > > > > Some comments on v5: > > > > Thanks a lot for your review. > > > > > > > > -- Whether it makes sense to use a single list of pair structs instead > > > of two parallel OID lists (replicaIdentityIndexOids + > > > replicaIdentityTableOids) to avoid accidental desync. > > > > I don’t think that helps much. The current code of rebuilding index uses two lists changedIndexOids and changedIndexDefs. So, this patch matches the pattern of the existing code. > > > > > > > > -- It would be better to make lock handling in > > > find_partition_replica_identity_indexes() consistent > > > (relation_open(..., NoLock) if child is already locked, and avoid > > > mixed relation_close(..., lockmode)/NoLock behavior). > > > > That’s because if we are going to update a partition, then we need to hold the lock on the partition. > > There is one locking cleanup in find_partition_replica_identity_indexes(). > > find_inheritance_children(relId, lockmode) already acquires lockmode on > every partition it returns, so I think the later relation_open() should use > NoLock, not lockmode. For the same reason, all relation_close() calls in > this function should use NoLock as well. > > Today the code does: > > partRel =relation_open(partRelOid, lockmode); > ... > relation_close(partRel, lockmode); > > That does not cause a correctness issue, because the lock manager > reference-counts same-transaction acquisitions, so the lock remains held > either way. But it is misleading: it suggests that relation_open() is where > the partition lock is taken, and that the early relation_close(..., lockmode) > is intentionally releasing it. Neither is actually true here, because the lock > was already acquired by find_inheritance_children(). > > So I think this should be adjusted to: > > partRel = relation_open(partRelOid, NoLock); > > and all close sites in this function should be: > > relation_close(partRel, NoLock); > > The comment on the early-close path should also be updated, since it is not > really unlocking the partition. Something like "No matching partition index; > just close the relcache entry" would match the actual behavior better. > Okay, in find_partition_replica_identity_indexes, we can use NOLOCK to open partitions as they have been locked by find_inheritance_children. But for those partitions that we won’t touch, we still want to unlock them. PFA v7. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
On Tue, Mar 24, 2026 at 3:26 PM Chao Li wrote: > > > > > On Mar 23, 2026, at 16:41, Xuneng Zhou wrote: > > > > Hi, > > > > On Mon, Mar 23, 2026 at 3:57 PM Chao Li wrote: > > > > > > > > > > > > > On Mar 21, 2026, at 18:29, Xuneng Zhou wrote: > > > > > > > > On Thu, Mar 19, 2026 at 1:07 PM Chao Li wrote: > > > >> > > > >> > > > >> > > > >>> On Feb 26, 2026, at 14:59, Chao Li wrote: > > > >>> > > > >>> > > > >>> > > > >>>> On Jan 28, 2026, at 10:49, Chao Li wrote: > > > >>>> > > > >>>> > > > >>>> > > > >>>>> On Jan 27, 2026, at 16:30, Chao Li wrote: > > > >>>>> > > > >>>>> > > > >>>>> > > > >>>>>> On Jan 27, 2026, at 15:59, Chao Li wrote: > > > >>>>>> > > > >>>>>> > > > >>>>>> > > > >>>>>>> On Jan 27, 2026, at 15:39, Michael Paquier wrote: > > > >>>>>>> > > > >>>>>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote: > > > >>>>>>>> I found this bug while working on a related patch [1]. > > > >>>>>>>> > > > >>>>>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and > > > >>>>>>>> that index is used as REPLICA IDENTITY on a partitioned table, the > > > >>>>>>>> replica identity marking on partitions can be silently lost after the > > > >>>>>>>> rebuild. > > > >>>>>>> > > > >>>>>>> I am slightly confused by the tests included in the proposed patch. > > > >>>>>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests > > > >>>>>>> pass. If I run the tests of the patch with the changes of > > > >>>>>>> tablecmds.c, the tests also pass. > > > >>>>>> > > > >>>>>> Oops, that isn’t supposed to be so. I’ll check the test. > > > >>>>>> > > > >>>>> > > > >>>>> Okay, I see the problem is here: > > > >>>>> ``` > > > >>>>> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id); > > > >>>>> ``` > > > >>>>> > > > >>>>> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild. > > > >>>>> > > > >>>>> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea? > > > >>>> > > > >>>> I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved. > > > >>>> > > > >>>> I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions: > > > >>>> ``` > > > >>>> @@ -360,9 +360,9 @@ > > > >>>> ORDER BY b.index_name; > > > >>>> index_name | rebuilt | ri_lost > > > >>>> ---------------------------------------------------+---------+--------- > > > >>>> - test_replica_identity_partitioned_p1_id_val_idx | t | f > > > >>>> - test_replica_identity_partitioned_p2_1_id_val_idx | t | f > > > >>>> - test_replica_identity_partitioned_p2_2_id_val_idx | t | f > > > >>>> + test_replica_identity_partitioned_p1_id_val_idx | t | t > > > >>>> + test_replica_identity_partitioned_p2_1_id_val_idx | t | t > > > >>>> + test_replica_identity_partitioned_p2_2_id_val_idx | t | t > > > >>>> test_replica_identity_partitioned_p2_id_val_idx | t | f > > > >>>> test_replica_identity_partitioned_pkey | t | f > > > >>>> (5 rows) > > > >>>> ``` > > > >>>> > > > >>>> With this patch, the test passes and all replica identity are preserved. > > > >>>> > > > >>>> PFA v3: > > > >>>> * Enhanced the test. > > > >>>> * A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it. > > > >>>> > > > >>>> Best regards, > > > >>>> -- > > > >>>> Chao Li (Evan) > > > >>>> HighGo Software Co., Ltd. > > > >>>> https://www.highgo.com/ > > > >>>> > > > >>>> > > > >>>> > > > >>>> > > > >>>> > > > >>> > > > >>> The CF asked for a rebase, thus rebased as v4. > > > >>> > > > > > > > > > > > > Hi, I reproduced this with the test case, and the patch appears > > > > to resolve it. > > > > > > > > Some comments on v5: > > > > > > Thanks a lot for your review. > > > > > > > > > > > -- Whether it makes sense to use a single list of pair structs instead > > > > of two parallel OID lists (replicaIdentityIndexOids + > > > > replicaIdentityTableOids) to avoid accidental desync. > > > > > > I don’t think that helps much. The current code of rebuilding index uses two lists changedIndexOids and changedIndexDefs. So, this patch matches the pattern of the existing code. > > > > > > > > > > > -- It would be better to make lock handling in > > > > find_partition_replica_identity_indexes() consistent > > > > (relation_open(..., NoLock) if child is already locked, and avoid > > > > mixed relation_close(..., lockmode)/NoLock behavior). > > > > > > That’s because if we are going to update a partition, then we need to hold the lock on the partition. > > > > There is one locking cleanup in find_partition_replica_identity_indexes(). > > > > find_inheritance_children(relId, lockmode) already acquires lockmode on > > every partition it returns, so I think the later relation_open() should use > > NoLock, not lockmode. For the same reason, all relation_close() calls in > > this function should use NoLock as well. > > > > Today the code does: > > > > partRel =relation_open(partRelOid, lockmode); > > ... > > relation_close(partRel, lockmode); > > > > That does not cause a correctness issue, because the lock manager > > reference-counts same-transaction acquisitions, so the lock remains held > > either way. But it is misleading: it suggests that relation_open() is where > > the partition lock is taken, and that the early relation_close(..., lockmode) > > is intentionally releasing it. Neither is actually true here, because the lock > > was already acquired by find_inheritance_children(). > > > > So I think this should be adjusted to: > > > > partRel = relation_open(partRelOid, NoLock); > > > > and all close sites in this function should be: > > > > relation_close(partRel, NoLock); > > > > The comment on the early-close path should also be updated, since it is not > > really unlocking the partition. Something like "No matching partition index; > > just close the relcache entry" would match the actual behavior better. > > > > Okay, in find_partition_replica_identity_indexes, we can use NOLOCK to open partitions as they have been locked by find_inheritance_children. But for those partitions that we won’t touch, we still want to unlock them. > > PFA v7. > v7 LGTM. -- Best, Xuneng
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
> On Apr 6, 2026, at 14:04, Xuneng Zhou wrote: > > On Tue, Mar 24, 2026 at 3:26 PM Chao Li wrote: >> >> >> >>> On Mar 23, 2026, at 16:41, Xuneng Zhou wrote: >>> >>> Hi, >>> >>> On Mon, Mar 23, 2026 at 3:57 PM Chao Li wrote: >>>> >>>> >>>> >>>>> On Mar 21, 2026, at 18:29, Xuneng Zhou wrote: >>>>> >>>>> On Thu, Mar 19, 2026 at 1:07 PM Chao Li wrote: >>>>>> >>>>>> >>>>>> >>>>>>> On Feb 26, 2026, at 14:59, Chao Li wrote: >>>>>>> >>>>>>> >>>>>>> >>>>>>>> On Jan 28, 2026, at 10:49, Chao Li wrote: >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>>> On Jan 27, 2026, at 16:30, Chao Li wrote: >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>>> On Jan 27, 2026, at 15:59, Chao Li wrote: >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>>> On Jan 27, 2026, at 15:39, Michael Paquier wrote: >>>>>>>>>>> >>>>>>>>>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote: >>>>>>>>>>>> I found this bug while working on a related patch [1]. >>>>>>>>>>>> >>>>>>>>>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and >>>>>>>>>>>> that index is used as REPLICA IDENTITY on a partitioned table, the >>>>>>>>>>>> replica identity marking on partitions can be silently lost after the >>>>>>>>>>>> rebuild. >>>>>>>>>>> >>>>>>>>>>> I am slightly confused by the tests included in the proposed patch. >>>>>>>>>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests >>>>>>>>>>> pass. If I run the tests of the patch with the changes of >>>>>>>>>>> tablecmds.c, the tests also pass. >>>>>>>>>> >>>>>>>>>> Oops, that isn’t supposed to be so. I’ll check the test. >>>>>>>>>> >>>>>>>>> >>>>>>>>> Okay, I see the problem is here: >>>>>>>>> ``` >>>>>>>>> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id); >>>>>>>>> ``` >>>>>>>>> >>>>>>>>> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild. >>>>>>>>> >>>>>>>>> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea? >>>>>>>> >>>>>>>> I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved. >>>>>>>> >>>>>>>> I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions: >>>>>>>> ``` >>>>>>>> @@ -360,9 +360,9 @@ >>>>>>>> ORDER BY b.index_name; >>>>>>>> index_name | rebuilt | ri_lost >>>>>>>> ---------------------------------------------------+---------+--------- >>>>>>>> - test_replica_identity_partitioned_p1_id_val_idx | t | f >>>>>>>> - test_replica_identity_partitioned_p2_1_id_val_idx | t | f >>>>>>>> - test_replica_identity_partitioned_p2_2_id_val_idx | t | f >>>>>>>> + test_replica_identity_partitioned_p1_id_val_idx | t | t >>>>>>>> + test_replica_identity_partitioned_p2_1_id_val_idx | t | t >>>>>>>> + test_replica_identity_partitioned_p2_2_id_val_idx | t | t >>>>>>>> test_replica_identity_partitioned_p2_id_val_idx | t | f >>>>>>>> test_replica_identity_partitioned_pkey | t | f >>>>>>>> (5 rows) >>>>>>>> ``` >>>>>>>> >>>>>>>> With this patch, the test passes and all replica identity are preserved. >>>>>>>> >>>>>>>> PFA v3: >>>>>>>> * Enhanced the test. >>>>>>>> * A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it. >>>>>>>> >>>>>>>> Best regards, >>>>>>>> -- >>>>>>>> Chao Li (Evan) >>>>>>>> HighGo Software Co., Ltd. >>>>>>>> https://www.highgo.com/ >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>> >>>>>>> The CF asked for a rebase, thus rebased as v4. >>>>>>> >>>>> >>>>> >>>>> Hi, I reproduced this with the test case, and the patch appears >>>>> to resolve it. >>>>> >>>>> Some comments on v5: >>>> >>>> Thanks a lot for your review. >>>> >>>>> >>>>> -- Whether it makes sense to use a single list of pair structs instead >>>>> of two parallel OID lists (replicaIdentityIndexOids + >>>>> replicaIdentityTableOids) to avoid accidental desync. >>>> >>>> I don’t think that helps much. The current code of rebuilding index uses two lists changedIndexOids and changedIndexDefs. So, this patch matches the pattern of the existing code. >>>> >>>>> >>>>> -- It would be better to make lock handling in >>>>> find_partition_replica_identity_indexes() consistent >>>>> (relation_open(..., NoLock) if child is already locked, and avoid >>>>> mixed relation_close(..., lockmode)/NoLock behavior). >>>> >>>> That’s because if we are going to update a partition, then we need to hold the lock on the partition. >>> >>> There is one locking cleanup in find_partition_replica_identity_indexes(). >>> >>> find_inheritance_children(relId, lockmode) already acquires lockmode on >>> every partition it returns, so I think the later relation_open() should use >>> NoLock, not lockmode. For the same reason, all relation_close() calls in >>> this function should use NoLock as well. >>> >>> Today the code does: >>> >>> partRel =relation_open(partRelOid, lockmode); >>> ... >>> relation_close(partRel, lockmode); >>> >>> That does not cause a correctness issue, because the lock manager >>> reference-counts same-transaction acquisitions, so the lock remains held >>> either way. But it is misleading: it suggests that relation_open() is where >>> the partition lock is taken, and that the early relation_close(..., lockmode) >>> is intentionally releasing it. Neither is actually true here, because the lock >>> was already acquired by find_inheritance_children(). >>> >>> So I think this should be adjusted to: >>> >>> partRel = relation_open(partRelOid, NoLock); >>> >>> and all close sites in this function should be: >>> >>> relation_close(partRel, NoLock); >>> >>> The comment on the early-close path should also be updated, since it is not >>> really unlocking the partition. Something like "No matching partition index; >>> just close the relcache entry" would match the actual behavior better. >>> >> >> Okay, in find_partition_replica_identity_indexes, we can use NOLOCK to open partitions as they have been locked by find_inheritance_children. But for those partitions that we won’t touch, we still want to unlock them. >> >> PFA v7. >> > > v7 LGTM. > > -- > Best, > Xuneng Rebased as v8. Nothing changed. Per [1], to participate Peter E.’s “in-person commitfest” session, I am adding tag “PGConf.dev” to the CF entry: https://commitfest.postgresql.org/patch/6440/ [1] https://postgr.es/m/9dd5b2f4-fba6-416b-b732-45f284d4097b@eisentraut.org Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
> On Apr 14, 2026, at 11:13, Chao Li wrote: > > > >> On Apr 6, 2026, at 14:04, Xuneng Zhou wrote: >> >> On Tue, Mar 24, 2026 at 3:26 PM Chao Li wrote: >>> >>> >>> >>>> On Mar 23, 2026, at 16:41, Xuneng Zhou wrote: >>>> >>>> Hi, >>>> >>>> On Mon, Mar 23, 2026 at 3:57 PM Chao Li wrote: >>>>> >>>>> >>>>> >>>>>> On Mar 21, 2026, at 18:29, Xuneng Zhou wrote: >>>>>> >>>>>> On Thu, Mar 19, 2026 at 1:07 PM Chao Li wrote: >>>>>>> >>>>>>> >>>>>>> >>>>>>>> On Feb 26, 2026, at 14:59, Chao Li wrote: >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>>> On Jan 28, 2026, at 10:49, Chao Li wrote: >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>>> On Jan 27, 2026, at 16:30, Chao Li wrote: >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>>> On Jan 27, 2026, at 15:59, Chao Li wrote: >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>>> On Jan 27, 2026, at 15:39, Michael Paquier wrote: >>>>>>>>>>>> >>>>>>>>>>>> On Tue, Jan 27, 2026 at 01:13:32PM +0800, Chao Li wrote: >>>>>>>>>>>>> I found this bug while working on a related patch [1]. >>>>>>>>>>>>> >>>>>>>>>>>>> When ALTER TABLE ... ALTER COLUMN TYPE causes an index rebuild, and >>>>>>>>>>>>> that index is used as REPLICA IDENTITY on a partitioned table, the >>>>>>>>>>>>> replica identity marking on partitions can be silently lost after the >>>>>>>>>>>>> rebuild. >>>>>>>>>>>> >>>>>>>>>>>> I am slightly confused by the tests included in the proposed patch. >>>>>>>>>>>> On HEAD, if I undo the proposed changes of tablecmds.c, the tests >>>>>>>>>>>> pass. If I run the tests of the patch with the changes of >>>>>>>>>>>> tablecmds.c, the tests also pass. >>>>>>>>>>> >>>>>>>>>>> Oops, that isn’t supposed to be so. I’ll check the test. >>>>>>>>>>> >>>>>>>>>> >>>>>>>>>> Okay, I see the problem is here: >>>>>>>>>> ``` >>>>>>>>>> +CREATE UNIQUE INDEX test_replica_identity_partitioned_pkey ON test_replica_identity_partitioned (id); >>>>>>>>>> ``` >>>>>>>>>> >>>>>>>>>> I missed to add column “val” into the index, so that alter type of val didn’t cause index rebuild. >>>>>>>>>> >>>>>>>>>> Ideally, it’s better to also verify that index OIDs should have changed before and after alter column type, but I haven’t figured out how to do so. Do you have an idea? >>>>>>>>> >>>>>>>>> I just updated the test to store index OIDs before and after rebuild into 2 temp tables, so that we can compare the OIDs to verify rebuild happens and replica identity preserved. >>>>>>>>> >>>>>>>>> I tried to port the test to master branch, and the test failed. From the test diff file, we can see replica identity lost on 3 leaf partitions: >>>>>>>>> ``` >>>>>>>>> @@ -360,9 +360,9 @@ >>>>>>>>> ORDER BY b.index_name; >>>>>>>>> index_name | rebuilt | ri_lost >>>>>>>>> ---------------------------------------------------+---------+--------- >>>>>>>>> - test_replica_identity_partitioned_p1_id_val_idx | t | f >>>>>>>>> - test_replica_identity_partitioned_p2_1_id_val_idx | t | f >>>>>>>>> - test_replica_identity_partitioned_p2_2_id_val_idx | t | f >>>>>>>>> + test_replica_identity_partitioned_p1_id_val_idx | t | t >>>>>>>>> + test_replica_identity_partitioned_p2_1_id_val_idx | t | t >>>>>>>>> + test_replica_identity_partitioned_p2_2_id_val_idx | t | t >>>>>>>>> test_replica_identity_partitioned_p2_id_val_idx | t | f >>>>>>>>> test_replica_identity_partitioned_pkey | t | f >>>>>>>>> (5 rows) >>>>>>>>> ``` >>>>>>>>> >>>>>>>>> With this patch, the test passes and all replica identity are preserved. >>>>>>>>> >>>>>>>>> PFA v3: >>>>>>>>> * Enhanced the test. >>>>>>>>> * A small change in find_partition_replica_identity_indexes(): if we will not update a partition, then unlock it. >>>>>>>>> >>>>>>>>> Best regards, >>>>>>>>> -- >>>>>>>>> Chao Li (Evan) >>>>>>>>> HighGo Software Co., Ltd. >>>>>>>>> https://www.highgo.com/ >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>> >>>>>>>> The CF asked for a rebase, thus rebased as v4. >>>>>>>> >>>>>> >>>>>> >>>>>> Hi, I reproduced this with the test case, and the patch appears >>>>>> to resolve it. >>>>>> >>>>>> Some comments on v5: >>>>> >>>>> Thanks a lot for your review. >>>>> >>>>>> >>>>>> -- Whether it makes sense to use a single list of pair structs instead >>>>>> of two parallel OID lists (replicaIdentityIndexOids + >>>>>> replicaIdentityTableOids) to avoid accidental desync. >>>>> >>>>> I don’t think that helps much. The current code of rebuilding index uses two lists changedIndexOids and changedIndexDefs. So, this patch matches the pattern of the existing code. >>>>> >>>>>> >>>>>> -- It would be better to make lock handling in >>>>>> find_partition_replica_identity_indexes() consistent >>>>>> (relation_open(..., NoLock) if child is already locked, and avoid >>>>>> mixed relation_close(..., lockmode)/NoLock behavior). >>>>> >>>>> That’s because if we are going to update a partition, then we need to hold the lock on the partition. >>>> >>>> There is one locking cleanup in find_partition_replica_identity_indexes(). >>>> >>>> find_inheritance_children(relId, lockmode) already acquires lockmode on >>>> every partition it returns, so I think the later relation_open() should use >>>> NoLock, not lockmode. For the same reason, all relation_close() calls in >>>> this function should use NoLock as well. >>>> >>>> Today the code does: >>>> >>>> partRel =relation_open(partRelOid, lockmode); >>>> ... >>>> relation_close(partRel, lockmode); >>>> >>>> That does not cause a correctness issue, because the lock manager >>>> reference-counts same-transaction acquisitions, so the lock remains held >>>> either way. But it is misleading: it suggests that relation_open() is where >>>> the partition lock is taken, and that the early relation_close(..., lockmode) >>>> is intentionally releasing it. Neither is actually true here, because the lock >>>> was already acquired by find_inheritance_children(). >>>> >>>> So I think this should be adjusted to: >>>> >>>> partRel = relation_open(partRelOid, NoLock); >>>> >>>> and all close sites in this function should be: >>>> >>>> relation_close(partRel, NoLock); >>>> >>>> The comment on the early-close path should also be updated, since it is not >>>> really unlocking the partition. Something like "No matching partition index; >>>> just close the relcache entry" would match the actual behavior better. >>>> >>> >>> Okay, in find_partition_replica_identity_indexes, we can use NOLOCK to open partitions as they have been locked by find_inheritance_children. But for those partitions that we won’t touch, we still want to unlock them. >>> >>> PFA v7. >>> >> >> v7 LGTM. >> >> -- >> Best, >> Xuneng > > Rebased as v8. Nothing changed. > > Per [1], to participate Peter E.’s “in-person commitfest” session, I am adding tag “PGConf.dev” to the CF entry: https://commitfest.postgresql.org/patch/6440/ > > [1] https://postgr.es/m/9dd5b2f4-fba6-416b-b732-45f284d4097b@eisentraut.org > > Best regards, > -- > Chao Li (Evan) > HighGo Software Co., Ltd. > https://www.highgo.com/ > > > > > PFA v9: * Rebased to resolve new conflicts * Fixed a missed case where a partitioned table itself doesn’t use index as RI but some partitions use. * Simplified the new tests by using \gset to avoid creating temp tables. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
От:
Sami Imseih <samimseih@gmail.com>
Дата:
Hi, I spent some time looking at this, and I think there are still some issues with v9. For example: ``` CREATE TABLE p (id int NOT NULL, val int NOT NULL) PARTITION BY RANGE (id); CREATE TABLE c1 (id int NOT NULL, val int NOT NULL); CREATE UNIQUE INDEX my_custom_ri ON c1 (id, val); ALTER TABLE c1 REPLICA IDENTITY USING INDEX my_custom_ri; CREATE UNIQUE INDEX p_idx ON p (id, val); ALTER TABLE p ATTACH PARTITION c1 FOR VALUES FROM (0) TO (100); ALTER TABLE p ALTER COLUMN val TYPE bigint; ERROR: index "my_custom_ri" for table "c1" does not exist ``` This is because generateClonedIndexStmt() sets idxname = NULL when building the partition indexes. ``` IndexStmt * generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx, ... /* * We don't try to preserve the name of the source index; instead, * just let DefineIndex() choose a reasonable name. (If we tried to * preserve the name, we'd get duplicate-relation-name failures * unless the source table was in a different schema.) */ index->idxname = NULL; ``` This causes ChooseIndexName() to generate a new default name. v9 then tries to restore replica identity by the old name, which no longer exists. This index name-change behavior during ALTER TABLE ... ALTER COLUMN TYPE is existing and undocumented, and I would argue is wrong. The user expects index names to be stable at the end of this operation. CREATE TABLE ... LIKE (INCLUDING INDEXES) is a case where we should not create an index with the same name as the source and choosing a default name makes sense, but it should not apply to all callers. Perhaps callers like ALTER TABLE ... ALTER COLUMN TYPE should track the old index names and be allowed to pass them to generateClonedIndexStmt? That sounds like a broader improvement, and one that the changes in v9 can inherit. What do you think? The current v9 tests happen to work only because they use default names that ChooseIndexName() re-generates the same way. Also, this issue is not limited to replica identity. CLUSTER ON does not copy/restore indisclustered on partition indexes at all, and this should be addressed as well, right? -- Sami Imseih Amazon Web Services (AWS)
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
Hi, > PFA v11 - addressed Sami’s comments on v10. Thanks! v11-0001 looks good with some minor comment changes on my end: v11-0002 also looks good. I renamed find_partition_index_properties to RememberPartitionIndexPropertiesForRebuilding Attached is a v12 with these minor fixes. I also improved the commit message for 0002. -- Sami Imseih Amazon Web Services (AWS)
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Jul 30, 2026, at 01:07, Sami Imseih wrote: > > Hi, > >> PFA v11 - addressed Sami’s comments on v10. > > Thanks! > > v11-0001 looks good with some minor comment changes on my end: > > v11-0002 also looks good. I renamed find_partition_index_properties > to RememberPartitionIndexPropertiesForRebuilding > > Attached is a v12 with these minor fixes. I also improved the commit > message for 0002. > > -- > Sami Imseih > Amazon Web Services (AWS) > Thanks for updating the patch. V12 looks good to me. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
От:
"Alberto Piai" <alberto.piai@gmail.com>
Дата:
Hi, On Wed Jul 29, 2026 at 7:07 PM CEST, Sami Imseih wrote: > Attached is a v12 with these minor fixes. I also improved the commit > message for 0002. A small comment on v12 0001: the patch adds a parameter to generateClonedIndexStmt() to pass the index name. Is there a reason for setting the index name in the function, rather than setting it in DefineIndex() directly on the newly generated statement? The approach and the patches look otherwise good to me. Regards, Alberto -- Alberto Piai Sensational AG Zürich, Switzerland
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
От:
Sami Imseih <samimseih.pg@gmail.com>
Дата:
Hi Chao,
Thanks for the update.
> I have addressed them in v15 expect for the hash-table suggestion. I tried using an HTAB, but ran into several build failures.
One other point from yesterday remains. We should invoke
InvokeObjectPostAlterHook() after the CatalogTupleUpdate() in
SetIndexStatTargets(), and InvokeObjectPostAlterHookArg() after the direct
pg_index CatalogTupleUpdate() in DefineIndex() inside the
stmt->idxisclustered || stmt->idxisreplident block.
I think this is the right thing to do because, although we are restoring
existing values, these are being restored onto a newly created index with a
new OID.
--
Sami Imseih
Amazon Web Services (AWS)
Thanks for the update.
> I have addressed them in v15 expect for the hash-table suggestion. I tried using an HTAB, but ran into several build failures.
The v15 changes LGTM. I am fine with leaving the HTAB idea aside. This is
not a performance critical path in general, so I do not think it is worth
adding complexity there. After thinking more about it, I can see why it
could be difficult, especially if we need to track hash state outside of
IndexStmt. We can still optimize the lookup by scoping an HTAB inside
DefineIndex(). In that case we would still carry the list in IndexStmt, but
we could make the lookups within DefineIndex() more efficient with a
function-scoped HTAB. But, again, not sure it's worth it.
One other point from yesterday remains. We should invoke
InvokeObjectPostAlterHook() after the CatalogTupleUpdate() in
SetIndexStatTargets(), and InvokeObjectPostAlterHookArg() after the direct
pg_index CatalogTupleUpdate() in DefineIndex() inside the
stmt->idxisclustered || stmt->idxisreplident block.
I think this is the right thing to do because, although we are restoring
existing values, these are being restored onto a newly created index with a
new OID.
--
Sami Imseih
Amazon Web Services (AWS)
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
> On Aug 27, 2026, at 05:17, Sami Imseih wrote: > > Hi Chao, > > Thanks for the update. > > > I have addressed them in v15 expect for the hash-table suggestion. I tried using an HTAB, but ran into several build failures. > > The v15 changes LGTM. I am fine with leaving the HTAB idea aside. This is > not a performance critical path in general, so I do not think it is worth > adding complexity there. After thinking more about it, I can see why it > could be difficult, especially if we need to track hash state outside of > IndexStmt. We can still optimize the lookup by scoping an HTAB inside > DefineIndex(). In that case we would still carry the list in IndexStmt, but > we could make the lookups within DefineIndex() more efficient with a > function-scoped HTAB. But, again, not sure it's worth it. > > One other point from yesterday remains. We should invoke > InvokeObjectPostAlterHook() after the CatalogTupleUpdate() in > SetIndexStatTargets(), and InvokeObjectPostAlterHookArg() after the direct > pg_index CatalogTupleUpdate() in DefineIndex() inside the > stmt->idxisclustered || stmt->idxisreplident block. > > I think this is the right thing to do because, although we are restoring > existing values, these are being restored onto a newly created index with a > new OID. Sorry for missing this comment. Yes, I agree that it makes sense to call the hook. The two hooks are added to 0001 and 0002 accordingly in v15. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Aug 27, 2026, at 07:21, Zsolt Parragi wrote:
>
> Hello
>
> Thanks for including 0001 here!
>
> I reviewed the latest v15 version, and I think overall there are a few
> leftover issues:
Thanks for the review.
>
> 1. leaf tablespace is still lost when default_tablespace is set
>
> CREATE TABLESPACE ts LOCATION '/path/to/empty/dir';
> CREATE TABLE t (id int, val int) PARTITION BY RANGE (id);
> CREATE TABLE t1 PARTITION OF t FOR VALUES FROM (0) TO (100);
> CREATE INDEX t_idx ON t (val) TABLESPACE ts;
> ALTER INDEX t1_val_idx SET TABLESPACE pg_default; -- leaf
> deliberately elsewhere
>
> SET default_tablespace = 'ts';
> ALTER TABLE t ALTER COLUMN val TYPE bigint;
> RESET default_tablespace;
>
> SELECT c.relname, coalesce(s.spcname,'') AS tablespace
> FROM pg_class c LEFT JOIN pg_tablespace s ON s.oid = c.reltablespace
> WHERE c.relname IN ('t_idx','t1_val_idx') ORDER BY 1;
Confirmed and fixed in v15.
>
> 2. intermediate partitioned indexes are still stripped
>
> CREATE TABLE root (id int, val int) PARTITION BY RANGE (id);
> CREATE TABLE mid (id int, val int) PARTITION BY RANGE (id);
> CREATE TABLE leaf PARTITION OF mid FOR VALUES FROM (0) TO (50);
> ALTER TABLE root ATTACH PARTITION mid FOR VALUES FROM (0) TO (100);
> CREATE INDEX root_idx ON root ((val + 1));
>
> ALTER INDEX mid_val_1_idx RENAME TO mid_custom;
> ALTER INDEX leaf_val_1_idx RENAME TO leaf_custom;
> COMMENT ON INDEX mid_custom IS 'mid comment';
> COMMENT ON INDEX leaf_custom IS 'leaf comment';
> ALTER INDEX mid_custom ALTER COLUMN 1 SET STATISTICS 777;
> ALTER INDEX leaf_custom ALTER COLUMN 1 SET STATISTICS 888;
>
> SELECT c.relname, obj_description(c.oid,'pg_class') AS comment,
> (SELECT attstattarget FROM pg_attribute a
> WHERE a.attrelid = c.oid AND a.attnum = 1) AS stattarget
> FROM pg_class c WHERE c.relkind IN ('i','I') AND c.relname !~ '^pg_'
> ORDER BY 1;
> ALTER TABLE root ALTER COLUMN val TYPE bigint;
> SELECT c.relname, obj_description(c.oid,'pg_class') AS comment,
> (SELECT attstattarget FROM pg_attribute a
> WHERE a.attrelid = c.oid AND a.attnum = 1) AS stattarget
> FROM pg_class c WHERE c.relkind IN ('i','I') AND c.relname !~ '^pg_'
> ORDER BY 1;
>
Confirmed and fixed in v15.
> 2b: that includes the replica identity
>
> CREATE TABLE root (id int not null, val int not null) PARTITION BY RANGE (id);
> CREATE TABLE mid (id int not null, val int not null) PARTITION BY RANGE (id);
> CREATE TABLE leaf PARTITION OF mid FOR VALUES FROM (0) TO (50);
> ALTER TABLE root ATTACH PARTITION mid FOR VALUES FROM (0) TO (100);
> CREATE UNIQUE INDEX root_uq ON root (id, val);
> ALTER TABLE mid REPLICA IDENTITY USING INDEX mid_id_val_idx;
>
> SELECT tc.relname AS "table", tc.relreplident, c.relname AS index,
> i.indisreplident
> FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid
> JOIN pg_class tc ON tc.oid = i.indrelid
> WHERE tc.relname IN ('root','mid','leaf') ORDER BY 1;
> ALTER TABLE root ALTER COLUMN val TYPE bigint;
> SELECT tc.relname AS "table", tc.relreplident, c.relname AS index,
> i.indisreplident
> FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid
> JOIN pg_class tc ON tc.oid = i.indrelid
> WHERE tc.relname IN ('root','mid','leaf') ORDER BY 1;
Confirmed and fixed in v15.
>
> 3: leaf constraint comments are still lost, while comments on its
> indexes are now preserved
>
> CREATE TABLE cc (id int not null, val int not null) PARTITION BY RANGE (id);
> CREATE TABLE cc1 PARTITION OF cc FOR VALUES FROM (0) TO (100);
> ALTER TABLE cc ADD CONSTRAINT cc_pkey PRIMARY KEY (id, val);
> COMMENT ON CONSTRAINT cc1_pkey ON cc1 IS 'leaf constraint comment';
> COMMENT ON INDEX cc1_pkey IS 'leaf index comment';
>
> SELECT obj_description('cc1_pkey'::regclass,'pg_class') AS index_comment,
> obj_description(oid,'pg_constraint') AS constraint_comment
> FROM pg_constraint WHERE conrelid='cc1'::regclass AND contype='p';
> ALTER TABLE cc ALTER COLUMN val TYPE bigint;
> SELECT obj_description('cc1_pkey'::regclass,'pg_class') AS index_comment,
> obj_description(oid,'pg_constraint') AS constraint_comment
> FROM pg_constraint WHERE conrelid='cc1'::regclass AND contype='p';
Confirmed and fixed in v15.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
> On Aug 27, 2026, at 11:42, Zhijie Hou (Fujitsu) wrote:
>
>
> Hi,
>
> On Wednesday, August 26, 2026 1:30 PM Chao Li wrote:
>> I reviewed v14 and found that a leaf index’s tablespace can also be lost and
>> needs to be restored. I fixed that in v15-0002.
>>
>> I also addressed Sami’s review comments in v15, except for the hash-table
>> suggestion. I tried using an HTAB, but ran into several build failures. I didn’t
>> want to spend more time on it, so I gave up on that approach.
>
> I have few thoughts for the patch:
Hi Zhijie,
Thank you very much for the review.
>
> 1.
>
> I noticed that the code updating idxisclustered and idxisreplident looks a bit
> unusual to me. These updates are placed inside DefineIndex(), where a fresh
> pg_index and pg_class row are already being created for the new index. It seems
> a bit confusing that the patch also performs post-updates for these rows within
> the same function.
>
> Perhaps we could add a comment explaining why these values can't be set directly
> when creating the new rows - that would help future readers understand the
> reasoning.
Updated the comment in v15.
>
> I'm also concerned that updating the flag this way won't fire the
> ObjectPostAlterHook (as Sami also mentioned). I think some analysis or comments
> are worth adding here.
Added the hook call in v15.
>
> Few nits:
>
> 2. I think this should report an ERROR, since we don't expect the tuple to be
> concurrently updated, IIUC.
>
> classtup = SearchSysCache1(RELOID, ObjectIdGetDatum(leafIndexOid));
> if (!HeapTupleIsValid(classtup))
> {
> ReleaseSysCache(idxtup);
> continue;
> }
Agreed and fixed in v15.
>
> 3. It's unnecessary to set stattargets = NIL when you'll overwrite it right
> after anyway.
>
> props->stattargets = NIL;
> ...
> props->stattargets = GetIndexStatTargets(leafIndexOid);
>
Agreed and fixed in v15.
BTW, I have run a check-world test with v15.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
My testing found a name collision scenario in v18: create schema w1; set search_path = w1; create table p (id int not null, a int not null) partition by list (id); create table p1 partition of p for values in (1); create table p2 partition of p for values in (2); create index p_idx on only p (a); -- invalid parent: nothing attached create index tmp on p2 (a); alter index p_idx attach partition tmp; -- p2 attached, p1 still not alter index tmp rename to p1_a_idx; -- p2's index holds p1's default name alter table p alter column a type bigint; -- ERROR: relation "p1_a_idx" already exists; should work Also, with the additional split 0003 now deletes a test case added by 0001, is that intentional?
Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
От:
Álvaro Rodríguez <alvaro@datadoghq.com>
Дата:
Hi all, I have been working on another patch [0] and was pointed to this thread. My changes are focused on avoiding preventable rebuilds of the leaf indexes on partitioned tables, when we do ALTER TABLE ALTER COLUMN TYPE. I change the order in which indexes are processed and do some other minor changes to ensure that they are correctly remembered on rebuild. I believe commit 0003 here addresses a subset of the issues that my patch fixes. To validate, I have combined 0001 + 0002 + my patch + 0003's tests and the tests seem to be passing (the only change in test output is the at_partitioned test, where I get the storage changes that my patch is fixing). Commits 0001 and 0002 are not addressed by my patch at all, as far as I can tell. I wonder if it's worth joining efforts! Best, Álvaro [0] https://www.postgresql.org/message-id/flat/CA%2BC_kKX9Qk0JQZ%2BbjyHWNiCBLYN4kyCRdJoJ%2Bgtr1K2%2BXjo55g%40mail.gmail.com