Re: tablecmds: fix bug where index rebuild loses replica identity on partitions

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
Date: 2026-03-19 05:06:24
Message-ID: 260E4616-43B7-4C24-BF53-352DC8DEB7AA@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Feb 26, 2026, at 14:59, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
>
>
>
>> On Jan 28, 2026, at 10:49, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
>>
>>
>>
>>> On Jan 27, 2026, at 16:30, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
>>>
>>>
>>>
>>>> On Jan 27, 2026, at 15:59, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
>>>>
>>>>
>>>>
>>>>> On Jan 27, 2026, at 15:39, Michael Paquier <michael(at)paquier(dot)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.
>
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
>
>
>
>
> <v4-0001-tablecmds-fix-bug-where-index-rebuild-loses-repli.patch>

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/

Attachment Content-Type Size
v5-0001-tablecmds-fix-bug-where-index-rebuild-loses-repli.patch application/octet-stream 21.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Hayato Kuroda (Fujitsu) 2026-03-19 05:30:41 RE: Patch for migration of the pg_commit_ts directory
Previous Message David Rowley 2026-03-19 04:28:11 Re: Reduce planning time for large NOT IN lists containing NULL