@@ -144,20 +144,21 @@ aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
144
144
List * selectivities = NULL ;
145
145
List * clauses ;
146
146
int fss = 0 ;
147
+ MemoryContext old_ctx_m ;
147
148
148
149
if (IsQueryDisabled ())
149
150
/* Fast path. */
150
151
goto default_estimator ;
151
152
153
+ old_ctx_m = MemoryContextSwitchTo (AQOPredictMemCtx );
154
+
152
155
if (query_context .use_aqo || query_context .learn_aqo )
153
156
selectivities = get_selectivities (root , rel -> baserestrictinfo , 0 ,
154
157
JOIN_INNER , NULL );
155
158
156
159
if (!query_context .use_aqo )
157
160
{
158
- if (query_context .learn_aqo )
159
- list_free_deep (selectivities );
160
-
161
+ MemoryContextSwitchTo (old_ctx_m );
161
162
goto default_estimator ;
162
163
}
163
164
@@ -175,10 +176,8 @@ aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
175
176
& fss );
176
177
rel -> fss_hash = fss ;
177
178
178
- list_free (rels .hrels );
179
- list_free (rels .signatures );
180
- list_free_deep (selectivities );
181
- list_free (clauses );
179
+ /* Return to the caller's memory context. */
180
+ MemoryContextSwitchTo (old_ctx_m );
182
181
183
182
if (predicted >= 0 )
184
183
{
@@ -225,14 +224,16 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
225
224
int * eclass_hash ;
226
225
int current_hash ;
227
226
int fss = 0 ;
227
+ MemoryContext oldctx ;
228
228
229
229
if (IsQueryDisabled ())
230
230
/* Fast path */
231
231
goto default_estimator ;
232
232
233
+ oldctx = MemoryContextSwitchTo (AQOPredictMemCtx );
234
+
233
235
if (query_context .use_aqo || query_context .learn_aqo )
234
236
{
235
- MemoryContext old_ctx_m ;
236
237
237
238
selectivities = list_concat (
238
239
get_selectivities (root , param_clauses , rel -> relid ,
@@ -248,8 +249,6 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
248
249
rte = planner_rt_fetch (rel -> relid , root );
249
250
get_eclasses (allclauses , & nargs , & args_hash , & eclass_hash );
250
251
251
- old_ctx_m = MemoryContextSwitchTo (AQO_cache_mem_ctx );
252
-
253
252
forboth (l , allclauses , l2 , selectivities )
254
253
{
255
254
current_hash = get_clause_hash (
@@ -258,19 +257,11 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
258
257
cache_selectivity (current_hash , rel -> relid , rte -> relid ,
259
258
* ((double * ) lfirst (l2 )));
260
259
}
261
-
262
- MemoryContextSwitchTo (old_ctx_m );
263
- pfree (args_hash );
264
- pfree (eclass_hash );
265
260
}
266
261
267
262
if (!query_context .use_aqo )
268
263
{
269
- if (query_context .learn_aqo )
270
- {
271
- list_free_deep (selectivities );
272
- list_free (allclauses );
273
- }
264
+ MemoryContextSwitchTo (oldctx );
274
265
275
266
goto default_estimator ;
276
267
}
@@ -284,8 +275,9 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
284
275
}
285
276
286
277
predicted = predict_for_relation (allclauses , selectivities , rels .signatures , & fss );
287
- list_free (rels .hrels );
288
- list_free (rels .signatures );
278
+
279
+ /* Return to the caller's memory context */
280
+ MemoryContextSwitchTo (oldctx );
289
281
290
282
predicted_ppi_rows = predicted ;
291
283
fss_ppi_hash = fss ;
@@ -319,20 +311,20 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
319
311
List * outer_selectivities ;
320
312
List * current_selectivities = NULL ;
321
313
int fss = 0 ;
314
+ MemoryContext old_ctx_m ;
322
315
323
316
if (IsQueryDisabled ())
324
317
/* Fast path */
325
318
goto default_estimator ;
326
319
320
+ old_ctx_m = MemoryContextSwitchTo (AQOPredictMemCtx );
321
+
327
322
if (query_context .use_aqo || query_context .learn_aqo )
328
323
current_selectivities = get_selectivities (root , restrictlist , 0 ,
329
324
sjinfo -> jointype , sjinfo );
330
-
331
325
if (!query_context .use_aqo )
332
326
{
333
- if (query_context .learn_aqo )
334
- list_free_deep (current_selectivities );
335
-
327
+ MemoryContextSwitchTo (old_ctx_m );
336
328
goto default_estimator ;
337
329
}
338
330
@@ -349,8 +341,9 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
349
341
350
342
predicted = predict_for_relation (allclauses , selectivities , rels .signatures ,
351
343
& fss );
352
- list_free (rels .hrels );
353
- list_free (rels .signatures );
344
+
345
+ /* Return to the caller's memory context */
346
+ MemoryContextSwitchTo (old_ctx_m );
354
347
355
348
rel -> fss_hash = fss ;
356
349
@@ -391,20 +384,21 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
391
384
List * outer_selectivities ;
392
385
List * current_selectivities = NULL ;
393
386
int fss = 0 ;
387
+ MemoryContext old_ctx_m ;
394
388
395
389
if (IsQueryDisabled ())
396
390
/* Fast path */
397
391
goto default_estimator ;
398
392
393
+ old_ctx_m = MemoryContextSwitchTo (AQOPredictMemCtx );
394
+
399
395
if (query_context .use_aqo || query_context .learn_aqo )
400
396
current_selectivities = get_selectivities (root , clauses , 0 ,
401
397
sjinfo -> jointype , sjinfo );
402
398
403
399
if (!query_context .use_aqo )
404
400
{
405
- if (query_context .learn_aqo )
406
- list_free_deep (current_selectivities );
407
-
401
+ MemoryContextSwitchTo (old_ctx_m );
408
402
goto default_estimator ;
409
403
}
410
404
@@ -419,8 +413,8 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
419
413
420
414
predicted = predict_for_relation (allclauses , selectivities , rels .signatures ,
421
415
& fss );
422
- list_free ( rels . hrels );
423
- list_free ( rels . signatures );
416
+ /* Return to the caller's memory context */
417
+ MemoryContextSwitchTo ( old_ctx_m );
424
418
425
419
predicted_ppi_rows = predicted ;
426
420
fss_ppi_hash = fss ;
@@ -455,8 +449,6 @@ predict_num_groups(PlannerInfo *root, Path *subpath, List *group_exprs,
455
449
clauses = get_path_clauses (subpath , root , & selectivities );
456
450
(void ) predict_for_relation (clauses , selectivities , rels .signatures ,
457
451
& child_fss );
458
- list_free (rels .hrels );
459
- list_free (rels .signatures );
460
452
}
461
453
462
454
* fss = get_grouped_exprs_hash (child_fss , group_exprs );
@@ -477,6 +469,7 @@ aqo_estimate_num_groups_hook(PlannerInfo *root, List *groupExprs,
477
469
{
478
470
int fss ;
479
471
double predicted ;
472
+ MemoryContext old_ctx_m ;
480
473
481
474
if (!query_context .use_aqo )
482
475
goto default_estimator ;
@@ -495,12 +488,15 @@ aqo_estimate_num_groups_hook(PlannerInfo *root, List *groupExprs,
495
488
if (groupExprs == NIL )
496
489
return 1.0 ;
497
490
491
+ old_ctx_m = MemoryContextSwitchTo (AQOPredictMemCtx );
492
+
498
493
predicted = predict_num_groups (root , subpath , groupExprs , & fss );
499
494
if (predicted > 0. )
500
495
{
501
496
grouped_rel -> predicted_cardinality = predicted ;
502
497
grouped_rel -> rows = predicted ;
503
498
grouped_rel -> fss_hash = fss ;
499
+ MemoryContextSwitchTo (old_ctx_m );
504
500
return predicted ;
505
501
}
506
502
else
@@ -510,6 +506,8 @@ aqo_estimate_num_groups_hook(PlannerInfo *root, List *groupExprs,
510
506
*/
511
507
grouped_rel -> predicted_cardinality = -1 ;
512
508
509
+ MemoryContextSwitchTo (old_ctx_m );
510
+
513
511
default_estimator :
514
512
return default_estimate_num_groups (root , groupExprs , subpath , grouped_rel ,
515
513
pgset , estinfo );
0 commit comments