Re: Significant performance issues with array_agg() + HashAggregate plans on Postgres 17 - Mailing list pgsql-performance

From Scott Carey
Subject Re: Significant performance issues with array_agg() + HashAggregate plans on Postgres 17
Date
Msg-id CA+vubOEBHMFKvYfqEW63b++V7yia2baYtmz6X9n-dBt0w2kYvQ@mail.gmail.com
Whole thread Raw
In response to Re: Significant performance issues with array_agg() + HashAggregate plans on Postgres 17  (David Rowley <dgrowleyml@gmail.com>)
Responses Re: Significant performance issues with array_agg() + HashAggregate plans on Postgres 17
List pgsql-performance


On Tue, Mar 31, 2026 at 3:49 PM David Rowley <dgrowleyml@gmail.com> wrote:
On Wed, 1 Apr 2026 at 08:26, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Also grasping at straws and wondering if it's related to L3
contention. Hash tables mostly always have very unpredictable memory
access which the hardware prefetcher can't deal with. If useful
cachelines are being evicted from L3 by other processes, then that'll
mean more stalls waiting on RAM when probing the hash table.

I can reproduce it on some near idle systems as well as the busy one.  Queries are sometimes slowed by 10% to 30% on the busy system vs the near idle readonly streaming replica. 

Cache contention and memory contention can certainly slow hash access.    And although we often model a hash as O(1) access, there is no such thing as true O(1) performance scaling for random memory access on today's hardware.   The difference between the speed of accessing something entirely in L1/L2 cache vs something mostly in RAM is pretty huge.
 



I'm curious to know how consistent the run times are and if the
json_agg() query can be just as slow as the array_agg() one. Could it
be that the json_agg() version was just run at a time the server
wasn't as busy with other things... ?

The run times are consistent enough (within a 15% range most of the time on the busy server), yet we are talking about a 50x performance difference, dwarfing the time variation.

I have run each of these tests dozens of times now, back-to-back or with a delay, and  this is extremely consistent no matter the timing.  On the low load servers or laptop, it is even more consistent.  The only time it is 'unexpectedly fast' is if I do something that triggers a different query plan, like one that uses sort + group aggregate or one that does an index scan.    Every time it does the HashAggregate over the sequential scan it reproduces.

-Scott


David

pgsql-performance by date:

Previous
From: Scott Carey
Date:
Subject: Re: Significant performance issues with array_agg() + HashAggregate plans on Postgres 17
Next
From: David Rowley
Date:
Subject: Re: Significant performance issues with array_agg() + HashAggregate plans on Postgres 17