diff --git a/src/backend/statistics/dependencies.c b/src/backend/statistics/dependencies.c index e7f2d6c0ca..445a0db7c5 100644 --- a/src/backend/statistics/dependencies.c +++ b/src/backend/statistics/dependencies.c @@ -29,6 +29,8 @@ #include "utils/fmgroids.h" #include "utils/fmgrprotos.h" #include "utils/lsyscache.h" +#include "utils/memutils.h" +#include "utils/selfuncs.h" #include "utils/syscache.h" #include "utils/typcache.h" @@ -321,11 +323,6 @@ dependency_degree(int numrows, HeapTuple *rows, int k, AttrNumber *dependency, group_size++; } - pfree(items); - pfree(values); - pfree(isnull); - pfree(mss); - /* Compute the 'degree of validity' as (supporting/total). */ return (n_supporting_rows * 1.0 / numrows); } @@ -358,6 +355,7 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs, /* result */ MVDependencies *dependencies = NULL; + MemoryContext cxt; numattrs = bms_num_members(attrs); @@ -372,6 +370,11 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs, Assert(numattrs >= 2); + /* tracks memory allocated by dependency_degree calls */ + cxt = AllocSetContextCreate(CurrentMemoryContext, + "dependency_degree cxt", + ALLOCSET_DEFAULT_SIZES); + /* * We'll try build functional dependencies starting from the smallest ones * covering just 2 columns, to the largest ones, covering all columns @@ -390,10 +393,17 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs, { double degree; MVDependency *d; + MemoryContext oldcxt; + + /* release memory used by dependency degree calculation */ + oldcxt = MemoryContextSwitchTo(cxt); /* compute how valid the dependency seems */ degree = dependency_degree(numrows, rows, k, dependency, stats, attrs); + MemoryContextSwitchTo(oldcxt); + MemoryContextReset(cxt); + /* * if the dependency seems entirely invalid, don't store it */ @@ -435,6 +445,8 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs, DependencyGenerator_free(DependencyGenerator); } + MemoryContextDelete(cxt); + return dependencies; }