Skip to content

Commit c0386f4

Browse files
committed
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
1 parent b564eb0 commit c0386f4

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/backend/statistics/dependencies.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "utils/fmgroids.h"
3131
#include "utils/fmgrprotos.h"
3232
#include "utils/lsyscache.h"
33+
#include "utils/memutils.h"
3334
#include "utils/selfuncs.h"
3435
#include "utils/syscache.h"
3536
#include "utils/typcache.h"
@@ -332,13 +333,6 @@ dependency_degree(int numrows, HeapTuple *rows, int k, AttrNumber *dependency,
332333
group_size++;
333334
}
334335

335-
if (items)
336-
pfree(items);
337-
338-
pfree(mss);
339-
pfree(attnums);
340-
pfree(attnums_dep);
341-
342336
/* Compute the 'degree of validity' as (supporting/total). */
343337
return (n_supporting_rows * 1.0 / numrows);
344338
}
@@ -370,6 +364,7 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs,
370364

371365
/* result */
372366
MVDependencies *dependencies = NULL;
367+
MemoryContext cxt;
373368

374369
/*
375370
* 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,
378373

379374
Assert(numattrs >= 2);
380375

376+
/* tracks memory allocated by dependency_degree calls */
377+
cxt = AllocSetContextCreate(CurrentMemoryContext,
378+
"dependency_degree cxt",
379+
ALLOCSET_DEFAULT_SIZES);
380+
381381
/*
382382
* We'll try build functional dependencies starting from the smallest ones
383383
* covering just 2 columns, to the largest ones, covering all columns
@@ -396,10 +396,17 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs,
396396
{
397397
double degree;
398398
MVDependency *d;
399+
MemoryContext oldcxt;
400+
401+
/* release memory used by dependency degree calculation */
402+
oldcxt = MemoryContextSwitchTo(cxt);
399403

400404
/* compute how valid the dependency seems */
401405
degree = dependency_degree(numrows, rows, k, dependency, stats, attrs);
402406

407+
MemoryContextSwitchTo(oldcxt);
408+
MemoryContextReset(cxt);
409+
403410
/*
404411
* if the dependency seems entirely invalid, don't store it
405412
*/
@@ -441,6 +448,8 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs,
441448
DependencyGenerator_free(DependencyGenerator);
442449
}
443450

451+
MemoryContextDelete(cxt);
452+
444453
return dependencies;
445454
}
446455

0 commit comments

Comments
 (0)