Skip to content

Commit 7fddd5a

Browse files
committed
Bugfix. AQOUtilityMemCtx is reset although some allocated data still in use.
Remove the AQOUtilityMemCtx memory context at all. It is used for too small operations. I don't buy that such operations can allocate so much memory that backend must free memory right after the end of operation to avoid OOM. I guess, prediction, planning and execution memory context set is good enough.
1 parent 565b170 commit 7fddd5a

File tree

6 files changed

+8
-41
lines changed

6 files changed

+8
-41
lines changed

aqo.c

+1-12
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ MemoryContext AQOTopMemCtx = NULL;
8787
/* Is released at the end of transaction */
8888
MemoryContext AQOCacheMemCtx = NULL;
8989

90-
/* Should be released in-place, just after a huge calculation */
91-
MemoryContext AQOUtilityMemCtx = NULL;
92-
9390
/* Is released at the end of planning */
9491
MemoryContext AQOPredictMemCtx = NULL;
9592

@@ -348,15 +345,7 @@ _PG_init(void)
348345
AQOCacheMemCtx = AllocSetContextCreate(AQOTopMemCtx,
349346
"AQOCacheMemCtx",
350347
ALLOCSET_DEFAULT_SIZES);
351-
/*
352-
* AQOUtilityMemoryContext containe short-lived information which
353-
* is appeared from having got clause, selectivity arrays and relid lists
354-
* while calculating hashes. It clean up inside calculated
355-
* function or immediately after her having completed.
356-
*/
357-
AQOUtilityMemCtx = AllocSetContextCreate(AQOTopMemCtx,
358-
"AQOUtilityMemoryContext",
359-
ALLOCSET_DEFAULT_SIZES);
348+
360349
/*
361350
* AQOPredictMemoryContext save necessary information for making predict of plan nodes
362351
* and clean up in the execution stage of query.

aqo.h

-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ extern int njoins;
225225
/* AQO Memory contexts */
226226
extern MemoryContext AQOTopMemCtx;
227227
extern MemoryContext AQOCacheMemCtx;
228-
extern MemoryContext AQOUtilityMemCtx;
229228
extern MemoryContext AQOPredictMemCtx;
230229
extern MemoryContext AQOLearnMemCtx;
231230

