Re: Preserve index stats during ALTER TABLE ... TYPE ...

From: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
To: Sami Imseih <samimseih(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Preserve index stats during ALTER TABLE ... TYPE ...
Date: 2025-10-10 15:52:58
Message-ID: aOkr2l09kaeJoCzp@ip-10-97-1-34.eu-west-3.compute.internal
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-10-10 15:58:42 Re: RFC: extensible planner state
Previous Message Nathan Bossart 2025-10-10 15:46:29 Re: [PATCH] Remove make_temptable_name_n()