summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomas Vondra <tomas.vondra@postgresql.org>2021-09-21 01:13:11 +0200
committerTomas Vondra <tomas.vondra@postgresql.org>2021-09-23 18:34:01 +0200
commitc0386f403a832f50d61fc7bb9bb265f7700273f3 (patch)
tree0c8e33055cd516842f911cd0d9ff243ad497a2e5
parentb564eb0181e678ee58c1be2e9f3c53eeb68f8c4b (diff)
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
-rw-r--r--src/backend/statistics/dependencies.c23
1 files changed, 16 insertions, 7 deletions
diff --git a/src/backend/statistics/dependencies.c b/src/backend/statistics/dependencies.c
index 552843f397c..38eb26f3112 100644
--- a/src/backend/statistics/dependencies.c
+++ b/src/backend/statistics/dependencies.c
@@ -30,6 +30,7 @@
#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"
@@ -332,13 +333,6 @@ dependency_degree(int numrows, HeapTuple *rows, int k, AttrNumber *dependency,
group_size++;
}
- if (items)
- pfree(items);
-
- pfree(mss);
- pfree(attnums);
- pfree(attnums_dep);
-
/* Compute the 'degree of validity' as (supporting/total). */
return (n_supporting_rows * 1.0 / numrows);
}
@@ -370,6 +364,7 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs,
/* result */
MVDependencies *dependencies = NULL;
+ MemoryContext cxt;
/*
* Transform the bms into an array, to make accessing i-th member easier.
@@ -378,6 +373,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
@@ -396,10 +396,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
*/
@@ -441,6 +448,8 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs,
DependencyGenerator_free(DependencyGenerator);
}
+ MemoryContextDelete(cxt);
+
return dependencies;
}