From 205f40f5b27392b54a7ffba3059522ccc2a5d269 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Wed, 14 Oct 2026 10:16:26 +0900 Subject: [PATCH] Release memory allocated by ndistinct_for_combination() Building extended statistics of type "ndistinct" calls ndistinct_for_combination() once for every combination of columns. Each call allocates arrays for the whole sample and never frees them, with memory accumulating until the whole statistics object is built. The sample has 300 multiplied by target rows. With large statistics targets (default_statistics_target up to 10k), this can add up to gigabytes worth of memory, useless once a combination is computed. ndistinct_for_combination() is now called in a dedicated memory context, reset after each combination is computed. We only need the estimated number of distinct values, so there is no need to copy anything out of the memory context. Similar fixes have been done in 83772cc78e03 and ad8a166ca868 for other areas of the extended stats computations. Author: Ilia Evdokimov Discussion: https://postgr.es/m/20d6689e-f490-4ca7-a92f-e92f691ba07e@tantorlabs.com Backpatch-through: 14 --- src/backend/statistics/mvdistinct.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/backend/statistics/mvdistinct.c b/src/backend/statistics/mvdistinct.c index 4b4ecec9361..bf63f2b9a73 100644 --- a/src/backend/statistics/mvdistinct.c +++ b/src/backend/statistics/mvdistinct.c @@ -33,6 +33,7 @@ #include "statistics/statistics.h" #include "utils/fmgrprotos.h" #include "utils/lsyscache.h" +#include "utils/memutils.h" #include "utils/syscache.h" #include "utils/typcache.h" @@ -93,6 +94,7 @@ statext_ndistinct_build(double totalrows, StatsBuildData *data) int itemcnt; int numattrs = data->nattnums; int numcombs = num_combinations(numattrs); + MemoryContext cxt; result = palloc(offsetof(MVNDistinct, items) + numcombs * sizeof(MVNDistinctItem)); @@ -100,6 +102,10 @@ statext_ndistinct_build(double totalrows, StatsBuildData *data) result->type = STATS_NDISTINCT_TYPE_BASIC; result->nitems = numcombs; + cxt = AllocSetContextCreate(CurrentMemoryContext, + "ndistinct_for_combination cxt", + ALLOCSET_DEFAULT_SIZES); + itemcnt = 0; for (k = 2; k <= numattrs; k++) { @@ -113,6 +119,7 @@ statext_ndistinct_build(double totalrows, StatsBuildData *data) { MVNDistinctItem *item = &result->items[itemcnt]; int j; + MemoryContext oldcxt; item->attributes = palloc(sizeof(AttrNumber) * k); item->nattributes = k; @@ -125,8 +132,12 @@ statext_ndistinct_build(double totalrows, StatsBuildData *data) Assert(AttributeNumberIsValid(item->attributes[j])); } + /* release memory used by ndistinct calculation */ + oldcxt = MemoryContextSwitchTo(cxt); item->ndistinct = ndistinct_for_combination(totalrows, data, k, combination); + MemoryContextSwitchTo(oldcxt); + MemoryContextReset(cxt); itemcnt++; Assert(itemcnt <= result->nitems); @@ -135,6 +146,8 @@ statext_ndistinct_build(double totalrows, StatsBuildData *data) generator_free(generator); } + MemoryContextDelete(cxt); + /* must consume exactly the whole output array */ Assert(itemcnt == result->nitems);