Commit abb2f914 authored by Tomas Vondra's avatar Tomas Vondra

Release memory allocated by dependency_degree

Calculating degree of a functional dependency may allocate a lot of
memory - we have released mot of the explicitly allocated memory, but
e.g. detoasted varlena values were left behind. That may be an issue,
because we consider a lot of dependencies (all combinations), and the
detoasting may happen for each one again.

Fixed by calling dependency_degree() in a dedicated context, and
resetting it after each call. We only need the calculated dependency
degree, so we don't need to copy anything.

Backpatch to PostgreSQL 10, where extended statistics were introduced.

Backpatch-through: 10
Discussion: https://www.postgresql.org/message-id/20210915200928.GP831%40telsasoft.com
parent bb7628e5
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "utils/fmgroids.h" #include "utils/fmgroids.h"
#include "utils/fmgrprotos.h" #include "utils/fmgrprotos.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/selfuncs.h" #include "utils/selfuncs.h"
#include "utils/syscache.h" #include "utils/syscache.h"
#include "utils/typcache.h" #include "utils/typcache.h"
...@@ -325,12 +326,6 @@ dependency_degree(StatsBuildData *data, int k, AttrNumber *dependency) ...@@ -325,12 +326,6 @@ dependency_degree(StatsBuildData *data, int k, AttrNumber *dependency)
group_size++; group_size++;
} }
if (items)
pfree(items);
pfree(mss);
pfree(attnums_dep);
/* Compute the 'degree of validity' as (supporting/total). */ /* Compute the 'degree of validity' as (supporting/total). */
return (n_supporting_rows * 1.0 / data->numrows); return (n_supporting_rows * 1.0 / data->numrows);
} }
...@@ -359,9 +354,15 @@ statext_dependencies_build(StatsBuildData *data) ...@@ -359,9 +354,15 @@ statext_dependencies_build(StatsBuildData *data)
/* result */ /* result */
MVDependencies *dependencies = NULL; MVDependencies *dependencies = NULL;
MemoryContext cxt;
Assert(data->nattnums >= 2); Assert(data->nattnums >= 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 * We'll try build functional dependencies starting from the smallest ones
* covering just 2 columns, to the largest ones, covering all columns * covering just 2 columns, to the largest ones, covering all columns
...@@ -380,10 +381,17 @@ statext_dependencies_build(StatsBuildData *data) ...@@ -380,10 +381,17 @@ statext_dependencies_build(StatsBuildData *data)
{ {
double degree; double degree;
MVDependency *d; MVDependency *d;
MemoryContext oldcxt;
/* release memory used by dependency degree calculation */
oldcxt = MemoryContextSwitchTo(cxt);
/* compute how valid the dependency seems */ /* compute how valid the dependency seems */
degree = dependency_degree(data, k, dependency); degree = dependency_degree(data, k, dependency);
MemoryContextSwitchTo(oldcxt);
MemoryContextReset(cxt);
/* /*
* if the dependency seems entirely invalid, don't store it * if the dependency seems entirely invalid, don't store it
*/ */
...@@ -425,6 +433,8 @@ statext_dependencies_build(StatsBuildData *data) ...@@ -425,6 +433,8 @@ statext_dependencies_build(StatsBuildData *data)
DependencyGenerator_free(DependencyGenerator); DependencyGenerator_free(DependencyGenerator);
} }
MemoryContextDelete(cxt);
return dependencies; return dependencies;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment