Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w8k82-000pn8-0U for pgsql-performance@arkaria.postgresql.org; Fri, 03 Apr 2026 19:24:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w8k80-00DWFp-1s for pgsql-performance@arkaria.postgresql.org; Fri, 03 Apr 2026 19:24:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w8k80-00DWFh-0t for pgsql-performance@lists.postgresql.org; Fri, 03 Apr 2026 19:24:24 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w8k7v-00000000QLq-0PHo for pgsql-performance@lists.postgresql.org; Fri, 03 Apr 2026 19:24:24 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 633JOFGq3825076; Fri, 3 Apr 2026 15:24:16 -0400 From: Tom Lane To: Scott Carey cc: Jeff Davis , David Rowley , pgsql-performance@lists.postgresql.org Subject: Re: Significant performance issues with array_agg() + HashAggregate plans on Postgres 17 In-reply-to: <3671092.1775167393@sss.pgh.pa.us> References: <2764544.1774985169@sss.pgh.pa.us> <2897039.1775051081@sss.pgh.pa.us> <3579239.1775151498@sss.pgh.pa.us> <3671092.1775167393@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 02 Apr 2026 18:03:13 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3825074.1775244255.1@sss.pgh.pa.us> Date: Fri, 03 Apr 2026 15:24:15 -0400 Message-ID: <3825075.1775244255@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I wrote: > So the main problem here is we're leaking the arrays made by > array_agg, and a secondary problem is that that drives the > cost of hash_agg_check_limits to an unacceptable level. After further study I no longer think there's a leak. This test query involves 50000 GROUP BY groups, and we have an array being accumulated for each one. The difference between the fast array_agg implementation and the slow one is just that the fast one keeps all its working state in the aggregate context, while the slow one makes a separate sub-context for each "expanded array". v12 creates 50000 "expanded arrays" too, but it's not noticeably slow. So the problem is exactly that repeating hash_agg_check_limits each time we start a new group is O(N^2), because if there is a sub-context for each group then MemoryContextMemAllocated requires O(N) time. The other little problem with this approach is acknowledged in the comments: * ... Allocations may happen without adding new groups (for instance, * if the transition state size grows), so this check is imperfect. So really the whole thing is kind of unsatisfactory. I'm not seeing cheap ways to make it better though. A very quick and dirty idea is to tell MemoryContextMemAllocated not to recurse, which would restore it to constant-time. But that would make the results much less accurate in cases like this one. regards, tom lane