Skip to content

Incorrect memory accounting in array_agg function #16517

Closed

Description

@sfluor

Describe the bug

When aggregating with array_agg using a limited memory pool we hit ResourceExhausted errors in the array_agg accumulator.

After investigation this seems to come from over-accounting memory from shared arrow buffers. We might keep a single value from a 8k rows arrow array but will account the memory taken by the whole arrow array rather than just the value we kept.

To Reproduce

Run datafusion-cli with a memory limit datafusion-cli -m 100m

Create a table:

CREATE table logs AS

SELECT 
('Chrome-' || (random() * 1000)::int) as user_agent,
    (n.value % 40000 )::text as client_id,
    (n.value % 21 ==0) as is_internal
FROM
generate_series(1, 300000) n;

Try running a simple array_agg query:

WITH
  user_agents AS (
    SELECT 
    DISTINCT user_agent, client_id
    FROM
     logs
    WHERE
      NOT is_internal
  )
SELECT
  client_id,
  ARRAY_AGG(user_agent) AS user_agent
FROM
  user_agents
GROUP BY
  client_id

Fails with:

Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
  GroupedHashAggregateStream[6] (array_agg(user_agents.user_agent))#44(can spill: true) consumed 2.9 MB,
  GroupedHashAggregateStream[2] (array_agg(user_agents.user_agent))#36(can spill: true) consumed 2.2 MB,
  GroupedHashAggregateStream[3] (array_agg(user_agents.user_agent))#38(can spill: true) consumed 2.2 MB.
Error: Failed to allocate additional 609.0 MB for GroupedHashAggregateStream[0] (array_agg(user_agents.user_agent)) with 0.0 B already allocated for this reservation - 75.3 MB remain available 
for the total pool

Further inspection (adding debug prints) shows that we over-report memory in the array_agg merge_batch function:

Addr:0x418911c18b0 Data type:Utf8 Length: 1, Size of first value: 9, Mem size: 162362
[X][Utf8] 1 Size before: 56 + mem_size:162362 -> 9
[X][Utf8] 1 Size after: 162482

Expected behavior

Query should succeed within the memory limits

Additional context

Similar to: #16055