hash.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,20 @@ get_fss_for_object(List *relsigns, List *clauselist,
194194
int sh = 0,
195195
old_sh;
196196
int fss_hash;
197-
MemoryContext old_ctx_m;
198197

199198
n = list_length(clauselist);
200199

201200
/* Check parameters state invariant. */
202201
Assert(n == list_length(selectivities) ||
203202
(nfeatures == NULL && features == NULL));
204203

204+
/*
205+
* It should be allocated in a caller memory context, because it will be
206+
* returned.
207+
*/
205208
if (nfeatures != NULL)
206209
*features = palloc0(sizeof(**features) * n);
207210

208-
old_ctx_m = MemoryContextSwitchTo(AQOUtilityMemCtx);
209-
210211
get_eclasses(clauselist, &nargs, &args_hash, &eclass_hash);
211212
clause_hashes = palloc(sizeof(*clause_hashes) * n);
212213
clause_has_consts = palloc(sizeof(*clause_has_consts) * n);
@@ -281,9 +282,6 @@ get_fss_for_object(List *relsigns, List *clauselist,
281282
relations_hash = get_relations_hash(relsigns);
282283
fss_hash = get_fss_hash(clauses_hash, eclasses_hash, relations_hash);
283284

284-
MemoryContextSwitchTo(old_ctx_m);
285-
MemoryContextReset(AQOUtilityMemCtx);
286-
287285
if (nfeatures != NULL)
288286
{
289287
*nfeatures = n - sh;

postprocessing.c

-8
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,13 @@ restore_selectivities(List *clauselist, List *relidslist, JoinType join_type,
178178
double *cur_sel;
179179
int cur_hash;
180180
int cur_relid;
181-
MemoryContext old_ctx_m;
182181

183182
parametrized_sel = was_parametrized && (list_length(relidslist) == 1);
184183
if (parametrized_sel)
185184
{
186185
cur_relid = linitial_int(relidslist);
187186

188-
old_ctx_m = MemoryContextSwitchTo(AQOUtilityMemCtx);
189187
get_eclasses(clauselist, &nargs, &args_hash, &eclass_hash);
190-
MemoryContextSwitchTo(old_ctx_m);
191188
}
192189

193190
foreach(l, clauselist)
@@ -221,11 +218,6 @@ restore_selectivities(List *clauselist, List *relidslist, JoinType join_type,
221218
lst = lappend(lst, cur_sel);
222219
}
223220

224-
if (parametrized_sel)
225-
{
226-
MemoryContextReset(AQOUtilityMemCtx);
227-
}
228-
229221
return lst;
230222
}
231223

preprocessing.c

+3-10
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ aqo_planner(Query *parse,
127127
ParamListInfo boundParams)
128128
{
129129
bool query_is_stored = false;
130-
MemoryContext oldctx;
130+
MemoryContext oldctx;
131+
131132
oldctx = MemoryContextSwitchTo(AQOPredictMemCtx);
132133

133134
/*
@@ -156,21 +157,14 @@ aqo_planner(Query *parse,
156157
}
157158

158159
selectivity_cache_clear();
159-
MemoryContextSwitchTo(oldctx);
160160

161-
oldctx = MemoryContextSwitchTo(AQOUtilityMemCtx);
162161
/* Check unlucky case (get a hash of zero) */
163162
if (parse->queryId == UINT64CONST(0))
164163
JumbleQuery(parse, query_string);
165164

166165
Assert(parse->utilityStmt == NULL);
167166
Assert(parse->queryId != UINT64CONST(0));
168167
query_context.query_hash = parse->queryId;
169-
MemoryContextSwitchTo(oldctx);
170-
171-
MemoryContextReset(AQOUtilityMemCtx);
172-
173-
oldctx = MemoryContextSwitchTo(AQOPredictMemCtx);
174168

175169
/* By default, they should be equal */
176170
query_context.fspace_hash = query_context.query_hash;
@@ -191,15 +185,14 @@ aqo_planner(Query *parse,
191185
cursorOptions,
192186
boundParams);
193187
}
194-
MemoryContextSwitchTo(oldctx);
195188

196189
elog(DEBUG1, "AQO will be used for query '%s', class "UINT64_FORMAT,
197190
query_string ? query_string : "null string", query_context.query_hash);
198191

192+
MemoryContextSwitchTo(oldctx);
199193
oldctx = MemoryContextSwitchTo(AQOCacheMemCtx);
200194
cur_classes = lappend_uint64(cur_classes, query_context.query_hash);
201195
MemoryContextSwitchTo(oldctx);
202-
203196
oldctx = MemoryContextSwitchTo(AQOPredictMemCtx);
204197

205198
if (aqo_mode == AQO_MODE_DISABLED)

storage.c

-4
Original file line numberDiff line numberDiff line change
@@ -2096,7 +2096,6 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
20962096
for(i = 0; i < dentry->nrels; i++)
20972097
{
20982098
Oid reloid = ObjectIdGetDatum(*(Oid *)ptr);
2099-
MemoryContext oldctx = MemoryContextSwitchTo(AQOUtilityMemCtx);
21002099

21012100
if (!SearchSysCacheExists1(RELOID, reloid))
21022101
/* Remember this value */
@@ -2105,7 +2104,6 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
21052104
else
21062105
actual_fss = list_append_unique_int(actual_fss,
21072106
dentry->key.fss);
2108-
MemoryContextSwitchTo(oldctx);
21092107

21102108
ptr += sizeof(Oid);
21112109
}
@@ -2155,8 +2153,6 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
21552153
/* Query class preferences */
21562154
(*fs_num) += (int) _aqo_queries_remove(entry->queryid);
21572155
}
2158-
2159-
MemoryContextReset(AQOUtilityMemCtx);
21602156
}
21612157

21622158
/*

0 commit comments

Comments
 (0)