Skip to content

Commit 2a2f858

Browse files
committed
Significantly reduce memory usage in AggregatingInOrderTransform
Clean the aggregates pools (Arena's objects) between flushes, this will reduce memory usage significantly (since Arena is not intended for memory reuse in the already full Chunk's) Before this patch you cannot run SELECT FROM huge_table GROUP BY primary_key SETTINGS optimize_aggregation_in_order=1 (and the whole point of optimize_aggregation_in_order got lost), while after, this should be possible.
1 parent d9ac79c commit 2a2f858

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Processors/Transforms/AggregatingInOrderTransform.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
namespace DB
55
{
66

7+
namespace ErrorCodes
8+
{
9+
extern const int LOGICAL_ERROR;
10+
}
11+
12+
713
AggregatingInOrderTransform::AggregatingInOrderTransform(
814
Block header, AggregatingTransformParamsPtr params_,
915
const SortDescription & group_by_description_, size_t res_block_size_)
@@ -140,6 +146,24 @@ void AggregatingInOrderTransform::consume(Chunk chunk)
140146
block_end_reached = true;
141147
need_generate = true;
142148
cur_block_size = 0;
149+
150+
/// Arenas cannot be destroyed here, since later, in FinalizingSimpleTransform
151+
/// there will be finalizeChunk(), but even after
152+
/// finalizeChunk() we cannot destroy arena, since some memory
153+
/// from Arena still in use, so we attach it to the Chunk to
154+
/// remove it once it will be consumed.
155+
if (params->final)
156+
{
157+
if (variants.aggregates_pools.size() != 1)
158+
throw Exception("Too much arenas", ErrorCodes::LOGICAL_ERROR);
159+
160+
Arenas arenas(1, std::make_shared<Arena>());
161+
std::swap(variants.aggregates_pools, arenas);
162+
variants.aggregates_pool = variants.aggregates_pools.at(0).get();
163+
164+
chunk.setChunkInfo(std::make_shared<AggregatedArenasChunkInfo>(std::move(arenas)));
165+
}
166+
143167
return;
144168
}
145169

src/Processors/Transforms/AggregatingTransform.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
namespace DB
99
{
1010

11+
class AggregatedArenasChunkInfo : public ChunkInfo
12+
{
13+
public:
14+
Arenas arenas;
15+
AggregatedArenasChunkInfo(Arenas arenas_)
16+
: arenas(std::move(arenas_))
17+
{}
18+
};
19+
1120
class AggregatedChunkInfo : public ChunkInfo
1221
{
1322
public:

0 commit comments

Comments
 (0)