1818 *
1919 *******************************************************************************
2020 *
21- * Copyright (c) 2016-2022 , Postgres Professional
21+ * Copyright (c) 2016-2023 , Postgres Professional
2222 *
2323 * IDENTIFICATION
2424 * aqo/cardinality_hooks.c
@@ -81,6 +81,7 @@ aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
8181if (!query_context .use_aqo )
8282{
8383MemoryContextSwitchTo (old_ctx_m );
84+ MemoryContextReset (AQOPredictMemCtx );
8485gotodefault_estimator ;
8586}
8687
@@ -99,6 +100,7 @@ aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
99100
100101/* Return to the caller's memory context. */
101102MemoryContextSwitchTo (old_ctx_m );
103+ MemoryContextReset (AQOPredictMemCtx );
102104
103105if (predicted < 0 )
104106gotodefault_estimator ;
@@ -190,12 +192,15 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
190192cache_selectivity (current_hash ,rel -> relid ,rte -> relid ,
191193* ((double * )lfirst (l2 )));
192194}
195+
196+ pfree (args_hash );
197+ pfree (eclass_hash );
193198}
194199
195200if (!query_context .use_aqo )
196201{
197202MemoryContextSwitchTo (oldctx );
198-
203+ MemoryContextReset ( AQOPredictMemCtx );
199204gotodefault_estimator ;
200205}
201206
@@ -210,6 +215,7 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
210215
211216/* Return to the caller's memory context */
212217MemoryContextSwitchTo (oldctx );
218+ MemoryContextReset (AQOPredictMemCtx );
213219
214220predicted_ppi_rows = predicted ;
215221fss_ppi_hash = fss ;
@@ -264,6 +270,7 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
264270if (!query_context .use_aqo )
265271{
266272MemoryContextSwitchTo (old_ctx_m );
273+ MemoryContextReset (AQOPredictMemCtx );
267274gotodefault_estimator ;
268275}
269276
@@ -283,6 +290,7 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
283290
284291/* Return to the caller's memory context */
285292MemoryContextSwitchTo (old_ctx_m );
293+ MemoryContextReset (AQOPredictMemCtx );
286294
287295rel -> fss_hash = fss ;
288296
@@ -342,6 +350,7 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
342350if (!query_context .use_aqo )
343351{
344352MemoryContextSwitchTo (old_ctx_m );
353+ MemoryContextReset (AQOPredictMemCtx );
345354gotodefault_estimator ;
346355}
347356
@@ -358,6 +367,7 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
358367& fss );
359368/* Return to the caller's memory context */
360369MemoryContextSwitchTo (old_ctx_m );
370+ MemoryContextReset (AQOPredictMemCtx );
361371
362372predicted_ppi_rows = predicted ;
363373fss_ppi_hash = fss ;
@@ -445,6 +455,7 @@ aqo_estimate_num_groups(PlannerInfo *root, List *groupExprs,
445455grouped_rel -> rows = predicted ;
446456grouped_rel -> fss_hash = fss ;
447457MemoryContextSwitchTo (old_ctx_m );
458+ MemoryContextReset (AQOPredictMemCtx );
448459return predicted ;
449460}
450461else
@@ -455,6 +466,7 @@ aqo_estimate_num_groups(PlannerInfo *root, List *groupExprs,
455466grouped_rel -> predicted_cardinality = -1 ;
456467
457468MemoryContextSwitchTo (old_ctx_m );
469+ MemoryContextReset (AQOPredictMemCtx );
458470
459471default_estimator :
460472if (aqo_estimate_num_groups_next )