Activity

  1. sfluor commented on Jun 24, 2025

    @sfluor
    ContributorAuthor

    take

  2. alamb commented on Sep 29, 2025

    @alamb
    Contributor

    A proper GroupsAccumulator for array_agg might fix this:

  3. alamb commented on Jun 4, 2026

    @alamb
    Contributor

    I filed an epic with a bunch of related memory items to try and coordinate some response

  4. alamb commented on Jun 4, 2026

    @alamb
    Contributor

    This query now seems to work

    ndrewlamb@Andrews-MacBook-Pro-3:~/Downloads/apache-datafusion-54.0.0$ datafusion-cli -m 100m
    DataFusion CLI v53.1.0
    > CREATE table logs AS
    
    SELECT
    ('Chrome-' || (random() * 1000)::int) as user_agent,
        (n.value % 40000 )::text as client_id,
        (n.value % 21 ==0) as is_internal
    FROM
    generate_series(1, 300000) n;
    0 row(s) fetched.
    Elapsed 0.016 seconds.
    
    > WITH
      user_agents AS (
        SELECT
        DISTINCT user_agent, client_id
        FROM
         logs
        WHERE
          NOT is_internal
      )
    SELECT
      client_id,
      ARRAY_AGG(user_agent) AS user_agent
    FROM
      user_agents
    GROUP BY
      client_id;
    +-----------+--------------------------------------------------------------------------------------------------+
    | client_id | user_agent                                                                                       |
    +-----------+--------------------------------------------------------------------------------------------------+
    | 2993      | [Chrome-58, Chrome-204, Chrome-562, Chrome-264, Chrome-380, Chrome-462, Chrome-370, Chrome-784]  |
    | 3002      | [Chrome-876, Chrome-514, Chrome-285, Chrome-391, Chrome-46, Chrome-963, Chrome-357]              |
    | 3410      | [Chrome-626, Chrome-653, Chrome-90, Chrome-89, Chrome-283, Chrome-15, Chrome-870, Chrome-326]    |
    | 4023      | [Chrome-94, Chrome-533, Chrome-985, Chrome-720, Chrome-509, Chrome-857, Chrome-242, Chrome-49]   |
    | 4447      | [Chrome-963, Chrome-90, Chrome-255, Chrome-46, Chrome-941, Chrome-500, Chrome-449, Chrome-78]    |
    | 4662      | [Chrome-910, Chrome-432, Chrome-2, Chrome-961, Chrome-758, Chrome-621, Chrome-471]               |
    | 4781      | [Chrome-576, Chrome-668, Chrome-375, Chrome-348, Chrome-458, Chrome-380]                         |
    | 4819      | [Chrome-777, Chrome-767, Chrome-514, Chrome-65, Chrome-869, Chrome-75, Chrome-394]               |
    | 5812      | [Chrome-838, Chrome-762, Chrome-924, Chrome-297, Chrome-140, Chrome-1, Chrome-304, Chrome-584]   |
    | 6143      | [Chrome-731, Chrome-358, Chrome-974, Chrome-102, Chrome-472, Chrome-997, Chrome-766, Chrome-530] |
    | 6280      | [Chrome-974, Chrome-506, Chrome-917, Chrome-569, Chrome-196, Chrome-109, Chrome-213, Chrome-829] |
    | 6833      | [Chrome-854, Chrome-303, Chrome-84, Chrome-728, Chrome-813, Chrome-652, Chrome-258, Chrome-187]  |
    | 6953      | [Chrome-651, Chrome-999, Chrome-618, Chrome-334, Chrome-289, Chrome-551, Chrome-57, Chrome-98]   |
    | 7109      | [Chrome-230, Chrome-847, Chrome-329, Chrome-299, Chrome-317, Chrome-718, Chrome-880, Chrome-791] |
    | 7123      | [Chrome-542, Chrome-577, Chrome-875, Chrome-129, Chrome-294, Chrome-31, Chrome-984]              |
    | 7310      | [Chrome-461, Chrome-608, Chrome-754, Chrome-491, Chrome-245, Chrome-844, Chrome-533, Chrome-997] |
    | 7338      | [Chrome-145, Chrome-984, Chrome-887, Chrome-122, Chrome-232, Chrome-693, Chrome-311]             |
    | 7452      | [Chrome-515, Chrome-811, Chrome-216, Chrome-806, Chrome-675, Chrome-821, Chrome-426, Chrome-286] |
    | 7719      | [Chrome-150, Chrome-179, Chrome-125, Chrome-573, Chrome-314, Chrome-444, Chrome-566]             |
    | 8535      | [Chrome-685, Chrome-827, Chrome-11, Chrome-853, Chrome-738, Chrome-232, Chrome-111]              |
    | 8725      | [Chrome-654, Chrome-45, Chrome-711, Chrome-213, Chrome-594, Chrome-256, Chrome-993]              |
    | 8909      | [Chrome-203, Chrome-86, Chrome-410, Chrome-48, Chrome-804, Chrome-406, Chrome-330]               |
    | 9014      | [Chrome-630, Chrome-187, Chrome-459, Chrome-784, Chrome-690, Chrome-215, Chrome-382]             |
    | 9322      | [Chrome-920, Chrome-448, Chrome-203, Chrome-117, Chrome-314, Chrome-151, Chrome-389, Chrome-542] |
    | 9917      | [Chrome-82, Chrome-839, Chrome-45, Chrome-807, Chrome-412, Chrome-933, Chrome-323]               |
    | 10017     | [Chrome-73, Chrome-981, Chrome-219, Chrome-41, Chrome-387, Chrome-813, Chrome-8]                 |
    | 10239     | [Chrome-756, Chrome-272, Chrome-392, Chrome-104, Chrome-833, Chrome-625, Chrome-449, Chrome-348] |
    | 10328     | [Chrome-411, Chrome-377, Chrome-425, Chrome-298, Chrome-398, Chrome-141, Chrome-84, Chrome-795]  |
    | 10460     | [Chrome-579, Chrome-111, Chrome-612, Chrome-754, Chrome-129, Chrome-510, Chrome-704, Chrome-464] |
    | 10553     | [Chrome-469, Chrome-377, Chrome-907, Chrome-215, Chrome-843, Chrome-716, Chrome-629, Chrome-445] |
    | 10621     | [Chrome-666, Chrome-655, Chrome-148, Chrome-367, Chrome-986, Chrome-800, Chrome-222, Chrome-338] |
    | 10649     | [Chrome-604, Chrome-94, Chrome-164, Chrome-905, Chrome-611, Chrome-491, Chrome-527, Chrome-430]  |
    | 10936     | [Chrome-816, Chrome-647, Chrome-409, Chrome-354, Chrome-104, Chrome-842, Chrome-233, Chrome-840] |
    | 14035     | [Chrome-303, Chrome-979, Chrome-838, Chrome-254, Chrome-503, Chrome-343, Chrome-788, Chrome-627] |
    | 14589     | [Chrome-137, Chrome-175, Chrome-416, Chrome-436, Chrome-857, Chrome-203, Chrome-118]             |
    | 14643     | [Chrome-354, Chrome-712, Chrome-150, Chrome-43, Chrome-597, Chrome-628, Chrome-515, Chrome-238]  |
    | 15184     | [Chrome-746, Chrome-307, Chrome-941, Chrome-171, Chrome-808, Chrome-238, Chrome-352, Chrome-61]  |
    | 15310     | [Chrome-419, Chrome-907, Chrome-837, Chrome-434, Chrome-57, Chrome-457, Chrome-50, Chrome-718]   |
    | 15748     | [Chrome-77, Chrome-926, Chrome-41, Chrome-83, Chrome-330, Chrome-171, Chrome-999, Chrome-92]     |
    | 16181     | [Chrome-777, Chrome-496, Chrome-972, Chrome-63, Chrome-718, Chrome-650, Chrome-333, Chrome-880]  |
    | .                                                                                                            |
    | .                                                                                                            |
    | .                                                                                                            |
    +-----------+--------------------------------------------------------------------------------------------------+
    40000 row(s) fetched. (First 40 displayed. Use --maxrows to adjust)

    Likely thanks to @neilconway in #20504

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions