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: Sami Imseih <samimseih(at)gmail(dot)com>
Cc: Alberto Piai <alberto(dot)piai(at)gmail(dot)com>, Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Xuneng Zhou <xunengzhou(at)gmail(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>
Subject: Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
Date: 2026-07-28 08:54:25
Message-ID: 5490D332-1381-4548-9CCF-C36365A6495F@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


> On Jul 28, 2026, at 06:10, Sami Imseih <samimseih(at)gmail(dot)com> wrote:
>
> Hi,
>
> I spent some time looking at this, and I think there are still some
> issues with v9.
>

Hi Sami, thank you very much for reviewing.

> 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.
>

Agreed. So, this is a separate bug.

> 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?
>

I didn’t notice that issue.

PFA v10:

0001- preserve index name for partition indexes
0002 - preserve replica identity and cluster on index

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

Attachment Content-Type Size
v10-0001-tablecmds-preserve-partition-index-names-during-.patch application/octet-stream 12.5 KB
v10-0002-tablecmds-preserve-partition-index-properties-du.patch application/octet-stream 31.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tender Wang 2026-07-28 08:58:58 Re: remove_useless_joins vs. bug #19560
Previous Message Andrei Lepikhov 2026-07-28 08:53:43 Skip .DS_Store files in check_mode_recursive