CREATE INDEX vs. TOAST table stats
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
Commit d611f8b158 taught CREATE INDEX to avoid updating table stats when
autovacuum = off so that restored stats weren't wiped out. It included
RELKIND_TOASTVALUE in the list of affected relkinds, which I think is
incorrect for the following reasons:
* You can't restore stats for TOAST tables.
* If toast.autovacuum_enabled is unset for the TOAST table, it should
inherit the main table's setting, and the code doesn't do that. Per the
above point, there's little point in teaching it to do so.
This bug probably doesn't have much impact, other than perhaps increasing
the TOAST table's autovacuum priority immediately after restore. I think
we should just back-patch something like the following down to v18:
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 4c5da7e5db0..993a68fbc5e 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -2893,7 +2893,6 @@ index_update_stats(Relation rel,
* created, and we want to preserve the restored table statistics.
*/
if (rel->rd_rel->relkind == RELKIND_RELATION ||
- rel->rd_rel->relkind == RELKIND_TOASTVALUE ||
rel->rd_rel->relkind == RELKIND_MATVIEW)
{
if (AutoVacuumingActive())
--
nathan
Hi,
I agree on removing RELKIND_TOASTVALUE. A toast table has no restorable stats,
so we should not attempt to preserve the stats here. Also, main
relation and toast
drifting in stats already happens. We are not introducing any issue
here, AFAICT.
if (rel->rd_rel->relkind == RELKIND_RELATION ||
- rel->rd_rel->relkind == RELKIND_TOASTVALUE ||
rel->rd_rel->relkind == RELKIND_MATVIEW)
One thing is CREATE INDEX, mentioned in the subject, never calls
index_update_stats()
on the toast relation, so the toast stats are not updated by it. This
will only be called when
the toast's own index is rebuilt, i.e. REINDEX, VACUUM FULL, CLUSTER, REPACK.
+1 on backpatching to v18.
--
Sami Imseih
Amazon Web Services (AWS)
On Tue, Aug 11, 2026 at 02:25:10PM -0500, Sami Imseih wrote:
One thing is CREATE INDEX, mentioned in the subject, never calls
index_update_stats() on the toast relation, so the toast stats are not
updated by it. This will only be called when the toast's own index is
rebuilt, i.e. REINDEX, VACUUM FULL, CLUSTER, REPACK.
Sorry, I'm untangling a few different things in this area and got it mixed
up. I think you're right, and I think I also misstated the impact. But
IMHO the fix is still the way to go, for the two reasons I mentioned
upthread.
+1 on backpatching to v18.
Thanks for reviewing.
--
nathan