git.postgresql.org Git - postgresql.git/commitdiff

git projects / postgresql.git / commitdiff
? search:
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c7aeb77)
Free memory after building each statistics object
2021年9月20日 23:14:11 +0000 (01:14 +0200)
2021年9月23日 16:05:10 +0000 (18:05 +0200)
Until now, all extended statistics on a given relation were built in the
same memory context, without resetting. Some of the memory was released
explicitly, but not all of it - for example memory allocated while
detoasting values is hard to free. This is how it worked since extended
statistics were introduced in PostgreSQL 10, but adding support for
extended stats on expressions made the issue somewhat worse as it
increases the number of statistics to build.

Fixed by adding a memory context which gets reset after building each
statistics object (all the statistics kinds included in it). Resetting
it after building each statistics kind would be even better, but it
would require more invasive changes and copying of results, making it
harder to backpatch.

Backpatch to PostgreSQL 10, where extended statistics were introduced.

Author: Justin Pryzby
Reported-by: Justin Pryzby
Reviewed-by: Tomas Vondra
Backpatch-through: 10
Discussion: https://www.postgresql.org/message-id/20210915200928.GP831%40telsasoft.com


diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c
index 2e55913bc8f39cb4c7b441f6cdc3377a3218cc8b..5fa36e0036e4b559e3b990f1096107bda2893c1a 100644 (file)
--- a/src/backend/statistics/extended_stats.c
+++ b/src/backend/statistics/extended_stats.c
@@ -125,14 +125,16 @@ BuildRelationExtStatistics(Relation onerel, double totalrows,
if (!natts)
return;
+ /* the list of stats has to be allocated outside the memory context */
+ pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
+ statslist = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
+
+ /* memory context for building each statistics object */
cxt = AllocSetContextCreate(CurrentMemoryContext,
"BuildRelationExtStatistics",
ALLOCSET_DEFAULT_SIZES);
oldcxt = MemoryContextSwitchTo(cxt);
- pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
- statslist = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
-
/* report this phase */
if (statslist != NIL)
{
@@ -234,14 +236,16 @@ BuildRelationExtStatistics(Relation onerel, double totalrows,
pgstat_progress_update_param(PROGRESS_ANALYZE_EXT_STATS_COMPUTED,
++ext_cnt);
- /* free the build data (allocated as a single chunk) */
- pfree(data);
+ /* free the data used for building this statistics object */
+ MemoryContextReset(cxt);
}
- table_close(pg_stext, RowExclusiveLock);
-
MemoryContextSwitchTo(oldcxt);
MemoryContextDelete(cxt);
+
+ list_free(statslist);
+
+ table_close(pg_stext, RowExclusiveLock);
}
/*
This is the main PostgreSQL git repository.
RSS Atom

AltStyle によって変換されたページ (->オリジナル) /