Skip to content

Commit 748ce30

Browse files
committed
Fix grouped aggregation memory tracking
1 parent 47adf6a commit 748ce30

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

core/trino-main/src/main/java/io/trino/operator/aggregation/builder/InMemoryHashAggregationBuilder.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,17 @@ public long getGroupCount()
228228
@Override
229229
public WorkProcessor<Page> buildResult()
230230
{
231+
groupByHash.startReleasingOutput();
231232
for (GroupedAggregator groupedAggregator : groupedAggregators) {
232233
groupedAggregator.prepareFinal();
233234
}
234-
// Only incrementally release memory for final aggregations, since partial aggregations have a fixed
235-
// memory limit and can be expected to fully flush and release their output quickly
235+
// Always update the current memory usage after calling GroupedAggregator#prepareFinal(), since it can increase
236+
// memory consumption significantly in some situations. This also captures any memory usage reduction the
237+
// groupByHash may have initiated for releasing output
238+
updateMemory();
239+
// Only incrementally release memory while producing output for final aggregations, since partial aggregations
240+
// have a fixed memory limit and can be expected to fully flush and release their output quickly
236241
boolean releaseMemoryOnOutput = !partial;
237-
if (releaseMemoryOnOutput) {
238-
groupByHash.startReleasingOutput();
239-
}
240242
return buildResult(consecutiveGroupIds(), new PageBuilder(buildTypes()), false, releaseMemoryOnOutput);
241243
}
242244

0 commit comments

Comments
 (0)