Skip to content

Commit ad8a166

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 83772cc commit ad8a166

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/backend/statistics/dependencies.c

Lines changed: 16 additions & 6 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"
@@ -325,12 +326,6 @@ dependency_degree(StatsBuildData *data, int k, AttrNumber *dependency)
325326
group_size++;
326327
}
327328

328-
if (items)
329-
pfree(items);
330-
331-
pfree(mss);
332-
pfree(attnums_dep);
333-
334329
/* Compute the 'degree of validity' as (supporting/total). */
335330
return (n_supporting_rows * 1.0 / data->numrows);
336331
}
@@ -359,9 +354,15 @@ statext_dependencies_build(StatsBuildData *data)
359354

360355
/* result */
361356
MVDependencies *dependencies = NULL;
357+
MemoryContext cxt;
362358

363359
Assert(data->nattnums >= 2);
364360

361+
/* tracks memory allocated by dependency_degree calls */
362+
cxt = AllocSetContextCreate(CurrentMemoryContext,
363+
"dependency_degree cxt",
364+
ALLOCSET_DEFAULT_SIZES);
365+
365366
/*
366367
* We'll try build functional dependencies starting from the smallest ones
367368
* covering just 2 columns, to the largest ones, covering all columns
@@ -380,10 +381,17 @@ statext_dependencies_build(StatsBuildData *data)
380381
{
381382
double degree;
382383
MVDependency *d;
384+
MemoryContext oldcxt;
385+
386+
/* release memory used by dependency degree calculation */
387+
oldcxt = MemoryContextSwitchTo(cxt);
383388

384389
/* compute how valid the dependency seems */
385390
degree = dependency_degree(data, k, dependency);
386391

392+
MemoryContextSwitchTo(oldcxt);
393+
MemoryContextReset(cxt);
394+
387395
/*
388396
* if the dependency seems entirely invalid, don't store it
389397
*/
@@ -425,6 +433,8 @@ statext_dependencies_build(StatsBuildData *data)
425433
DependencyGenerator_free(DependencyGenerator);
426434
}
427435

436+
MemoryContextDelete(cxt);
437+
428438
return dependencies;
429439
}
430440

0 commit comments

Comments
 (0)