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 1w8kJg-000pxo-2q for pgsql-performance@arkaria.postgresql.org; Fri, 03 Apr 2026 19:36: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 1w8kJf-00Dj4A-2L for pgsql-performance@arkaria.postgresql.org; Fri, 03 Apr 2026 19:36:28 +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 1w8kJf-00Dj41-1P for pgsql-performance@lists.postgresql.org; Fri, 03 Apr 2026 19:36:27 +0000 Received: from mail-dy1-x1331.google.com ([2607:f8b0:4864:20::1331]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w8kJd-00000000PIP-3oh5 for pgsql-performance@lists.postgresql.org; Fri, 03 Apr 2026 19:36:27 +0000 Received: by mail-dy1-x1331.google.com with SMTP id 5a478bee46e88-2cbdd9852aaso1516468eec.0 for ; Fri, 03 Apr 2026 12:36:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20251104.gappssmtp.com; s=20251104; t=1775244985; x=1775849785; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=x+6x3aqrQN45HUCYiVTAoqsridBs3WN3pQOFrdCUO2U=; b=YxJ0kxLXcJnjz/m9SH9RSLcVDhbLOw8QucR4MjrcT/ufTI/ijrMcgXai5nPIfZevS+ 7QRfXlTQLUEo9dVlRbcDhiN2mqi7NmnCLA2al21GgrdBZdQ3+TO4FXOad2kaNLuaemcf DArwhm5N3xiVHxt1Vl546vKzZLYe4wAgnJm4/E7n09z8waKrxIyK0HCDD+Bv5nSHOeND a31tUyxByy2gzwELf6E4Pbm3mmF2RO85Hnlsmn8znRARXYKdqTS5ivmiFQA5/OEeNSii mXryVIVHi8vDKhIKfrQBZY9STaiQA1z4nXV2Y8KvyrpXzQ//hhQO1sXyGSEVeKHjrFEp etxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775244985; x=1775849785; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x+6x3aqrQN45HUCYiVTAoqsridBs3WN3pQOFrdCUO2U=; b=o+NmS0TTnLVvW+JMzILdfOqMzBWVloiYNQGSZxQuH9rjc9yXBbjKx+XtrB0aHK7Xuk QPX4VdRwnNv9TrD4yJXh8Gg7ekc3mznKk3dy2xOX8NvO0NOH93phY3ANlgBB7MJPLnpF f5pNT2XA+sn87XIvPfGEdr/68L5kvi2+GgZz/ebtjWpflzK2Vq1c+RHEWTWGevf5Bkpn RrcJ8ppKUSLJ/oLE7ea2Q15fAx4DH4RwXEQL4Jdq9xwnOgcQCSvJnDI1YmyD9n4HnXwN lmUfY2lslk5HneLB68jxoM6KQOSY/RnCwTJwDzWthPr83BKruFfJStkpxPbOxMeZ4qWQ ga3w== X-Forwarded-Encrypted: i=1; AJvYcCVUNbrpJbhbpu1DHbFPKO9NMFyJ7ui/dK72zvAo3Uz488L2zAdYjre697c9aPBKyBgZpZGHo5ZBjuOxwBig9oxErA==@lists.postgresql.org X-Gm-Message-State: AOJu0YyCFmwDkG+Z54XpyKVOKfSlTufrUiMlV4zxcb0h1rMLDXTtxMTZ jOxYIltXpiwSoCxX3FeYIZL0bx60mXKb5Ow6Xj+o2coYt0COCPo4UwJP0fea2VtuGQ== X-Gm-Gg: AeBDieukl0L4NBx6b0564VfRVmhmo1nCg2YiKSJy5q8oKe0qwzuuOoXZOC0yTZnYLmr nGneq+B7JryZXzmxQ8p49xVcJvDeDNjWtBLg5XuqaSjHbd87oFleToGy7WXnndNdyE73PpqOSbf U+AbqOJQEtSLfSqea6nKAIoizg54l1O5B9Q40SLpXwdcr8iRwMGskvoHJ6r0QJjBLTZcCN45nbx nKM0NLrJnuL8MpzcDT9Q8ZMzVwbXnMmrrmGD8t8j+h2zUdaWl3OL2t513QhCT/Bo5MtubHKGLpF x1DOZgBwG+mMxmJjfuRL/r0JCEABNRvVeKatlKo0JE8dO7XumO+zVT/X4KPNtcoPjtV2hBGd9+H cRQs42TcvdLTcnckrNLotren7B2hpudRDaemGgKqmoV+3Ius3Zgs+CorOHddefY4MYTiDzPYRLX l/Vkp5DeHfVst3l1TLcKSYVD+SQMxomVc2DehFfZX5z0bPNbtb3tg= X-Received: by 2002:a05:7301:4090:b0:2c4:dd55:ffc1 with SMTP id 5a478bee46e88-2cbf9af8898mr1890435eec.2.1775244985052; Fri, 03 Apr 2026 12:36:25 -0700 (PDT) Received: from jeff-laptop.lan (c-24-7-19-3.hsd1.ca.comcast.net. [24.7.19.3]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2ca793ea2f2sm5735265eec.9.2026.04.03.12.36.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Apr 2026 12:36:24 -0700 (PDT) Message-ID: <3b569609adc36db30aacbf05e487b472fa8edc84.camel@j-davis.com> Subject: Re: Significant performance issues with array_agg() + HashAggregate plans on Postgres 17 From: Jeff Davis To: Tom Lane , Scott Carey Cc: David Rowley , pgsql-performance@lists.postgresql.org Date: Fri, 03 Apr 2026 12:36:23 -0700 In-Reply-To: <3825075.1775244255@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> <3825075.1775244255@sss.pgh.pa.us> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.3-0ubuntu1.1 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2026-04-03 at 15:24 -0400, Tom Lane wrote: > After further study I no longer think there's a leak.=C2=A0 This > test query involves 50000 GROUP BY groups, and we have an > array being accumulated for each one. I was coming to a similar conclusion, though trying to work through the details=C2=A0of expanded arrays and how the datums are copied around during aggregation. I don't (yet) see a problem with the correctness of the memory handling there. A lot of tiny memory contexts are not ideal, but as long as it's one per group, that seems within reason. > So really the whole thing is kind of unsatisfactory. > I'm not seeing cheap ways to make it better though. >=20 > A very quick and dirty idea is to tell MemoryContextMemAllocated > not to recurse, which would restore it to constant-time.=C2=A0 But > that would make the results much less accurate in cases like > this one. One idea would be to update parent contexts' memory totals recursively each time a subcontext allocates a new block. Block allocations are infrequent enough that may be acceptable. If we are worried about affecting unrelated cases, we could set an "accounting_enabled" flag for the contexts we care about, which would be automatically inherited by subcontexts, and then stop recursing up when that flag is false. Regards, Jeff Davis