Skip to content

Commit 4185632

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 16d394c commit 4185632

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/backend/statistics/dependencies.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "utils/fmgroids.h"
3131
#include "utils/fmgrprotos.h"
3232
#include "utils/lsyscache.h"
33+
#include "utils/memutils.h"
34+
#include "utils/selfuncs.h"
3335
#include "utils/syscache.h"
3436
#include "utils/typcache.h"
3537

@@ -326,13 +328,6 @@ dependency_degree(int numrows, HeapTuple *rows, int k, AttrNumber *dependency,
326328
group_size++;
327329
}
328330

329-
if (items)
330-
pfree(items);
331-
332-
pfree(mss);
333-
pfree(attnums);
334-
pfree(attnums_dep);
335-
336331
/* Compute the 'degree of validity' as (supporting/total). */
337332
return (n_supporting_rows * 1.0 / numrows);
338333
}
@@ -364,6 +359,7 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs,
364359

365360
/* result */
366361
MVDependencies *dependencies = NULL;
362+
MemoryContext cxt;
367363

368364
/*
369365
* Transform the bms into an array, to make accessing i-th member easier.
@@ -372,6 +368,11 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs,
372368

373369
Assert(numattrs >= 2);
374370

371+
/* tracks memory allocated by dependency_degree calls */
372+
cxt = AllocSetContextCreate(CurrentMemoryContext,
373+
"dependency_degree cxt",
374+
ALLOCSET_DEFAULT_SIZES);
375+
375376
/*
376377
* We'll try build functional dependencies starting from the smallest ones
377378
* covering just 2 columns, to the largest ones, covering all columns
@@ -390,10 +391,17 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs,
390391
{
391392
double degree;
392393
MVDependency *d;
394+
MemoryContext oldcxt;
395+
396+
/* release memory used by dependency degree calculation */
397+
oldcxt = MemoryContextSwitchTo(cxt);
393398

394399
/* compute how valid the dependency seems */
395400
degree = dependency_degree(numrows, rows, k, dependency, stats, attrs);
396401

402+
MemoryContextSwitchTo(oldcxt);
403+
MemoryContextReset(cxt);
404+
397405
/*
398406
* if the dependency seems entirely invalid, don't store it
399407
*/
@@ -435,6 +443,8 @@ statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs,
435443
DependencyGenerator_free(DependencyGenerator);
436444
}
437445

446+
MemoryContextDelete(cxt);
447+
438448
return dependencies;
439449
}
440450

0 commit comments

Comments
 (0)