docs/design/2021-06-23-spilled-unparallel-hashagg.md
This proposal describes the implementation of Unparalleled HashAgg that can spill intermediate data when memory usage is higher than memory quota. Spilling for paralleled HashAgg will be supported later.
Currently, the calculation logic of the aggregate executors in the TiDB is divided into two types, parallel and unparallel. However, when SQL memory usage exceeds the memory quota, neither implementation can use external memory to control memory usage and can only kill the SQL that is executing. In order to enable SQL to execute normally in the case of insufficient memory, we introduce the spilling algorithm for the unparallel aggregate executor.
In aggregate processing, memory increases when tuples are inserted into the hash table. So we can use the following algorithm to control the memory increasing: