diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 8951543b7e..e0aece6b51 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -90,14 +90,15 @@ BuildRelationExtStatistics(Relation onerel, double totalrows, MemoryContext cxt; MemoryContext oldcxt; + pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock); + stats = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel)); + + /* memory context for building each statistics object */ cxt = AllocSetContextCreate(CurrentMemoryContext, "BuildRelationExtStatistics", ALLOCSET_DEFAULT_SIZES); oldcxt = MemoryContextSwitchTo(cxt); - pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock); - stats = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel)); - foreach(lc, stats) { StatExtEntry *stat = (StatExtEntry *) lfirst(lc); @@ -148,12 +149,17 @@ BuildRelationExtStatistics(Relation onerel, double totalrows, /* store the statistics in the catalog */ statext_store(stat->statOid, ndistinct, dependencies, mcv, stats); - } - table_close(pg_stext, RowExclusiveLock); + /* free the data used for building this statistics object */ + MemoryContextReset(cxt); + } MemoryContextSwitchTo(oldcxt); MemoryContextDelete(cxt); + + list_free(stats); + + table_close(pg_stext, RowExclusiveLock); } /*