Re: Preserve index stats during ALTER TABLE ... TYPE ... - Mailing list pgsql-hackers

From Bertrand Drouvot
Subject Re: Preserve index stats during ALTER TABLE ... TYPE ...
Date
Msg-id aOkr2l09kaeJoCzp@ip-10-97-1-34.eu-west-3.compute.internal
Whole thread Raw
In response to Re: Preserve index stats during ALTER TABLE ... TYPE ...  (Sami Imseih <samimseih@gmail.com>)
List pgsql-hackers
Hi,

On Fri, Oct 10, 2025 at 07:37:59AM -0500, Sami Imseih wrote:
> Hi,
> 
> Thanks for raising this issue and for the patch!

Thanks for looking at it!

> > As you can see, the index stats (linked to the column that has been altered) are
> > not preserved. I think that they should be preserved (like a REINDEX does).
> 
> I agree.
> 
> > - We can not use pgstat_copy_relation_stats() because the old index is dropped
> > before the new one is created, so the patch adds a new PgStat_StatTabEntry
> > pointer in AlteredTableInfo.
> 
> I wonder if it will be good to have a pgstat_save_relation_stats() routine that
> gets called in all code paths that will need to restore the stats. This way
> pgstat_copy_relation_stats can also be used. This will be cleaner than code
> paths that need this having to deal with pgstat_fetch_stat_tabentry?

pgstat_copy_relation_stats() needs 2 Relation, I'm not sure how a new
pgstat_save_relation_stats() could help using pgstat_copy_relation_stats()
here.

> The current patch does not work for partitioned tables because
> the "oldId" is that of the parent index which has no stats. So we
> are just copying zeros to the new entry.

Doh, of course. I've spend some time on it and now have something working. 

The idea is to:

- store a List of savedIndexStats. The savedIndexStats struct would get the
PgStat_StatTabEntry + all the information needed to be able to use 
CompareIndexInfo() when restoring the stats (so that we can restore each PgStat_StatTabEntry
in the right index).

- Iterate on all the indexes and populate this new list in AlteredTableInfo in
ATPostAlterTypeParse().

- Iterate on all the indexes and use the list above and CompareIndexInfo() to
restore the stats in ATExecAddIndex().

Will polish and share next week.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com



pgsql-hackers by date:

Previous
From: Nathan Bossart
Date:
Subject: Re: [PATCH] Remove make_temptable_name_n()
Next
From: Tom Lane
Date:
Subject: Re: RFC: extensible planner state