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 1w8Lzx-000RwZ-0U for pgsql-performance@arkaria.postgresql.org; Thu, 02 Apr 2026 17:38:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w8Lzv-007CFT-1V for pgsql-performance@arkaria.postgresql.org; Thu, 02 Apr 2026 17:38:27 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w8Lzv-007CFL-0X for pgsql-performance@lists.postgresql.org; Thu, 02 Apr 2026 17:38:27 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w8Lzt-00000000Dm5-2NKl for pgsql-performance@lists.postgresql.org; Thu, 02 Apr 2026 17:38:26 +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 632HcI6Q3579240; Thu, 2 Apr 2026 13:38:18 -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: References: <2764544.1774985169@sss.pgh.pa.us> <2897039.1775051081@sss.pgh.pa.us> Comments: In-reply-to Scott Carey message dated "Thu, 02 Apr 2026 00:23:43 -0700" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3579197.1775151468.0@sss.pgh.pa.us> Date: Thu, 02 Apr 2026 13:38:18 -0400 Message-ID: <3579239.1775151498@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3579197.1775151468.1@sss.pgh.pa.us> Scott Carey writes: > I have discovered the root cause. > This database is old. It pre-dates Postgres 8.4 which introduced > array_agg. Apparently, in some version prior to 8.4 array_agg was added > as a user function, defined as below for bigint: > create AGGREGATE array_agg( > BASETYPE = bigint, > SFUNC = array_append, > STYPE = bigint[], > INITCOND = '{}' > ); > So if you create a test database and run the previous test, performance > will be fine and the query will be fast. Then run: > create AGGREGATE array_agg(BASETYPE = bigint, SFUNC = array_append,STYPE = > bigint[], INITCOND = '{}'); > It will be slow and reproduce this behavior. Thank you for running that to ground! I confirm your results that v13 and up are far slower for this example than v12 was. > Why would this run so much more slowly after updating from postgres 12 to > 17? It is a user defined aggregate, although maybe not as optimized as > the intrinsic one it shouldn't behave this way. I did some bisecting using the attached simplified test case, and found that the query execution time jumps from circa 60ms to circa 7500ms here: 1f39bce021540fde00990af55b4432c55ef4b3c7 is the first bad commit commit 1f39bce021540fde00990af55b4432c55ef4b3c7 Author: Jeff Davis Date: Wed Mar 18 15:42:02 2020 -0700 Disk-based Hash Aggregation. While performing hash aggregation, track memory usage when adding new groups to a hash table. If the memory usage exceeds work_mem, enter "spill mode". (Times quoted are on a Mac M4 Pro, but in assert-enabled builds so maybe not directly comparable to production.) I'm bemused as to why: the test case has work_mem set high enough that we shouldn't be triggering spill mode, so why did this change affect it at all? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/plain; name="hashaggperformance.sql"; charset="us-ascii" Content-ID: <3579197.1775151468.2@sss.pgh.pa.us> Content-Description: hashaggperformance.sql CREATE AGGREGATE array_agg( BASETYPE = bigint, SFUNC = array_append, STYPE = bigint[], INITCOND = '{}' ); drop table if exists array_agg_test; create table array_agg_test(product_id bigint not null, region_id bigint not null, available boolean not null); insert into array_agg_test (product_id, region_id, available) SELECT generate_series(1, 50000) as product_id, (ARRAY[1,2,3,4])[floor(random()*4)+1] as region_id, true as available; insert into array_agg_test (product_id, region_id, available) SELECT generate_series(1, 50000) as product_id, (ARRAY[11,12,13,14])[floor(random()*4)+1] as region_id, true as available; insert into array_agg_test (product_id, region_id, available) SELECT generate_series(1, 50000) as product_id, (ARRAY[111,112,113,114])[floor(random()*4)+1] as region_id, true as available; insert into array_agg_test (product_id, region_id, available) SELECT generate_series(1, 50000) as product_id, (ARRAY[1111,1112,1113,1114])[floor(random()*4)+1] as region_id, true as available; vacuum analyze array_agg_test; \set ECHO all -- set hash_mem_multiplier = 2; set work_mem = "200MB"; explain (analyze, buffers) select product_id, array_agg(region_id) from array_agg_test group by product_id; ------- =_aaaaaaaaaa0--